From 9e06836fdc70a4045f6dd0e01c97c4610aecbd5e Mon Sep 17 00:00:00 2001
From: zhangzengfei <zhangzengfei@smartai.com>
Date: 星期四, 14 十月 2021 15:45:45 +0800
Subject: [PATCH] add ptzTrack toPreset fun

---
 hikptz_test.go |   19 ++++
 .gitignore     |    2 
 hikptz.go      |   52 +++++-------
 hikptz.h       |   14 +-
 hikptz.cpp     |  117 +++++++++++++++++++++++++++++
 5 files changed, 168 insertions(+), 36 deletions(-)

diff --git a/.gitignore b/.gitignore
index 967e1de..cf98025 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,5 +1,7 @@
 # Compiled Object files, Static and Dynamic libs (Shared Objects)
 
+.idea/
+.vscode/
 
 # Folders
 _obj
diff --git a/hikptz.cpp b/hikptz.cpp
new file mode 100644
index 0000000..3ba09ff
--- /dev/null
+++ b/hikptz.cpp
@@ -0,0 +1,117 @@
+#include "HCNetSDK.h"
+#include <stdio.h>
+#include <string.h>
+#include <time.h>
+using namespace std;
+
+#include "hikptz.h"
+
+void ptzTrack(char *ip, char *userName, char *password, int port, int channel, int xBottom, int xTop, int yBottom, int yTop)
+{
+    // 鍒濆鍖�
+    NET_DVR_Init();
+
+    //璁剧疆杩炴帴鏃堕棿涓庨噸杩炴椂闂�
+    NET_DVR_SetConnectTime(2000, 1);
+    NET_DVR_SetReconnect(10000, true);
+
+    //璁剧疆寮傚父娑堟伅鍥炶皟鍑芥暟
+    NET_DVR_SetExceptionCallBack_V30(0, NULL, NULL, NULL);
+
+    // 娉ㄥ唽璁惧
+
+    //鐧诲綍鍙傛暟锛屽寘鎷澶囧湴鍧�銆佺櫥褰曠敤鎴枫�佸瘑鐮佺瓑
+    NET_DVR_USER_LOGIN_INFO struLoginInfo = {0};
+    struLoginInfo.bUseAsynLogin = 0;           //鍚屾鐧诲綍鏂瑰紡
+    strcpy(struLoginInfo.sDeviceAddress, ip);  //璁惧IP鍦板潃
+    struLoginInfo.wPort = port;                //璁惧鏈嶅姟绔彛
+    strcpy(struLoginInfo.sUserName, userName); //璁惧鐧诲綍鐢ㄦ埛鍚�
+    strcpy(struLoginInfo.sPassword, password); //璁惧鐧诲綍瀵嗙爜
+
+    //璁惧淇℃伅, 杈撳嚭鍙傛暟
+    NET_DVR_DEVICEINFO_V40 struDeviceInfoV40 = {0};
+
+    LONG lUserID;
+    lUserID = NET_DVR_Login_V40(&struLoginInfo, &struDeviceInfoV40);
+    if (lUserID < 0)
+    {
+        printf("Login failed, error code: %d\n", NET_DVR_GetLastError());
+        NET_DVR_Cleanup();
+        return;
+    }
+
+    LPNET_DVR_POINT_FRAME zoneInfo = {0};
+    zoneInfo->xTop = xTop;       //
+    zoneInfo->yTop = yTop;       //
+    zoneInfo->xBottom = xBottom; //
+    zoneInfo->yBottom = yBottom; //
+
+    int iChannel = channel; //璁惧閫氶亾鍙�
+    if (!NET_DVR_PTZSelZoomIn_EX(lUserID, iChannel, zoneInfo))
+    {
+        printf("PAN_LEFT start failed, error code: %d\n", NET_DVR_GetLastError());
+        NET_DVR_Logout(lUserID);
+        NET_DVR_Cleanup();
+        return;
+    }
+
+    //娉ㄩ攢鐢ㄦ埛
+    NET_DVR_Logout(lUserID);
+
+    //閲婃斁SDK璧勬簮
+    NET_DVR_Cleanup();
+
+    return;
+}
+
+void toPreset(char *ip, char *userName, char *password, int port, int channel)
+{
+    // 鍒濆鍖�
+    NET_DVR_Init();
+    //璁剧疆杩炴帴鏃堕棿涓庨噸杩炴椂闂�
+    NET_DVR_SetConnectTime(2000, 1);
+    NET_DVR_SetReconnect(10000, true);
+
+    //---------------------------------------
+    //璁剧疆寮傚父娑堟伅鍥炶皟鍑芥暟
+    NET_DVR_SetExceptionCallBack_V30(0, NULL, NULL, NULL);
+
+    //---------------------------------------
+    // 娉ㄥ唽璁惧
+    LONG lUserID;
+    //鐧诲綍鍙傛暟锛屽寘鎷澶囧湴鍧�銆佺櫥褰曠敤鎴枫�佸瘑鐮佺瓑
+    NET_DVR_USER_LOGIN_INFO struLoginInfo = {0};
+    struLoginInfo.bUseAsynLogin = 0;           //鍚屾鐧诲綍鏂瑰紡
+    strcpy(struLoginInfo.sDeviceAddress, ip);  //璁惧IP鍦板潃
+    struLoginInfo.wPort = port;                //璁惧鏈嶅姟绔彛
+    strcpy(struLoginInfo.sUserName, userName); //璁惧鐧诲綍鐢ㄦ埛鍚�
+    strcpy(struLoginInfo.sPassword, password); //璁惧鐧诲綍瀵嗙爜
+
+    //璁惧淇℃伅, 杈撳嚭鍙傛暟
+    NET_DVR_DEVICEINFO_V40 struDeviceInfoV40 = {0};
+
+    lUserID = NET_DVR_Login_V40(&struLoginInfo, &struDeviceInfoV40);
+    if (lUserID < 0)
+    {
+        printf("Login failed, error code: %d\n", NET_DVR_GetLastError());
+        NET_DVR_Cleanup();
+        return;
+    }
+
+    int iChannel = channel;           //璁惧閫氶亾鍙�
+    DWORD dwPTZCommand = GOTO_PRESET; //杞埌棰勭疆鐐�
+    if (!NET_DVR_PTZPreset_Other(lUserID, iChannel, GOTO_PRESET, 1))
+    {
+        printf("PAN_LEFT start failed, error code: %d\n", NET_DVR_GetLastError());
+        NET_DVR_Logout(lUserID);
+        NET_DVR_Cleanup();
+        return;
+    }
+    //娉ㄩ攢鐢ㄦ埛
+    NET_DVR_Logout(lUserID);
+
+    //閲婃斁SDK璧勬簮
+    NET_DVR_Cleanup();
+
+    return;
+}
\ No newline at end of file
diff --git a/hikptz.go b/hikptz.go
index 45d0d19..f58964a 100644
--- a/hikptz.go
+++ b/hikptz.go
@@ -1,45 +1,39 @@
-package hikvoice
+package hikControl
 
 /*
-#cgo CFLAGS: -I${SRCDIR}/include -w -g
-#cgo LDFLAGS: -L${SRCDIR}/libs
-#cgo LDFLAGS: -lHikVoice -lAudioRender -lHCCore -lhcnetsdk -lhpr
-#cgo LDFLAGS: -ldl -lm
-
+#cgo CXXFLAGS: -I${SRCDIR}/include -w -g
+#cgo LDFLAGS: -L${SRCDIR}/lib
+#cgo LDFLAGS: -lhcnetsdk -lhpr -lHCCore
 #include <stdlib.h>
 #include "hikptz.h"
 */
 import "C"
