refactor(ops): optimize badge device status management and add sync job
Some checks failed
Java CI with Maven / build (11) (push) Has been cancelled
Java CI with Maven / build (17) (push) Has been cancelled
Java CI with Maven / build (8) (push) Has been cancelled

This commit is contained in:
lzh
2026-01-29 11:35:11 +08:00
parent afa5837160
commit 5142b38d12
9 changed files with 690 additions and 28 deletions

View File

@@ -10,8 +10,10 @@ import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.List;
/**
* IoT 设备状态<EFBFBD><EFBFBD>询 API
* IoT 设备状态询 API
* <p>
* 提供 RPC 接口供其他模块(如 Ops 模块)查询设备状态
*
@@ -33,4 +35,9 @@ public interface IotDeviceStatusQueryApi {
@Parameter(name = "deviceId", description = "设备ID", required = true, example = "1")
CommonResult<DeviceStatusRespDTO> getDeviceStatus(@RequestParam("deviceId") Long deviceId);
@GetMapping(PREFIX + "/batch-get-status")
@Operation(summary = "批量获取设备状态")
@Parameter(name = "deviceIds", description = "设备ID列表", required = true, example = "[1,2,3]")
CommonResult<List<DeviceStatusRespDTO>> batchGetDeviceStatus(@RequestParam("deviceIds") List<Long> deviceIds);
}

View File

@@ -14,6 +14,8 @@ import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import static com.viewsh.framework.common.pojo.CommonResult.success;
/**
@@ -54,13 +56,46 @@ public class IotDeviceStatusQueryApiImpl implements IotDeviceStatusQueryApi {
.status(IotDeviceStateEnum.OFFLINE.getState())
.build());
}
return success(DeviceStatusRespDTO.builder()
return success(buildDeviceStatusRespDTO(device));
}
@Override
@GetMapping(PREFIX + "/batch-get-status")
@Operation(summary = "批量获取设备状态")
public CommonResult<List<DeviceStatusRespDTO>> batchGetDeviceStatus(
@RequestParam("deviceIds") List<Long> deviceIds) {
if (deviceIds == null || deviceIds.isEmpty()) {
return success(List.of());
}
List<DeviceStatusRespDTO> result = deviceIds.stream()
.map(deviceId -> {
IotDeviceDO device = deviceService.getDeviceFromCache(deviceId);
if (device == null) {
return DeviceStatusRespDTO.builder()
.deviceId(deviceId)
.status(IotDeviceStateEnum.OFFLINE.getState())
.build();
}
return buildDeviceStatusRespDTO(device);
})
.toList();
return success(result);
}
/**
* 构建设备状态响应 DTO
*/
private DeviceStatusRespDTO buildDeviceStatusRespDTO(IotDeviceDO device) {
return DeviceStatusRespDTO.builder()
.deviceId(device.getId())
.deviceCode(device.getSerialNumber())
.status(device.getState())
.statusChangeTime(IotDeviceStateEnum.ONLINE.getState().equals(device.getState()) ?
device.getOnlineTime() : device.getOfflineTime())
.build());
.deviceCode(device.getSerialNumber())
.status(device.getState())
.statusChangeTime(
IotDeviceStateEnum.ONLINE.getState().equals(device.getState()) ? device.getOnlineTime()
: device.getOfflineTime())
.build();
}
}