#952 人防告警代码优化

Открыто
fuyuchuan хочет смерджить 5 коммит(ов) из uskycloud/fyc-cdi в uskycloud/master

+ 11 - 33
service-cdi/service-cdi-biz/src/main/java/com/usky/cdi/controller/AlarmDataController.java

@@ -2,9 +2,8 @@ package com.usky.cdi.controller;
 
 
 import com.usky.cdi.service.impl.AlarmDataTransferService;
 import com.usky.cdi.service.impl.AlarmDataTransferService;
 import com.usky.cdi.service.vo.alarm.AlarmMessageVO;
 import com.usky.cdi.service.vo.alarm.AlarmMessageVO;
-import com.usky.cdi.service.vo.alarm.AlarmMessage1VO;
-import lombok.extern.slf4j.Slf4j;
-import org.springframework.beans.factory.annotation.Autowired;
+import com.usky.common.core.bean.ApiResult;
+import lombok.RequiredArgsConstructor;
 import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
 import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
 import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.RequestBody;
@@ -12,50 +11,29 @@ import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
 import org.springframework.web.bind.annotation.RestController;
 
 
 /**
 /**
- * 基础类数据传输控制器
- * 提供基础类数据上报的接口
+ * 告警数据 HTTP 入口:将告警上报至市适配平台(MQTT)。
  *
  *
  * @author han
  * @author han
  * @date 2025/12/08
  * @date 2025/12/08
  */
  */
-@Slf4j
 @RestController
 @RestController
 @RequestMapping("/api/alarm")
 @RequestMapping("/api/alarm")
 @ConditionalOnProperty(prefix = "mqtt", value = {"enabled"}, havingValue = "true")
 @ConditionalOnProperty(prefix = "mqtt", value = {"enabled"}, havingValue = "true")
+@RequiredArgsConstructor
 public class AlarmDataController {
 public class AlarmDataController {
-    @Autowired
-    private AlarmDataTransferService alarmDataTransferService;
 
 
-    /**
-     * 上报人防工程基础信息
-     */
-    @PostMapping("/alarmMessage")
-    public String sendAlarmMessage(@RequestBody AlarmMessageVO vo) {
-        boolean success = alarmDataTransferService.sendAlarmMessage(vo);
-        return success ? "上报成功" : "上报失败";
-    }
+    private final AlarmDataTransferService alarmDataTransferService;
 
 
-    /**
-     * 上报人防工程基础信息
-     */
-    @PostMapping("/alarmMessage1")
-    public String sendAlarmMessage1(@RequestBody AlarmMessageVO vo) {
-        boolean success = alarmDataTransferService.sendAlarmMessage1(vo);
-        return success ? "上报成功" : "上报失败";
+    private static ApiResult<String> toSubmitResult(boolean success) {
+        return success ? ApiResult.success("上报成功") : ApiResult.error("上报失败");
     }
     }
 
 
     /**
     /**
-     * 上报倾斜、位移、裂缝监测事件
+     * 告警推送
      */
      */
-    @PostMapping("/alarmMessage2")
-    public String sendAlarmMessage2(@RequestBody AlarmMessageVO vo) {
-        boolean success = alarmDataTransferService.sendAlarmMessage2(vo);
-        return success ? "上报成功" : "上报失败";
+    @PostMapping("/alarmMessage")
+    public ApiResult<String> alarmMessage(@RequestBody AlarmMessageVO<?> vo) {
+        return toSubmitResult(alarmDataTransferService.publishAlarm(vo));
     }
     }
 
 
-    @PostMapping("/alarmMessage3")
-    public String sendAlarmMessage3(@RequestBody AlarmMessageVO vo) {
-        boolean success = alarmDataTransferService.sendEngineeringBase(vo);
-        return success ? "上报成功" : "上报失败";
-    }
 }
 }

+ 32 - 147
service-cdi/service-cdi-biz/src/main/java/com/usky/cdi/service/impl/AlarmDataTransferService.java

@@ -1,26 +1,17 @@
 package com.usky.cdi.service.impl;
 package com.usky.cdi.service.impl;
 
 
 import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSON;
-import com.alibaba.fastjson.JSONObject;
-// 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.mqtt.MqttConnectionTool;
 import com.usky.cdi.service.util.SnowflakeIdGenerator;
 import com.usky.cdi.service.util.SnowflakeIdGenerator;
 import com.usky.cdi.service.vo.alarm.AlarmMessageVO;
 import com.usky.cdi.service.vo.alarm.AlarmMessageVO;
 import lombok.extern.slf4j.Slf4j;
 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 org.springframework.stereotype.Service;
 
 