-
 import (
-    "unsafe"
+	"unsafe"
 )
 
 // SendVoice Send Voice
-func PtzTrack(ip,password,userName string, port, channel,xBottom,xTop,yBottom,yTop int)  {
-	
-	cip := C.CString(ip)
-    defer C.free(unsafe.Pointer(cip))
-    cusrname := C.CString(userName)
-    defer C.free(unsafe.Pointer(cusrname))
-    cpasswd := C.CString(password)
-    defer C.free(unsafe.Pointer(cpasswd))
- 
+func PtzTrack(ip, userName, password string, port, channel, xBottom, xTop, yBottom, yTop int) {
 
-	C.PtzTrack(cip, cusrname, cpasswd, C.int(port), C.int(channel),C.int(xBottom),C.int(xTop),C.int(yBottom),C.int(yTop) )
+	cIp := C.CString(ip)
+	cUsrname := C.CString(userName)
+	cPasswd := C.CString(password)
+
+	defer C.free(unsafe.Pointer(cIp))
+	defer C.free(unsafe.Pointer(cUsrname))
+	defer C.free(unsafe.Pointer(cPasswd))
+
+	C.ptzTrack(cIp, cUsrname, cPasswd, C.int(port), C.int(channel), C.int(xBottom), C.int(xTop), C.int(yBottom), C.int(yTop))
 }
 
-// void  ToPreset(char *ip, char *userName, char *password, char *port  ,int channel )
+func ToPreset(ip, userName, password string, port, ch int) {
 
-func ToPreset(ip,userName,password string, port , ch int)  {
-	
 	cip := C.CString(ip)
-    defer C.free(unsafe.Pointer(cip))
-    cusrname := C.CString(userName)
-    defer C.free(unsafe.Pointer(cusrname))
-    cpasswd := C.CString(password)
-    defer C.free(unsafe.Pointer(cpasswd))
-    
+	defer C.free(unsafe.Pointer(cip))
+	cusrname := C.CString(userName)
+	defer C.free(unsafe.Pointer(cusrname))
+	cpasswd := C.CString(password)
+	defer C.free(unsafe.Pointer(cpasswd))
 
-	C.PtzTrack(cip, cusrname, cpasswd,  C.int(port), C.int(ch) )
-}
\ No newline at end of file
+	C.toPreset(cip, cusrname, cpasswd, C.int(port), C.int(ch))
+}
diff --git a/hikptz.h b/hikptz.h
index 66090b4..d1a587b 100644
--- a/hikptz.h
+++ b/hikptz.h
@@ -1,15 +1,15 @@
 #ifndef _HIKPTZ_H
 #define _HIKPTZ_H
- 
+
 #ifdef __cplusplus
-extern "C" { 
+extern "C"
+{
 #endif
- 
-void  PtzTrack(char *ip, char *userName, char *password, char *port  ,int channel ,int xBottom ,int xTop ,int yBottom ,int yTop  )
-void  ToPreset(char *ip, char *userName, char *password, int port  ,int channel )
- 
+
+    void ptzTrack(char *ip, char *userName, char *password, int port, int channel, int xBottom, int xTop, int yBottom, int yTop);
+    void toPreset(char *ip, char *userName, char *password, int port, int channel);
 #ifdef __cplusplus
 }
 #endif
- 
+
 #endif
\ No newline at end of file
diff --git a/hikptz_test.go b/hikptz_test.go
new file mode 100644
index 0000000..e01112e
--- /dev/null
+++ b/hikptz_test.go
@@ -0,0 +1,19 @@
+package hikControl
+
+import (
+	"fmt"
+	"testing"
+)
+
+func TestMain(m *testing.M) {
+	fmt.Println("begin")
+	ip := "192.168.5.35"
+	username := "admin"
+	passwd := "a1234567"
+	port := 8000
+	PtzTrack(ip, username, passwd, port, 1, 50, 50, 50, 50)
+	fmt.Println("end PtzTrack")
+
+	ToPreset(ip, username, passwd, port, 1)
+	fmt.Println("end ToPreset")
+}

--
Gitblit v1.8.0