Pārlūkot izejas kodu

Merge remote-tracking branch 'origin/master'

zhouhao 5 gadi atpakaļ
vecāks
revīzija
956f67c7e8

+ 2 - 0
jetlinks-manager/device-manager/src/main/java/org/jetlinks/community/device/entity/DeviceInstanceEntity.java

@@ -20,6 +20,7 @@ import javax.persistence.GeneratedValue;
 import javax.persistence.Index;
 import javax.persistence.Table;
 import javax.validation.constraints.NotBlank;
+import javax.validation.constraints.Pattern;
 import java.sql.JDBCType;
 import java.util.Collections;
 import java.util.Map;
@@ -33,6 +34,7 @@ public class DeviceInstanceEntity extends GenericEntity<String> implements Recor
 
     @Override
     @GeneratedValue(generator = Generators.SNOW_FLAKE)
+    @Pattern(regexp = "^[0-9a-zA-Z_\\-]+$", message = "ID只能由英文下划线和中划线组成",groups = CreateGroup.class)
     public String getId() {
         return super.getId();
     }

+ 5 - 1
jetlinks-manager/device-manager/src/main/java/org/jetlinks/community/device/entity/DeviceProductEntity.java

@@ -15,6 +15,7 @@ import javax.persistence.Column;
 import javax.persistence.GeneratedValue;
 import javax.persistence.Table;
 import javax.validation.constraints.NotBlank;
+import javax.validation.constraints.Pattern;
 import java.sql.JDBCType;
 import java.util.Map;
 
@@ -25,6 +26,10 @@ public class DeviceProductEntity extends GenericEntity<String> implements Record
 
     @Override
     @GeneratedValue(generator = Generators.SNOW_FLAKE)
+    @Pattern(
+        regexp = "^[0-9a-zA-Z_\\-]+$",
+        message = "ID只能由英文下划线和中划线组成",
+        groups = CreateGroup.class)
     public String getId() {
         return super.getId();
     }
@@ -61,7 +66,6 @@ public class DeviceProductEntity extends GenericEntity<String> implements Record
     @Comment("协议元数据")
     @Column(name = "metadata")
     @ColumnType(jdbcType = JDBCType.CLOB)
-    @NotBlank(message = "元数据不能为空",groups = CreateGroup.class)
     private String metadata;
 
     @Comment("传输协议: MQTT,COAP,UDP")

+ 15 - 77
jetlinks-manager/device-manager/src/main/java/org/jetlinks/community/device/service/LocalDeviceInstanceService.java

@@ -101,50 +101,6 @@ public class LocalDeviceInstanceService extends GenericReactiveCrudService<Devic
             .as(super::save);
     }
 
