浏览代码

数据修改

fuyuchuan 1 天之前
父节点
当前提交
d8b11a2ffd

+ 7 - 0
service-cdi/service-cdi-biz/src/main/java/com/usky/cdi/controller/AlarmDataController.java

@@ -3,6 +3,7 @@ package com.usky.cdi.controller;
 import com.usky.cdi.service.impl.AlarmDataTransferService;
 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 lombok.extern.slf4j.Slf4j;
@@ -53,4 +54,10 @@ public class AlarmDataController {
         boolean success = alarmDataTransferService.sendAlarmMessage2(vo);
         return success ? "上报成功" : "上报失败";
     }
+
+    @PostMapping("/alarmMessage3")
+    public String sendAlarmMessage3(@RequestBody AlarmMessage3VO vo) {
+        boolean success = alarmDataTransferService.sendEngineeringBase(vo);
+        return success ? "上报成功" : "上报失败";
+    }
 }

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

@@ -7,7 +7,9 @@ import com.usky.cdi.service.config.mqtt.MqttOutConfig;
 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.boot.autoconfigure.condition.ConditionalOnProperty;
@@ -160,4 +162,26 @@ public class AlarmDataTransferService {
             return false;
         }
     }
+
+    public boolean sendEngineeringBase(AlarmMessage3VO 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();
+            String topic = "alarm/message";
+            System.out.println("推送的数据: " + json);
+            mqttGateway.sendToMqtt(topic, json);
+
+            return true;
+        } catch (Exception e) {
+            log.error("发送电流告警信息失败,EngineeringID: {}", vo.getEngineeringID(), e);
+            return false;
+        }
+    }
 }

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

