Jelajahi Sumber

合并门禁和视频功能

yanzheng 1 tahun lalu
induk
melakukan
4300ea523b

+ 31 - 0
pom.xml

@@ -80,6 +80,37 @@
             <version>2.6.2</version>
         </dependency>
 
+        <!-- for video updated -->
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-data-redis</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>redis.clients</groupId>
+            <artifactId>jedis</artifactId>
+            <type>jar</type>
+        </dependency>
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-data-redis-reactive</artifactId>
+            <version>2.6.3</version>
+        </dependency>
+<!--        <dependency>
+            <groupId>io.projectreactor</groupId>
+            <artifactId>reactor-test</artifactId>
+            <scope>test</scope>
+        </dependency>-->
+        <dependency>
+            <groupId>net.java.dev.jna</groupId>
+            <artifactId>jna</artifactId>
+            <version>5.4.0</version>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.httpcomponents</groupId>
+            <artifactId>httpclient</artifactId>
+            <version>4.5.13</version>
+        </dependency>
+        <!-- for video updated end-->
     </dependencies>
 
     <build>

+ 15 - 1
src/main/java/com/greentech/gateservice/GateServiceApplication.java

@@ -3,13 +3,27 @@ package com.greentech.gateservice;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
 import org.springframework.scheduling.annotation.EnableScheduling;
-
+import redis.clients.jedis.JedisPool;
+import com.greentech.gateservice.service.*;
+import com.greentech.gateservice.controller.CameraController;
+import com.greentech.gateservice.redis.SubThread;
 
 @SpringBootApplication
 @EnableScheduling
 public class GateServiceApplication {
+    private static final JedisPool jedisPool = new JedisPool("47.96.12.136", 6379);
 
     public static void main(String[] args) {
+
+        boolean ok = CameraController.init(new CameraDisConnect(jedisPool), new CameraReConnect(jedisPool));
+        if (!ok) {
+            System.out.println("dahua sdk init fail");
+            System.exit(1);
+        }
+
+        SubThread subThread = new SubThread(jedisPool);
+        subThread.start();
+
         SpringApplication.run(GateServiceApplication.class, args);
     }
 

+ 282 - 0
src/main/java/com/greentech/gateservice/controller/CameraController.java

@@ -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;
+    }
+}

+ 60 - 0
src/main/java/com/greentech/gateservice/entity/CameraDeviceInfo.java

@@ -0,0 +1,60 @@
+package com.greentech.gateservice.entity;
+
+import com.netsdk.lib.NetSDKLib;
+
+public class CameraDeviceInfo extends CameraResponse {
+    public String serialNumber;
+    public String ip;
+    public Integer port;
+    public String user;
+    public String password;
+    public NetSDKLib.NET_DEVICEINFO_Ex deviceInfoEx;
+    public NetSDKLib.LLong loginHandle;
+    public int lastCommand;
+
+    public CameraDeviceInfo(String sn, String sip, Integer sport, String suser, String spassword) {
+        serialNumber = sn;
+        ip = sip;
+        port = sport;
+        user = suser;
+        password = spassword;
+    }
+
+    public Integer getLastCommand() {
+        return this.lastCommand;
+    }
+
+    public String getSerialNumber() {
+        return this.serialNumber;
+    }
+
+    public String getIp() {
+        return this.ip;
+    }
+
+    public Integer getPort() {
+        return this.port;
+    }
+
+    public String getUser() {
+        return this.user;
+    }
+
+    public String getPassword() {
+        return "******";
+    }
+
+    public String getDeviceInfoEx() {
+        if (this.deviceInfoEx == null) {
+            return "";
+        }
+        return this.deviceInfoEx.toString();
+    }
+
+    public String getLoginHandle() {
+        if (this.loginHandle == null) {
+            return "";
+        }
+        return this.loginHandle.toString();
+    }
+}

+ 46 - 0
src/main/java/com/greentech/gateservice/entity/CameraResponse.java

@@ -0,0 +1,46 @@
+package com.greentech.gateservice.entity;
+//原package com.greentech.controller.Controller.java;
+
+import org.springframework.web.bind.annotation.ResponseBody;
+
+
+public class CameraResponse {
+
+    @ResponseBody
+    public static ResponseStruct response(Integer code, String msg, Object data) {
+        return new ResponseStruct(code, msg, data);
+    }
+
+
+    public static class ResponseStruct {
+        Integer code;
+        String msg;
+        Object data;
+
+        public ResponseStruct(Integer code, String msg, Object data) {
+            this.code = code;
+            this.msg = msg;
+            this.data = data;
+        }
+
+        public void setCode(Integer code) {
+            this.code = code;
+        }
+
+        public Integer getCode() {
+            return this.code;
+        }
+
+        public String getMsg() {
+            return msg;
+        }
+
+        public void setMsg(String msg) {
+            this.msg = msg;
+        }
+
+        public Object getData() {
+            return this.data;
+        }
+    }
+}

+ 8 - 0
src/main/java/com/greentech/gateservice/entity/PTZControlParams.java

