Przeglądaj źródła

优化设备实例API

zhouhao 5 lat temu
rodzic
commit
4c26b30032

+ 38 - 16
jetlinks-manager/device-manager/src/main/java/org/jetlinks/community/device/service/LocalDeviceInstanceService.java

@@ -7,11 +7,15 @@ import com.alibaba.fastjson.JSON;
 import lombok.extern.slf4j.Slf4j;
 import org.hswebframework.ezorm.core.dsl.Query;
 import org.hswebframework.ezorm.core.param.QueryParam;
+import org.hswebframework.ezorm.core.param.TermType;
+import org.hswebframework.web.api.crud.entity.PagerResult;
+import org.hswebframework.web.api.crud.entity.QueryParamEntity;
 import org.hswebframework.web.bean.FastBeanCopier;
 import org.hswebframework.web.crud.service.GenericReactiveCrudService;
 import org.hswebframework.web.exception.BusinessException;
 import org.hswebframework.web.exception.NotFoundException;
 import org.hswebframework.web.logger.ReactiveLogger;
+import org.jetlinks.community.device.entity.DeviceOperationLogEntity;
 import org.jetlinks.core.device.DeviceConfigKey;
 import org.jetlinks.core.device.DeviceOperator;
 import org.jetlinks.core.device.DeviceRegistry;
@@ -58,6 +62,8 @@ import java.util.List;
 import java.util.Map;
 import java.util.stream.Collectors;
 
