fuyuchuan 3 дней назад
Родитель
Сommit
6f127a2658

+ 27 - 24
service-cdi/service-cdi-biz/src/main/java/com/usky/cdi/service/impl/AlarmDataTransferService.java

@@ -2,23 +2,19 @@ package com.usky.cdi.service.impl;
 
 import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONObject;
-import com.alibaba.nacos.shaded.com.google.gson.Gson;
+// import com.alibaba.nacos.shaded.com.google.gson.Gson;
 import com.usky.cdi.service.config.mqtt.MqttOutConfig;
+import com.usky.cdi.service.mqtt.MqttConnectionTool;
 import com.usky.cdi.service.util.SnowflakeIdGenerator;
-import com.usky.cdi.service.vo.alarm.AlarmMessage1VO;
-import com.usky.cdi.service.vo.alarm.AlarmMessage2VO;
-import com.usky.cdi.service.vo.alarm.AlarmMessage3VO;
 import com.usky.cdi.service.vo.alarm.AlarmMessageVO;
-import com.usky.cdi.service.vo.base.EngineeringBaseVO;
-import com.usky.cdi.service.vo.base.FloorPlaneVO;
 import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
 import org.springframework.stereotype.Service;
 
 import javax.annotation.Resource;
-import java.nio.file.Files;
-import java.nio.file.Paths;
 import java.text.SimpleDateFormat;
+import java.time.LocalDateTime;
 import java.util.Date;
 import java.util.HashMap;
 
@@ -31,9 +27,10 @@ import java.util.HashMap;
  */
 @Slf4j
 @Service
-@ConditionalOnProperty(prefix = "mqtt", value = {"enabled"}, havingValue = "true")
 public class AlarmDataTransferService {
 
+    @Autowired
+    private MqttConnectionTool mqttConnectionTool;
     @Resource
     private MqttOutConfig.MqttGateway mqttGateway;
 
@@ -42,7 +39,7 @@ public class AlarmDataTransferService {
 
     public AlarmDataTransferService() {
         // 使用默认的workerId和datacenterId,实际项目中可以从配置读取
-        this.idGenerator = new SnowflakeIdGenerator(1L, 1L);
+        this.idGenerator = new SnowflakeIdGenerator(3L, 3L);
     }
 
     /**
@@ -72,10 +69,10 @@ public class AlarmDataTransferService {
                 vo.setDataPacketID(generateDataPacketID());
             }
             if (vo.getPublishTime() == null) {
-                vo.setPublishTime(getCurrentTime());
+                vo.setPublishTime(LocalDateTime.now());
             }
 
-            HashMap<String, Object> map = new HashMap<>();
+//            HashMap<String, Object> map = new HashMap<>();
 //            map.put("dataPacketID", vo.getDataPacketID());
 //            map.put("engineeringID", vo.getEngineeringID());
 //            map.put("floor", vo.getFloor());
@@ -86,12 +83,13 @@ public class AlarmDataTransferService {
 //            map.put("filePixHeight", vo.getFilePixHeight());
 //            map.put("floorFile", imageBytes);
 //            map.put("publishTime", vo.getPublishTime());
-            Gson gson = new Gson();
+//            Gson gson = new Gson();
             JSONObject jsonObject = (JSONObject) JSON.toJSON(vo);
             String json = jsonObject.toJSONString();
             System.out.println(json);
+            MqttConnectionTool.MqttGateway IQeIRyXG = mqttConnectionTool.connectOrRefresh("3101070011", "5RqhJ7VG");
             String topic = "alarm/message";
-            mqttGateway.sendToMqtt(topic, json);
+            IQeIRyXG.sendToMqtt(topic, json);
 
             return true;
         } catch (Exception e) {
@@ -107,13 +105,15 @@ public class AlarmDataTransferService {
      * @param vo 楼层平面图信息
      * @return 是否发送成功
      */
-    public boolean sendAlarmMessage1(AlarmMessage1VO vo) {
+    public boolean sendAlarmMessage1(AlarmMessageVO vo) {
+        MqttConnectionTool.MqttGateway IQeIRyXG = mqttConnectionTool.connectOrRefresh("3101070011", "5RqhJ7VG");
+
         try {
             if (vo.getDataPacketID() == null) {
                 vo.setDataPacketID(generateDataPacketID());
             }
             if (vo.getPublishTime() == null) {
-                vo.setPublishTime(getCurrentTime());
+                vo.setPublishTime(LocalDateTime.now());
             }
 
             HashMap<String, Object> map = new HashMap<>();
@@ -127,12 +127,12 @@ public class AlarmDataTransferService {
 //            map.put("filePixHeight", vo.getFilePixHeight());
 //            map.put("floorFile", imageBytes);
 //            map.put("publishTime", vo.getPublishTime());
-            Gson gson = new Gson();
+//             Gson gson = new Gson();
             JSONObject jsonObject = (JSONObject) JSON.toJSON(vo);
             String json = jsonObject.toJSONString();
             System.out.println(json);
             String topic = "alarm/message";
-            mqttGateway.sendToMqtt(topic, json);
+            IQeIRyXG.sendToMqtt(topic, json);
 
             return true;
         } catch (Exception e) {
@@ -141,20 +141,21 @@ public class AlarmDataTransferService {
         }
     }
 
-    public boolean sendAlarmMessage2(AlarmMessage2VO vo) {
+    public boolean sendAlarmMessage2(AlarmMessageVO vo) {
         try {
             if (vo.getDataPacketID() == null) {
                 vo.setDataPacketID(generateDataPacketID());
             }
             if (vo.getPublishTime() == null) {
-                vo.setPublishTime(getCurrentTime());
+                vo.setPublishTime(LocalDateTime.now());
             }
 
             JSONObject jsonObject = (JSONObject) JSON.toJSON(vo);
             String json = jsonObject.toJSONString();
             System.out.println(json);
+            MqttConnectionTool.MqttGateway IQeIRyXG = mqttConnectionTool.connectOrRefresh("3101070011", "5RqhJ7VG");
             String topic = "alarm/message";
-            mqttGateway.sendToMqtt(topic, json);
+            IQeIRyXG.sendToMqtt(topic, json);
 
             return true;
         } catch (Exception e) {
@@ -163,20 +164,22 @@ public class AlarmDataTransferService {
         }
     }
 
-    public boolean sendEngineeringBase(AlarmMessage3VO vo) {
+    public boolean sendEngineeringBase(AlarmMessageVO vo) {
         try {
             if (vo.getDataPacketID() == null) {
                 vo.setDataPacketID(generateDataPacketID());
             }
             if (vo.getPublishTime() == null) {
-                vo.setPublishTime(getCurrentTime());
+                vo.setPublishTime(LocalDateTime.now());
             }
 
+            MqttConnectionTool.MqttGateway IQeIRyXG = mqttConnectionTool.connectOrRefresh("3101070011", "5RqhJ7VG");
+
             JSONObject jsonObject = (JSONObject) JSON.toJSON(vo);
             String json = jsonObject.toJSONString();
             String topic = "alarm/message";
             System.out.println("推送的数据: " + json);
-            mqttGateway.sendToMqtt(topic, json);
+            IQeIRyXG.sendToMqtt(topic, json);
 
             return true;
         } catch (Exception e) {