-    /**
-     * 获取设备所有信息
-     *
-     * @param id 设备ID
-     * @return 设备详情信息
-     */
-    @Deprecated
-    public Mono<DeviceAllInfoResponse> getDeviceAllInfo(String id) {
-
-        return findById(id)//设备信息
-            .zipWhen(instance -> deviceProductService.findById(instance.getProductId()), DeviceInfo::of) //产品型号信息
-            .switchIfEmpty(Mono.error(NotFoundException::new))
-            .zipWhen(deviceInfo -> getDeviceRunRealInfo(id), DeviceAllInfoResponse::of) //设备运行状态
-            .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()); //事件数量统计
-                },
-                DeviceAllInfoResponse::ofEventCounts)
-            ;
-    }
-
-    /**
-     * 获取设备事件上报次数
-     *
-     * @param events    设备事件元数据
-     * @param deviceId  设备Id
-     * @param productId 型号id
-     * @return
-     */
-    private Mono<Map<String, Integer>> getEventCounts(List<EventMetadata> events, String deviceId, String productId) {
-        return Flux.merge(
-            events
-                .stream()
-                .map(Metadata::getId)
-                .map(eventId -> Query.of()
-                    .where("deviceId", deviceId)
-                    .execute(timeSeriesManager.getService(DeviceTimeSeriesMetric.deviceEventMetric(productId, eventId))::count)
-                    .map(count -> Tuples.of(eventId, count)))
-                .collect(Collectors.toList())
-        ).collect(Collectors.toMap(Tuple2::getT1, Tuple2::getT2));
-    }
-
-
     /**
      * 发布设备到设备注册中心
      *
@@ -214,6 +170,21 @@ public class LocalDeviceInstanceService extends GenericReactiveCrudService<Devic
                     .execute()));
     }
 
+    /**
+     * 批量注销设备
+     * @param ids 设备ID
+     * @return 注销结果
+     */
+    public Mono<Integer> unregisterDevice(Publisher<String> ids) {
+        return Flux.from(ids)
+            .flatMap(id -> registry.unregisterDevice(id).thenReturn(id))
+            .collectList()
+            .flatMap(list -> createUpdate()
+                .set(DeviceInstanceEntity::getState, DeviceState.notActive.getValue())
+                .where().in(DeviceInstanceEntity::getId, list)
+                .execute());
+    }
+
     public Mono<DeviceDetail> getDeviceDetail(String deviceId) {
         return this.findById(deviceId)
             .zipWhen(
@@ -257,39 +228,6 @@ public class LocalDeviceInstanceService extends GenericReactiveCrudService<Devic
             .defaultIfEmpty(DeviceState.notActive);
     }
 
-    @Deprecated
-    public Mono<DeviceRunInfo> getDeviceRunInfo(String deviceId) {
-        return getDeviceRunRealInfo(deviceId);
-    }
-
-    @Deprecated
-    private Mono<DeviceRunInfo> getDeviceRunRealInfo(String deviceId) {
-        return registry.getDevice(deviceId)
-            .flatMap(deviceOperator -> Mono.zip(
-                deviceOperator.getOnlineTime().switchIfEmpty(Mono.just(0L)),// 1
-                deviceOperator.getOfflineTime().switchIfEmpty(Mono.just(0L)),// 2
-                deviceOperator.checkState()
-                    .switchIfEmpty(deviceOperator.getState())
-                    .map(DeviceState::of)
-                    .defaultIfEmpty(DeviceState.notActive),// 3
-                deviceOperator.getConfig(DeviceConfigKey.metadata).switchIfEmpty(Mono.just("")),//4
-                deviceOperator.getConfig(DeviceConfigKey.productId).switchIfEmpty(Mono.just(""))//5
-                ).map(tuple4 -> DeviceRunInfo.of(
-                tuple4.getT1(), //1. 上线时间
-                tuple4.getT2(), //2. 离线时间
-                tuple4.getT3(), //3. 状态
-                tuple4.getT4(),  //4. 设备模型元数据
-                tuple4.getT5() //5. 设备类型ID
-                )
-                ).flatMap(deviceRunInfo -> createUpdate()
-                    .set(DeviceInstanceEntity::getState, deviceRunInfo.getState())
-                    .where(DeviceInstanceEntity::getId, deviceId)
-                    .execute()
-                    .thenReturn(deviceRunInfo))
-            );
-    }
-
-
     public Mono<PagerResult<DevicePropertiesEntity>> queryDeviceProperties(String deviceId, QueryParamEntity entity) {
         return registry.getDevice(deviceId)
             .flatMap(operator -> operator.getSelfConfig(DeviceConfigKey.productId))

+ 1 - 1
jetlinks-manager/device-manager/src/main/java/org/jetlinks/community/device/service/LocalDeviceProductService.java

@@ -29,7 +29,7 @@ public class LocalDeviceProductService extends GenericReactiveCrudService<Device
 
     public Mono<Integer> deploy(String id) {
         return findById(Mono.just(id))
-            .flatMap(product -> registry.registry(
+            .flatMap(product -> registry.register(
                 ProductInfo.builder()
                     .id(id)
                     .protocol(product.getMessageProtocol())

+ 37 - 42
jetlinks-manager/device-manager/src/main/java/org/jetlinks/community/device/web/DeviceInstanceController.java

@@ -17,10 +17,7 @@ import org.hswebframework.web.api.crud.entity.PagerResult;
 import org.hswebframework.web.api.crud.entity.QueryParamEntity;
 import org.hswebframework.web.authorization.Authentication;
 import org.hswebframework.web.authorization.Dimension;
-import org.hswebframework.web.authorization.annotation.Authorize;
-import org.hswebframework.web.authorization.annotation.QueryAction;
-import org.hswebframework.web.authorization.annotation.Resource;
-import org.hswebframework.web.authorization.annotation.SaveAction;
+import org.hswebframework.web.authorization.annotation.*;
 import org.hswebframework.web.bean.FastBeanCopier;
 import org.hswebframework.web.crud.web.reactive.ReactiveServiceCrudController;
 import org.hswebframework.web.exception.BusinessException;
@@ -72,8 +69,6 @@ public class DeviceInstanceController implements
     @Getter
     private final LocalDeviceInstanceService service;
 
-    private final TimeSeriesManager timeSeriesManager;
-
     private final DeviceRegistry registry;
 
     private final LocalDeviceProductService productService;
@@ -84,13 +79,11 @@ public class DeviceInstanceController implements
 
     @SuppressWarnings("all")
     public DeviceInstanceController(LocalDeviceInstanceService service,
-                                    TimeSeriesManager timeSeriesManager,
                                     DeviceRegistry registry,
                                     LocalDeviceProductService productService,
                                     ImportExportService importExportService,
                                     ReactiveRepository<DeviceTagEntity, String> tagRepository) {
         this.service = service;
-        this.timeSeriesManager = timeSeriesManager;
         this.registry = registry;
         this.productService = productService;
         this.importExportService = importExportService;
@@ -112,36 +105,15 @@ public class DeviceInstanceController implements
         return service.getDeviceState(id);
     }
 
-    //已弃用 下一个版本删除
-    @GetMapping("/info/{id:.+}")
-    @QueryAction
-    @Deprecated
-    public Mono<DeviceInfo> getDeviceInfoById(@PathVariable String id) {
-        return service.getDeviceInfoById(id);
-    }
-
-    //已弃用 下一个版本删除
-    @GetMapping("/run-info/{id:.+}")
-    @QueryAction
-    @Deprecated
-    public Mono<DeviceRunInfo> getRunDeviceInfoById(@PathVariable String id) {
-        return service.getDeviceRunInfo(id);
-    }
-
-
-    @PostMapping({
-        "/deploy/{deviceId:.+}",//todo 已弃用 下一个版本删除
-        "/{deviceId:.+}/deploy"
-    })
+    //激活
+    @PostMapping("/{deviceId:.+}/deploy")
     @SaveAction
     public Mono<DeviceDeployResult> deviceDeploy(@PathVariable String deviceId) {
         return service.deploy(deviceId);
     }
 
-    @PostMapping({
-        "/cancelDeploy/{deviceId:.+}", //todo 已弃用 下一个版本删除
-        "/{deviceId:.+}/undeploy"
-    })
+    //注销
+    @PostMapping( "/{deviceId:.+}/undeploy")
     @SaveAction
     public Mono<Integer> cancelDeploy(@PathVariable String deviceId) {
         return service.cancelDeploy(deviceId);
@@ -192,15 +164,6 @@ public class DeviceInstanceController implements
             .defaultIfEmpty(0);
     }
 
-    //已废弃
-    @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
@@ -291,6 +254,38 @@ public class DeviceInstanceController implements
             .thenMany(getDeviceTags(deviceId));
     }
 
+    /**
+     * 批量删除设备,只会删除未激活的设备.
+     *
+     * @param idList ID列表
+     * @return 被删除数量
+     * @since 1.1
+     */
+    @PutMapping("/batch/_delete")
+    @DeleteAction
+    public Mono<Integer> deleteBatch(@RequestBody Flux<String> idList) {
+        return idList
+            .collectList()
+            .flatMap(list -> service.createDelete()
+                .where()
+                .in(DeviceInstanceEntity::getId, list)
+                .and(DeviceInstanceEntity::getState, DeviceState.notActive)
+                .execute());
+    }
+
+    /**
+     * 批量注销设备
+     *
+     * @param idList ID列表
+     * @return 被注销的数量
+     * @since 1.1
+     */
+    @PutMapping("/batch/_unDeploy")
+    @SaveAction
+    public Mono<Integer> unDeployBatch(@RequestBody Flux<String> idList) {
+        return service.unregisterDevice(idList);
+    }
+
 
     @GetMapping(value = "/import", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
     @ApiOperation("批量导入数据")

BIN
simulator/demo-protocol-1.0.jar