Core.go 12 KB

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