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

@@ -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();
}
}