|
|
@@ -15,6 +15,7 @@ import org.springframework.stereotype.Component;
|
|
|
|
|
|
import java.text.DecimalFormat;
|
|
|
import java.util.*;
|
|
|
+import java.util.concurrent.ConcurrentHashMap;
|
|
|
import java.util.concurrent.ThreadLocalRandom;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
@@ -43,6 +44,42 @@ public class DeviceDataQuery {
|
|
|
private static final DecimalFormat FORMAT_3_2 = new DecimalFormat("000.00"); // 3位整数+2位小数
|
|
|
private static final DecimalFormat FORMAT_4_2 = new DecimalFormat("0000.00"); // 4位整数+2位小数
|
|
|
|
|
|
+ // ========== 新增缓存相关成员变量 ==========
|
|
|
+ // 缓存A/B/C相电流值
|
|
|
+ private static final Map<String, Double> currentValueCache = new ConcurrentHashMap<>();
|
|
|
+ // 缓存各相复用次数
|
|
|
+ private static final Map<String, Integer> currentReuseCountCache = new ConcurrentHashMap<>();
|
|
|
+ // 最大复用次数(可调整)
|
|
|
+ private static final int MAX_REUSE_COUNT = 3;
|
|
|
+ // 复用概率(0.5=50%概率复用,可调整)
|
|
|
+ private static final double REUSE_PROBABILITY = 0.5;
|
|
|
+
|
|
|
+ // 定义模拟数据范围常量
|
|
|
+ private static final double TEMP_RANGE_MIN = 10.0;
|
|
|
+ private static final double TEMP_RANGE_MAX = 20.0;
|
|
|
+ private static final double HUMIDITY_RANGE_MIN = 40.0;
|
|
|
+ private static final double HUMIDITY_RANGE_MAX = 41.0;
|
|
|
+ private static final double OXYGEN_RANGE_MIN = 20.0;
|
|
|
+ private static final double OXYGEN_RANGE_MAX = 21.0;
|
|
|
+ private static final double CO2_RANGE_MIN = 480.0;
|
|
|
+ private static final double CO2_RANGE_MAX = 490.0;
|
|
|
+ private static final double VOLTAGE_RANGE_MIN = 220.0;
|
|
|
+ private static final double VOLTAGE_RANGE_MAX = 230.0;
|
|
|
+ private static final double CURRENT_RANGE_MIN = 0.0;
|
|
|
+ private static final double CURRENT_RANGE_MAX = 1.5;
|
|
|
+ private static final double POWER_RANGE_MIN = 1.0;
|
|
|
+ private static final double POWER_RANGE_MAX = 20.0;
|
|
|
+ private static final double TEMP_LINE_RANGE_MIN = 20.0;
|
|
|
+ private static final double TEMP_LINE_RANGE_MAX = 50.0;
|
|
|
+ private static final double LEAKAGE_CURRENT_RANGE_MIN = 50.0;
|
|
|
+ private static final double LEAKAGE_CURRENT_RANGE_MAX = 60.0;
|
|
|
+ private static final double FLOATING_RANGE_MIN = 0.0;
|
|
|
+ private static final double FLOATING_RANGE_MAX = 0.5;
|
|
|
+ private static final double FLOATING_RANGE_MIN1 = 0.0;
|
|
|
+ private static final double FLOATING_RANGE_MAX1 = 1.0;
|
|
|
+
|
|
|
+ private static int callCount = 0;
|
|
|
+
|
|
|
/**
|
|
|
* 获取指定设备类型的设备数据
|
|
|
* @param transferVO 设备数据传输请求参数
|
|
|
@@ -208,34 +245,11 @@ public class DeviceDataQuery {
|
|
|
*/
|
|
|
private List<JSONObject> generateSimulationData(Integer deviceType, List<DmpDevice> devices) {
|
|
|
|
|
|
+ Map<String, Double> stringDoubleMap = WeatherFetcher.fetchWeather();
|
|
|
|
|
|
List<JSONObject> simulationList = new ArrayList<>();
|
|
|
long currentTime = System.currentTimeMillis();
|
|
|
|
|
|
- // 定义模拟数据范围常量
|
|
|
- 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 = 41.0;
|
|
|
- final double OXYGEN_RANGE_MIN = 20.0;
|
|
|
- final double OXYGEN_RANGE_MAX = 21.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 = 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 = 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();
|
|
|
simulationData.put("time", currentTime);
|
|
|
@@ -249,13 +263,12 @@ public class DeviceDataQuery {
|
|
|
switch (deviceType) {
|
|
|
// 单一温度传感器(707)
|
|
|
case 707:
|
|
|
- Map<String, Double> stringDoubleMap1 = WeatherFetcher.fetchWeather();
|
|
|
double temp707 = 0.0;
|
|
|
- if (stringDoubleMap1.isEmpty()) {
|
|
|
+ if (stringDoubleMap.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;
|
|
|
+ temp707 = stringDoubleMap.get("temperature") + 0.5 + floating;
|
|
|
}
|
|
|
simulationData.put("wd", formatNumber(temp707, FORMAT_2_2));
|
|
|
break;
|
|
|
@@ -294,7 +307,6 @@ public class DeviceDataQuery {
|
|
|
|
|
|
// 电气火灾(704)
|
|
|
case 704:
|
|
|
- Map<String, Double> stringDoubleMap = WeatherFetcher.fetchWeather();
|
|
|
// A/B/C相电压:3位整数+2位小数(220.00~230.00V)
|
|
|
double aVoltage = ThreadLocalRandom.current().nextDouble(VOLTAGE_RANGE_MIN, VOLTAGE_RANGE_MAX);
|
|
|
simulationData.put("aVoltage", formatNumber(aVoltage, FORMAT_3_2));
|
|
|
@@ -303,12 +315,13 @@ public class DeviceDataQuery {
|
|
|
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)
|
|
|
- double aElectricity = ThreadLocalRandom.current().nextDouble(CURRENT_RANGE_MIN, CURRENT_RANGE_MAX);
|
|
|
+ // A/B/C相电流:3位整数+2位小数(0.00~1.50A)
|
|
|
+ String deviceId = device.getDeviceId();
|
|
|
+ double aElectricity = getCurrentValue(deviceId, "a");
|
|
|
simulationData.put("aElectricity", formatNumber(aElectricity, FORMAT_3_2));
|
|
|
- double bElectricity = ThreadLocalRandom.current().nextDouble(CURRENT_RANGE_MIN, CURRENT_RANGE_MAX);
|
|
|
+ double bElectricity = getCurrentValue(deviceId, "b");
|
|
|
simulationData.put("bElectricity", formatNumber(bElectricity, FORMAT_3_2));
|
|
|
- double cElectricity = ThreadLocalRandom.current().nextDouble(CURRENT_RANGE_MIN, CURRENT_RANGE_MAX);
|
|
|
+ double cElectricity = getCurrentValue(deviceId, "c");
|
|
|
simulationData.put("cElectricity", formatNumber(cElectricity, FORMAT_3_2));
|
|
|
|
|
|
// 总功率:4位整数+2位小数(1.00~20.00)
|
|
|
@@ -356,4 +369,42 @@ public class DeviceDataQuery {
|
|
|
return format.format(value);
|
|
|
}
|
|
|
|
|
|
+ // ========== 获取带缓存的电流值 ==========
|
|
|
+ private double getCurrentValue(String deviceId, String phase) {
|
|
|
+
|
|
|
+ // 构建设备+相的唯一缓存Key(核心!)
|
|
|
+ String cacheKey = deviceId + "_" + phase;
|
|
|
+ // 后续逻辑不变,只是把所有phase替换为cacheKey
|
|
|
+ int currentReuseCount = currentReuseCountCache.getOrDefault(cacheKey, 0);
|
|
|
+
|
|
|
+ System.out.println("cacheKey=" + cacheKey +
|
|
|
+ " 命中=" + currentValueCache.containsKey(cacheKey) +
|
|
|
+ " 复用次数=" + currentReuseCountCache.getOrDefault(cacheKey, 0) +
|
|
|
+ "值=" + currentValueCache.get(cacheKey));
|
|
|
+
|
|
|
+ if (!currentValueCache.containsKey(cacheKey) || currentReuseCount >= MAX_REUSE_COUNT) {
|
|
|
+ return generateNewCurrent(cacheKey);
|
|
|
+ }
|
|
|
+
|
|
|
+ boolean reuse = ThreadLocalRandom.current().nextDouble() < REUSE_PROBABILITY;
|
|
|
+ if (reuse) {
|
|
|
+ int newReuseCount = currentReuseCount + 1;
|
|
|
+ currentReuseCountCache.put(cacheKey, newReuseCount);
|
|
|
+ System.out.println("复用值=" + currentValueCache.get(cacheKey));
|
|
|
+ return currentValueCache.get(cacheKey);
|
|
|
+ } else {
|
|
|
+ double newValue = generateNewCurrent(cacheKey);
|
|
|
+ System.out.println("生成新值=" + newValue);
|
|
|
+ return newValue;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 同步修改generateNewCurrent方法(参数改为cacheKey)
|
|
|
+ private double generateNewCurrent(String cacheKey) {
|
|
|
+ double newValue = ThreadLocalRandom.current().nextDouble(CURRENT_RANGE_MIN, CURRENT_RANGE_MAX);
|
|
|
+ newValue = Math.round(newValue * 100.0) / 100.0;
|
|
|
+ currentValueCache.put(cacheKey, newValue);
|
|
|
+ currentReuseCountCache.put(cacheKey, 0);
|
|
|
+ return newValue;
|
|
|
+ }
|
|
|
}
|