|
@@ -0,0 +1,282 @@
|
|
|
+package com.greentech.gateservice.controller;
|
|
|
+
|
|
|
+import com.netsdk.lib.NetSDKLib;
|
|
|
+import com.netsdk.lib.ToolKits;
|
|
|
+import com.sun.jna.Pointer;
|
|
|
+import org.springframework.web.bind.annotation.*;
|
|
|
+
|
|
|
+import java.io.File;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.Objects;
|
|
|
+import java.util.concurrent.atomic.AtomicReference;
|
|
|
+import com.greentech.gateservice.entity.*;
|
|
|
+
|
|
|
+@RestController
|
|
|
+public class CameraController {
|
|
|
+
|
|
|
+ public static NetSDKLib netsdk;
|
|
|
+ private static boolean bInit;
|
|
|
+ private static boolean bLogopen;
|
|
|
+
|
|
|
+ private final static Map<String, CameraDeviceInfo> cameras = new HashMap<String, CameraDeviceInfo>();
|
|
|
+
|
|
|
+ public CameraController() {
|
|
|
+ if (cameras.isEmpty()) {
|
|
|
+ CameraController.initCameras();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /*
|
|
|
+ 192.168.1.22 37777 7L0CA4EPAN7C226
|
|
|
+ 192.168.1.18 37777 7L0CA4EPAN4FD32
|
|
|
+ 192.168.1.20 37777 7L0CA4EPAN792C3
|
|
|
+ 192.168.1.39 37777 7L0CA4EPAN559BF
|
|
|
+ 192.168.1.42 37777 7L0CA4EPANB2BC5
|
|
|
+ 192.168.1.41 37777 7L0CA4EPAN1EB90
|
|
|
+ 192.168.1.38 37777 7L0CA4EPAN18D6F
|
|
|
+ 192.168.1.29 37777 7L05B8FPAN300FB
|
|
|
+ 192.168.1.36 37777 7L0CA4EPAN1183F
|
|
|
+ 192.168.1.40 37777 7L0CA4EPAN96585
|
|
|
+ 192.168.1.17 37777 7L0CA4EPANFD507
|
|
|
+ 192.168.1.19 37777 7L0CA4EPANB6A9C
|
|
|
+ * */
|
|
|
+ private static void initCameras() {
|
|
|
+ cameras.put("7L07A7FPAZDD374", new CameraDeviceInfo("7L07A7FPAZDD374", "192.168.31.240", 37777, "admin", "hao123456"));
|
|
|
+ cameras.put("7L0CA4EPAN7C226", new CameraDeviceInfo("7L0CA4EPAN7C226", "192.168.1.22", 37777, "admin", "hao123456"));
|
|
|
+ cameras.put("7L0CA4EPAN4FD32", new CameraDeviceInfo("7L0CA4EPAN4FD32", "192.168.1.18", 37777, "admin", "hao123456"));
|
|
|
+ cameras.put("7L0CA4EPAN792C3", new CameraDeviceInfo("7L0CA4EPAN792C3", "192.168.1.20", 37777, "admin", "hao123456"));
|
|
|
+ cameras.put("7L0CA4EPAN559BF", new CameraDeviceInfo("7L0CA4EPAN559BF", "192.168.1.39", 37777, "admin", "hao123456"));
|
|
|
+ cameras.put("7L0CA4EPANB2BC5", new CameraDeviceInfo("7L0CA4EPANB2BC5", "192.168.1.42", 37777, "admin", "hao123456"));
|
|
|
+ cameras.put("7L0CA4EPAN1EB90", new CameraDeviceInfo("7L0CA4EPAN1EB90", "192.168.1.41", 37777, "admin", "hao123456"));
|
|
|
+ cameras.put("7L0CA4EPAN18D6F", new CameraDeviceInfo("7L0CA4EPAN18D6F", "192.168.1.38", 37777, "admin", "hao123456"));
|
|
|
+ cameras.put("7L05B8FPAN300FB", new CameraDeviceInfo("7L05B8FPAN300FB", "192.168.1.29", 37777, "admin", "hao123456"));
|
|
|
+ cameras.put("7L0CA4EPAN1183F", new CameraDeviceInfo("7L0CA4EPAN1183F", "192.168.1.36", 37777, "admin", "hao123456"));
|
|
|
+ cameras.put("7L0CA4EPAN96585", new CameraDeviceInfo("7L0CA4EPAN96585", "192.168.1.40", 37777, "admin", "hao123456"));
|
|
|
+ cameras.put("7L0CA4EPANFD507", new CameraDeviceInfo("7L0CA4EPANFD507", "192.168.1.17", 37777, "admin", "hao123456"));
|
|
|
+ cameras.put("7L0CA4EPANB6A9C", new CameraDeviceInfo("7L0CA4EPANB6A9C", "192.168.1.19", 37777, "admin", "hao123456"));
|
|
|
+ System.out.format("init cameras finish total: %d", cameras.size());
|
|
|
+ }
|
|
|
+
|
|
|
+ public static CameraDeviceInfo getCameraDeviceInfoForIp(String ip) {
|
|
|
+ AtomicReference<CameraDeviceInfo> cdi = new AtomicReference<>(new CameraDeviceInfo("", ip, 37777, "admin", "hao123456"));
|
|
|
+ cameras.forEach((k, item) -> {
|
|
|
+ if (Objects.equals(item.ip, ip)) {
|
|
|
+ cdi.set(item);
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ return cdi.get();
|
|
|
+ }
|
|
|
+
|
|
|
+ @ResponseBody
|
|
|
+ @PostMapping("/camera/ctl")
|
|
|
+ public CameraResponse.ResponseStruct ctl(@RequestBody PTZControlParams controlParams) {
|
|
|
+ CameraDeviceInfo deviceInfo = cameras.get(controlParams.serialNumber);
|
|
|
+ if (deviceInfo == null) {
|
|
|
+ return CameraDeviceInfo.response(10001, String.format("not found serial number: %s", controlParams.serialNumber), null);
|
|
|
+ }
|
|
|
+
|
|
|
+ int cmd = parsePTZCommand(controlParams.vertical, controlParams.horizontal);
|
|
|
+ int p1 = 0, p2 = 0, stop = 0;
|
|
|
+ if (cmd == -1) {
|
|
|
+ // 停止先前的操作
|
|
|
+ cmd = deviceInfo.lastCommand;
|
|
|
+ stop = 1;
|
|
|
+ } else if (cmd == 0 || cmd == 1) {
|
|
|
+ p2 = Math.abs(controlParams.vertical);
|
|
|
+ } else if (cmd == 2 || cmd == 3) {
|
|
|
+ p2 = Math.abs(controlParams.horizontal);
|
|
|
+ } else {
|
|
|
+ p1 = Math.abs(controlParams.vertical);
|
|
|
+ p2 = Math.abs(controlParams.horizontal);
|
|
|
+ }
|
|
|
+
|
|
|
+ boolean ctl = netsdk.CLIENT_DHPTZControlEx(deviceInfo.loginHandle, 0, cmd, p1, p2, 0, stop);
|
|
|
+ if (!ctl) {
|
|
|
+ return CameraResponse.response(10007, String.format("control ptz fail: %s", controlParams.serialNumber), null);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 操作成功后, 记录上一次的指令:方向
|
|
|
+ deviceInfo.lastCommand = cmd;
|
|
|
+
|
|
|
+ return CameraResponse.response(0, "control ptz success", deviceInfo);
|
|
|
+ }
|
|
|
+
|
|
|
+ @ResponseBody
|
|
|
+ @PostMapping("/camera/login")
|
|
|
+ public CameraResponse.ResponseStruct login(@RequestParam String serialNumber) {
|
|
|
+ CameraDeviceInfo deviceInfo = cameras.get(serialNumber);
|
|
|
+ if (deviceInfo == null) {
|
|
|
+ return CameraResponse.response(10001, String.format("not found serial number: %s", serialNumber), null);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!login(deviceInfo)) {
|
|
|
+ return CameraResponse.response(10002, "device login fail", null);
|
|
|
+ }
|
|
|
+
|
|
|
+ return CameraResponse.response(0, "login success", deviceInfo);
|
|
|
+ }
|
|
|
+
|
|
|
+ @PostMapping("/camera")
|
|
|
+ public CameraResponse.ResponseStruct post(@RequestBody CameraDeviceInfo deviceInfo) {
|
|
|
+ if (cameras.containsKey(deviceInfo.serialNumber)) {
|
|
|
+ return CameraResponse.response(10003, "device exists, allow modify", null);
|
|
|
+ }
|
|
|
+
|
|
|
+ synchronized (cameras) {
|
|
|
+ cameras.put(deviceInfo.serialNumber, deviceInfo);
|
|
|
+ }
|
|
|
+
|
|
|
+ return CameraResponse.response(0, String.format("add device ok and len is %d", cameras.size()), deviceInfo);
|
|
|
+ }
|
|
|
+
|
|
|
+ @PutMapping("/camera")
|
|
|
+ public CameraResponse.ResponseStruct put(@RequestBody CameraDeviceInfo deviceInfo) {
|
|
|
+ if (!cameras.containsKey(deviceInfo.serialNumber)) {
|
|
|
+ return CameraResponse.response(10004, "device not exists, not allow modify", null);
|
|
|
+ }
|
|
|
+
|
|
|
+ synchronized (cameras) {
|
|
|
+ cameras.replace(deviceInfo.serialNumber, deviceInfo);
|
|
|
+ }
|
|
|
+
|
|
|
+ return CameraResponse.response(0, "update device ok", null);
|
|
|
+ }
|
|
|
+
|
|
|
+ @GetMapping("/camera")
|
|
|
+ public CameraResponse.ResponseStruct get(@RequestParam String serialNumber) {
|
|
|
+ CameraDeviceInfo deviceInfo = cameras.get(serialNumber);
|
|
|
+
|
|
|
+ if (deviceInfo == null) {
|
|
|
+ return CameraResponse.response(10005, String.format("device not exists: %s", serialNumber), null);
|
|
|
+ }
|
|
|
+
|
|
|
+ return CameraResponse.response(0, "ok", deviceInfo);
|
|
|
+ }
|
|
|
+
|
|
|
+ @DeleteMapping("/camera")
|
|
|
+ public CameraResponse.ResponseStruct delete(@RequestParam String serialNumber) {
|
|
|
+ if (!cameras.containsKey(serialNumber)) {
|
|
|
+ return CameraResponse.response(10006, "device not exists", null);
|
|
|
+ }
|
|
|
+
|
|
|
+ CameraDeviceInfo deviceInfo;
|
|
|
+
|
|
|
+ synchronized (cameras) {
|
|
|
+ deviceInfo = cameras.remove(serialNumber);
|
|
|
+ }
|
|
|
+
|
|
|
+ return CameraResponse.response(0, String.format("remove success and reside: %d", cameras.size()), deviceInfo);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static boolean login(CameraDeviceInfo deviceInfo) {
|
|
|
+ deviceInfo.deviceInfoEx = new NetSDKLib.NET_DEVICEINFO_Ex();
|
|
|
+ deviceInfo.loginHandle = new NetSDKLib.LLong(0L);
|
|
|
+
|
|
|
+ NetSDKLib.NET_IN_LOGIN_WITH_HIGHLEVEL_SECURITY pstInParam = new NetSDKLib.NET_IN_LOGIN_WITH_HIGHLEVEL_SECURITY();
|
|
|
+ pstInParam.nPort = deviceInfo.port;
|
|
|
+ pstInParam.szIP = deviceInfo.ip.getBytes();
|
|
|
+ pstInParam.szPassword = deviceInfo.password.getBytes();
|
|
|
+ pstInParam.szUserName = deviceInfo.user.getBytes();
|
|
|
+ NetSDKLib.NET_OUT_LOGIN_WITH_HIGHLEVEL_SECURITY pstOutParam = new NetSDKLib.NET_OUT_LOGIN_WITH_HIGHLEVEL_SECURITY();
|
|
|
+ pstOutParam.stuDeviceInfo = deviceInfo.deviceInfoEx;
|
|
|
+ deviceInfo.loginHandle = netsdk.CLIENT_LoginWithHighLevelSecurity(pstInParam, pstOutParam);
|
|
|
+ if (deviceInfo.loginHandle.longValue() == 0L) {
|
|
|
+ System.err.printf("Login Device[%s] Port[%d]Failed. %s\n", deviceInfo.ip, deviceInfo.port, ToolKits.getErrorCodePrint());
|
|
|
+ return false;
|
|
|
+ } else {
|
|
|
+ System.out.println("Login Success [ " + deviceInfo.ip + " ]");
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+//
|
|
|
+// return m_hLoginHandle.longValue() != 0L;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static int parsePTZCommand(Integer vertical, Integer horizontal) {
|
|
|
+ // 右上
|
|
|
+ if (vertical > 0 && horizontal > 0) {
|
|
|
+ return 33;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 左上
|
|
|
+ if (vertical > 0 && horizontal < 0) {
|
|
|
+ return 32;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 左下
|
|
|
+ if (vertical < 0 && horizontal < 0) {
|
|
|
+ return 34;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 右下
|
|
|
+ if (vertical < 0 && horizontal > 0) {
|
|
|
+ return 35;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 上
|
|
|
+ if (vertical > 0) {
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 下
|
|
|
+ if (vertical < 0) {
|
|
|
+ return 1;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 左
|
|
|
+ if (horizontal < 0) {
|
|
|
+ return 2;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 右
|
|
|
+ if (horizontal > 0) {
|
|
|
+ return 3;
|
|
|
+ }
|
|
|
+
|
|
|
+ // -1 表示不支持的方向, 此时可停止上一次的指令
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static boolean init(NetSDKLib.fDisConnect disConnect, NetSDKLib.fHaveReConnect haveReConnect) {
|
|
|
+ bInit = netsdk.CLIENT_Init(disConnect, (Pointer)null);
|
|
|
+ if (!bInit) {
|
|
|
+ System.out.println("Initialize SDK failed");
|
|
|
+ return false;
|
|
|
+ } else {
|
|
|
+ NetSDKLib.LOG_SET_PRINT_INFO setLog = new NetSDKLib.LOG_SET_PRINT_INFO();
|
|
|
+ File path = new File("./sdklog/");
|
|
|
+ if (!path.exists()) {
|
|
|
+ path.mkdir();
|
|
|
+ }
|
|
|
+
|
|
|
+ String logPath = path.getAbsoluteFile().getParent() + "\\sdklog\\" + ToolKits.getDate() + ".log";
|
|
|
+ setLog.nPrintStrategy = 0;
|
|
|
+ setLog.bSetFilePath = 1;
|
|
|
+ System.arraycopy(logPath.getBytes(), 0, setLog.szLogFilePath, 0, logPath.getBytes().length);
|
|
|
+ System.out.println(logPath);
|
|
|
+ setLog.bSetPrintStrategy = 1;
|
|
|
+ bLogopen = netsdk.CLIENT_LogOpen(setLog);
|
|
|
+ if (!bLogopen) {
|
|
|
+ System.err.println("Failed to open NetSDK log");
|
|
|
+ }
|
|
|
+
|
|
|
+ netsdk.CLIENT_SetAutoReconnect(haveReConnect, (Pointer)null);
|
|
|
+ int waitTime = 5000;
|
|
|
+ int tryTimes = 1;
|
|
|
+ netsdk.CLIENT_SetConnectTime(waitTime, tryTimes);
|
|
|
+ NetSDKLib.NET_PARAM netParam = new NetSDKLib.NET_PARAM();
|
|
|
+ netParam.nConnectTime = 10000;
|
|
|
+ netParam.nGetConnInfoTime = 3000;
|
|
|
+ netParam.nGetDevInfoTime = 3000;
|
|
|
+ netsdk.CLIENT_SetNetworkParam(netParam);
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ static {
|
|
|
+ netsdk = NetSDKLib.NETSDK_INSTANCE;
|
|
|
+ bInit = false;
|
|
|
+ bLogopen = false;
|
|
|
+ }
|
|
|
+}
|