fix: 设备最后上数时间,新增字段,接口调用,小程序显示。

This commit is contained in:
tianyongbao
2026-06-04 13:53:30 +08:00
parent 2596fe6551
commit f4bd1c6942
13 changed files with 259 additions and 0 deletions

View File

@@ -1,9 +1,11 @@
package com.intc.iot.controller;
import cn.dev33.satoken.annotation.SaCheckPermission;
import com.intc.common.core.domain.R;
import com.intc.common.mybatis.core.page.PageQuery;
import com.intc.common.mybatis.core.page.TableDataInfo;
import com.intc.common.satoken.utils.LoginHelper;
import com.intc.common.tenant.helper.TenantHelper;
import com.intc.common.web.core.BaseController;
import com.intc.common.core.config.properties.DeviceTypeProperties;
import com.intc.fishery.domain.Device;
@@ -46,6 +48,7 @@ import com.intc.iot.utils.ControllerHelper;
import com.intc.iot.service.IotCloudService;
import com.intc.iot.constant.IOTPropertyName;
import com.intc.fishery.utils.MessageOpRecordUtil;
import com.intc.tdengine.service.IDeviceSensorDataService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
@@ -57,6 +60,9 @@ import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.Date;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;
@@ -72,6 +78,8 @@ import java.util.Map;
@Tag(name = "生活物联网平台管理", description = "阿里云飞燕平台对接接口")
public class IotController extends BaseController {
private static final DateTimeFormatter DATETIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
private final DeviceTypeProperties deviceTypeProperties;
@Autowired(required = false)
@@ -101,6 +109,9 @@ public class IotController extends BaseController {
@Autowired(required = false)
private DeviceDataHandler deviceDataHandler;
@Autowired(required = false)
private IDeviceSensorDataService deviceSensorDataService;
@Autowired(required = false)
private javax.jms.Connection amqpConnection;
@@ -191,6 +202,82 @@ public class IotController extends BaseController {
}
}
@Operation(summary = "回填设备最后上数时间")
@SaCheckPermission("fishery:device:edit")
@PostMapping("/device/backfill-last-report-time")
public R<Map<String, Object>> backfillLastReportTime(
@RequestParam(defaultValue = "false") boolean ignoreTenant) {
if (deviceMapper == null || deviceSensorDataService == null) {
return R.fail("设备或TDengine服务未启用");
}
Map<String, Object> result = ignoreTenant
? TenantHelper.ignore((java.util.function.Supplier<Map<String, Object>>) this::doBackfillLastReportTime)
: doBackfillLastReportTime();
result.put("ignoreTenant", ignoreTenant);
return R.ok(result);
}
private Map<String, Object> doBackfillLastReportTime() {
int total = 0;
int updated = 0;
int skipped = 0;
int failed = 0;
List<Device> devices = deviceMapper.selectList(
new com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper<Device>()
.isNotNull(Device::getSerialNum)
.ne(Device::getSerialNum, "")
.select(Device::getId, Device::getSerialNum)
);
total = devices.size();
for (Device device : devices) {
try {
String latestTime = deviceSensorDataService.getLatestReportTime(device.getSerialNum());
if (latestTime == null || latestTime.isBlank()) {
skipped++;
continue;
}
Device updateDevice = new Device();
updateDevice.setId(device.getId());
updateDevice.setLastReportTime(parseReportTime(latestTime));
if (deviceMapper.updateById(updateDevice) > 0) {
updated++;
} else {
skipped++;
}
} catch (Exception e) {
failed++;
log.warn("回填设备最后上数时间失败: deviceId={}, serialNum={}, error={}",
device.getId(), device.getSerialNum(), e.getMessage());
}
}
Map<String, Object> result = new java.util.HashMap<>();
result.put("total", total);
result.put("updated", updated);
result.put("skipped", skipped);
result.put("failed", failed);
return result;
}
private Date parseReportTime(String time) {
String normalized = normalizeReportTime(time);
return Date.from(LocalDateTime.parse(normalized, DATETIME_FORMATTER)
.atZone(ZoneId.systemDefault())
.toInstant());
}
private String normalizeReportTime(String time) {
String normalized = time.trim();
if (normalized.length() > 19) {
normalized = normalized.substring(0, 19);
}
return normalized.replace('T', ' ');
}
@Operation(summary = "根据 ProductKey 和 DeviceName 查询设备信息")
@GetMapping("/device/find")
public R<Map<String, Object>> findDeviceByProductKeyAndName(

View File

@@ -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>