fix: 设备最后上数时间,新增字段,接口调用,小程序显示。
This commit is contained in:
@@ -40,7 +40,9 @@ import org.springframework.stereotype.Component;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -2595,6 +2597,7 @@ public class DeviceDataHandler {
|
||||
if (sensorData.getBattery() != null) {
|
||||
updateDevice.setValueBattery(sensorData.getBattery().intValue());
|
||||
}
|
||||
updateDevice.setLastReportTime(parseSensorDataTime(sensorData));
|
||||
int updated = deviceMapper.updateById(updateDevice);
|
||||
if (updated > 0) {
|
||||
log.debug("设备实时数据已更新: {}", deviceName);
|
||||
@@ -2604,6 +2607,29 @@ public class DeviceDataHandler {
|
||||
}
|
||||
}
|
||||
|
||||
private Date parseSensorDataTime(DeviceSensorData sensorData) {
|
||||
String time = StrUtil.blankToDefault(sensorData.getCreateTime(), sensorData.getTime());
|
||||
if (StrUtil.isBlank(time)) {
|
||||
return new Date();
|
||||
}
|
||||
try {
|
||||
return Date.from(LocalDateTime.parse(normalizeSensorDataTime(time), DATETIME_FORMATTER)
|
||||
.atZone(ZoneId.systemDefault())
|
||||
.toInstant());
|
||||
} catch (Exception e) {
|
||||
log.warn("解析传感器上数时间失败,使用当前服务器时间: {}", time);
|
||||
return new Date();
|
||||
}
|
||||
}
|
||||
|
||||
private String normalizeSensorDataTime(String time) {
|
||||
String normalized = time.trim();
|
||||
if (normalized.length() > 19) {
|
||||
normalized = normalized.substring(0, 19);
|
||||
}
|
||||
return normalized.replace('T', ' ');
|
||||
}
|
||||
|
||||
/**
|
||||
* 溶解氧联动控制(参考 C# SetDeviceSwitchLinkedOpen 逻辑)
|
||||
* <ul>
|
||||
|
||||
Reference in New Issue
Block a user