Core.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. package HikSDK
  2. /*
  3. #cgo windows,amd64 LDFLAGS: -L${SRCDIR}/library/amd64_windows/lib -lHCCore -lHCNetSDK
  4. #cgo linux,amd64 LDFLAGS: -L${SRCDIR}/library/amd64_linux/lib -lHCCore -lhcnetsdk
  5. #cgo linux,arm64 LDFLAGS: -L${SRCDIR}/library/arm64_linux/lib -lHCCore -lhcnetsdk
  6. #cgo windows,amd64 CFLAGS: -I${SRCDIR}/library/amd64_windows/include
  7. #cgo linux,amd64 CFLAGS: -I${SRCDIR}/library/amd64_linux/include
  8. #cgo linux,arm64 CFLAGS: -I${SRCDIR}/library/arm64_linux/include
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <stddef.h>
  12. #include <string.h>
  13. #include <unistd.h>
  14. #include <stdint.h>
  15. #ifdef _WIN32
  16. #include <windows.h>
  17. #else
  18. #include <unistd.h>
  19. #endif
  20. void sleep_seconds(int seconds) {
  21. #ifdef _WIN32
  22. Sleep(seconds * 1000); // Sleep 参数是毫秒
  23. #else
  24. sleep(seconds); // sleep 参数是秒
  25. #endif
  26. }
  27. void sys_sleep_seconds(int seconds) {
  28. #ifdef _WIN32
  29. Sleep(seconds * 1000); // Sleep 参数是毫秒
  30. #endif
  31. }
  32. #include "HCNetSDK.h"
  33. typedef struct DEVICEINFO
  34. {
  35. BYTE byChanNum;
  36. BYTE byStartChan;
  37. } DEVICEINFO;
  38. typedef struct PTZ{
  39. WORD P;
  40. WORD T;
  41. WORD Z;
  42. } PTZ;
  43. typedef struct TimeRange
  44. {
  45. DWORD StartYear;
  46. DWORD StartMonth;
  47. DWORD StartDay;
  48. DWORD StartHour;
  49. DWORD StartMinute;
  50. DWORD StartSecond;
  51. DWORD EndYear;
  52. DWORD EndMonth;
  53. DWORD EndDay;
  54. DWORD EndHour;
  55. DWORD EndMinute;
  56. DWORD EndSecond;
  57. } TimeRange;
  58. LONG Login(char *sDVRIP,WORD wDVRPort,char *sUserName,char *sPassword,struct DEVICEINFO *info){
  59. NET_DVR_DEVICEINFO hikInfo;
  60. LONG lUserID = NET_DVR_Login(sDVRIP, wDVRPort, sUserName, sPassword,&hikInfo);
  61. info->byChanNum = hikInfo.byChanNum;
  62. info->byStartChan = hikInfo.byStartChan;
  63. return lUserID;
  64. }
  65. int GetTimeZone(LONG lUserID){
  66. NET_DVR_NETAPPCFG NTPData;
  67. DWORD RESLEN;
  68. NET_DVR_GetDVRConfig(lUserID,222,0,&NTPData,sizeof(NTPData),&RESLEN);
  69. return NTPData.struNtpClientParam.cTimeDifferenceH*60 + NTPData.struNtpClientParam.cTimeDifferenceM*((NTPData.struNtpClientParam.cTimeDifferenceH>>7)?-1:1);
  70. }
  71. void GetPTZ(LONG lUserID,PTZ *info){
  72. NET_DVR_PTZPOS PTZPOS;
  73. DWORD RESLEN;
  74. NET_DVR_GetDVRConfig(lUserID,293,1,&PTZPOS,sizeof(PTZPOS),&RESLEN);
  75. info->P = PTZPOS.wPanPos;
  76. info->T = PTZPOS.wTiltPos;
  77. info->Z = PTZPOS.wZoomPos;
  78. return;
  79. }
  80. void SetPTZ(LONG lUserID,WORD action,WORD p,WORD t,WORD z){
  81. NET_DVR_PTZPOS PTZPOS;
  82. PTZPOS.wAction = action;
  83. PTZPOS.wPanPos = p;
  84. PTZPOS.wTiltPos = t;
  85. PTZPOS.wZoomPos = z;
  86. NET_DVR_SetDVRConfig(lUserID,292,1,&PTZPOS,sizeof(PTZPOS));
  87. }
  88. void QueryMonth(LONG lUserID,WORD Year,BYTE Month,DWORD Channel,BYTE *byRecordDistribution){
  89. NET_DVR_MRD_SEARCH_PARAM SEARCH_PARAM;
  90. SEARCH_PARAM.dwSize = sizeof(SEARCH_PARAM);
  91. SEARCH_PARAM.wYear = Year;
  92. SEARCH_PARAM.byMonth = Month;
  93. SEARCH_PARAM.struStreamInfo.dwChannel = 32 + Channel;
  94. SEARCH_PARAM.byLocalOrUTC = 1;
  95. SEARCH_PARAM.byDrawFrame = 0;
  96. SEARCH_PARAM.byStreamType = 0;
  97. LPVOID lpStatusList = malloc(sizeof(SEARCH_PARAM));
  98. NET_DVR_MRD_SEARCH_RESULT SEARCH_RESULT;
  99. NET_DVR_GetDeviceConfig(lUserID,6164,0,&SEARCH_PARAM,sizeof(SEARCH_PARAM),lpStatusList,&SEARCH_RESULT,sizeof(SEARCH_RESULT));
  100. free(lpStatusList);
  101. memcpy(byRecordDistribution,SEARCH_RESULT.byRecordDistribution,32);
  102. }
  103. LONG QueryDayHandle(LONG lUserID,DWORD Year,DWORD Month,DWORD Day,LONG Channel){
  104. NET_DVR_FILECOND FindFile;
  105. FindFile.dwFileType = 0xff;
  106. FindFile.dwIsLocked = 0xff;
  107. FindFile.dwUseCardNo = 0;
  108. FindFile.lChannel = 32 + Channel;
  109. FindFile.struStartTime.dwYear = Year;
  110. FindFile.struStartTime.dwMonth = Month;
  111. FindFile.struStartTime.dwDay = Day;
  112. FindFile.struStartTime.dwHour = 0;
  113. FindFile.struStartTime.dwMinute = 0;
  114. FindFile.struStartTime.dwSecond = 0;
  115. FindFile.struStopTime.dwYear = Year;
  116. FindFile.struStopTime.dwMonth = Month;
  117. FindFile.struStopTime.dwDay = Day;
  118. FindFile.struStopTime.dwHour = 23;
  119. FindFile.struStopTime.dwMinute = 59;
  120. FindFile.struStopTime.dwSecond = 59;
  121. return NET_DVR_FindFile_V30(lUserID, &FindFile);
  122. }
  123. LONG QueryDayNextFile(LONG QueryHandle,struct TimeRange *TimeRange){
  124. NET_DVR_FINDDATA_V30 Data;
  125. LONG STATE = NET_DVR_FindNextFile_V30(QueryHandle,&Data);
  126. TimeRange->StartYear = Data.struStartTime.dwYear;
  127. TimeRange->StartMonth = Data.struStartTime.dwMonth;
  128. TimeRange->StartDay = Data.struStartTime.dwDay;
  129. TimeRange->StartHour = Data.struStartTime.dwHour;
  130. TimeRange->StartMinute = Data.struStartTime.dwMinute;
  131. TimeRange->StartSecond = Data.struStartTime.dwSecond;
  132. TimeRange->EndYear = Data.struStopTime.dwYear;
  133. TimeRange->EndMonth = Data.struStopTime.dwMonth;
  134. TimeRange->EndDay = Data.struStopTime.dwDay;
  135. TimeRange->EndHour = Data.struStopTime.dwHour;
  136. TimeRange->EndMinute = Data.struStopTime.dwMinute;
  137. TimeRange->EndSecond = Data.struStopTime.dwSecond;
  138. return STATE;
  139. }
  140. struct SerialData
  141. {
  142. float p;
  143. float t;
  144. float z;
  145. };
  146. struct SerialData serialData;
  147. // 自定义 ntohs 函数(仅适用于小端平台)
  148. uint16_t my_ntohs(uint16_t netshort) {
  149. return (netshort >> 8) | (netshort << 8);
  150. }
  151. void CALLBACK g_fSerialDataCallBack(LONG lSerialHandle, char *pRecvDataBuffer, DWORD dwBufSize, DWORD dwUser)
  152. {
  153. char type = pRecvDataBuffer[3];
  154. printf("type: %x\n", type);
  155. uint16_t raw_value;
  156. memcpy(&raw_value, pRecvDataBuffer + 4, sizeof(uint16_t));
  157. float converted = (float)my_ntohs(raw_value) / 100.0f;
  158. switch (type) {
  159. case 0x59:
  160. serialData.p = converted;
  161. break;
  162. case 0x5b:
  163. serialData.t = converted;
  164. break;
  165. case 0x5d:
  166. serialData.z = converted;
  167. break;
  168. default:
  169. // 未知类型
  170. break;
  171. }
  172. }
  173. void GetPTZPOS(LONG lUserID)
  174. {
  175. char p[7] = {0xff, 0x01, 0x00, 0x51, 0x00, 0x00, 0x52};
  176. char t[7] = {0xff, 0x01, 0x00, 0x53, 0x00, 0x00, 0x54};
  177. char z[7] = {0xff, 0x01, 0x00, 0x55, 0x00, 0x00, 0x56};
  178. LONG lTranHandle = NET_DVR_SerialStart(lUserID, 2,g_fSerialDataCallBack,6);
  179. LONG lSerialChan = 0;
  180. sleep_seconds(1);
  181. NET_DVR_SerialSend(lTranHandle, lSerialChan, p, 7);
  182. sleep_seconds(1);
  183. NET_DVR_SerialSend(lTranHandle, lSerialChan, t, 7);
  184. sleep_seconds(1);
  185. NET_DVR_SerialSend(lTranHandle, lSerialChan, z, 7);
  186. sleep_seconds(1);
  187. NET_DVR_SerialStop(lTranHandle);
  188. }
  189. void SetPTZPOS(LONG lUserID,int action,char* P,char* T,char *Z)
  190. {
  191. LONG lTranHandle = NET_DVR_SerialStart(lUserID, 2,g_fSerialDataCallBack,6);
  192. LONG lSerialChan = 0;
  193. if (action == 1) {
  194. sys_sleep_seconds(1);
  195. NET_DVR_SerialSend(lTranHandle, lSerialChan, P, 7);
  196. sys_sleep_seconds(1);
  197. NET_DVR_SerialSend(lTranHandle, lSerialChan, T, 7);
  198. sys_sleep_seconds(1);
  199. NET_DVR_SerialSend(lTranHandle, lSerialChan, Z, 7);
  200. sys_sleep_seconds(1);
  201. }else if (action == 2) {
  202. sys_sleep_seconds(1);
  203. NET_DVR_SerialSend(lTranHandle, lSerialChan, P, 7);
  204. sys_sleep_seconds(1);
  205. }else if (action == 3) {
  206. sys_sleep_seconds(1);
  207. NET_DVR_SerialSend(lTranHandle, lSerialChan, T, 7);
  208. sys_sleep_seconds(1);
  209. }else if (action == 4) {
  210. sys_sleep_seconds(1);
  211. NET_DVR_SerialSend(lTranHandle, lSerialChan, Z, 7);
  212. sys_sleep_seconds(1);
  213. }else {
  214. sys_sleep_seconds(1);
  215. NET_DVR_SerialSend(lTranHandle, lSerialChan, P, 7);
  216. sys_sleep_seconds(1);
  217. NET_DVR_SerialSend(lTranHandle, lSerialChan, T, 7);
  218. sys_sleep_seconds(1);
  219. }
  220. NET_DVR_SerialStop(lTranHandle);
  221. sleep(1);
  222. }
  223. */
  224. import "C"
  225. import (
  226. "math"
  227. "sync"
  228. "unsafe"
  229. )
  230. type LONG int32
  231. type BYTE uint8
  232. type DWORD uint32
  233. type WORD uint16
  234. type NET_DVR_DEVICEINFO struct {
  235. ByChanNum BYTE
  236. ByStartChan BYTE // 起始数字通道号,0表示无数字通道,比如DVR或IPC
  237. }
  238. // Init 初始化SDK,调用其他SDK函数的前提。
  239. func Init() error {
  240. if C.NET_DVR_Init() != 1 {
  241. return lastError("NET_DVR_Init")
  242. }
  243. return nil
  244. }
  245. // Cleanup 释放SDK资源,在程序结束之前调用。在调用时不能同时调用其他任何SDK接口。 Init 和 Cleanup 需要配对使用,即程序里面调用多少次 Init ,退出时就需要调用多少次 Cleanup。
  246. func Cleanup() error {
  247. if C.NET_DVR_Cleanup() != 1 {
  248. return lastError("NET_DVR_Cleanup")
  249. }
  250. return nil
  251. }
  252. func login(Ip string, Port int, Username string, Password string) (LONG, NET_DVR_DEVICEINFO, error) {
  253. cIp := C.CString(Ip)
  254. cUser := C.CString(Username)
  255. cPass := C.CString(Password)
  256. defer C.free(unsafe.Pointer(cIp))
  257. defer C.free(unsafe.Pointer(cUser))
  258. defer C.free(unsafe.Pointer(cPass))
  259. var info C.DEVICEINFO
  260. userId := LONG(C.Login(cIp, C.WORD(Port), cUser, cPass, &info))
  261. err := lastError("login")
  262. if err != nil {
  263. return 0, NET_DVR_DEVICEINFO{}, err
  264. }
  265. return userId, NET_DVR_DEVICEINFO{
  266. ByChanNum: BYTE(info.byChanNum),
  267. ByStartChan: BYTE(info.byStartChan)},
  268. nil
  269. }
  270. func lastError(funcName string) error {
  271. ErrorCode := uint(C.NET_DVR_GetLastError())
  272. if ErrorCode == 0 {
  273. return nil
  274. }
  275. var text string
  276. if ErrorCode == 3 {
  277. text = "sdk not init."
  278. return NewHcnetError(int(ErrorCode), text, funcName)
  279. }
  280. cErrorCode := C.LONG(ErrorCode)
  281. cText := C.NET_DVR_GetErrorMsg(&cErrorCode)
  282. text = C.GoString(cText)
  283. return NewHcnetError(int(ErrorCode), text, funcName)
  284. }
  285. func getTimeZone(lUserId LONG) int {
  286. return int(C.GetTimeZone(C.LONG(lUserId)))
  287. }
  288. func deviceOnline(lUserId LONG) bool {
  289. return C.NET_DVR_RemoteControl(C.LONG(lUserId), C.DWORD(20005), nil, C.DWORD(0)) == C.TRUE
  290. }
  291. func logout(lUserId LONG) error {
  292. if lUserId > -1 {
  293. cResult := C.NET_DVR_Logout(C.LONG(lUserId))
  294. if cResult != 1 {
  295. return lastError("NET_DVR_Logout")
  296. }
  297. }
  298. return nil
  299. }
  300. func pTZControlWithSpeed_Other(lUserID LONG, dwPTZCommand DWORD, dwStop DWORD, dwSpeed DWORD) error {
  301. state := C.NET_DVR_PTZControlWithSpeed_Other(C.LONG(lUserID), 1, C.DWORD(dwPTZCommand), C.DWORD(dwStop), C.DWORD(dwSpeed))
  302. if state == C.FALSE {
  303. return lastError("NET_DVR_PTZControlWithSpeed_Other")
  304. }
  305. return nil
  306. }
  307. func getPTZBase(lUserID LONG) (PTZ, error) {
  308. var info C.PTZ
  309. C.GetPTZ(C.LONG(lUserID), &info)
  310. err := lastError("GetPTZ")
  311. if err != nil {
  312. return PTZ{}, err
  313. }
  314. return PTZ{
  315. P: HEX2DEC(WORD(info.P)),
  316. T: HEX2DEC(WORD(info.T)),
  317. Z: HEX2DEC(WORD(info.Z))},
  318. nil
  319. }
  320. func setPTZBase(lUserID LONG, ptz PTZ) error {
  321. C.SetPTZ(C.LONG(lUserID), C.WORD(ptz.Action), C.WORD(DEC2HEX(float64(ptz.P))), C.WORD(DEC2HEX(float64(ptz.T))), C.WORD(DEC2HEX(float64(ptz.Z))))
  322. return lastError("SetPTZ")
  323. }
  324. // HEX2DEC 将十六进制整数转换为十进制浮点数
  325. func HEX2DEC(hex WORD) float64 {
  326. // 提取十六进制数的千位(对应十进制的百位)
  327. bai := byte(hex >> 12)
  328. hex = hex - WORD(bai)*WORD(math.Pow(16, 3))
  329. // 提取十六进制数的百位(对应十进制的十位)
  330. shi := byte(hex >> 8)
  331. hex = hex - WORD(shi)*WORD(math.Pow(16, 2))
  332. // 提取十六进制数的十位(对应十进制的个位)
  333. ge := byte(hex >> 4)
  334. hex = hex - WORD(ge)*WORD(math.Pow(16, 1))
  335. // 提取十六进制数的个位(对应十进制的十分位)
  336. xiao := byte(hex)
  337. // 计算对应的十进制浮点数
  338. return float64(bai)*math.Pow(10, 2) + float64(shi)*math.Pow(10, 1) + float64(ge)*math.Pow(10, 0) + float64(xiao)*math.Pow(10, -1)
  339. }
  340. // DEC2HEX 将十进制浮点数转换为十六进制整数
  341. func DEC2HEX(dec float64) WORD {
  342. // 提取千位(对应十进制的百位)
  343. bai := uint16(dec / math.Pow(10, 2))
  344. dec -= float64(bai) * math.Pow(10, 2)
  345. // 提取百位(对应十进制的十位)
  346. shi := uint16(dec / math.Pow(10, 1))
  347. dec -= float64(shi) * math.Pow(10, 1)
  348. // 提取十位(对应十进制的个位)
  349. ge := uint16(dec / math.Pow(10, 0))
  350. dec -= float64(ge) * math.Pow(10, 0)
  351. // 提取个位(对应十进制的十分位)
  352. xiao := uint16(dec * 10)
  353. // 合成十六进制数
  354. hex := WORD(bai)<<12 | WORD(shi)<<8 | WORD(ge)<<4 | WORD(xiao)
  355. return hex
  356. }
  357. func QueryMonth(lUserID LONG, Year WORD, Month BYTE, Channel DWORD) (res []uint8, err error) {
  358. buf := (*C.BYTE)(C.malloc(32))
  359. defer C.free(unsafe.Pointer(buf))
  360. C.QueryMonth(C.LONG(lUserID), C.WORD(Year), C.BYTE(Month), C.DWORD(Channel), buf)
  361. goBuf := C.GoBytes(unsafe.Pointer(buf), 32)
  362. for i := 0; i < 32; i++ {
  363. if goBuf[i] != 0 {
  364. res = append(res, uint8(i+1))
  365. }
  366. }
  367. err = lastError("QueryMonth")
  368. return
  369. }
  370. func QueryDayHandle(lUserID LONG, Year, Month, Day DWORD, Channel LONG) LONG {
  371. return LONG(C.QueryDayHandle(C.LONG(lUserID), C.DWORD(Year), C.DWORD(Month), C.DWORD(Day), C.LONG(Channel)))
  372. }
  373. func QueryDayNextFile(Handle LONG) (TimeRange, LONG, error) {
  374. var FindData C.TimeRange
  375. state := LONG(C.QueryDayNextFile(C.LONG(Handle), &FindData))
  376. if state < 0 {
  377. return TimeRange{}, state, lastError("QueryDayNextFile")
  378. }
  379. return TimeRange{
  380. StartYear: DWORD(FindData.StartYear),
  381. StartMonth: DWORD(FindData.StartMonth),
  382. StartDay: DWORD(FindData.StartDay),
  383. StartHour: DWORD(FindData.StartHour),
  384. StartMinute: DWORD(FindData.StartMinute),
  385. StartSecond: DWORD(FindData.StartSecond),
  386. EndYear: DWORD(FindData.EndYear),
  387. EndMonth: DWORD(FindData.EndMonth),
  388. EndDay: DWORD(FindData.EndDay),
  389. EndHour: DWORD(FindData.EndHour),
  390. EndMinute: DWORD(FindData.EndMinute),
  391. EndSecond: DWORD(FindData.EndSecond),
  392. }, state, nil
  393. }
  394. func FindClose(Handle LONG) error {
  395. C.NET_DVR_FindClose_V30(C.LONG(Handle))
  396. return lastError("NET_DVR_FindClose_V30")
  397. }
  398. var SerialMux sync.Mutex
  399. func SetPTZPos(lUserID LONG, Action int, P []byte, T []byte, Z []byte) error {
  400. cP := C.CBytes(P)
  401. cT := C.CBytes(T)
  402. cZ := C.CBytes(Z)
  403. defer C.free(cP)
  404. defer C.free(cT)
  405. defer C.free(cZ)
  406. SerialMux.Lock()
  407. C.SetPTZPOS(C.LONG(lUserID), C.int(Action), (*C.char)(cP), (*C.char)(cT), (*C.char)(cZ))
  408. SerialMux.Unlock()
  409. return lastError("SetPTZPOS")
  410. }
  411. func GetPTZPOS(lUserID LONG) (PTZ, error) {
  412. SerialMux.Lock()
  413. C.GetPTZPOS(C.LONG(lUserID))
  414. p := float32(C.serialData.p)
  415. t := float32(C.serialData.t)
  416. z := float32(C.serialData.z)
  417. SerialMux.Unlock()
  418. return PTZ{
  419. P: float64(p),
  420. T: float64(t),
  421. Z: float64(z),
  422. }, lastError("GetPTZPOS")
  423. }