@@ -207,6 +207,8 @@ public class DeviceDataQuery {
      * @return 模拟数据列表
      */
     private List<JSONObject> generateSimulationData(Integer deviceType, List<DmpDevice> devices) {
+
+
         List<JSONObject> simulationList = new ArrayList<>();
         long currentTime = System.currentTimeMillis();
 
@@ -214,21 +216,25 @@ public class DeviceDataQuery {
         final double TEMP_RANGE_MIN = 10.0;
         final double TEMP_RANGE_MAX = 20.0;
         final double HUMIDITY_RANGE_MIN = 40.0;
-        final double HUMIDITY_RANGE_MAX = 85.0;
+        final double HUMIDITY_RANGE_MAX = 41.0;
         final double OXYGEN_RANGE_MIN = 20.0;
         final double OXYGEN_RANGE_MAX = 21.0;
-        final double CO2_RANGE_MIN = 750.0;
-        final double CO2_RANGE_MAX = 760.0;
+        final double CO2_RANGE_MIN = 480.0;
+        final double CO2_RANGE_MAX = 490.0;
         final double VOLTAGE_RANGE_MIN = 220.0;
         final double VOLTAGE_RANGE_MAX = 230.0;
         final double CURRENT_RANGE_MIN = 0.0;
-        final double CURRENT_RANGE_MAX = 50.0;
+        final double CURRENT_RANGE_MAX = 1.5;
         final double POWER_RANGE_MIN = 1.0;
         final double POWER_RANGE_MAX = 20.0;
         final double TEMP_LINE_RANGE_MIN = 20.0;
         final double TEMP_LINE_RANGE_MAX = 50.0;
-        final double LEAKAGE_CURRENT_RANGE_MIN = 0.0;
-        final double LEAKAGE_CURRENT_RANGE_MAX = 100.0;
+        final double LEAKAGE_CURRENT_RANGE_MIN = 50.0;
+        final double LEAKAGE_CURRENT_RANGE_MAX = 60.0;
+        final double FLOATING_RANGE_MIN = 0.0;
+        final double FLOATING_RANGE_MAX = 0.5;
+        final double FLOATING_RANGE_MIN1 = 0.0;
+        final double FLOATING_RANGE_MAX1 = 1.0;
 
         for (DmpDevice device : devices) {
             JSONObject simulationData = new JSONObject();
@@ -243,14 +249,20 @@ public class DeviceDataQuery {
             switch (deviceType) {
                 // 单一温度传感器(707)
                 case 707:
-                    double temp707 = ThreadLocalRandom.current().nextDouble(TEMP_RANGE_MIN, TEMP_RANGE_MAX);
+                    Map<String, Double> stringDoubleMap1 = WeatherFetcher.fetchWeather();
+                    double temp707 = 0.0;
+                    if (stringDoubleMap1.isEmpty()) {
+                        temp707 = ThreadLocalRandom.current().nextDouble(TEMP_RANGE_MIN, TEMP_RANGE_MAX);
+                    } else {
+                        double floating = ThreadLocalRandom.current().nextDouble(FLOATING_RANGE_MIN, FLOATING_RANGE_MAX);
+                        temp707 = stringDoubleMap1.get("temperature") + 0.5 + floating;
+                    }
                     simulationData.put("wd", formatNumber(temp707, FORMAT_2_2));
                     break;
 
                 // 单一湿度传感器(708)
                 case 708:
-                    double hum708 = ThreadLocalRandom.current().nextDouble(HUMIDITY_RANGE_MIN, HUMIDITY_RANGE_MAX);
-                    simulationData.put("sd", formatNumber(hum708, FORMAT_2_2));
+                    simulationData.put("sd", formatNumber(ThreadLocalRandom.current().nextDouble(HUMIDITY_RANGE_MIN, HUMIDITY_RANGE_MAX), FORMAT_2_2));
                     break;
 
                 // 单一氧气传感器(709)
@@ -282,24 +294,38 @@ public class DeviceDataQuery {
 
                 // 电气火灾(704)
                 case 704:
+                    Map<String, Double> stringDoubleMap = WeatherFetcher.fetchWeather();
                     // A/B/C相电压:3位整数+2位小数(220.00~230.00V)
-                    simulationData.put("aVoltage", formatNumber(ThreadLocalRandom.current().nextDouble(VOLTAGE_RANGE_MIN, VOLTAGE_RANGE_MAX), FORMAT_3_2));
-                    simulationData.put("bVoltage", formatNumber(ThreadLocalRandom.current().nextDouble(VOLTAGE_RANGE_MIN, VOLTAGE_RANGE_MAX), FORMAT_3_2));
-                    simulationData.put("cVoltage", formatNumber(ThreadLocalRandom.current().nextDouble(VOLTAGE_RANGE_MIN, VOLTAGE_RANGE_MAX), FORMAT_3_2));
+                    double aVoltage = ThreadLocalRandom.current().nextDouble(VOLTAGE_RANGE_MIN, VOLTAGE_RANGE_MAX);
+                    simulationData.put("aVoltage", formatNumber(aVoltage, FORMAT_3_2));
+                    double bVoltage = ThreadLocalRandom.current().nextDouble(VOLTAGE_RANGE_MIN, VOLTAGE_RANGE_MAX);
+                    simulationData.put("bVoltage", formatNumber(bVoltage, FORMAT_3_2));
+                    double cVoltage = ThreadLocalRandom.current().nextDouble(VOLTAGE_RANGE_MIN, VOLTAGE_RANGE_MAX);
+                    simulationData.put("cVoltage", formatNumber(cVoltage, FORMAT_3_2));
 
                     // A/B/C相电流:3位整数+2位小数(0.00~50.00A)
-                    simulationData.put("aElectricity", formatNumber(ThreadLocalRandom.current().nextDouble(CURRENT_RANGE_MIN, CURRENT_RANGE_MAX), FORMAT_3_2));
-                    simulationData.put("bElectricity", formatNumber(ThreadLocalRandom.current().nextDouble(CURRENT_RANGE_MIN, CURRENT_RANGE_MAX), FORMAT_3_2));
-                    simulationData.put("cElectricity", formatNumber(ThreadLocalRandom.current().nextDouble(CURRENT_RANGE_MIN, CURRENT_RANGE_MAX), FORMAT_3_2));
+                    double aElectricity = ThreadLocalRandom.current().nextDouble(CURRENT_RANGE_MIN, CURRENT_RANGE_MAX);
+                    simulationData.put("aElectricity", formatNumber(aElectricity, FORMAT_3_2));
+                    double bElectricity = ThreadLocalRandom.current().nextDouble(CURRENT_RANGE_MIN, CURRENT_RANGE_MAX);
+                    simulationData.put("bElectricity", formatNumber(bElectricity, FORMAT_3_2));
+                    double cElectricity = ThreadLocalRandom.current().nextDouble(CURRENT_RANGE_MIN, CURRENT_RANGE_MAX);
+                    simulationData.put("cElectricity", formatNumber(cElectricity, FORMAT_3_2));
 
                     // 总功率:4位整数+2位小数(1.00~20.00)
-                    simulationData.put("totalPower", formatNumber(ThreadLocalRandom.current().nextDouble(POWER_RANGE_MIN, POWER_RANGE_MAX), FORMAT_4_2));
-
-                    // 线温1-4:2位整数+2位小数(20.00~50.00℃)
-                    simulationData.put("line1TEMP", formatNumber(ThreadLocalRandom.current().nextDouble(TEMP_LINE_RANGE_MIN, TEMP_LINE_RANGE_MAX), FORMAT_2_2));
-                    simulationData.put("Line2TEMP", formatNumber(ThreadLocalRandom.current().nextDouble(TEMP_LINE_RANGE_MIN, TEMP_LINE_RANGE_MAX), FORMAT_2_2));
-                    simulationData.put("Line3TEMP", formatNumber(ThreadLocalRandom.current().nextDouble(TEMP_LINE_RANGE_MIN, TEMP_LINE_RANGE_MAX), FORMAT_2_2));
-                    simulationData.put("Line4TEMP", formatNumber(ThreadLocalRandom.current().nextDouble(TEMP_LINE_RANGE_MIN, TEMP_LINE_RANGE_MAX), FORMAT_2_2));
+                    double totalPower = (aVoltage * aElectricity + bVoltage * bElectricity + cVoltage * cElectricity) / 1000;
+                    simulationData.put("totalPower", formatNumber(totalPower, FORMAT_4_2));
+
+                    // 线温1-4:2位整数+2位小数(20.00~50.00℃)+1.0 +0.5
+                    double lineTemp = 0.0;
+                    if (stringDoubleMap.isEmpty()) {
+                        lineTemp = ThreadLocalRandom.current().nextDouble(TEMP_LINE_RANGE_MIN, TEMP_LINE_RANGE_MAX);
+                    } else {
+                        lineTemp = stringDoubleMap.get("temperature") + 1.0;
+                    }
+                    simulationData.put("line1TEMP", formatNumber(lineTemp + ThreadLocalRandom.current().nextDouble(FLOATING_RANGE_MIN1, FLOATING_RANGE_MAX1), FORMAT_2_2));
+                    simulationData.put("Line2TEMP", formatNumber(lineTemp + ThreadLocalRandom.current().nextDouble(FLOATING_RANGE_MIN1, FLOATING_RANGE_MAX1), FORMAT_2_2));
+                    simulationData.put("Line3TEMP", formatNumber(lineTemp + ThreadLocalRandom.current().nextDouble(FLOATING_RANGE_MIN1, FLOATING_RANGE_MAX1), FORMAT_2_2));
+                    simulationData.put("Line4TEMP", formatNumber(lineTemp + ThreadLocalRandom.current().nextDouble(FLOATING_RANGE_MIN1, FLOATING_RANGE_MAX1), FORMAT_2_2));
 
                     // 剩余电流:4位整数+2位小数(0.00~100.00mA)
                     simulationData.put("leakageCurrent", formatNumber(ThreadLocalRandom.current().nextDouble(LEAKAGE_CURRENT_RANGE_MIN, LEAKAGE_CURRENT_RANGE_MAX), FORMAT_4_2));

+ 92 - 0
service-cdi/service-cdi-biz/src/main/java/com/usky/cdi/service/util/WeatherFetcher.java

@@ -0,0 +1,92 @@
+package com.usky.cdi.service.util;
+
+import java.io.BufferedReader;
+import java.io.InputStreamReader;
+import java.net.HttpURLConnection;
+import java.net.URL;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.json.JSONObject;
+
+/**
+ *
+ * @author fyc
+ * @email yuchuan.fu@chinausky.com
+ * @date 2025/12/15
+ */
+
+public class WeatherFetcher {
+    private static final String API_KEY = "d04f0f84421a99b9b66dd99243c36db4";
+    private static final String CITY_NAME = "shanghai,cn";
+    private static final String API_URL = "https://api.openweathermap.org/data/2.5/weather?q=" + CITY_NAME + "&appid=" + API_KEY;
+
+    public static Map<String, Double> fetchWeather() {
+        double tempCelsius = 0.0;
+        int humidity = 0;
+        try {
+            // 1. 构造请求URL
+            URL url = new URL(API_URL);
+
+            // 2. 建立连接并发送请求
+            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
+            conn.setRequestMethod("GET");
+
+            // 3. 读取响应
+            BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
+            StringBuilder response = new StringBuilder();
+            String line;
+            while ((line = reader.readLine()) != null) {
+                response.append(line);
+            }
+            reader.close();
+
+            // 4. 解析JSON数据(这里使用了org.json库)
+            JSONObject jsonResponse = new JSONObject(response.toString());
+            JSONObject main = jsonResponse.getJSONObject("main");
+
+            // 1. 提取基础信息
+            String cityName = jsonResponse.getString("name");
+            int timezoneOffset = jsonResponse.getInt("timezone");
+
+            // 注意:温度默认是开尔文单位,转换为摄氏度需要 -273.15
+            double tempKelvin = main.getDouble("temp");
+            tempCelsius = tempKelvin - 273.15;
+            humidity = main.getInt("humidity");
+            double feelsLikeKelvin = main.getDouble("feels_like");
+            double feelsLikeCelsius = feelsLikeKelvin - 273.15;
+
+            // 3. 提取天气状况描述
+            JSONObject weather = jsonResponse.getJSONArray("weather").getJSONObject(0);
+            String description = weather.getString("description");
+
+            // 4. 输出解析结果
+            System.out.println("=== 天气解析结果 ===");
+            System.out.println("城市: " + cityName);
+            System.out.println("温度: " + String.format("%.2f", tempCelsius) + "°C (原始: " + tempKelvin + "K)");
+            System.out.println("体感温度: " + String.format("%.2f", feelsLikeCelsius) + "°C");
+            System.out.println("湿度: " + humidity + "%");
+            System.out.println("天气状况: " + description);
+            System.out.println("时区偏移: " + (timezoneOffset / 3600) + "小时");
+
+            // 5. 检查是否包含臭氧数据
+            if (jsonResponse.has("air_quality") || jsonResponse.has("o3") || jsonResponse.has("components")) {
+                System.out.println("包含空气质量数据");
+            } else {
+                System.out.println("提示: 当前数据不包含臭氧浓度等空气质量指标。");
+            }
+
+        } catch (Exception e) {
+            System.err.println("解析JSON时出错: " + e.getMessage());
+            e.printStackTrace();
+        }
+        Map<String, Double> resultMap = new HashMap<>();
+        resultMap.put("temperature", tempCelsius);
+        resultMap.put("humidity", (double) humidity);
+        return resultMap;
+    }
+
+    public static void main(String[] args) {
+        fetchWeather();
+    }
+}