-import javax.annotation.Resource;
-import java.text.SimpleDateFormat;
 import java.time.LocalDateTime;
 import java.time.LocalDateTime;
-import java.util.Date;
-import java.util.HashMap;
+import java.time.format.DateTimeFormatter;
 
 
 /**
 /**
- * 告警类数据传输服务
- * 负责向市适配平台发送告警类数据
+ * 告警类数据传输服务:向市适配平台通过 MQTT 上报告警数据。
  *
  *
  * @author han
  * @author han
  * @date 2025/12/08
  * @date 2025/12/08
@@ -29,162 +20,56 @@ import java.util.HashMap;
 @Service
 @Service
 public class AlarmDataTransferService {
 public class AlarmDataTransferService {
 
 
-    @Autowired
-    private MqttConnectionTool mqttConnectionTool;
-    @Resource
-    private MqttOutConfig.MqttGateway mqttGateway;
+    private static final String MQTT_USERNAME = "3101100021";
+    private static final String MQTT_PASSWORD = "SIixzph1";
+    private static final String ALARM_TOPIC = "alarm/message";
+    private static final DateTimeFormatter PUBLISH_TIME_FORMAT =
+            DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
 
 
-    private final SnowflakeIdGenerator idGenerator;
-    private final SimpleDateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
+    private final MqttConnectionTool mqttConnectionTool;
+    private final SnowflakeIdGenerator idGenerator = new SnowflakeIdGenerator(3L, 3L);
 
 
-    public AlarmDataTransferService() {
-        // 使用默认的workerId和datacenterId,实际项目中可以从配置读取
-        this.idGenerator = new SnowflakeIdGenerator(3L, 3L);
+    public AlarmDataTransferService(MqttConnectionTool mqttConnectionTool) {
+        this.mqttConnectionTool = mqttConnectionTool;
     }
     }
 
 
-    /**
-     * 获取当前时间字符串
-     */
     private String getCurrentTime() {
     private String getCurrentTime() {
-        return timeFormat.format(new Date());
+        return LocalDateTime.now().format(PUBLISH_TIME_FORMAT);
     }
     }
 
 
-    /**
-     * 生成数据包ID
-     */
-    private Long generateDataPacketID() {
-        return idGenerator.nextPacketId10();
+    private long generateDataPacketId() {
+        return idGenerator.nextPacketId();
     }
     }
 
 
     /**
     /**
-     * 发送告警信息
-     * Topic: base/floorPlane
+     * 上报告警消息至 MQTT(topic: alarm/message)。
      *
      *
-     * @param vo 楼层平面图信息
-     * @return 是否发送成功
+     * @return 是否已成功投递到底层网关(不代表敌方平台一定消费成功)
      */
      */
