Forráskód Böngészése

feat(产品): 根据指定的接入方式获取产品需要的配置定义 (#266)

bestfeng1020 2 éve
szülő
commit
5fc41d5bf1

+ 3 - 0
jetlinks-components/gateway-component/src/main/java/org/jetlinks/community/gateway/supports/DeviceGatewayProperties.java

@@ -31,6 +31,9 @@ public class DeviceGatewayProperties implements ValueObject {
 
     private String protocol;
 
+    private String transport;
+
+
     private Map<String,Object> configuration=new HashMap<>();
 
     @Override

+ 18 - 0
jetlinks-manager/device-manager/src/main/java/org/jetlinks/community/device/service/DefaultDeviceConfigMetadataManager.java

@@ -1,5 +1,6 @@
 package org.jetlinks.community.device.service;
 
+import lombok.extern.slf4j.Slf4j;
 import org.jetlinks.community.device.spi.DeviceConfigMetadataSupplier;
 import org.jetlinks.core.metadata.*;
 import org.springframework.beans.factory.config.BeanPostProcessor;
@@ -13,6 +14,7 @@ import java.util.List;
 import java.util.concurrent.CopyOnWriteArrayList;
 
 @Component
+@Slf4j
 public class DefaultDeviceConfigMetadataManager implements DeviceConfigMetadataManager, BeanPostProcessor {
 
     private final List<DeviceConfigMetadataSupplier> suppliers = new CopyOnWriteArrayList<>();
@@ -62,6 +64,22 @@ public class DefaultDeviceConfigMetadataManager implements DeviceConfigMetadataM
                    .filter(meta -> org.apache.commons.collections4.CollectionUtils.isNotEmpty(meta.getProperties()));
     }
 
+
+    @Override
+    public Flux<ConfigMetadata> getProductConfigMetadataByAccessId(String productId,
+                                                                   String accessId) {
+        return Flux.fromIterable(suppliers)
+                   .flatMap(supplier -> supplier
+                       .getProductConfigMetadataByAccessId(productId, accessId)
+                       .onErrorResume(e -> {
+                           log.error("get product config metatada by gateway error", e);
+                           return Flux.empty();
+                       }))
+                   .map(config -> config.copy(DeviceConfigScope.product))
+                   .filter(config -> !CollectionUtils.isEmpty(config.getProperties()))
+                   .sort(Comparator.comparing(ConfigMetadata::getName));
+    }
+
     @Override
     public Object postProcessAfterInitialization(@Nonnull Object bean, @Nonnull String beanName) {
         if (bean instanceof DeviceConfigMetadataSupplier) {

+ 14 - 0
jetlinks-manager/device-manager/src/main/java/org/jetlinks/community/device/service/DefaultDeviceConfigMetadataSupplier.java

@@ -5,6 +5,7 @@ import org.hswebframework.web.exception.BusinessException;
 import org.jetlinks.community.device.entity.DeviceInstanceEntity;
 import org.jetlinks.community.device.entity.DeviceProductEntity;
 import org.jetlinks.community.device.spi.DeviceConfigMetadataSupplier;
+import org.jetlinks.community.gateway.supports.DeviceGatewayPropertiesManager;
 import org.jetlinks.core.ProtocolSupport;
 import org.jetlinks.core.ProtocolSupports;
 import org.jetlinks.core.message.codec.Transport;
@@ -32,6 +33,8 @@ public class DefaultDeviceConfigMetadataSupplier implements DeviceConfigMetadata
 
     private final ProtocolSupports protocolSupports;
 
+    private final DeviceGatewayPropertiesManager gatewayPropertiesManager;
+
     @Override
     @SuppressWarnings("all")
     public Flux<ConfigMetadata> getDeviceConfigMetadata(String deviceId) {
@@ -79,6 +82,17 @@ public class DefaultDeviceConfigMetadataSupplier implements DeviceConfigMetadata
             .flatMapMany(Function.identity());
     }
 
+    @Override
+    public Flux<ConfigMetadata> getProductConfigMetadataByAccessId(String productId,
+                                                                   String accessId) {
+        return gatewayPropertiesManager
+            .getProperties(accessId)
+            .flatMapMany(properties -> protocolSupports
+                .getProtocol(properties.getProtocol())
+                .onErrorMap(e -> new BusinessException("error.unable_to_load_protocol_by_access_id", 404, properties.getProtocol()))
+                .flatMap(support -> support.getConfigMetadata(Transport.of(properties.getTransport()))));
+    }
+
     private Flux<ConfigMetadata> getProductConfigMetadata0(String productId) {
         return productService
             .findById(productId)

+ 19 - 0
jetlinks-manager/device-manager/src/main/java/org/jetlinks/community/device/service/DeviceConfigMetadataManager.java

@@ -75,6 +75,25 @@ public interface DeviceConfigMetadataManager {
                                                   String typeId,
                                                   ConfigScope... scopes);
 
+
+    /**
+     * 根据产品ID和网关ID获取配置信息
+     * <p>
+     * 使用指定的接入方式查询,忽略产品当前绑定的接入方式
+     * <p>
+     * 当配置来自产品绑定关系时,可根据productId查询
+     * <p>
+     * 当配置来自接入方式时,可根据accessId查询
+     * <p>
+     * 当配置来自协议包时,可根据accessId关联的协议查询
+     *
+     * @param productId 产品ID
+     * @param accessId 网关ID
+     * @return 配置信息
+     */
+    Flux<ConfigMetadata> getProductConfigMetadataByAccessId(String productId,
+                                                            String accessId);
+
     Flux<Feature> getProductFeatures(String productId);
 
 }

+ 8 - 0
jetlinks-manager/device-manager/src/main/java/org/jetlinks/community/device/spi/DeviceConfigMetadataSupplier.java

@@ -41,6 +41,14 @@ public interface DeviceConfigMetadataSupplier {
         return Flux.empty();
     }
 
+    /**
+     * @see org.jetlinks.community.device.service.DeviceConfigMetadataManager#getProductConfigMetadataByAccessId(String, String)
+     */
+    @Generated
+    default Flux<ConfigMetadata> getProductConfigMetadataByAccessId(String productId, String accessId) {
+        return Flux.empty();
+    }
+
 
     /**
      * @see org.jetlinks.community.device.service.DeviceConfigMetadataManager#getProductFeatures(String)

+ 10 - 0
jetlinks-manager/device-manager/src/main/java/org/jetlinks/community/device/web/DeviceProductController.java

@@ -97,6 +97,16 @@ public class DeviceProductController implements ReactiveServiceCrudController<De
         return configMetadataManager.getProductConfigMetadata(id);
     }
 
+    @GetMapping("/{id:.+}/{accessId:.+}/config-metadata")
+    @QueryAction
+    @Operation(summary = "根据指定的接入方式获取产品需要的配置定义信息")
+    public Flux<ConfigMetadata> getProductConfigMetadataByAccessId(@PathVariable @Parameter(description = "产品ID") String id,
+                                                                   @PathVariable
+                                                                   @Parameter(description = "接入方式ID") String accessId) {
+        return configMetadataManager.getProductConfigMetadataByAccessId(id, accessId);
+    }
+
+
     @GetMapping("/{id:.+}/config-metadata/{metadataType}/{metadataId}/{typeId}")
     @QueryAction
     @Operation(summary = "获取产品物模型的拓展配置定义")