|
@@ -20,7 +20,7 @@ import vip.xiaonuo.coldchain.core.config.JfcloudColdChainConstants;
|
|
|
import vip.xiaonuo.coldchain.core.service.dataprocess.model.PowerEnum;
|
|
|
|
|
|
import java.time.Instant;
|
|
|
-import java.time.format.DateTimeFormatter;
|
|
|
+import java.time.ZoneOffset;
|
|
|
import java.util.Optional;
|
|
|
|
|
|
@Data
|
|
@@ -43,19 +43,14 @@ public class SensorData extends JfcloudInFluxEntity {
|
|
|
* 二氧化碳浓度字段
|
|
|
*/
|
|
|
@Column(name = "co2")
|
|
|
- private double co2 = 0;
|
|
|
+ private Float co2;
|
|
|
|
|
|
- /**
|
|
|
- * 路数,默认路数1
|
|
|
- */
|
|
|
- @Column(name = "roads")
|
|
|
- private Integer roads = 1;
|
|
|
|
|
|
/**
|
|
|
* 电池剩余电量
|
|
|
*/
|
|
|
@Column(name = "battery", tag = true)
|
|
|
- private double battery = 0;
|
|
|
+ private Float battery;
|
|
|
|
|
|
|
|
|
/**
|
|
@@ -74,13 +69,13 @@ public class SensorData extends JfcloudInFluxEntity {
|
|
|
* 经度字段
|
|
|
*/
|
|
|
@Column(name = "longitude", tag = true)
|
|
|
- private double lng;
|
|
|
+ private Double lng;
|
|
|
|
|
|
/**
|
|
|
* 纬度字段
|
|
|
*/
|
|
|
@Column(name = "latitude", tag = true)
|
|
|
- private double lat;
|
|
|
+ private Double lat;
|
|
|
|
|
|
/**
|
|
|
* 设备ID标签
|
|
@@ -88,6 +83,12 @@ public class SensorData extends JfcloudInFluxEntity {
|
|
|
@Column(name = "device_id", tag = true)
|
|
|
private String deviceId;
|
|
|
|
|
|
+ /**
|
|
|
+ * 路数,默认路数1
|
|
|
+ */
|
|
|
+ @Column(name = "roads", tag = true)
|
|
|
+ private Integer roads = 1;
|
|
|
+
|
|
|
/**
|
|
|
* 采集设备型号
|
|
|
*/
|
|
@@ -102,27 +103,75 @@ public class SensorData extends JfcloudInFluxEntity {
|
|
|
sensorData.setRoads(getIntegerValue(record, "roads"));
|
|
|
sensorData.setTemperature(getDoubleValue(record, "temperature"));
|
|
|
sensorData.setHumidity(getDoubleValue(record, "humidity"));
|
|
|
- sensorData.setCo2(getDoubleValue(record, "co2"));
|
|
|
- sensorData.setBattery(getDoubleValue(record, "battery"));
|
|
|
+ sensorData.setCo2(getFloatValue(record, "co2"));
|
|
|
+ sensorData.setBattery(getFloatValue(record, "battery"));
|
|
|
sensorData.setPlugInStatus(Optional.ofNullable(record.getValueByKey("plugInStatus")).map(Object::toString).orElse(null));
|
|
|
sensorData.setLocation(Optional.ofNullable(record.getValueByKey("location")).map(Object::toString).orElse(null));
|
|
|
sensorData.setLng(getDoubleValue(record, "longitude"));
|
|
|
sensorData.setLat(getDoubleValue(record, "latitude"));
|
|
|
Instant time = record.getTime();
|
|
|
if (time != null) {
|
|
|
- sensorData.setTime(Instant.parse(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(time)));
|
|
|
+ // 将 LocalDateTime 转换为 ZonedDateTime,附带时区信息
|
|
|
+ ZoneOffset offset = ZoneOffset.UTC; // 或者使用 ZoneId.systemDefault() 获取系统时区
|
|
|
+ sensorData.setTime(time.atOffset(offset).toInstant());
|
|
|
}
|
|
|
return sensorData;
|
|
|
}
|
|
|
|
|
|
// Helper method to safely extract Integer values
|
|
|
private static Integer getIntegerValue(FluxRecord record, String key) {
|
|
|
- return Optional.ofNullable(record.getValueByKey(key)).map(value -> (Integer) value).orElse(null);
|
|
|
+ return Optional.ofNullable(record.getValueByKey(key)) // Step 1: Get value by key (might be null)
|
|
|
+ .filter(value -> value instanceof Integer) // Step 2: Ensure the value is an instance of Integer
|
|
|
+ .map(value -> (Integer) value) // Step 3: Safely cast to Integer
|
|
|
+ .orElseGet(() -> {
|
|
|
+ // If not an Integer, try to parse it as a String (fallback mechanism)
|
|
|
+ Object value = record.getValueByKey(key);
|
|
|
+ if (value instanceof String) {
|
|
|
+ try {
|
|
|
+ return Integer.valueOf((String) value); // Try to parse String to Integer
|
|
|
+ } catch (NumberFormatException e) {
|
|
|
+ // If parsing fails, return null
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return null; // Return null if not a String or Integer
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
- // Helper method to safely extract Double values
|
|
|
+
|
|
|
private static Double getDoubleValue(FluxRecord record, String key) {
|
|
|
- return Optional.ofNullable(record.getValueByKey(key)).map(value -> (Double) value).orElse(null);
|
|
|
+ return Optional.ofNullable(record.getValueByKey(key))
|
|
|
+ .map(value -> {
|
|
|
+ if (value instanceof Double) {
|
|
|
+ return (Double) value; // 直接返回 Double 类型
|
|
|
+ } else if (value instanceof String) {
|
|
|
+ try {
|
|
|
+ return Double.valueOf((String) value); // 尝试将 String 转换为 Double
|
|
|
+ } catch (NumberFormatException e) {
|
|
|
+ return null; // 如果转换失败,返回 0.0 或其他默认值
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return null; // 返回默认值,如果值既不是 Double 也不是 String
|
|
|
+ })
|
|
|
+ .orElse(null); // 如果没有值,返回 0.0
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private static Float getFloatValue(FluxRecord record, String key) {
|
|
|
+ return Optional.ofNullable(record.getValueByKey(key))
|
|
|
+ .map(value -> {
|
|
|
+ if (value instanceof Float) {
|
|
|
+ return (Float) value; // 直接返回 Double 类型
|
|
|
+ } else if (value instanceof String) {
|
|
|
+ try {
|
|
|
+ return Float.valueOf((String) value);
|
|
|
+ } catch (NumberFormatException e) {
|
|
|
+ return null; // 如果转换失败,返回 0.0 或其他默认值
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return null; // 返回默认值,如果值既不是 Double 也不是 String
|
|
|
+ })
|
|
|
+ .orElse(null); // 如果没有值,返回 0.0
|
|
|
}
|
|
|
|
|
|
}
|