Core.go 13 KB

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