+import static org.jetlinks.community.device.timeseries.DeviceTimeSeriesMetric.devicePropertyMetric;
+
 @Service
 @Slf4j
 public class LocalDeviceInstanceService extends GenericReactiveCrudService<DeviceInstanceEntity, String> {
@@ -78,6 +84,7 @@ public class LocalDeviceInstanceService extends GenericReactiveCrudService<Devic
     private TimeSeriesManager timeSeriesManager;
 
 
+
     /**
      * 获取设备所有信息
      *
@@ -90,7 +97,7 @@ public class LocalDeviceInstanceService extends GenericReactiveCrudService<Devic
             .zipWhen(instance -> deviceProductService.findById(instance.getProductId()), DeviceInfo::of) //产品型号信息
             .switchIfEmpty(Mono.error(NotFoundException::new))
             .zipWhen(deviceInfo -> getDeviceRunRealInfo(id), DeviceAllInfoResponse::of) //设备运行状态
-            .zipWhen(info -> getProperties(info.getDeviceInfo().getProductId(), id).collectList(), DeviceAllInfoResponse::ofProperties) //设备属性
+            .zipWhen(info -> getDeviceLatestProperties( id).collectList(), DeviceAllInfoResponse::ofProperties) //设备属性
             .zipWhen(info -> {
                     DeviceMetadata deviceMetadata = new JetLinksDeviceMetadata(JSON.parseObject(info.getDeviceInfo().getDeriveMetadata()));
                     return getEventCounts(deviceMetadata.getEvents(), id, info.getDeviceInfo().getProductId()); //事件数量统计
@@ -224,35 +231,50 @@ public class LocalDeviceInstanceService extends GenericReactiveCrudService<Devic
     }
 
 
-    public Mono<DevicePropertiesEntity> getProperty(String deviceId, String property) {
-        return createQuery()
-            .where(DeviceInstanceEntity::getId, deviceId)
-            .fetchOne()
-            .map(DeviceInstanceEntity::getProductId)
-            .flatMap(productId -> doGetDeviceProperty(productId, deviceId, property));
+    public Mono<PagerResult<DevicePropertiesEntity>> queryDeviceProperties(String deviceId, QueryParamEntity entity) {
+        return registry.getDevice(deviceId)
+            .flatMap(operator -> operator.getSelfConfig(DeviceConfigKey.productId))
+            .flatMap(productId -> timeSeriesManager
+                .getService(devicePropertyMetric(productId))
+                .queryPager(entity.and("deviceId", TermType.eq, deviceId), data -> data.as(DevicePropertiesEntity.class)))
+            .defaultIfEmpty(PagerResult.empty());
+    }
+
+    public Mono<DevicePropertiesEntity> getDeviceLatestProperty(String deviceId, String property) {
+        return registry
+            .getDevice(deviceId)
+            .flatMap(operator -> operator.getSelfConfig(DeviceConfigKey.productId))
+            .flatMap(productId -> doGetLatestDeviceProperty(productId, deviceId, property));
     }
 
-    public Flux<DevicePropertiesEntity> getProperties(String productId, String deviceId) {
+    public Flux<DevicePropertiesEntity> getDeviceLatestProperties(String deviceId) {
         return registry.getDevice(deviceId)
-            .flatMap(DeviceOperator::getMetadata)
-            .flatMapMany(metadata -> Flux.merge(metadata.getProperties()
+            .flatMap(operator -> Mono.zip(operator.getMetadata(), operator.getSelfConfig(DeviceConfigKey.productId)))
+            .flatMapMany(zip -> Flux.merge(zip.getT1().getProperties()
                 .stream()
-                .map(property -> doGetDeviceProperty(productId, deviceId, property.getId()))
-                .collect(Collectors.toList())))
-            ;
+                .map(property -> doGetLatestDeviceProperty(zip.getT2(), deviceId, property.getId()))
+                .collect(Collectors.toList())));
     }
 
-    private Mono<DevicePropertiesEntity> doGetDeviceProperty(String productId, String deviceId, String property) {
+    private Mono<DevicePropertiesEntity> doGetLatestDeviceProperty(String productId, String deviceId, String property) {
         return Query.of()
             .and(DevicePropertiesEntity::getDeviceId, deviceId)
             .and(DevicePropertiesEntity::getProperty, property)
             .doPaging(0, 1)
-            .execute(timeSeriesManager
-                .getService(DeviceTimeSeriesMetric.devicePropertyMetric(productId))::query)
+            .execute(timeSeriesManager.getService(devicePropertyMetric(productId))::query)
             .map(data -> data.as(DevicePropertiesEntity.class))
             .singleOrEmpty();
     }
 
+    public Mono<PagerResult<DeviceOperationLogEntity>> queryDeviceLog(String deviceId, QueryParamEntity entity) {
+        return registry.getDevice(deviceId)
+            .flatMap(operator -> operator.getSelfConfig(DeviceConfigKey.productId))
+            .flatMap(productId -> timeSeriesManager
+                .getService(DeviceTimeSeriesMetric.deviceLogMetric(productId))
+                .queryPager(entity.and("deviceId", TermType.eq, deviceId),
+                    data -> data.as(DeviceOperationLogEntity.class)))
+            .defaultIfEmpty(PagerResult.empty());
+    }
 
     @PostConstruct
     public void init() {

+ 21 - 14
jetlinks-manager/device-manager/src/main/java/org/jetlinks/community/device/web/DeviceInstanceController.java

@@ -135,31 +135,38 @@ public class DeviceInstanceController implements
             .as(flux -> service.syncStateBatch(flux, true));
     }
 
+    //已废弃
     @GetMapping("/{productId:.+}/{deviceId:.+}/properties")
+    @Deprecated
+    @QueryAction
+    public Flux<DevicePropertiesEntity> getDeviceLatestProperties(@PathVariable String productId,
+                                                                  @PathVariable String deviceId) {
+        return service.getDeviceLatestProperties(deviceId);
+    }
+
+    @GetMapping("/{deviceId:.+}/properties/latest")
     @QueryAction
-    public Flux<DevicePropertiesEntity> getDeviceProperties(@PathVariable String productId, @PathVariable String deviceId) {
-        return service.getProperties(productId, deviceId);
+    public Flux<DevicePropertiesEntity> getDeviceLatestProperties(@PathVariable String deviceId) {
+        return service.getDeviceLatestProperties(deviceId);
     }
 
     @GetMapping("/{deviceId:.+}/property/{property:.+}")
     @QueryAction
-    public Mono<DevicePropertiesEntity> getDeviceProperty(@PathVariable String deviceId, @PathVariable String property) {
-        return service.getProperty(deviceId, property);
+    public Mono<DevicePropertiesEntity> getDeviceLatestProperty(@PathVariable String deviceId, @PathVariable String property) {
+        return service.getDeviceLatestProperty(deviceId, property);
     }
 
-    @GetMapping({
-        "/{deviceId:.+}/logs",
-    })
+    @GetMapping("/{deviceId:.+}/properties/_query")
+    @QueryAction
+    public Mono<PagerResult<DevicePropertiesEntity>> queryDeviceProperties(@PathVariable String deviceId, QueryParamEntity entity) {
+        return service.queryDeviceProperties(deviceId, entity);
+    }
+
+    @GetMapping("/{deviceId:.+}/logs")
     @QueryAction
     public Mono<PagerResult<DeviceOperationLogEntity>> queryDeviceLog(@PathVariable String deviceId,
                                                                       QueryParamEntity entity) {
-        return registry.getDevice(deviceId)
-            .flatMap(operator -> operator.getSelfConfig(DeviceConfigKey.productId))
-            .flatMap(productId -> timeSeriesManager
-                .getService(DeviceTimeSeriesMetric.deviceLogMetric(productId))
-                .queryPager(entity.and("deviceId", TermType.eq, deviceId),
-                    data -> data.as(DeviceOperationLogEntity.class)))
-            .defaultIfEmpty(PagerResult.empty());
+        return service.queryDeviceLog(deviceId,entity);
     }
 
     //已废弃