获取设备详情时尝试同步设备状态

This commit is contained in:
zhou-hao 2020-03-25 11:11:17 +08:00
parent 2de3a9c69a
commit b76d430299
2 changed files with 22 additions and 5 deletions

View File

@ -135,12 +135,13 @@ public class DeviceDetail {
setId(device.getId());
setName(device.getName());
setState(device.getState());
setOrgId(device.getOrgId());
Optional.ofNullable(device.getRegistryTime())
.ifPresent(this::setRegisterTime);
setCreateTime(device.getCreateTime());
setOrgId(device.getOrgId());
Optional.ofNullable(device.getCreateTime())
.ifPresent(this::setCreateTime);
if (!CollectionUtils.isEmpty(device.getConfiguration())) {
setConfiguration(device.getConfiguration());

View File

@ -221,9 +221,25 @@ public class LocalDeviceInstanceService extends GenericReactiveCrudService<Devic
public Mono<DeviceDetail> getDeviceDetail(String deviceId) {
return this.findById(deviceId)
.zipWhen(device -> deviceProductService.findById(device.getProductId()),
(device, product) -> new DeviceDetail().with(device).with(product))
.flatMap(detail -> registry.getDevice(deviceId).flatMap(detail::with).defaultIfEmpty(detail))
.zipWhen(
//合并设备和型号信息
(device) -> deviceProductService.findById(device.getProductId()),
(device, product) -> new DeviceDetail().with(device).with(product)
).flatMap(detail -> registry
.getDevice(deviceId)
.flatMap(
operator -> operator.checkState() //检查设备的真实状态,设备已经离线,但是数据库状态未及时更新的.
.map(DeviceState::of)
.filter(state -> state != detail.getState())
.doOnNext(detail::setState)
.flatMap(state -> createUpdate()
.set(DeviceInstanceEntity::getState, state)
.where(DeviceInstanceEntity::getId, deviceId)
.execute())
.thenReturn(operator))
.flatMap(detail::with)
.defaultIfEmpty(detail))
//设备标签信息
.flatMap(detail -> tagRepository
.createQuery()
.where(DeviceTagEntity::getDeviceId, deviceId)