-    public boolean sendAlarmMessage(AlarmMessageVO vo) {
-        try {
-            if (vo.getDataPacketID() == null) {
-                vo.setDataPacketID(generateDataPacketID());
-            }
-            if (vo.getPublishTime() == null) {
-                vo.setPublishTime(getCurrentTime());
-            }
-
-//            HashMap<String, Object> map = new HashMap<>();
-//            map.put("dataPacketID", vo.getDataPacketID());
-//            map.put("engineeringID", vo.getEngineeringID());
-//            map.put("floor", vo.getFloor());
-//            map.put("floorFileID", vo.getFloorFileID());
-//            map.put("floorFileName", vo.getFloorFileName());
-//            map.put("floorFileSuffix", vo.getFloorFileSuffix());
-//            map.put("filePixWidth", vo.getFilePixWidth());
-//            map.put("filePixHeight", vo.getFilePixHeight());
-//            map.put("floorFile", imageBytes);
-//            map.put("publishTime", vo.getPublishTime());
-//            Gson gson = new Gson();
-            JSONObject jsonObject = (JSONObject) JSON.toJSON(vo);
-            String json = jsonObject.toJSONString();
-            System.out.println(json);
-            MqttConnectionTool.MqttGateway IQeIRyXG = mqttConnectionTool.connectOrRefresh("3101100017", "gjB4v1bh");
-            String topic = "alarm/message";
-            IQeIRyXG.sendToMqtt(topic, json);
-
-            return true;
-        } catch (Exception e) {
-            log.error("发送告警信息失败,AlarmID: {}", vo.getAlarmID(), e);
-            return false;
-        }
-    }
-
-    /**
-     * 发送告警信息
-     * Topic: base/floorPlane
-     *
-     * @param vo 楼层平面图信息
-     * @return 是否发送成功
-     */
-    public boolean sendAlarmMessage1(AlarmMessageVO vo) {
-
+    public boolean publishAlarm(AlarmMessageVO<?> vo) {
         try {
         try {
-            if (vo.getDataPacketID() == null) {
-                vo.setDataPacketID(generateDataPacketID());
-            }
-            if (vo.getPublishTime() == null) {
-                vo.setPublishTime(getCurrentTime());
-            }
-
-            // HashMap<String, Object> map = new HashMap<>();
-//            map.put("dataPacketID", vo.getDataPacketID());
-//            map.put("engineeringID", vo.getEngineeringID());
-//            map.put("floor", vo.getFloor());
-//            map.put("floorFileID", vo.getFloorFileID());
-//            map.put("floorFileName", vo.getFloorFileName());
-//            map.put("floorFileSuffix", vo.getFloorFileSuffix());
-//            map.put("filePixWidth", vo.getFilePixWidth());
-//            map.put("filePixHeight", vo.getFilePixHeight());
-//            map.put("floorFile", imageBytes);
-//            map.put("publishTime", vo.getPublishTime());
-//             Gson gson = new Gson();
-            JSONObject jsonObject = (JSONObject) JSON.toJSON(vo);
-            String json = jsonObject.toJSONString();
-            System.out.println(json);
-            String topic = "alarm/message";
-            MqttConnectionTool.MqttGateway IQeIRyXG = mqttConnectionTool.connectOrRefresh("3101100017", "gjB4v1bh");
-            IQeIRyXG.sendToMqtt(topic, json);
-
+            fillDefaults(vo);
+            String json = JSON.toJSONString(vo);
+            log.info("告警 MQTT 载荷: {}", json);
+            MqttConnectionTool.MqttGateway gateway =
+                    mqttConnectionTool.connectOrRefresh(MQTT_USERNAME, MQTT_PASSWORD);
+            gateway.sendToMqtt(ALARM_TOPIC, json);
             return true;
             return true;
         } catch (Exception e) {
         } catch (Exception e) {
-            log.error("发送告警信息失败,AlarmID: {}", vo.getAlarmID(), e);
+            log.error("发送告警信息失败, alarmID: {}, engineeringID: {}",
+                    vo != null ? vo.getAlarmID() : null,
+                    vo != null ? vo.getEngineeringID() : null,
+                    e);
             return false;
             return false;
         }
         }
     }
     }
 
 
-    public boolean sendAlarmMessage2(AlarmMessageVO vo) {
-        try {
-            if (vo.getDataPacketID() == null) {
-                vo.setDataPacketID(generateDataPacketID());
-            }
-            if (vo.getPublishTime() == null) {
-                vo.setPublishTime(getCurrentTime());
-            }
-
-            JSONObject jsonObject = (JSONObject) JSON.toJSON(vo);
-            String json = jsonObject.toJSONString();
-            System.out.println(json);
-            MqttConnectionTool.MqttGateway IQeIRyXG = mqttConnectionTool.connectOrRefresh("3101100017", "gjB4v1bh");
-            String topic = "alarm/message";
-            IQeIRyXG.sendToMqtt(topic, json);
-
-            return true;
-        } catch (Exception e) {
-            log.error("发送告警信息失败,AlarmID: {}", vo.getAlarmID(), e);
-            return false;
+    private void fillDefaults(AlarmMessageVO<?> vo) {
+        if (vo.getDataPacketID() == null) {
+            vo.setDataPacketID(generateDataPacketId());
         }
         }
-    }
-
-    public boolean sendEngineeringBase(AlarmMessageVO vo) {
-        try {
-            if (vo.getDataPacketID() == null) {
-                vo.setDataPacketID(generateDataPacketID());
-            }
-            if (vo.getPublishTime() == null) {
-                vo.setPublishTime(getCurrentTime());
-            }
-
-            MqttConnectionTool.MqttGateway IQeIRyXG = mqttConnectionTool.connectOrRefresh("3101100017", "3101100017");
-
-            JSONObject jsonObject = (JSONObject) JSON.toJSON(vo);
-            String json = jsonObject.toJSONString();
-            String topic = "alarm/message";
-            System.out.println("推送的数据: " + json);
-            IQeIRyXG.sendToMqtt(topic, json);
-
-            return true;
-        } catch (Exception e) {
-            log.error("发送电流告警信息失败,EngineeringID: {}", vo.getEngineeringID(), e);
-            return false;
+        if (vo.getPublishTime() == null) {
+            vo.setPublishTime(getCurrentTime());
         }
         }
     }
     }
 }
 }

