ソースを参照

人防代码优化

fuyuchuan 3 週間 前
コミット
8a7741b3ae

+ 7 - 0
service-cdi/service-cdi-biz/pom.xml

@@ -74,6 +74,13 @@
             <artifactId>mysql-connector-java</artifactId>
             <scope>runtime</scope>
         </dependency>
+
+        <dependency>
+            <groupId>com.mchange</groupId>
+            <artifactId>mchange-commons-java</artifactId>
+            <version>0.2.15</version>
+            <scope>compile</scope>
+        </dependency>
     </dependencies>
 
     <build>

+ 5 - 4
service-cdi/service-cdi-biz/src/main/java/com/usky/cdi/controller/IotDataController.java

@@ -1,5 +1,6 @@
 package com.usky.cdi.controller;
 
+import com.alibaba.fastjson.JSONObject;
 import com.usky.cdi.service.impl.IotDataTransferService;
 import com.usky.cdi.service.vo.base.*;
 import lombok.extern.slf4j.Slf4j;
@@ -29,8 +30,8 @@ public class IotDataController {
      * 上报水浸状态
      */
     @PostMapping("/flooded")
-    public String sendWaterLeak() {
-        boolean success = iotDataTransferService.sendWaterLeak();
+    public String sendWaterLeak(@RequestBody WaterLeakVO vo) {
+        boolean success = iotDataTransferService.sendWaterLeak(vo);
         return success ? "上报成功" : "上报失败";
     }
 
@@ -38,8 +39,8 @@ public class IotDataController {
      * 上报温度、湿度、氧气、一氧化碳、二氧化碳
      */
     @PostMapping("/envData")
-    public String sendEnvData() {
-        boolean success = iotDataTransferService.sendEnvData();
+    public String sendEnvData(@RequestBody JSONObject jsonObject) {
+        boolean success = iotDataTransferService.sendEnvData(jsonObject);
         return success ? "上报成功" : "上报失败";
     }
 

+ 195 - 99
service-cdi/service-cdi-biz/src/main/java/com/usky/cdi/service/impl/IotDataTransferService.java

@@ -9,17 +9,18 @@ import com.usky.cdi.service.util.SnowflakeIdGenerator;
 import com.usky.cdi.service.vo.base.*;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
 import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
 import org.springframework.stereotype.Service;
 
+import javax.annotation.PostConstruct;
 import java.time.Instant;
 import java.time.LocalDateTime;
 import java.time.ZoneId;
 import java.util.List;
-import java.util.Random;
+import java.util.concurrent.ThreadLocalRandom;
 
 /**
- *
  * @author fyc
  * @email yuchuan.fu@chinausky.com
  * @date 2025/11/20
@@ -37,10 +38,13 @@ public class IotDataTransferService {
     @Autowired
     private DeviceDataQuery deviceDataQuery;
 
+    @Value("${config.engineeringID}")
+    private Long engineeringID;
+
 
     public IotDataTransferService() {
         // 使用默认的workerId和datacenterId,实际项目中可以从配置读取
-        this.idGenerator = new SnowflakeIdGenerator(1L, 1L);
+        this.idGenerator = new SnowflakeIdGenerator(2L, 2L);
     }
 
     /**
@@ -63,28 +67,46 @@ public class IotDataTransferService {
      *
      * @return 是否发送成功
      */
-    public boolean sendWaterLeak() {
+    public boolean sendWaterLeak(WaterLeakVO waterLeakVO) {
         if (mqttGateway == null) {
             log.warn("MQTT Gateway未初始化,无法发送消息");
             return false;
         }
         try {
-            List<JSONObject> deviceData = deviceDataQuery.getDeviceData(702);
+            String deviceUuid = waterLeakVO.getDeviceUuid() == null ? "" : waterLeakVO.getDeviceUuid();
+            List<JSONObject> deviceData = deviceDataQuery.getDeviceData(702, deviceUuid);
 
             if (deviceData.isEmpty()) {
-                log.warn("没有获取到水浸数据!");
+                log.warn("没有获取到水浸数据!deviceUuid:{}", deviceUuid);
                 return false;
             }
 
             for (JSONObject deviceDataItem : deviceData) {
-                Integer leachStatus = Integer.valueOf(deviceDataItem.getString("leach_status"));
+                // 增加空值判断
+                String leachStatusStr = deviceDataItem.getString("leach_status");
+                if (leachStatusStr == null || leachStatusStr.isEmpty()) {
+                    log.warn("设备{}的leach_status为空", deviceDataItem.getString("device_id"));
+                    continue;
+                }
+                Integer leachStatus;
+                try {
+                    leachStatus = Integer.valueOf(leachStatusStr);
+                } catch (NumberFormatException e) {
+                    log.error("leach_status转换失败:{}", leachStatusStr, e);
+                    continue;
+                }
+
                 Long dataTime = deviceDataItem.getLong("time");
+                if (dataTime == null) {
+                    log.warn("设备{}的time为空", deviceDataItem.getString("device_id"));
+                    continue;
+                }
                 LocalDateTime dataEndTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(dataTime), ZoneId.systemDefault());
 
                 WaterLeakVO vo = new WaterLeakVO();
                 vo.setDataPacketID(generateDataPacketID());
-                vo.setSensorID(4399L);
-                vo.setEngineeringID(9527L);
+                vo.setSensorID(deviceDataItem.getIntValue("device_id"));
+                vo.setEngineeringID(engineeringID);
                 vo.setPublishTime(getCurrentTime());
                 vo.setSensorValue(leachStatus);
                 vo.setDataEndTime(dataEndTime);
@@ -104,89 +126,69 @@ public class IotDataTransferService {
     }
 
     /**
-     * 发送温湿度及气体浓度数据(设备类型:701)
+     * 发送温湿度及气体浓度数据(设备类型:701,707-711)
      * 包含: wd(温度), sd(湿度), o2(氧气), co(一氧化碳), co2(二氧化碳)
      *
      * @return 是否发送成功
      */
-    public boolean sendEnvData() {
+    public boolean sendEnvData(JSONObject jsonObject) {
         if (mqttGateway == null) {
             log.warn("MQTT Gateway未初始化,无法发送消息");
             return false;
         }
         try {
-            List<JSONObject> deviceData = deviceDataQuery.getDeviceData(701);
+            Integer deviceType = jsonObject.getInteger("deviceType");
+            String deviceUuid = jsonObject.getString("deviceUuid");
+
+            if (deviceType == null) {
+                log.error("deviceType不能为空");
+                return false;
+            }
+
+            List<JSONObject> deviceData = deviceDataQuery.getDeviceData(deviceType, deviceUuid);
 
             if (deviceData.isEmpty()) {
-                log.warn("没有获取到空气质量数据!");
+                log.warn("没有获取到空气质量数据!deviceType:{}, deviceUuid:{}", deviceType, deviceUuid);
                 return false;
             }
 
             for (JSONObject deviceDataItem : deviceData) {
                 Long dataTime = deviceDataItem.getLong("time");
+                if (dataTime == null) {
+                    log.warn("设备{}的time为空", deviceDataItem.getString("device_id"));
+                    continue;
+                }
                 LocalDateTime dataEndTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(dataTime), ZoneId.systemDefault());
-
-                // 发送温度
-                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);
+                Integer deviceId = deviceDataItem.getIntValue("device_id");
+
+                // 提取公共方法,减少代码冗余
+                switch (deviceType) {
+                    case 701:
+                        sendTempData(deviceId, dataEndTime, deviceDataItem);
+                        sendHumidityData(deviceId, dataEndTime, deviceDataItem);
+                        sendOxygenData(deviceId, dataEndTime, deviceDataItem);
+                        sendCoData(deviceId, dataEndTime, deviceDataItem);
+                        sendCo2Data(deviceId, dataEndTime, deviceDataItem);
+                        break;
+                    case 707:
+                        sendTempData(deviceId, dataEndTime, deviceDataItem);
+                        break;
+                    case 708:
+                        sendHumidityData(deviceId, dataEndTime, deviceDataItem);
+                        break;
+                    case 709:
+                        sendOxygenData(deviceId, dataEndTime, deviceDataItem);
+                        break;
+                    case 710:
+                        sendCo2Data(deviceId, dataEndTime, deviceDataItem);
+                        break;
+                    case 711:
+                        sendCoData(deviceId, dataEndTime, deviceDataItem);
+                        break;
+                    default:
+                        log.warn("不支持的设备类型:{}", deviceType);
+                }
             }
-
             return true;
         } catch (Exception e) {
             log.error("发送环境数据失败", e);
@@ -205,6 +207,10 @@ public class IotDataTransferService {
             log.warn("MQTT Gateway未初始化,无法发送消息");
             return false;
         }
+        if (vo == null) {
+            log.error("PersonPresenceVO不能为空");
+            return false;
+        }
         try {
             if (vo.getDataPacketID() == null) {
                 vo.setDataPacketID(generateDataPacketID());
@@ -212,6 +218,7 @@ public class IotDataTransferService {
             if (vo.getPublishTime() == null) {
                 vo.setPublishTime(getCurrentTime());
             }
+            vo.setEngineeringID(engineeringID); // 确保工程ID被设置
 
             String json = JSON.toJSONString(vo);
             String topic = EnvMonitorMqttTopic.PERSON_PRESENCE.getTopic();
@@ -235,39 +242,34 @@ public class IotDataTransferService {
             return false;
         }
         try {
-            List<JSONObject> deviceData = deviceDataQuery.getDeviceData(704);
+            List<JSONObject> deviceData = deviceDataQuery.getDeviceData(704, "");
 
             for (JSONObject deviceDataItem : deviceData) {
                 Long dataTime = deviceDataItem.getLong("time");
+                if (dataTime == null) {
+                    log.warn("设备{}的time为空", deviceDataItem.getString("device_id"));
+                    continue;
+                }
                 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());
-                vo.setSensorID(new Random().nextLong());
-                vo.setEngineeringID(new Random().nextLong());
+                vo.setSensorID(deviceDataItem.getIntValue("device_id"));
+                vo.setEngineeringID(engineeringID);
                 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);
+                vo.setAVoltage(deviceDataItem.getFloat("voltage_a"));
+                vo.setBVoltage(deviceDataItem.getFloat("voltage_b"));
+                vo.setCVoltage(deviceDataItem.getFloat("voltage_c"));
+                vo.setAElectricity(deviceDataItem.getFloat("current_a"));
+                vo.setBElectricity(deviceDataItem.getFloat("current_b"));
+                vo.setCElectricity(deviceDataItem.getFloat("current_c"));
+                vo.setLine1TEMP(deviceDataItem.getFloat("temperature_a"));
+                vo.setLine2TEMP(deviceDataItem.getFloat("temperature_b"));
+                vo.setLine3TEMP(deviceDataItem.getFloat("temperature_c"));
+                vo.setLeakageCurrent(deviceDataItem.getFloat("current_residual"));
+                // 使用线程安全的随机数生成器
+                vo.setTotalPower(ThreadLocalRandom.current().nextFloat() * 10f);
 
                 String json = JSON.toJSONString(vo);
                 String topic = EnvMonitorMqttTopic.ELECTRICITY_LOAD.getTopic();
@@ -281,5 +283,99 @@ public class IotDataTransferService {
         }
     }
 
+    // 提取公共发送方法,减少代码冗余
+    private void sendTempData(Integer deviceId, LocalDateTime dataEndTime, JSONObject deviceDataItem) {
+        Float value = deviceDataItem.getFloat("wd");
+        if (value == null) {
+            log.warn("设备{}的温度数据为空", deviceId);
+            return;
+        }
+        TempVO tempVO = new TempVO();
+        tempVO.setDataPacketID(generateDataPacketID());
+        tempVO.setSensorID(deviceId);
+        tempVO.setEngineeringID(engineeringID);
+        tempVO.setPublishTime(getCurrentTime());
+        tempVO.setSensorValue(value);
+        tempVO.setDataEndTime(dataEndTime);
+        String json = JSON.toJSONString(tempVO);
+        String topic = EnvMonitorMqttTopic.TEMP.getTopic();
+        mqttGateway.sendToMqtt(topic, json);
+        log.info("发送温度信息,Topic: {}, Data: {}", topic, json);
+    }
 
-}
+    private void sendHumidityData(Integer deviceId, LocalDateTime dataEndTime, JSONObject deviceDataItem) {
+        Float value = deviceDataItem.getFloat("sd");
+        if (value == null) {
+            log.warn("设备{}的湿度数据为空", deviceId);
+            return;
+        }
+        HumidityVO humidityVO = new HumidityVO();
+        humidityVO.setDataPacketID(generateDataPacketID());
+        humidityVO.setSensorID(deviceId);
+        humidityVO.setEngineeringID(engineeringID);
+        humidityVO.setPublishTime(getCurrentTime());
+        humidityVO.setSensorValue(value);
+        humidityVO.setDataEndTime(dataEndTime);
+        String json = JSON.toJSONString(humidityVO);
+        String topic = EnvMonitorMqttTopic.HUMIDITY.getTopic();
+        mqttGateway.sendToMqtt(topic, json);
+        log.info("发送湿度信息,Topic: {}, Data: {}", topic, json);
+    }
+
+    private void sendOxygenData(Integer deviceId, LocalDateTime dataEndTime, JSONObject deviceDataItem) {
+        Float value = deviceDataItem.getFloat("o2");
+        if (value == null) {
+            log.warn("设备{}的氧气浓度数据为空", deviceId);
+            return;
+        }
+        OxygenVO oxygenVO = new OxygenVO();
+        oxygenVO.setDataPacketID(generateDataPacketID());
+        oxygenVO.setSensorID(deviceId);
+        oxygenVO.setEngineeringID(engineeringID);
+        oxygenVO.setPublishTime(getCurrentTime());
+        oxygenVO.setSensorValue(value);
+        oxygenVO.setDataEndTime(dataEndTime);
+        String json = JSON.toJSONString(oxygenVO);
+        String topic = EnvMonitorMqttTopic.OXYGEN.getTopic();
+        mqttGateway.sendToMqtt(topic, json);
+        log.info("发送氧气浓度信息,Topic: {}, Data: {}", topic, json);
+    }
+
+    private void sendCoData(Integer deviceId, LocalDateTime dataEndTime, JSONObject deviceDataItem) {
+        Float value = deviceDataItem.getFloat("co");
+        if (value == null) {
+            log.warn("设备{}的一氧化碳浓度数据为空", deviceId);
+            return;
+        }
+        CoVO coVO = new CoVO();
+        coVO.setDataPacketID(generateDataPacketID());
+        coVO.setSensorID(deviceId);
+        coVO.setEngineeringID(engineeringID);
+        coVO.setPublishTime(getCurrentTime());
+        coVO.setSensorValue(value);
+        coVO.setDataEndTime(dataEndTime);
+        String json = JSON.toJSONString(coVO);
+        String topic = EnvMonitorMqttTopic.CO.getTopic();
+        mqttGateway.sendToMqtt(topic, json);
+        log.info("发送一氧化碳浓度信息,Topic: {}, Data: {}", topic, json);
+    }
+
+    private void sendCo2Data(Integer deviceId, LocalDateTime dataEndTime, JSONObject deviceDataItem) {
+        Float value = deviceDataItem.getFloat("co2");
+        if (value == null) {
+            log.warn("设备{}的二氧化碳浓度数据为空", deviceId);
+            return;
+        }
+        Co2VO co2VO = new Co2VO();
+        co2VO.setDataPacketID(generateDataPacketID());
+        co2VO.setSensorID(deviceId);
+        co2VO.setEngineeringID(engineeringID);
+        co2VO.setPublishTime(getCurrentTime());
+        co2VO.setSensorValue(value);
+        co2VO.setDataEndTime(dataEndTime);
+        String json = JSON.toJSONString(co2VO);
+        String topic = EnvMonitorMqttTopic.CO2.getTopic();
+        mqttGateway.sendToMqtt(topic, json);
+        log.info("发送二氧化碳浓度信息,Topic: {}, Data: {}", topic, json);
+    }
+}

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

@@ -4,6 +4,7 @@ import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.baomidou.mybatisplus.core.toolkit.StringUtils;
 import com.usky.cdi.domain.DmpDevice;
 import com.usky.cdi.mapper.DmpDeviceMapper;
 import lombok.Data;
@@ -14,6 +15,7 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
 import org.springframework.stereotype.Component;
 
 import java.util.*;
+import java.util.concurrent.ThreadLocalRandom;
 import java.util.stream.Collectors;
 
 /**
@@ -38,22 +40,23 @@ public class DeviceDataQuery {
     /**
      * 获取指定设备类型的设备数据
      */
-    public List<JSONObject> getDeviceData(Integer deviceType) {
-        List<String> deviceUuids = getDeviceUuids(deviceType);
-        if (deviceUuids.isEmpty()) {
+    public List<JSONObject> getDeviceData(Integer deviceType, String deviceUuid) {
+        List<DmpDevice> devices = getDeviceUuids(deviceType, deviceUuid);
+        if (devices.isEmpty()) {
             log.warn("该租户下没有注册设备!");
             return Collections.emptyList();
         }
+        List<String> deviceUuids = devices.stream().map(DmpDevice::getDeviceUuid).collect(Collectors.toList());
 
         JSONObject requestBody = new JSONObject();
         requestBody.put("deviceUuids", deviceUuids);
 
         String response = HttpClientUtils.doPostJson(baseUrl, String.valueOf(requestBody));
 
-        List<JSONObject> resultList = parseResponseData(response, deviceType);
+        List<JSONObject> resultList = parseResponseData(response, deviceType, devices);
 
         if (resultList.isEmpty() && simulation) {
-            resultList = generateSimulationData(deviceType, deviceUuids);
+            resultList = generateSimulationData(deviceType, devices);
         }
 
         return resultList;
@@ -63,23 +66,26 @@ public class DeviceDataQuery {
     /**
      * 获取指定设备类型的设备UUID列表
      */
-    private List<String> getDeviceUuids(Integer deviceType) {
+    private List<DmpDevice> getDeviceUuids(Integer deviceType, String deviceUuid) {
         LambdaQueryWrapper<DmpDevice> queryWrapper = new LambdaQueryWrapper<>();
         queryWrapper.eq(DmpDevice::getTenantId, 1208)
-                .eq(deviceType != null, DmpDevice::getDeviceType, deviceType);
-        List<DmpDevice> dmpDevices = dmpDeviceMapper.selectList(queryWrapper);
-        return dmpDevices.stream().map(DmpDevice::getDeviceUuid).collect(Collectors.toList());
+                .eq(deviceType != null, DmpDevice::getDeviceType, deviceType)
+                .eq(StringUtils.isNotBlank(deviceUuid), DmpDevice::getDeviceUuid, deviceUuid);
+        return dmpDeviceMapper.selectList(queryWrapper);
     }
 
     /**
      * 解析接口响应数据,提取指定字段
      */
-    private List<JSONObject> parseResponseData(String responseJson, Integer deviceType) {
+    private List<JSONObject> parseResponseData(String responseJson, Integer deviceType, List<DmpDevice> devices) {
         List<JSONObject> resultList = new ArrayList<>();
         if (responseJson == null) {
             return resultList;
         }
 
+        Map<String, Integer> uuid2Id = devices.stream()
+                .collect(Collectors.toMap(DmpDevice::getDeviceUuid, DmpDevice::getId));
+
         JSONObject responseObj = JSON.parseObject(responseJson);
         if (!"SUCCESS".equals(responseObj.getString("status")) || !"0".equals(responseObj.getString("code"))) {
             System.err.println("接口返回失败:" + responseObj.getString("msg"));
@@ -113,6 +119,12 @@ public class DeviceDataQuery {
             }
 
             targetData.put("deviceuuid", deviceUuid);
+
+            Integer deviceId = uuid2Id.get(deviceUuid);
+            if (deviceId != null) {
+                targetData.put("device_id", deviceId);
+            }
+
             if (hasValidData) {
                 resultList.add(targetData);
             }
@@ -138,55 +150,71 @@ public class DeviceDataQuery {
     /**
      * 生成模拟数据
      */
-    private List<JSONObject> generateSimulationData(Integer deviceType, List<String> deviceUuids) {
+    private List<JSONObject> generateSimulationData(Integer deviceType, List<DmpDevice> devices) {
         List<JSONObject> simulationList = new ArrayList<>();
         long currentTime = System.currentTimeMillis();
 
-        for (String deviceUuid : deviceUuids) {
+        for (DmpDevice device : devices) {
             JSONObject simulationData = new JSONObject();
             // simulationData.put("deviceuuid", deviceUuid);
             simulationData.put("time", currentTime);
+            simulationData.put("device_id", device.getId());
 
             switch (deviceType) {
                 // 空气质量
                 case 701:
-                    simulationData.put("wd", new Random().nextInt(50) - 10); // 温度:-10~40℃
-                    simulationData.put("sd", new Random().nextInt(101)); // 湿度:0~100%
-                    simulationData.put("o2", new Random().nextDouble() * 21); // 氧气:0~21%
-                    simulationData.put("co", new Random().nextDouble() * 100); // 一氧化碳:0~100ppm
-                    simulationData.put("co2", new Random().nextDouble() * 2000); // 二氧化碳:0~2000ppm
+                    simulationData.put("wd", ThreadLocalRandom.current().nextInt(50) - 10); // 温度:-10~40℃
+                    simulationData.put("sd", ThreadLocalRandom.current().nextInt(101)); // 湿度:0~100%
+                    simulationData.put("o2", ThreadLocalRandom.current().nextDouble() * 21); // 氧气:0~21%
+                    simulationData.put("co", ThreadLocalRandom.current().nextDouble() * 100); // 一氧化碳:0~100ppm
+                    simulationData.put("co2", ThreadLocalRandom.current().nextDouble() * 2000); // 二氧化碳:0~2000ppm
+                    break;
+                case 707: // 温度:wd
+                    simulationData.put("wd", ThreadLocalRandom.current().nextInt(50) - 10);
+                    break;
+                case 708: // 湿度:sd
+                    simulationData.put("sd", ThreadLocalRandom.current().nextInt(101));
+                    break;
+                case 709: // 氧气:o2
+                    simulationData.put("o2", ThreadLocalRandom.current().nextDouble() * 21);
+                    break;
+                case 710: // 二氧化碳:co2
+                    simulationData.put("co2", ThreadLocalRandom.current().nextDouble() * 2000);
+                    break;
+                case 711: // 一氧化碳:co
+                    simulationData.put("co", ThreadLocalRandom.current().nextDouble() * 100);
                     break;
                 // 水浸
                 case 702:
                     // 渗漏状态:0-正常,1-渗漏
-                    simulationData.put("leach_status", new Random().nextInt(2));
+                    simulationData.put("leach_status", ThreadLocalRandom.current().nextInt(2));
                     break;
                 // 人员统计
                 case 703:
                     // 流量数据:模拟正数
-                    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);
+                    simulationData.put("amount_into", ThreadLocalRandom.current().nextDouble() * 100);
+                    simulationData.put("amount_out", ThreadLocalRandom.current().nextDouble() * 100);
+                    simulationData.put("day_into", ThreadLocalRandom.current().nextDouble() * 1000);
+                    simulationData.put("day_out", ThreadLocalRandom.current().nextDouble() * 1000);
                     break;
                 // 电气火灾
                 case 704:
                     // 电气参数:模拟合理范围
-                    simulationData.put("voltage_a", 220 + new Random().nextDouble() * 10); // 电压A:220~230V
-                    simulationData.put("voltage_b", 220 + new Random().nextDouble() * 10);
-                    simulationData.put("voltage_c", 220 + new Random().nextDouble() * 10);
-                    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_b", 20 + new Random().nextDouble() * 30);
-                    simulationData.put("temperature_c", 20 + new Random().nextDouble() * 30);
-                    simulationData.put("current_residual", new Random().nextDouble() * 100); // 剩余电流:0~1A
+                    simulationData.put("voltage_a", 220 + ThreadLocalRandom.current().nextDouble() * 10); // 电压A:220~230V
+                    simulationData.put("voltage_b", 220 + ThreadLocalRandom.current().nextDouble() * 10);
+                    simulationData.put("voltage_c", 220 + ThreadLocalRandom.current().nextDouble() * 10);
+                    simulationData.put("current_a", ThreadLocalRandom.current().nextDouble() * 50); // 电流A:0~50A
+                    simulationData.put("current_b", ThreadLocalRandom.current().nextDouble() * 50);
+                    simulationData.put("current_c", ThreadLocalRandom.current().nextDouble() * 50);
+                    simulationData.put("temperature_a", 20 + ThreadLocalRandom.current().nextDouble() * 30); // 线温A:20~50℃
+                    simulationData.put("temperature_b", 20 + ThreadLocalRandom.current().nextDouble() * 30);
+                    simulationData.put("temperature_c", 20 + ThreadLocalRandom.current().nextDouble() * 30);
+                    simulationData.put("current_residual", ThreadLocalRandom.current().nextDouble() * 100); // 剩余电流:0~1A
                     break;
                 // 电能采集
                 case 705:
                     // 电能:模拟正数
-                    simulationData.put("electrical_energy", new Random().nextDouble() * 10000);
+                    simulationData.put("electrical_energy", ThreadLocalRandom.current().nextDouble() * 10000);
                     break;
                 default:
                     break;

+ 8 - 1
service-cdi/service-cdi-biz/src/main/java/com/usky/cdi/service/vo/base/BaseEnvMonitorPushVO.java

@@ -35,7 +35,14 @@ public abstract class BaseEnvMonitorPushVO implements Serializable {
      * 物联设施ID(必填)
      * 类型:Int,长度8(0 ~ 99999999)
      */
-    private Long sensorID;
+    private Integer sensorID;
+
+    /**
+     * 设备UUID
+     * 类型:String,长度16
+     * 说明:设备唯一标识
+     */
+    private String deviceUuid;
 
     /**
      * 监测时间(必填)

+ 0 - 11
service-cdi/service-cdi-biz/src/main/java/com/usky/cdi/service/vo/base/ElectricityLoadVO.java

@@ -20,11 +20,6 @@ public class ElectricityLoadVO extends BaseEnvMonitorPushVO {
     private static final long serialVersionUID = 1L;
 
     // ====================== 专属字段(严格映射数据包定义)======================
-    /**
-     * 物联设施ID(必填)
-     * 类型:Int,长度8(0 ~ 99999999)
-     */
-    private Long sensorID;
 
     /**
      * A相电压(必填)
@@ -114,7 +109,6 @@ public class ElectricityLoadVO extends BaseEnvMonitorPushVO {
     @Override
     protected void validateSensorValue() {
         // 1. 必填字段非空校验(父类已校验公共字段,此处校验专属必填字段)
-        if (sensorID == null) throw new IllegalArgumentException("物联设施ID(sensorID)为必填项");
         if (aVoltage == null) throw new IllegalArgumentException("A相电压(aVoltage)为必填项");
         if (bVoltage == null) throw new IllegalArgumentException("B相电压(bVoltage)为必填项");
         if (cVoltage == null) throw new IllegalArgumentException("C相电压(cVoltage)为必填项");
@@ -138,11 +132,6 @@ public class ElectricityLoadVO extends BaseEnvMonitorPushVO {
         if (line3TEMP != null) validateFloatRange(line3TEMP, -99.99f, 99.99f, "线温3", 2);
         if (line4TEMP != null) validateFloatRange(line4TEMP, -99.99f, 99.99f, "线温4", 2);
         if (leakageCurrent != null) validateFloatRange(leakageCurrent, 0.00f, 9999.99f, "剩余电流", 2);
-
-        // 4. 物联设施ID长度校验
-        if (String.valueOf(sensorID).length() > 8) {
-            throw new IllegalArgumentException("物联设施ID(sensorID)长度不能超过8位");
-        }
     }
 
     /**