@@ -0,0 +1,8 @@
+package com.greentech.gateservice.entity;
+
+public class PTZControlParams {
+    // Integer vertical, Integer horizontal)
+    public String serialNumber;
+    public int vertical;
+    public int horizontal;
+}

+ 45 - 0
src/main/java/com/greentech/gateservice/redis/Publisher.java

@@ -0,0 +1,45 @@
+package com.greentech.gateservice.redis;
+
+import redis.clients.jedis.Jedis;
+import redis.clients.jedis.JedisPool;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+
+public class Publisher extends Thread {
+    private final JedisPool jedisPool;
+
+
+    public Publisher(JedisPool jedisPool) {
+        this.jedisPool = jedisPool;
+    }
+
+    @Override
+    public void run() {
+        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
+        Jedis jedis = jedisPool.getResource();
+
+        while (true) {
+            String line = null;
+            try {
+                line = reader.readLine();
+                if (!"quit".equals(line)) {
+                    jedis.publish("channel:ptz:92", line);
+                } else {
+                    break;
+                }
+            } catch (IOException e) {
+                e.printStackTrace();
+//                throw new RuntimeException(e);
+            }
+        }
+
+    }
+
+    public void publish(String channel, String message) {
+        Jedis jedis = jedisPool.getResource();
+
+        jedis.publish(channel, message);
+    }
+}

+ 26 - 0
src/main/java/com/greentech/gateservice/redis/SubThread.java

@@ -0,0 +1,26 @@
+package com.greentech.gateservice.redis;
+
+import redis.clients.jedis.Jedis;
+import redis.clients.jedis.JedisPool;
+
+public class SubThread extends Thread{
+
+    private final JedisPool jedisPool;
+
+    private final Subscriber subscriber = new Subscriber();
+
+    public SubThread(JedisPool jedisPool) {
+        this.jedisPool = jedisPool;
+    }
+
+    @Override
+    public void run() {
+
+        try (Jedis jedis = jedisPool.getResource()) {
+            String channel = "channel:ptz:92";
+            jedis.subscribe(subscriber, channel);
+        } catch (Exception e) {
+            System.out.printf("subscriber channel error, %s%n\n", e);
+        }
+    }
+}

+ 71 - 0
src/main/java/com/greentech/gateservice/redis/Subscriber.java

@@ -0,0 +1,71 @@
+package com.greentech.gateservice.redis;
+
+import com.greentech.gateservice.controller.CameraController;
+import com.greentech.gateservice.entity.CameraResponse;
+import com.greentech.gateservice.entity.PTZControlParams;
+import redis.clients.jedis.JedisPubSub;
+
+import java.util.Objects;
+
+public class Subscriber extends JedisPubSub {
+    public Subscriber(){
+
+    }
+
+    @Override
+    public void onMessage(String channel, String message) {
+        System.out.println(String.format("reveive redis published message, channel %s, message %s", channel, message));
+
+        System.out.format("reveive message: %s\n", message);
+
+        String action = "";
+        String sn = "";
+        int v = 0;
+        int h = 0;
+
+        String[] msgStruct = message.split(":");
+        if (msgStruct.length >= 2) {
+            action = msgStruct[0];
+            sn = msgStruct[1];
+            if (Objects.equals(action, "control") && msgStruct.length >= 4) {
+                v = Integer.parseInt(msgStruct[2]);
+                h = Integer.parseInt(msgStruct[3]);
+            }
+        }
+
+        switch (action) {
+            case "login":
+                receivePTZLogin(sn);
+                break;
+            case "control":
+                receivePTZControl(sn, v, h);
+                break;
+            default:
+                System.out.format("not support action: %s\n", action);
+        }
+    }
+
+    public void receivePTZControl(String serialNumber, int vertical, int horizontal) {
+        PTZControlParams params = new PTZControlParams();
+        params.serialNumber = serialNumber;
+        params.vertical = vertical;
+        params.horizontal = horizontal;
+        CameraResponse.ResponseStruct res = new CameraController().ctl(params);
+        if (res.getCode() == 0) {
+            System.out.format("PTZControl control success for %s\n", serialNumber);
+        } else {
+            System.out.format("PTZControl control error for %s errors: %s\n", serialNumber, res.getMsg());
+        }
+    }
+
+    // redis topic: channel:ptz:92
+    // redis topic message -> login:${sn}
+    public void receivePTZLogin(String serialNumber) {
+        CameraResponse.ResponseStruct res = new CameraController().login(serialNumber);
+        if (res.getCode() == 0) {
+            System.out.format("PTZControl login success for %s\n", serialNumber);
+        } else {
+            System.out.format("PTZControl login error for %s errors: %s\n", serialNumber, res.getMsg());
+        }
+    }
+}

+ 35 - 0
src/main/java/com/greentech/gateservice/service/CameraDisConnect.java

