瀏覽代碼

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

zhou-hao 5 年之前
父節點
當前提交
b76d430299

+ 3 - 2
jetlinks-manager/device-manager/src/main/java/org/jetlinks/community/device/response/DeviceDetail.java

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

+ 19 - 3
jetlinks-manager/device-manager/src/main/java/org/jetlinks/community/device/service/LocalDeviceInstanceService.java

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