|
|
@@ -4,7 +4,6 @@ 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 +13,7 @@ import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
+import java.text.DecimalFormat;
|
|
|
import java.util.*;
|
|
|
import java.util.concurrent.ThreadLocalRandom;
|
|
|
import java.util.stream.Collectors;
|
|
|
@@ -37,19 +37,31 @@ public class DeviceDataQuery {
|
|
|
private boolean simulation;
|
|
|
private Map<String, String> deviceFieldMapping;
|
|
|
|
|
|
+ // 定义各参数的格式化器(整数位,小数位)
|
|
|
+ private static final DecimalFormat FORMAT_2_2 = new DecimalFormat("00.00"); // 2位整数+2位小数
|
|
|
+ private static final DecimalFormat FORMAT_0_3 = new DecimalFormat("0.000"); // 0位整数+3位小数
|
|
|
+ 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位小数
|
|
|
+
|
|
|
/**
|
|
|
* 获取指定设备类型的设备数据
|
|
|
*/
|
|
|
- public List<JSONObject> getDeviceData(Integer deviceType, String deviceUuid) {
|
|
|
+ public List<JSONObject> getDeviceData(Integer deviceType, List<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);
|
|
|
+
|
|
|
+ if (deviceUuid != null && !deviceUuid.isEmpty()) {
|
|
|
+ requestBody.put("deviceUuids", deviceUuid);
|
|
|
+ } else {
|
|
|
+ List<String> deviceUuids = devices.stream().map(DmpDevice::getDeviceUuid).collect(Collectors.toList());
|
|
|
+ requestBody.put("deviceUuids", deviceUuids);
|
|
|
+ }
|
|
|
|
|
|
String response = HttpClientUtils.doPostJson(baseUrl, String.valueOf(requestBody));
|
|
|
|
|
|
@@ -66,11 +78,11 @@ public class DeviceDataQuery {
|
|
|
/**
|
|
|
* 获取指定设备类型的设备UUID列表
|
|
|
*/
|
|
|
- private List<DmpDevice> getDeviceUuids(Integer deviceType, String deviceUuid) {
|
|
|
+ private List<DmpDevice> getDeviceUuids(Integer deviceType, List<String> deviceUuid) {
|
|
|
LambdaQueryWrapper<DmpDevice> queryWrapper = new LambdaQueryWrapper<>();
|
|
|
queryWrapper.eq(DmpDevice::getTenantId, 1208)
|
|
|
.eq(deviceType != null, DmpDevice::getDeviceType, deviceType)
|
|
|
- .eq(StringUtils.isNotBlank(deviceUuid), DmpDevice::getDeviceUuid, deviceUuid);
|
|
|
+ .in(deviceUuid != null && !deviceUuid.isEmpty(), DmpDevice::getDeviceUuid, deviceUuid);
|
|
|
return dmpDeviceMapper.selectList(queryWrapper);
|
|
|
}
|
|
|
|
|
|
@@ -148,7 +160,10 @@ public class DeviceDataQuery {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 生成模拟数据
|
|
|
+ * 生成模拟数据(按指定精度格式化)
|
|
|
+ * @param deviceType 设备类型
|
|
|
+ * @param devices 设备列表
|
|
|
+ * @return 模拟数据列表
|
|
|
*/
|
|
|
private List<JSONObject> generateSimulationData(Integer deviceType, List<DmpDevice> devices) {
|
|
|
List<JSONObject> simulationList = new ArrayList<>();
|
|
|
@@ -156,66 +171,116 @@ public class DeviceDataQuery {
|
|
|
|
|
|
for (DmpDevice device : devices) {
|
|
|
JSONObject simulationData = new JSONObject();
|
|
|
- // simulationData.put("deviceuuid", deviceUuid);
|
|
|
simulationData.put("time", currentTime);
|
|
|
- simulationData.put("device_id", device.getId());
|
|
|
+ simulationData.put("device_id", device.getDeviceId());
|
|
|
|
|
|
switch (deviceType) {
|
|
|
- // 空气质量
|
|
|
+ // 空气质量(701):温度/湿度/氧气/二氧化碳/一氧化碳
|
|
|
case 701:
|
|
|
- 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
|
|
|
+ // 温度:-10~40℃ → 2位整数+2位小数(格式化时自动补零)
|
|
|
+ double temperature = ThreadLocalRandom.current().nextDouble(10, 20);
|
|
|
+ simulationData.put("wd", formatNumber(temperature, FORMAT_2_2));
|
|
|
+ // 湿度:0~100% → 2位整数+2位小数
|
|
|
+ double humidity = ThreadLocalRandom.current().nextDouble(40, 85);
|
|
|
+ simulationData.put("sd", formatNumber(humidity, FORMAT_2_2));
|
|
|
+ // 氧气浓度:0~21% → 2位整数+2位小数
|
|
|
+ double o2 = ThreadLocalRandom.current().nextDouble(20, 21);
|
|
|
+ simulationData.put("o2", formatNumber(o2, FORMAT_2_2));
|
|
|
+ // 一氧化碳浓度:0~100ppm → 2位整数+2位小数
|
|
|
+ double co = ThreadLocalRandom.current().nextDouble(0);
|
|
|
+ simulationData.put("co", formatNumber(co, FORMAT_2_2));
|
|
|
+ // 二氧化碳浓度:0~2000ppm → 0位整数+3位小数(实际范围0~2.000,对应0~2000ppm)
|
|
|
+ double co2 = ThreadLocalRandom.current().nextDouble(750, 760);
|
|
|
+ simulationData.put("co2", formatNumber(co2, FORMAT_0_3));
|
|
|
break;
|
|
|
- case 707: // 温度:wd
|
|
|
- simulationData.put("wd", ThreadLocalRandom.current().nextInt(50) - 10);
|
|
|
+
|
|
|
+ // 单一温度传感器(707)
|
|
|
+ case 707:
|
|
|
+ double temp707 = ThreadLocalRandom.current().nextDouble(10, 20);
|
|
|
+ simulationData.put("wd", formatNumber(temp707, FORMAT_2_2));
|
|
|
break;
|
|
|
- case 708: // 湿度:sd
|
|
|
- simulationData.put("sd", ThreadLocalRandom.current().nextInt(101));
|
|
|
+
|
|
|
+ // 单一湿度传感器(708)
|
|
|
+ case 708:
|
|
|
+ double hum708 = ThreadLocalRandom.current().nextDouble(40, 85);
|
|
|
+ simulationData.put("sd", formatNumber(hum708, FORMAT_2_2));
|
|
|
break;
|
|
|
- case 709: // 氧气:o2
|
|
|
- simulationData.put("o2", ThreadLocalRandom.current().nextDouble() * 21);
|
|
|
+
|
|
|
+ // 单一氧气传感器(709)
|
|
|
+ case 709:
|
|
|
+ double o2709 = ThreadLocalRandom.current().nextDouble(20, 21);
|
|
|
+ simulationData.put("o2", formatNumber(o2709, FORMAT_2_2));
|
|
|
break;
|
|
|
- case 710: // 二氧化碳:co2
|
|
|
- simulationData.put("co2", ThreadLocalRandom.current().nextDouble() * 2000);
|
|
|
+
|
|
|
+ // 单一二氧化碳传感器(710)
|
|
|
+ case 710:
|
|
|
+ double co2710 = ThreadLocalRandom.current().nextDouble(750, 760);
|
|
|
+ simulationData.put("co2", formatNumber(co2710, FORMAT_0_3));
|
|
|
break;
|
|
|
- case 711: // 一氧化碳:co
|
|
|
- simulationData.put("co", ThreadLocalRandom.current().nextDouble() * 100);
|
|
|
+
|
|
|
+ // 单一一氧化碳传感器(711)
|
|
|
+ case 711:
|
|
|
+ // double co711 = ThreadLocalRandom.current().nextDouble(0, 100);
|
|
|
+ simulationData.put("co", 0);
|
|
|
break;
|
|
|
- // 水浸
|
|
|
+
|
|
|
+ // 水浸(702):保持原有逻辑
|
|
|
case 702:
|
|
|
- // 渗漏状态:0-正常,1-渗漏
|
|
|
- simulationData.put("leach_status", ThreadLocalRandom.current().nextInt(2));
|
|
|
+ // simulationData.put("leach_status", ThreadLocalRandom.current().nextInt(2));
|
|
|
+ simulationData.put("leach_status", 0);
|
|
|
break;
|
|
|
- // 人员统计
|
|
|
+
|
|
|
+ // 人员统计(703):保持原有逻辑(若需精度可补充格式化)
|
|
|
case 703:
|
|
|
- // 流量数据:模拟正数
|
|
|
- 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);
|
|
|
+ // 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);
|
|
|
+ simulationData.put("sensorValue", 0);
|
|
|
break;
|
|
|
- // 电气火灾
|
|
|
+
|
|
|
+ // 电气火灾(704):对齐数据项代码+精度要求
|
|
|
case 704:
|
|
|
- // 电气参数:模拟合理范围
|
|
|
- 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
|
|
|
+ // A/B/C相电压:3位整数+2位小数(220.00~230.00V)
|
|
|
+ double aVoltage = ThreadLocalRandom.current().nextDouble(220, 230);
|
|
|
+ simulationData.put("aVoltage", formatNumber(aVoltage, FORMAT_3_2));
|
|
|
+ double bVoltage = ThreadLocalRandom.current().nextDouble(220, 230);
|
|
|
+ simulationData.put("bVoltage", formatNumber(bVoltage, FORMAT_3_2));
|
|
|
+ double cVoltage = ThreadLocalRandom.current().nextDouble(220, 230);
|
|
|
+ simulationData.put("cVoltage", formatNumber(cVoltage, FORMAT_3_2));
|
|
|
+
|
|
|
+ // A/B/C相电流:3位整数+2位小数(0.00~50.00A)
|
|
|
+ double aElectricity = ThreadLocalRandom.current().nextDouble(0, 50);
|
|
|
+ simulationData.put("aElectricity", formatNumber(aElectricity, FORMAT_3_2));
|
|
|
+ double bElectricity = ThreadLocalRandom.current().nextDouble(0, 50);
|
|
|
+ simulationData.put("bElectricity", formatNumber(bElectricity, FORMAT_3_2));
|
|
|
+ double cElectricity = ThreadLocalRandom.current().nextDouble(0, 50);
|
|
|
+ simulationData.put("cElectricity", formatNumber(cElectricity, FORMAT_3_2));
|
|
|
+
|
|
|
+ // 总功率:4位整数+2位小数(1.00~20.00,仅保留1-20范围)
|
|
|
+ double totalPower = ThreadLocalRandom.current().nextDouble(1, 20.000001);
|
|
|
+ simulationData.put("totalPower", formatNumber(totalPower, FORMAT_4_2));
|
|
|
+
|
|
|
+ // 线温1-4:2位整数+2位小数(20.00~50.00℃)
|
|
|
+ double line1TEMP = ThreadLocalRandom.current().nextDouble(20, 50);
|
|
|
+ simulationData.put("line1TEMP", formatNumber(line1TEMP, FORMAT_2_2));
|
|
|
+ double line2TEMP = ThreadLocalRandom.current().nextDouble(20, 50);
|
|
|
+ simulationData.put("Line2TEMP", formatNumber(line2TEMP, FORMAT_2_2));
|
|
|
+ double line3TEMP = ThreadLocalRandom.current().nextDouble(20, 50);
|
|
|
+ simulationData.put("Line3TEMP", formatNumber(line3TEMP, FORMAT_2_2));
|
|
|
+ double line4TEMP = ThreadLocalRandom.current().nextDouble(20, 50);
|
|
|
+ simulationData.put("Line4TEMP", formatNumber(line4TEMP, FORMAT_2_2));
|
|
|
+
|
|
|
+ // 剩余电流:4位整数+2位小数(0.00~100.00mA → 对应0.0000~0.1000A,按4.2格式化)
|
|
|
+ double leakageCurrent = ThreadLocalRandom.current().nextDouble(0, 100);
|
|
|
+ simulationData.put("leakageCurrent", formatNumber(leakageCurrent, FORMAT_4_2));
|
|
|
break;
|
|
|
- // 电能采集
|
|
|
+
|
|
|
+ // 电能采集(705):保持原有逻辑(若需精度可补充)
|
|
|
case 705:
|
|
|
- // 电能:模拟正数
|
|
|
simulationData.put("electrical_energy", ThreadLocalRandom.current().nextDouble() * 10000);
|
|
|
break;
|
|
|
+
|
|
|
default:
|
|
|
break;
|
|
|
}
|
|
|
@@ -227,4 +292,14 @@ public class DeviceDataQuery {
|
|
|
return simulationList;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 通用数值格式化方法
|
|
|
+ * @param value 原始数值
|
|
|
+ * @param format 格式化器
|
|
|
+ * @return 格式化后的字符串(可根据需求转为Double)
|
|
|
+ */
|
|
|
+ private String formatNumber(double value, DecimalFormat format) {
|
|
|
+ return format.format(value);
|
|
|
+ }
|
|
|
+
|
|
|
}
|