refactor(ops): optimize badge device status management and add sync job
This commit is contained in:
@@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user