소스 검색

人防检测数据代码优化

fuyuchuan 16 시간 전
부모
커밋
7f65dfc77c

+ 3 - 6
service-cdi/service-cdi-biz/src/main/java/com/usky/cdi/service/impl/BaseDataTransferService.java

@@ -192,21 +192,18 @@ public class BaseDataTransferService {
             map.put("floorFileSuffix", vo.getFloorFileSuffix());
             map.put("filePixWidth", width);
             map.put("filePixHeight", height);
-            map.put("floorFile", Base64.getEncoder().encodeToString(imageBytes));
+            map.put("floorFile", imageBytes);
             map.put("publishTime", vo.getPublishTime());
 
             //使用Gson:
-            Gson gson = new GsonBuilder()
-                    .setLongSerializationPolicy(LongSerializationPolicy.STRING)
-                    .create();
-            String json = gson.toJson(map);
+            Gson gson = new Gson();
 
             // ========== 5. MQTT发送(修复版) ==========
             String topic = "base/floorPlane";
             MqttConnectionTool.MqttGateway gateway = mqttConnectionTool.connectOrRefresh("3101100021", "SIixzph1");
 
             // 发送JSON字符串
-            gateway.sendToMqtt(topic, json);
+            gateway.sendToMqtt(topic, gson.toJson(map));
 
             System.out.println("✅ MQTT发送成功 TOPIC: " + topic);
             return true;

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

@@ -984,7 +984,7 @@ public class IotDataTransferService {
                 vo.setSensorValue(value);
 
                 try {
-                    sendMqttMessage(MqttTopics.IotInfo.DEVIATION.getTopic(), vo, MqttTopics.IotInfo.DEVIATION.getDesc(), transferVO.getUsername());
+                    sendMqttMessage(MqttTopics.IotInfo.SEWAGE_LEVEL.getTopic(), vo, MqttTopics.IotInfo.SEWAGE_LEVEL.getDesc(), transferVO.getUsername());
                     result.put("successCount", result.get("successCount") + 1);
                 } catch (Exception e) {
                     log.warn("设备{}的水位数据推送失败:{}", deviceId, e.getMessage());

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

@@ -86,7 +86,7 @@ public class DeviceDataQuery {
     private static final double FLOATING_RANGE_MIN1 = 0.0;
     private static final double FLOATING_RANGE_MAX1 = 1.0;
     private static final double SEWAGE_LEVEL_MIN = 0.0;
-    private static final double SEWAGE_LEVEL_MAX = 0.5;
+    private static final double SEWAGE_LEVEL_MAX = 0.1;
 
     private static int callCount = 0;
 
@@ -123,7 +123,7 @@ public class DeviceDataQuery {
                 LambdaQueryWrapper<DmpDevice> queryWrapper = new LambdaQueryWrapper<>();
                 queryWrapper.eq(DmpDevice::getTenantId, 1205)
                         .eq(DmpDevice::getDeviceType, transferVO.getDeviceType())
-                        .orderByDesc(DmpDevice::getId)
+                        .orderByAsc(DmpDevice::getId)
                         .last("LIMIT 1");
                 DmpDevice xiangyuDevice = dmpDeviceMapper.selectOne(queryWrapper);
 
@@ -472,7 +472,9 @@ public class DeviceDataQuery {
                         break;
 
                     case 716:
-                        simData.put("sensorValue", formatNumber(ThreadLocalRandom.current().nextDouble(SEWAGE_LEVEL_MIN, SEWAGE_LEVEL_MAX), FORMAT_1_2));
+                        String deviceId1 = device.getDeviceId();
+                        double value = FixedWaterLevelGenerator.getSensorValue(deviceId1);
+                        simData.put("sensorValue", formatNumber(value, FORMAT_1_2));
                         break;
 
                     default:
@@ -520,7 +522,7 @@ public class DeviceDataQuery {
             double noise = original * noiseFactor * (ThreadLocalRandom.current().nextDouble(-1, 1));
             double newValue = original + noise;
 
-            // System.out.println("加噪: " + key + " | 原值=" + original + " | 噪声=" + noise + " | 新值=" + newValue);
+            System.out.println("加噪: " + key + " | 原值=" + original + " | 噪声=" + noise + " | 新值=" + newValue);
 
             // 保持原类型:原来是 String 就格式化回 String,原来是 Number 就放 Double
             if (value instanceof String) {

+ 5 - 5
service-cdi/service-cdi-biz/src/main/java/com/usky/cdi/service/util/DeviceDataSyncService.java

@@ -26,12 +26,12 @@ public class DeviceDataSyncService {
      * fixedDelay:任务执行完成后固定延迟29分钟执行下一次
      * initialDelay:初始化后立即执行第一次任务
      */
-    // @Scheduled(fixedDelay = 26 * 60 * 1000, initialDelay = 0)
+    // @Scheduled(fixedDelay = 2 * 60 * 1000, initialDelay = 0)
     // public void scheduledDeviceDataSync() {
-    //     Integer tenantId = 1205;
-    //     Long engineeringId = 3101070011L;
-    //     String username = "3101070011";
-    //     String password = "5RqhJ7VG";
+    //     Integer tenantId = 1222;
+    //     Long engineeringId = 3101100021L;
+    //     String username = "3101100021";
+    //     String password = "SIixzph1";
     //     log.info("开始执行桃浦象屿人防设备数据同步定时任务,租户ID:{},工程ID:{}", tenantId, engineeringId);
     //
     //     try {

+ 44 - 0
service-cdi/service-cdi-biz/src/main/java/com/usky/cdi/service/util/FixedWaterLevelGenerator.java

@@ -0,0 +1,44 @@
+package com.usky.cdi.service.util;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.concurrent.ThreadLocalRandom;
+
+/**
+ *
+ * @author fyc
+ * @email yuchuan.fu@chinausky.com
+ * @date 2026/4/28
+ */
+public class FixedWaterLevelGenerator {
+
+    // 关键:缓存每个设备的固定水位(只生成一次)
+    private static final Map<String, Double> deviceLevelMap = new HashMap<>();
+
+    /**
+     * 获取设备固定水位(永远不变)
+     */
+    public static double getSensorValue(String deviceId) {
+        // 如果这个设备还没生成过值 → 生成一个不重复的固定值
+        if (!deviceLevelMap.containsKey(deviceId)) {
+            double value = generateUniqueValue();
+            deviceLevelMap.put(deviceId, value);
+        }
+        // 直接返回固定值
+        return deviceLevelMap.get(deviceId);
+    }
+
+    /**
+     * 生成一个不重复、≤0.1 的水位
+     */
+    private static double generateUniqueValue() {
+        double val;
+        do {
+            // 生成 0.01 ~ 0.09 的随机数(保证不超0.1)
+            val = ThreadLocalRandom.current().nextDouble(0.01, 0.099);
+            // 保留2位小数
+            val = Math.round(val * 100) / 100.0;
+        } while (deviceLevelMap.containsValue(val)); // 确保不重复
+        return val;
+    }
+}