@@ -0,0 +1,35 @@
+package com.greentech.gateservice.service;
+
+//原com.greentech.controller.DisConnect.java
+import com.greentech.gateservice.util.HttpRequest;
+import com.netsdk.lib.NetSDKLib;
+import com.sun.jna.Pointer;
+import redis.clients.jedis.Jedis;
+import redis.clients.jedis.JedisPool;
+import com.greentech.gateservice.controller.CameraController;
+import com.greentech.gateservice.entity.*;
+
+public class CameraDisConnect implements NetSDKLib.fDisConnect {
+    private static JedisPool jedisPool = null;
+    public CameraDisConnect(JedisPool jedisPoolx) {
+        jedisPool = jedisPoolx;
+    }
+
+    @Override
+    public void invoke(NetSDKLib.LLong lLong, String s, int i, Pointer pointer) {
+        CameraDeviceInfo cdi = CameraController.getCameraDeviceInfoForIp(s);
+
+        if (cdi != null) {
+            System.out.format("offline:%s:%s\n", cdi.serialNumber, cdi.ip);
+            try {
+                Jedis publisher = jedisPool.getResource();
+
+                publisher.publish("channel:ptz:92",  String.format("offline:%s:%s", cdi.serialNumber, cdi.ip));
+                String response = HttpRequest.put(String.format("http://47.96.12.136:8788/api/v1/monitor/online_status/92/%s/0", cdi.serialNumber), "");
+                System.out.format("sync device online status to offline result: %s => %s", cdi.serialNumber, response);
+            } catch (Exception e) {
+                System.out.format("sync device online status to offline exception: %s => %s", cdi.serialNumber, e.getMessage());
+            }
+        }
+    }
+}

+ 35 - 0
src/main/java/com/greentech/gateservice/service/CameraReConnect.java

@@ -0,0 +1,35 @@
+package com.greentech.gateservice.service;
+
+////原com.greentech.controller.ReConnect.java
+import com.greentech.gateservice.util.HttpRequest;
+import com.netsdk.lib.NetSDKLib;
+import com.sun.jna.Pointer;
+import redis.clients.jedis.Jedis;
+import redis.clients.jedis.JedisPool;
+import com.greentech.gateservice.entity.*;
+import com.greentech.gateservice.controller.CameraController;
+
+public class CameraReConnect implements NetSDKLib.fHaveReConnect {
+
+    private static JedisPool jedisPool = null;
+    public CameraReConnect(JedisPool jedisPoolx) {
+        jedisPool = jedisPoolx;
+    }
+
+    @Override
+    public void invoke(NetSDKLib.LLong lLong, String s, int i, Pointer pointer) {
+        CameraDeviceInfo cdi = CameraController.getCameraDeviceInfoForIp(s);
+
+        if (cdi != null) {
+            System.out.format("online:%s:%s", cdi.serialNumber, cdi.ip);
+            try {
+                Jedis publisher = jedisPool.getResource();
+                publisher.publish("channel:ptz:92", String.format("online:%s:%s", cdi.serialNumber, cdi.ip));
+                String response = HttpRequest.put(String.format("http://47.96.12.136:8788/api/v1/monitor/online_status/92/%s/1", cdi.serialNumber), "");
+                System.out.format("sync device online status to online result: %s => %s", cdi.serialNumber, response);
+            } catch (Exception e) {
+                System.out.format("sync device online status to online exception: %s => %s", cdi.serialNumber, e.getMessage());
+            }
+        }
+    }
+}

+ 49 - 0
src/main/java/com/greentech/gateservice/util/HttpRequest.java

@@ -0,0 +1,49 @@
+package com.greentech.gateservice.util;
+//原import org.apache.http.util.EntityUtils.HttpRequest.java;
+import org.apache.http.HttpEntity;
+import org.apache.http.HttpResponse;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.client.methods.HttpPut;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClients;
+import org.apache.http.util.EntityUtils;
+
+import java.io.IOException;
+
+public class HttpRequest {
+
+    public static String post(String url, String requestBody) throws IOException {
+        CloseableHttpClient client = HttpClients.createDefault();
+        HttpPost httpPost = new HttpPost();
+        StringEntity requestEntify = new StringEntity(requestBody, "UTF-8");
+        httpPost.setEntity(requestEntify);
+
+        HttpResponse resp = client.execute(httpPost);
+
+        HttpEntity httpEntity = resp.getEntity();
+
+        if (httpEntity != null) {
+            return EntityUtils.toString(httpEntity);
+        }
+
+        return null;
+    }
+
+    public static String put(String url, String requestBody) throws IOException {
+        CloseableHttpClient client = HttpClients.createDefault();
+        HttpPut httpPut = new HttpPut();
+        StringEntity requestEntify = new StringEntity(requestBody, "UTF-8");
+        httpPut.setEntity(requestEntify);
+
+        HttpResponse resp = client.execute(httpPut);
+
+        HttpEntity httpEntity = resp.getEntity();
+
+        if (httpEntity != null) {
+            return EntityUtils.toString(httpEntity);
+        }
+
+        return null;
+    }
+}