소스 검색

人防代码提交

fuyuchuan 1 주 전
부모
커밋
7c216e622e

+ 6 - 42
service-cdi/service-cdi-biz/src/main/java/com/usky/cdi/controller/IotDataController.java

@@ -35,47 +35,11 @@ public class IotDataController {
     }
 
     /**
-     * 上报温度
+     * 上报温度、湿度、氧气、一氧化碳、二氧化碳
      */
-    @PostMapping("/temp")
-    public String sendTemp(@RequestBody TempVO vo) {
-        boolean success = iotDataTransferService.sendTemp(vo);
-        return success ? "上报成功" : "上报失败";
-    }
-
-    /**
-     * 上报湿度
-     */
-    @PostMapping("/rh")
-    public String batchSendHumidity(@RequestBody HumidityVO vo) {
-        boolean success = iotDataTransferService.batchSendHumidity(vo);
-        return success ? "上报成功" : "上报失败";
-    }
-
-    /**
-     * 上报氧气
-     */
-    @PostMapping("/o2")
-    public String sendOxygen(@RequestBody OxygenVO vo) {
-        boolean success = iotDataTransferService.sendOxygen(vo);
-        return success ? "上报成功" : "上报失败";
-    }
-
-    /**
-     * 上报二氧化碳
-     */
-    @PostMapping("/co2")
-    public String sendCarbonDioxide(@RequestBody Co2VO vo) {
-        boolean success = iotDataTransferService.sendCarbonDioxide(vo);
-        return success ? "上报成功" : "上报失败";
-    }
-
-    /**
-     * 上报一氧化碳
-     */
-    @PostMapping("/co")
-    public String sendCarbonMonoxide(@RequestBody CoVO vo) {
-        boolean success = iotDataTransferService.sendCarbonMonoxide(vo);
+    @PostMapping("/envData")
+    public String sendEnvData() {
+        boolean success = iotDataTransferService.sendEnvData();
         return success ? "上报成功" : "上报失败";
     }
 
@@ -92,8 +56,8 @@ public class IotDataController {
      * 上报用电负荷
      */
     @PostMapping("/electricityLoad")
-    public String sendElectricityLoad(@RequestBody ElectricityLoadVO vo) {
-        boolean success = iotDataTransferService.sendElectricityLoad(vo);
+    public String sendElectricityLoad() {
+        boolean success = iotDataTransferService.sendElectricityLoad();
         return success ? "上报成功" : "上报失败";
     }
 

+ 109 - 151
service-cdi/service-cdi-biz/src/main/java/com/usky/cdi/service/impl/IotDataTransferService.java

@@ -8,17 +8,15 @@ import com.usky.cdi.service.util.DeviceDataQuery;
 import com.usky.cdi.service.util.SnowflakeIdGenerator;
 import com.usky.cdi.service.vo.base.*;
 import lombok.extern.slf4j.Slf4j;
-import org.apache.tomcat.jni.Local;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
 import org.springframework.stereotype.Service;
 
-import java.text.SimpleDateFormat;
 import java.time.Instant;
 import java.time.LocalDateTime;
 import java.time.ZoneId;
-import java.util.Date;
 import java.util.List;
+import java.util.Random;
 
 /**
  *
@@ -60,7 +58,7 @@ public class IotDataTransferService {
     }
 
     /**
-     * 发送水浸状态
+     * 发送水浸状态(deviceType:702)
      * Topic: iotInfo/flooded
      *
      * @return 是否发送成功
@@ -106,158 +104,92 @@ public class IotDataTransferService {
     }
 
     /**
-     * 发送温度
-     * Topic: iotInfo/temp
+     * 发送温湿及气体浓度数据(设备类型:701)
+     * 包含: wd(温度), sd(湿度), o2(氧气), co(一氧化碳), co2(二氧化碳)
      *
-     * @param vo 温度信息
      * @return 是否发送成功
      */
-    public boolean sendTemp(TempVO vo) {
+    public boolean sendEnvData() {
         if (mqttGateway == null) {
             log.warn("MQTT Gateway未初始化,无法发送消息");
             return false;
         }
         try {
-            if (vo.getDataPacketID() == null) {
-                vo.setDataPacketID(generateDataPacketID());
-            }
-            if (vo.getPublishTime() == null) {
-                vo.setPublishTime(getCurrentTime());
-            }
-
-            String json = JSON.toJSONString(vo);
-            String topic = EnvMonitorMqttTopic.TEMP.getTopic();
-
-            log.info("发送温度信息,Topic: {}, Data: {}", topic, json);
-            mqttGateway.sendToMqtt(topic, json);
-            return true;
-        } catch (Exception e) {
-            log.error("发送温度信息失败", e);
-            return false;
-        }
-    }
-
-    /**
-     * 发送湿度
-     * Topic: iotInfo/rh
-     *
-     * @param vo 湿度信息
-     * @return 是否发送成功
-     */
-    public boolean batchSendHumidity(HumidityVO vo) {
-        if (mqttGateway == null) {
-            log.warn("MQTT Gateway未初始化,无法发送消息");
-            return false;
-        }
-        try {
-            if (vo.getDataPacketID() == null) {
-                vo.setDataPacketID(generateDataPacketID());
-            }
-            if (vo.getPublishTime() == null) {
-                vo.setPublishTime(getCurrentTime());
-            }
+            List<JSONObject> deviceData = deviceDataQuery.getDeviceData(701);
 
-            String json = JSON.toJSONString(vo);
-            String topic = EnvMonitorMqttTopic.HUMIDITY.getTopic();
-            log.info("发送湿度信息,Topic: {}, Data: {}", topic, json);
-            mqttGateway.sendToMqtt(topic, json);
-            return true;
-        } catch (Exception e) {
-            log.error("发送湿度信息失败", e);
-            return false;
-        }
-    }
-
-    /**
-     * 发送氧气浓度信息
-     * Topic: iotInfo/o2
-     *
-     * @param vo 氧气浓度
-     * @return 是否发送成功
-     */
-    public boolean sendOxygen(OxygenVO vo) {
-        if (mqttGateway == null) {
-            log.warn("MQTT Gateway未初始化,无法发送消息");
-            return false;
-        }
-        try {
-            if (vo.getDataPacketID() == null) {
-                vo.setDataPacketID(generateDataPacketID());
-            }
-            if (vo.getPublishTime() == null) {
-                vo.setPublishTime(getCurrentTime());
-            }
-
-            String json = JSON.toJSONString(vo);
-            String topic = EnvMonitorMqttTopic.OXYGEN.getTopic();
-            log.info("发送氧气浓度信息,Topic: {}, Data: {}", topic, json);
-            mqttGateway.sendToMqtt(topic, json);
-            return true;
-        } catch (Exception e) {
-            log.error("发送氧气浓度信息失败", e);
-            return false;
-        }
-    }
-
-    /**
-     * 发送二氧化碳浓度信息
-     * Topic: iotInfo/co2
-     *
-     * @param vo 二氧化碳浓度
-     * @return 是否发送成功
-     */
-    public boolean sendCarbonDioxide(Co2VO vo) {
-        if (mqttGateway == null) {
-            log.warn("MQTT Gateway未初始化,无法发送消息");
-            return false;
-        }
-        try {
-            if (vo.getDataPacketID() == null) {
-                vo.setDataPacketID(generateDataPacketID());
-            }
-            if (vo.getPublishTime() == null) {
-                vo.setPublishTime(getCurrentTime());
+            if (deviceData.isEmpty()) {
+                log.warn("没有获取到空气质量数据!");
+                return false;
             }
 
-            String json = JSON.toJSONString(vo);
-            String topic = EnvMonitorMqttTopic.CO2.getTopic();
-            log.info("发送二氧化碳浓度信息,Topic: {}, Data: {}", topic, json);
-
-            mqttGateway.sendToMqtt(topic, json);
-            return true;
-        } catch (Exception e) {
-            log.error("发送二氧化碳浓度信息失败", e);
-            return false;
-        }
-    }
+            for (JSONObject deviceDataItem : deviceData) {
+                Long dataTime = deviceDataItem.getLong("time");
+                LocalDateTime dataEndTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(dataTime), ZoneId.systemDefault());
 
-    /**
-     * 发送一氧化碳浓度信息
-     * Topic: iotInfo/co
-     *
-     * @param vo 一氧化碳浓度
-     * @return 是否发送成功
-     */
-    public boolean sendCarbonMonoxide(CoVO vo) {
-        if (mqttGateway == null) {
-            log.warn("MQTT Gateway未初始化,无法发送消息");
-            return false;
-        }
-        try {
-            if (vo.getDataPacketID() == null) {
-                vo.setDataPacketID(generateDataPacketID());
-            }
-            if (vo.getPublishTime() == null) {
-                vo.setPublishTime(getCurrentTime());
+                // 发送温度
+                TempVO tempVO = new TempVO();
+                tempVO.setDataPacketID(generateDataPacketID());
+                tempVO.setSensorID(4399L);
+                tempVO.setEngineeringID(9527L);
+                tempVO.setPublishTime(getCurrentTime());
+                tempVO.setSensorValue(deviceDataItem.getFloat("wd"));
+                tempVO.setDataEndTime(dataEndTime);
+                String tempJson = JSON.toJSONString(tempVO);
+                mqttGateway.sendToMqtt(EnvMonitorMqttTopic.TEMP.getTopic(), tempJson);
+                log.info("发送温度信息,Topic: {}, Data: {}", EnvMonitorMqttTopic.TEMP.getTopic(), tempJson);
+
+                // 发送湿度
+                HumidityVO humidityVO = new HumidityVO();
+                humidityVO.setDataPacketID(generateDataPacketID());
+                humidityVO.setSensorID(4399L);
+                humidityVO.setEngineeringID(9527L);
+                humidityVO.setPublishTime(getCurrentTime());
+                humidityVO.setSensorValue(deviceDataItem.getFloat("sd"));
+                humidityVO.setDataEndTime(dataEndTime);
+                String humidityJson = JSON.toJSONString(humidityVO);
+                mqttGateway.sendToMqtt(EnvMonitorMqttTopic.HUMIDITY.getTopic(), humidityJson);
+                log.info("发送湿度信息,Topic: {}, Data: {}", EnvMonitorMqttTopic.HUMIDITY.getTopic(), humidityJson);
+
+                // 发送氧气浓度
+                OxygenVO oxygenVO = new OxygenVO();
+                oxygenVO.setDataPacketID(generateDataPacketID());
+                oxygenVO.setSensorID(4399L);
+                oxygenVO.setEngineeringID(9527L);
+                oxygenVO.setPublishTime(getCurrentTime());
+                oxygenVO.setSensorValue(deviceDataItem.getFloat("o2"));
+                oxygenVO.setDataEndTime(dataEndTime);
+                String oxygenJson = JSON.toJSONString(oxygenVO);
+                mqttGateway.sendToMqtt(EnvMonitorMqttTopic.OXYGEN.getTopic(), oxygenJson);
+                log.info("发送氧气浓度信息,Topic: {}, Data: {}", EnvMonitorMqttTopic.OXYGEN.getTopic(), oxygenJson);
+
+                // 发送一氧化碳浓度
+                CoVO coVO = new CoVO();
+                coVO.setDataPacketID(generateDataPacketID());
+                coVO.setSensorID(4399L);
+                coVO.setEngineeringID(9527L);
+                coVO.setPublishTime(getCurrentTime());
+                coVO.setSensorValue(deviceDataItem.getFloat("co"));
+                coVO.setDataEndTime(dataEndTime);
+                String coJson = JSON.toJSONString(coVO);
+                mqttGateway.sendToMqtt(EnvMonitorMqttTopic.CO.getTopic(), coJson);
+                log.info("发送一氧化碳浓度信息,Topic: {}, Data: {}", EnvMonitorMqttTopic.CO.getTopic(), coJson);
+
+                // 发送二氧化碳浓度
+                Co2VO co2VO = new Co2VO();
+                co2VO.setDataPacketID(generateDataPacketID());
+                co2VO.setSensorID(4399L);
+                co2VO.setEngineeringID(9527L);
+                co2VO.setPublishTime(getCurrentTime());
+                co2VO.setSensorValue(deviceDataItem.getFloat("co2"));
+                co2VO.setDataEndTime(dataEndTime);
+                String co2Json = JSON.toJSONString(co2VO);
+                mqttGateway.sendToMqtt(EnvMonitorMqttTopic.CO2.getTopic(), co2Json);
+                log.info("发送二氧化碳浓度信息,Topic: {}, Data: {}", EnvMonitorMqttTopic.CO2.getTopic(), co2Json);
             }
 
-            String json = JSON.toJSONString(vo);
-            String topic = EnvMonitorMqttTopic.CO.getTopic();
-            log.info("发送一氧化碳浓度信息,Topic: {}, Data: {}", topic, json);
-            mqttGateway.sendToMqtt(topic, json);
             return true;
         } catch (Exception e) {
-            log.error("发送一氧化碳浓度信息失败", e);
+            log.error("发送环境数据失败", e);
             return false;
         }
     }
@@ -295,28 +227,54 @@ public class IotDataTransferService {
     /**
      * 发送人防用电负荷情况
      *
-     * @param vo 防人电用负荷
      * @return 是否发送成功
      **/
-    public boolean sendElectricityLoad(ElectricityLoadVO vo) {
+    public boolean sendElectricityLoad() {
         if (mqttGateway == null) {
             log.warn("MQTT Gateway未初始化,无法发送消息");
             return false;
         }
         try {
-            if (vo.getDataPacketID() == null) {
+            List<JSONObject> deviceData = deviceDataQuery.getDeviceData(704);
+
+            for (JSONObject deviceDataItem : deviceData) {
+                Long dataTime = deviceDataItem.getLong("time");
+                LocalDateTime dataEndTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(dataTime), ZoneId.systemDefault());
+                Integer voltageA = deviceDataItem.getInteger("voltage_a");
+                Integer voltageB = deviceDataItem.getInteger("voltage_b");
+                Integer voltageC = deviceDataItem.getInteger("voltage_c");
+                Integer currentA = deviceDataItem.getInteger("current_a");
+                Integer currentB = deviceDataItem.getInteger("current_b");
+                Integer currentC = deviceDataItem.getInteger("current_c");
+                Integer temperatureA = deviceDataItem.getInteger("temperature_a");
+                Integer temperatureB = deviceDataItem.getInteger("temperature_b");
+                Integer temperatureC = deviceDataItem.getInteger("temperature_c");
+                Integer currentResidual = deviceDataItem.getInteger("current_residual");
+
+                ElectricityLoadVO vo = new ElectricityLoadVO();
                 vo.setDataPacketID(generateDataPacketID());
-            }
-            if (vo.getPublishTime() == null) {
+                vo.setSensorID(new Random().nextLong());
+                vo.setEngineeringID(new Random().nextLong());
                 vo.setPublishTime(getCurrentTime());
-            }
+                vo.setDataEndTime(dataEndTime);
+                vo.setAVoltage(Float.valueOf(voltageA));
+                vo.setBVoltage(Float.valueOf(voltageB));
+                vo.setCVoltage(Float.valueOf(voltageC));
+                vo.setAElectricity(Float.valueOf(currentA));
+                vo.setBElectricity(Float.valueOf(currentB));
+                vo.setCElectricity(Float.valueOf(currentC));
+                vo.setLine1TEMP(Float.valueOf(temperatureA));
+                vo.setLine2TEMP(Float.valueOf(temperatureB));
+                vo.setLine3TEMP(Float.valueOf(temperatureC));
+                vo.setLeakageCurrent(Float.valueOf(currentResidual));
+                vo.setTotalPower(new Random().nextFloat() * 1000F);
 
-            String json = JSON.toJSONString(vo);
-            String topic = EnvMonitorMqttTopic.ELECTRICITY_LOAD.getTopic();
-            log.info("发送人防用电负荷情况,Topic: {}, Data: {}", topic, json);
-            mqttGateway.sendToMqtt(topic, json);
+                String json = JSON.toJSONString(vo);
+                String topic = EnvMonitorMqttTopic.ELECTRICITY_LOAD.getTopic();
+                log.info("发送人防用电负荷情况,Topic: {}, Data: {}", topic, json);
+                mqttGateway.sendToMqtt(topic, json);
+            }
             return true;
-
         } catch (Exception e) {
             log.error("发送人防用电负荷情况失败", e);
             return false;

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

@@ -144,7 +144,7 @@ public class DeviceDataQuery {
 
         for (String deviceUuid : deviceUuids) {
             JSONObject simulationData = new JSONObject();
-            simulationData.put("deviceuuid", deviceUuid);
+            // simulationData.put("deviceuuid", deviceUuid);
             simulationData.put("time", currentTime);
 
             switch (deviceType) {
@@ -164,10 +164,10 @@ public class DeviceDataQuery {
                 // 人员统计
                 case 703:
                     // 流量数据:模拟正数
-                    simulationData.put("amount_into", new Random().nextDouble() * 1000);
-                    simulationData.put("amount_out", new Random().nextDouble() * 1000);
-                    simulationData.put("day_into", new Random().nextDouble() * 10000);
-                    simulationData.put("day_out", new Random().nextDouble() * 10000);
+                    simulationData.put("amount_into", new Random().nextDouble() * 100);
+                    simulationData.put("amount_out", new Random().nextDouble() * 100);
+                    simulationData.put("day_into", new Random().nextDouble() * 1000);
+                    simulationData.put("day_out", new Random().nextDouble() * 1000);
                     break;
                 // 电气火灾
                 case 704:
@@ -178,10 +178,10 @@ public class DeviceDataQuery {
                     simulationData.put("current_a", new Random().nextDouble() * 50); // 电流A:0~50A
                     simulationData.put("current_b", new Random().nextDouble() * 50);
                     simulationData.put("current_c", new Random().nextDouble() * 50);
-                    simulationData.put("temperature_a", 20 + new Random().nextDouble() * 30); // 温A:20~50℃
+                    simulationData.put("temperature_a", 20 + new Random().nextDouble() * 30); // 线温A:20~50℃
                     simulationData.put("temperature_b", 20 + new Random().nextDouble() * 30);
                     simulationData.put("temperature_c", 20 + new Random().nextDouble() * 30);
-                    simulationData.put("current_residual", new Random().nextDouble() * 1); // 剩余电流:0~1A
+                    simulationData.put("current_residual", new Random().nextDouble() * 100); // 剩余电流:0~1A
                     break;
                 // 电能采集
                 case 705:

+ 3 - 2
service-cdi/service-cdi-biz/src/main/java/com/usky/cdi/service/vo/base/BaseEnvMonitorPushVO.java

@@ -1,5 +1,6 @@
 package com.usky.cdi.service.vo.base;
 
+import com.alibaba.fastjson.annotation.JSONField;
 import com.fasterxml.jackson.annotation.JsonFormat;
 import lombok.Data;
 
@@ -41,14 +42,14 @@ public abstract class BaseEnvMonitorPushVO implements Serializable {
      * 类型:Time(yyyy-MM-dd HH:mm:ss.SSS)
      * 说明:1小时平均周期的末点时间(如10:00表示9:00-10:00的平均数据)
      */
-    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss.SSS")
+    @JSONField(format = "yyyy-MM-dd HH:mm:ss.SSS")
     private LocalDateTime dataEndTime;
 
     /**
      * 上报时间(必填)
      * 类型:Time(yyyy-MM-dd HH:mm:ss.SSS)
      */
-    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss.SSS")
+    @JSONField(format = "yyyy-MM-dd HH:mm:ss.SSS")
     private LocalDateTime publishTime;
 
     /**