Browse Source

相机树开发

gao.qiang 1 tháng trước cách đây
mục cha
commit
bb9c3ce101

+ 86 - 75
business-service/src/main/java/com/ozs/service/utils/PublishClient.java

@@ -2,10 +2,6 @@ package com.ozs.service.utils;
 
 import com.alibaba.fastjson2.JSON;
 import com.alibaba.fastjson2.JSONObject;
-import com.alibaba.fastjson2.filter.Filter;
-import com.alibaba.fastjson2.filter.SimplePropertyPreFilter;
-import com.ozs.common.utils.sign.Md5Utils;
-import com.ozs.common.utils.stateSecrets.SM4Utils;
 import com.ozs.common.utils.uuid.IdUtils;
 import com.ozs.service.entity.BaseCameraManagement;
 import com.ozs.service.entity.vo.*;
@@ -19,14 +15,13 @@ import org.eclipse.paho.client.mqttv3.MqttPersistenceException;
 import org.eclipse.paho.client.mqttv3.MqttTopic;
 import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Component;
 
 import javax.annotation.PostConstruct;
+import java.security.SecureRandom;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.Map;
-import java.util.UUID;
 
 
 /**
@@ -52,6 +47,8 @@ public class PublishClient {
     @Autowired
     private RabbitMqConfig rabbitMqConfig;
 
+    private static final String CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
+    private static final int CLIENT_ID_LENGTH = 23;
     /**
      * 获取 MqttTopic
      */
@@ -118,7 +115,7 @@ public class PublishClient {
         Map<String, Object> map = new HashMap<>();
         res.put("device_sn", "11");
         res.put("sign", "rate" + IdUtils.fastSimpleUUID());
-        map.put("codec",codec);
+        map.put("codec", codec);
         res.put("data", map);
         String s = JSONObject.toJSONString(res);
 
@@ -169,81 +166,81 @@ public class PublishClient {
         /**
          * 发布客户端
          */
-            try {
-                JSONObject res = new JSONObject();
-                res.put("Name", "HeartRequest");
-                Codec codec = new Codec();
-                Venc venc = new Venc();
-                venc.setFps(Double.valueOf(value));
-                ArrayList<Venc> vencList = new ArrayList<>();
-                vencList.add(venc);
-                codec.setVenc(vencList);
-                Map<String, Object> map = new HashMap<>();
-                res.put("device_sn", baseCameraManagement.getCameraSn());
-                res.put("sign", "rate" + IdUtils.fastSimpleUUID());
-                map.put("codec",codec);
-                res.put("data", map);
-                String s = JSONObject.toJSONString(res);
+        try {
+            JSONObject res = new JSONObject();
+            res.put("Name", "HeartRequest");
+            Codec codec = new Codec();
+            Venc venc = new Venc();
+            venc.setFps(Double.valueOf(value));
+            ArrayList<Venc> vencList = new ArrayList<>();
+            vencList.add(venc);
+            codec.setVenc(vencList);
+            Map<String, Object> map = new HashMap<>();
+            res.put("device_sn", baseCameraManagement.getCameraSn());
+            res.put("sign", "rate" + IdUtils.fastSimpleUUID());
+            map.put("codec", codec);
+            res.put("data", map);
+            String s = JSONObject.toJSONString(res);
 
-                MqttMessage message = new MqttMessage();
-                /**
-                 * 保证消息能到达一次
-                 */
-                message.setQos(1);
-                /**
-                 * 消息保留
-                 */
+            MqttMessage message = new MqttMessage();
+            /**
+             * 保证消息能到达一次
+             */
+            message.setQos(1);
+            /**
+             * 消息保留
+             */
 //              message.setRetained(false);
-                /**
-                 * 消息内容
-                 */
-                message.setPayload(s.getBytes());
-                /**
-                 * 发布
-                 */
-                publish(getMqttTopic("config_" + baseCameraManagement.getCameraCode()), message);
-            } catch (MqttException e) {
-                log.error(e.getMessage());
-            }
+            /**
+             * 消息内容
+             */
+            message.setPayload(s.getBytes());
+            /**
+             * 发布
+             */
+            publish(getMqttTopic("config_" + baseCameraManagement.getCameraCode()), message);
+        } catch (MqttException e) {
+            log.error(e.getMessage());
+        }
     }
 
-    public static void configFrameRate(BaseCameraManagement baseCameraManagement,Integer mode) {
+    public static void configFrameRate(BaseCameraManagement baseCameraManagement, Integer mode) {
         /**
          * 发布客户端
          */
-            try {
-                JSONObject res = new JSONObject();
-                res.put("Name", "HeartRequest");
-                Codec codec = new Codec();
-                Day2night day2night = new Day2night();
-                day2night.setMode(mode);
-                codec.setDay2night(day2night);
-                Map<String, Object> map = new HashMap<>();
-                res.put("device_sn", baseCameraManagement.getCameraSn());
-                res.put("sign", "rate" + IdUtils.fastSimpleUUID());
-                map.put("codec",codec);
-                res.put("data", map);
-                String s = JSONObject.toJSONString(res);
-                MqttMessage message = new MqttMessage();
-                /**
-                 * 保证消息能到达一次
-                 */
-                message.setQos(1);
-                /**
-                 * 消息保留
-                 */
+        try {
+            JSONObject res = new JSONObject();
+            res.put("Name", "HeartRequest");
+            Codec codec = new Codec();
+            Day2night day2night = new Day2night();
+            day2night.setMode(mode);
+            codec.setDay2night(day2night);
+            Map<String, Object> map = new HashMap<>();
+            res.put("device_sn", baseCameraManagement.getCameraSn());
+            res.put("sign", "rate" + IdUtils.fastSimpleUUID());
+            map.put("codec", codec);
+            res.put("data", map);
+            String s = JSONObject.toJSONString(res);
+            MqttMessage message = new MqttMessage();
+            /**
+             * 保证消息能到达一次
+             */
+            message.setQos(1);
+            /**
+             * 消息保留
+             */
 //              message.setRetained(false);
-                /**
-                 * 消息内容
-                 */
-                message.setPayload(s.getBytes());
-                /**
-                 * 发布
-                 */
-                publish(getMqttTopic("config_" + baseCameraManagement.getCameraCode()), message);
-            } catch (MqttException e) {
-                log.error(e.getMessage());
-            }
+            /**
+             * 消息内容
+             */
+            message.setPayload(s.getBytes());
+            /**
+             * 发布
+             */
+            publish(getMqttTopic("config_" + baseCameraManagement.getCameraCode()), message);
+        } catch (MqttException e) {
+            log.error(e.getMessage());
+        }
     }
 
     @PostConstruct
@@ -308,7 +305,7 @@ public class PublishClient {
             /**
              * 设置发布回调
              */
-            client = new MqttClient("tcp://10.161.12.60:1883", "HAZARD-CAMERA-CLIENTID-123", new MemoryPersistence());
+            client = new MqttClient("tcp://10.161.12.60:1883", generateClientId(), new MemoryPersistence());
 
             client.setCallback(new PublishCallback());
             client.connect(options);
@@ -320,4 +317,18 @@ public class PublishClient {
         }
         log.error("重连成功!");
     }
+
+
+
+    public static String generateClientId() {
+        StringBuilder clientId = new StringBuilder(CLIENT_ID_LENGTH);
+        SecureRandom random = new SecureRandom();
+
+        for (int i = 0; i < CLIENT_ID_LENGTH; i++) {
+            int index = random.nextInt(CHARACTERS.length());
+            clientId.append(CHARACTERS.charAt(index));
+        }
+
+        return clientId.toString();
+    }
 }