+ 41 - 38
service-cdi/service-cdi-biz/src/main/java/com/usky/cdi/service/impl/BaseDataTransferService.java

@@ -69,9 +69,21 @@ public class BaseDataTransferService {
         this.idGenerator = new SnowflakeIdGenerator(1L, 1L);
         this.idGenerator = new SnowflakeIdGenerator(1L, 1L);
     }
     }
 
 
-    /**
-     * 获取当前时间字符串
-     */
+    private String convertFloor(String floor) {
+        if (floor == null || floor.trim().isEmpty()) {
+            return floor;
+        }
+        try {
+            int floorNum = Integer.parseInt(floor.trim());
+            if (floorNum < 0) {
+                return "B" + Math.abs(floorNum);
+            }
+        } catch (NumberFormatException e) {
+            log.warn("楼层格式转换失败,原始值: {}", floor);
+        }
+        return floor;
+    }
+
     private String getCurrentTime() {
     private String getCurrentTime() {
         return timeFormat.format(new Date());
         return timeFormat.format(new Date());
     }
     }
@@ -156,62 +168,53 @@ public class BaseDataTransferService {
                 vo.setDataPacketID(generateDataPacketID());
                 vo.setDataPacketID(generateDataPacketID());
             }
             }
             if (vo.getPublishTime() == null) {
             if (vo.getPublishTime() == null) {
-                vo.setPublishTime(timeFormat.format(new Date()));
+                vo.setPublishTime(getCurrentTime());
             }
             }
 
 
-            // ========== 2. 读取本地图片 ==========
-            String imagePath = "C:\\Users\\f\\Downloads\\45_平面图.jpg";
+            String imagePath = "D://k2-04.jpg";
+            // 将图片文件读取为字节数组
             byte[] imageBytes = Files.readAllBytes(Paths.get(imagePath));
             byte[] imageBytes = Files.readAllBytes(Paths.get(imagePath));
 
 
-            // 大小校验 ≤5MB
-            if (imageBytes.length > 5 * 1024 * 1024) {
-                System.err.println("文件超过5MB");
+            // 检查文件大小(不超过5MB)
+            if (vo.getFloorFile() != null && imageBytes.length > 5 * 1024 * 1024) {
+                log.error("楼层平面图文件大小超过5MB限制,FileID: {}", vo.getFloorFileID());
                 return false;
                 return false;
             }
             }
 
 
-            // 格式校验
-            if (!Arrays.asList("jpg", "jpeg", "png").contains(vo.getFloorFileSuffix().toLowerCase())) {
-                System.err.println("不支持的格式");
-                return false;
-            }
-
-            // 获取宽高
-            BufferedImage image = ImageIO.read(new ByteArrayInputStream(imageBytes));
-            int width = image.getWidth();
-            int height = image.getHeight();
-
-            // ========== 3. 时间格式化 ==========
-
-            // ========== 4. 构建标准JSON消息体 ==========
-            Map<String, Object> map = new HashMap<>();
+            HashMap<String, Object> map = new HashMap<>();
             map.put("dataPacketID", vo.getDataPacketID());
             map.put("dataPacketID", vo.getDataPacketID());
             map.put("engineeringID", vo.getEngineeringID());
             map.put("engineeringID", vo.getEngineeringID());
             map.put("floor", vo.getFloor());
             map.put("floor", vo.getFloor());
             map.put("floorFileID", vo.getFloorFileID());
             map.put("floorFileID", vo.getFloorFileID());
             map.put("floorFileName", vo.getFloorFileName());
             map.put("floorFileName", vo.getFloorFileName());
             map.put("floorFileSuffix", vo.getFloorFileSuffix());
             map.put("floorFileSuffix", vo.getFloorFileSuffix());
-            map.put("filePixWidth", width);
-            map.put("filePixHeight", height);
+            map.put("filePixWidth", vo.getFilePixWidth());
+            map.put("filePixHeight", vo.getFilePixHeight());
             map.put("floorFile", imageBytes);
             map.put("floorFile", imageBytes);
             map.put("publishTime", vo.getPublishTime());
             map.put("publishTime", vo.getPublishTime());
-
-            //使用Gson:
             Gson gson = new Gson();
             Gson gson = new Gson();
