|
@@ -6,21 +6,29 @@ import com.influxdb.annotations.Measurement;
|
|
|
import com.influxdb.query.FluxRecord;
|
|
|
import lombok.AllArgsConstructor;
|
|
|
import lombok.Data;
|
|
|
+import lombok.EqualsAndHashCode;
|
|
|
import lombok.NoArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import vip.xiaonuo.coldchain.core.config.JfcloudColdChainConstants;
|
|
|
import vip.xiaonuo.coldchain.core.service.dataprocess.model.PowerEnum;
|
|
|
|
|
|
+import java.lang.reflect.Field;
|
|
|
import java.time.Instant;
|
|
|
-import java.time.ZoneOffset;
|
|
|
-import java.util.Optional;
|
|
|
-
|
|
|
+import java.time.LocalDate;
|
|
|
+import java.util.Date;
|
|
|
+
|
|
|
+/**
|
|
|
+ * SensorData类,用于表示传感器数据,继承自JfcloudInFluxEntity。
|
|
|
+ * 使用反射将FluxRecord映射到此类的属性。
|
|
|
+ */
|
|
|
+@EqualsAndHashCode(callSuper = true)
|
|
|
@Data
|
|
|
@AllArgsConstructor
|
|
|
@NoArgsConstructor
|
|
|
@Measurement(name = JfcloudColdChainConstants.INFLUXDB_DEFAULT_MEASUREMENT_NAME)
|
|
|
@Slf4j
|
|
|
public class SensorData extends JfcloudInFluxEntity {
|
|
|
+
|
|
|
@Column(name = "temperature")
|
|
|
private Float temperature;
|
|
|
|
|
@@ -54,75 +62,159 @@ public class SensorData extends JfcloudInFluxEntity {
|
|
|
@Column(name = "model_name", tag = true)
|
|
|
private String modelName;
|
|
|
|
|
|
+ /**
|
|
|
+ * 使用反射和FluxRecord动态映射字段值到SensorData对象
|
|
|
+ *
|
|
|
+ * @param record FluxRecord数据记录
|
|
|
+ * @return 映射后的SensorData对象
|
|
|
+ */
|
|
|
public static SensorData mapToSensorData(FluxRecord record) {
|
|
|
SensorData sensorData = new SensorData();
|
|
|
try {
|
|
|
- sensorData.setDeviceId(getStringValue(record, "device_id"));
|
|
|
- sensorData.setRoads(getIntegerValue(record, "roads"));
|
|
|
- sensorData.setTemperature(getFloatValue(record, "temperature"));
|
|
|
- sensorData.setHumidity(getFloatValue(record, "humidity"));
|
|
|
- sensorData.setCo2(getFloatValue(record, "co2"));
|
|
|
- sensorData.setBattery(getFloatValue(record, "battery"));
|
|
|
- sensorData.setPlugInStatus(getStringValue(record, "plugInStatus"));
|
|
|
- sensorData.setLocation(getStringValue(record, "location"));
|
|
|
- sensorData.setLng(getDoubleValue(record, "longitude"));
|
|
|
- sensorData.setLat(getDoubleValue(record, "latitude"));
|
|
|
- sensorData.setTime(getInstantValue(record));
|
|
|
-
|
|
|
+ // 获取SensorData类的所有字段
|
|
|
+ Field[] fields = SensorData.class.getDeclaredFields();
|
|
|
+ for (Field field : fields) {
|
|
|
+ field.setAccessible(true); // 设置字段可访问
|
|
|
+ // 如果字段没有@Column注解,则跳过
|
|
|
+ if (field.getAnnotation(Column.class) == null) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ String fieldName = field.getName(); // 获取字段名
|
|
|
+ Object value = record.getValueByKey(fieldName); // 从FluxRecord中根据字段名获取值
|
|
|
+ if (value == null) {
|
|
|
+ continue; // 如果FluxRecord中没有该字段的值,则跳过
|
|
|
+ }
|
|
|
+
|
|
|
+ // 根据字段类型进行相应的值转换
|
|
|
+ if (field.getType().equals(String.class)) {
|
|
|
+ field.set(sensorData, value.toString());
|
|
|
+ } else if (field.getType().equals(Integer.class) || field.getType().equals(int.class)) {
|
|
|
+ field.set(sensorData, convertToInteger(value));
|
|
|
+ } else if (field.getType().equals(Float.class) || field.getType().equals(float.class)) {
|
|
|
+ field.set(sensorData, convertToFloat(value));
|
|
|
+ } else if (field.getType().equals(Double.class) || field.getType().equals(double.class)) {
|
|
|
+ field.set(sensorData, convertToDouble(value));
|
|
|
+ } else if (field.getType().equals(Instant.class)) {
|
|
|
+ field.set(sensorData, convertToInstant(value));
|
|
|
+ } else if (field.getType().equals(Date.class)) {
|
|
|
+ field.set(sensorData, convertToDate(value));
|
|
|
+ } else if (field.getType().equals(LocalDate.class)) {
|
|
|
+ field.set(sensorData, convertToLocalDate(value));
|
|
|
+ } else {
|
|
|
+ log.warn("无法处理字段类型: " + field.getName() + ",字段值: " + value);
|
|
|
+ }
|
|
|
+ }
|
|
|
} catch (Exception e) {
|
|
|
- log.error("Error mapping FluxRecord to SensorData", e);
|
|
|
+ log.error("从FluxRecord映射到SensorData时出错", e);
|
|
|
}
|
|
|
return sensorData;
|
|
|
}
|
|
|
|
|
|
- private static String getStringValue(FluxRecord record, String key) {
|
|
|
- return Optional.ofNullable(record.getValueByKey(key))
|
|
|
- .map(Object::toString)
|
|
|
- .orElse(null);
|
|
|
+ // 类型转换方法
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将值转换为Integer类型
|
|
|
+ *
|
|
|
+ * @param value 要转换的值
|
|
|
+ * @return 转换后的Integer值
|
|
|
+ */
|
|
|
+ private static Integer convertToInteger(Object value) {
|
|
|
+ try {
|
|
|
+ return (value instanceof Integer) ? (Integer) value : Integer.valueOf(value.toString());
|
|
|
+ } catch (NumberFormatException e) {
|
|
|
+ log.error("将值转换为Integer时出错: " + value, e);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- private static Integer getIntegerValue(FluxRecord record, String key) {
|
|
|
- return Optional.ofNullable(record.getValueByKey(key))
|
|
|
- .map(value -> {
|
|
|
- try {
|
|
|
- return (value instanceof Integer) ? (Integer) value :
|
|
|
- (value instanceof String) ? Integer.valueOf((String) value) : null;
|
|
|
- } catch (NumberFormatException e) {
|
|
|
- return null;
|
|
|
- }
|
|
|
- })
|
|
|
- .orElse(null);
|
|
|
+ /**
|
|
|
+ * 将值转换为Float类型
|
|
|
+ *
|
|
|
+ * @param value 要转换的值
|
|
|
+ * @return 转换后的Float值
|
|
|
+ */
|
|
|
+ private static Float convertToFloat(Object value) {
|
|
|
+ try {
|
|
|
+ if (value instanceof Double) {
|
|
|
+ return ((Double) value).floatValue(); // 将Double 转换为Float
|
|
|
+ }
|
|
|
+ return (value instanceof Float) ? (Float) value : Float.valueOf(value.toString());
|
|
|
+ } catch (NumberFormatException e) {
|
|
|
+ log.error("将值转换为Float时出错: " + value, e);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- private static Double getDoubleValue(FluxRecord record, String key) {
|
|
|
- return Optional.ofNullable(record.getValueByKey(key))
|
|
|
- .map(value -> {
|
|
|
- try {
|
|
|
- return (value instanceof Double) ? (Double) value :
|
|
|
- (value instanceof String) ? Double.valueOf((String) value) : null;
|
|
|
- } catch (NumberFormatException e) {
|
|
|
- return null;
|
|
|
- }
|
|
|
- })
|
|
|
- .orElse(null);
|
|
|
+ /**
|
|
|
+ * 将值转换为Double类型
|
|
|
+ *
|
|
|
+ * @param value 要转换的值
|
|
|
+ * @return 转换后的Double值
|
|
|
+ */
|
|
|
+ private static Double convertToDouble(Object value) {
|
|
|
+ try {
|
|
|
+ return (value instanceof Double) ? (Double) value : Double.valueOf(value.toString());
|
|
|
+ } catch (NumberFormatException e) {
|
|
|
+ log.error("将值转换为Double时出错: " + value, e);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- private static Float getFloatValue(FluxRecord record, String key) {
|
|
|
- return Optional.ofNullable(record.getValueByKey(key))
|
|
|
- .map(value -> {
|
|
|
- try {
|
|
|
- return (value instanceof Float) ? (Float) value :
|
|
|
- (value instanceof String) ? Float.valueOf((String) value) : null;
|
|
|
- } catch (NumberFormatException e) {
|
|
|
- return null;
|
|
|
- }
|
|
|
- })
|
|
|
- .orElse(null);
|
|
|
+ /**
|
|
|
+ * 将值转换为Instant类型
|
|
|
+ *
|
|
|
+ * @param value 要转换的值
|
|
|
+ * @return 转换后的Instant值
|
|
|
+ */
|
|
|
+ private static Instant convertToInstant(Object value) {
|
|
|
+ try {
|
|
|
+ return (value instanceof Instant) ? (Instant) value : Instant.parse(value.toString());
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("将值转换为Instant时出错: " + value, e);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将值转换为Date类型
|
|
|
+ *
|
|
|
+ * @param value 要转换的值
|
|
|
+ * @return 转换后的Date值
|
|
|
+ */
|
|
|
+ private static Date convertToDate(Object value) {
|
|
|
+ try {
|
|
|
+ if (value instanceof Instant) {
|
|
|
+ return Date.from((Instant) value);
|
|
|
+ } else if (value instanceof String) {
|
|
|
+ return Date.from(Instant.parse((String) value));
|
|
|
+ } else {
|
|
|
+ return (Date) value;
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("将值转换为Date时出错: " + value, e);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- private static Instant getInstantValue(FluxRecord record) {
|
|
|
- return Optional.ofNullable(record.getTime())
|
|
|
- .map(time -> time.atOffset(ZoneOffset.UTC).toInstant())
|
|
|
- .orElse(null);
|
|
|
+ /**
|
|
|
+ * 将值转换为LocalDate类型
|
|
|
+ *
|
|
|
+ * @param value 要转换的值
|
|
|
+ * @return 转换后的LocalDate值
|
|
|
+ */
|
|
|
+ private static LocalDate convertToLocalDate(Object value) {
|
|
|
+ try {
|
|
|
+ if (value instanceof Instant) {
|
|
|
+ return ((Instant) value).atZone(java.time.ZoneId.systemDefault()).toLocalDate();
|
|
|
+ } else if (value instanceof String) {
|
|
|
+ return LocalDate.parse((String) value);
|
|
|
+ } else {
|
|
|
+ return (LocalDate) value;
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("将值转换为LocalDate时出错: " + value, e);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
}
|
|
|
}
|