+            // 将字节数组转换为Base64编码
+            JSONObject jsonObject = (JSONObject) JSON.toJSON(vo);
+            vo.setFloorFile(imageBytes);
+//            jsonObject.put("floorFile", imageBytes);
+            if (vo.getFloorFile() != null) {
+                // 使用Base64编码传输二进制数据
+                String base64File = java.util.Base64.getEncoder().encodeToString(vo.getFloorFile());
+                jsonObject.put("floorFile", imageBytes);
+            }
 
 
-            // ========== 5. MQTT发送(修复版) ==========
+            String json = jsonObject.toJSONString();
+            System.out.println(gson.toJson(map));
             String topic = "base/floorPlane";
             String topic = "base/floorPlane";
-            MqttConnectionTool.MqttGateway gateway = mqttConnectionTool.connectOrRefresh("3101100021", "SIixzph1");
 
 
-            // 发送JSON字符串
-            gateway.sendToMqtt(topic, gson.toJson(map));
+            log.info("发送楼层平面图信息,Topic: {}, FileID: {}, FileSize: {} bytes",
+                    topic, vo.getFloorFileID(),
+                    vo.getFloorFile() != null ? vo.getFloorFile().length : 0);
+            mqttGateway.sendToMqtt(topic, gson.toJson(map));
 
 
-            System.out.println("✅ MQTT发送成功 TOPIC: " + topic);
             return true;
             return true;
-
         } catch (Exception e) {
         } catch (Exception e) {
-            // 打印完整异常
-            e.printStackTrace();
-            System.err.println("❌ 发送失败:" + e.getMessage());
+            log.error("发送楼层平面图信息失败,FileID: {}", vo.getFloorFileID(), e);
             return false;
             return false;
         }
         }
     }
     }
@@ -258,7 +261,7 @@ public class BaseDataTransferService {
             HashMap<String, Object> map = new HashMap<>();
             HashMap<String, Object> map = new HashMap<>();
             map.put("dataPacketID", generateDataPacketID());
             map.put("dataPacketID", generateDataPacketID());
             map.put("engineeringID", vo.getEngineeringID());
             map.put("engineeringID", vo.getEngineeringID());
-            map.put("floor", vo.getFloor());
+            map.put("floor", convertFloor(vo.getFloor()));
             map.put("floorFileID", 1);
             map.put("floorFileID", 1);
             map.put("sensorID", Integer.parseInt(vo.getDeviceId()));
             map.put("sensorID", Integer.parseInt(vo.getDeviceId()));
             map.put("sensorNo", vo.getDeviceUuid());
             map.put("sensorNo", vo.getDeviceUuid());

+ 0 - 1
service-cdi/service-cdi-biz/src/main/java/com/usky/cdi/service/util/DeviceDataQuery.java

@@ -12,7 +12,6 @@ import lombok.Data;
 import lombok.extern.slf4j.Slf4j;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.beans.factory.annotation.Value;
-import org.springframework.boot.context.properties.ConfigurationProperties;
 import org.springframework.stereotype.Component;
 import org.springframework.stereotype.Component;
 
 
 import java.text.DecimalFormat;
 import java.text.DecimalFormat;

+ 0 - 2
service-cdi/service-cdi-biz/src/main/java/com/usky/cdi/service/vo/alarm/AlarmMessageVO.java

@@ -93,7 +93,6 @@ public class AlarmMessageVO<T extends Number> implements Serializable {
      * 查询告警表 base_alarm 时新增告警取 alarm_type 字段,更新数据则取 handle_time 字段
      * 查询告警表 base_alarm 时新增告警取 alarm_type 字段,更新数据则取 handle_time 字段
      * 时间型(带毫秒),格式为 yyyy-MM-DD hh :mm :ss.SSS
      * 时间型(带毫秒),格式为 yyyy-MM-DD hh :mm :ss.SSS
      **/
      **/
-    @JSONField(format = "yyyy-MM-dd HH:mm:ss.SSS")
     private String alarmUpdateTime;
     private String alarmUpdateTime;
 
 
     /**
     /**
@@ -113,7 +112,6 @@ public class AlarmMessageVO<T extends Number> implements Serializable {
      * 获取当前时间
      * 获取当前时间
      * 时间型(带毫秒),格式为 yyyy-MM-DD hh :mm :ss.SSS
      * 时间型(带毫秒),格式为 yyyy-MM-DD hh :mm :ss.SSS
      **/
      **/
-    @JSONField(format = "yyyy-MM-dd HH:mm:ss.SSS")
     private String publishTime;
     private String publishTime;
 
 
     /** 告警数据字段 必填、通用
     /** 告警数据字段 必填、通用