优化子设备状态管理

This commit is contained in:
zhou-hao 2021-04-25 18:48:34 +08:00
parent 127028d32a
commit 342c59cdb0
4 changed files with 82 additions and 0 deletions

View File

@ -7,7 +7,9 @@ import org.hswebframework.ezorm.rdb.mapping.annotation.*;
import org.hswebframework.web.api.crud.entity.GenericEntity;
import org.hswebframework.web.api.crud.entity.RecordCreationEntity;
import org.hswebframework.web.crud.generator.Generators;
import org.hswebframework.web.dict.EnumDict;
import org.hswebframework.web.validator.CreateGroup;
import org.jetlinks.community.device.enums.DeviceFeature;
import org.jetlinks.core.device.DeviceConfigKey;
import org.jetlinks.core.device.DeviceInfo;
import org.jetlinks.community.device.enums.DeviceState;
@ -22,6 +24,7 @@ import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Pattern;
import java.sql.JDBCType;
import java.util.Map;
import java.util.stream.Stream;
@Getter
@Setter
@ -124,6 +127,14 @@ public class DeviceInstanceEntity extends GenericEntity<String> implements Recor
@Schema(description = "父级设备ID")
private String parentId;
//拓展特性,比如是否为子设备独立状态管理
@Column
@ColumnType(javaType = Long.class, jdbcType = JDBCType.BIGINT)
@EnumCodec(toMask = true)
@Schema(description = "设备特性")
@DefaultValue("0")
private DeviceFeature[] features;
public DeviceInfo toDeviceInfo() {
DeviceInfo info = org.jetlinks.core.device.DeviceInfo
.builder()
@ -134,6 +145,10 @@ public class DeviceInstanceEntity extends GenericEntity<String> implements Recor
info.addConfig("deviceName", name);
info.addConfig("productName", productName);
info.addConfig("orgId", orgId);
if (hasFeature(DeviceFeature.selfManageState)) {
info.addConfig(DeviceConfigKey.selfManageState, true);
}
if (!CollectionUtils.isEmpty(configuration)) {
info.addConfigs(configuration);
}
@ -142,4 +157,22 @@ public class DeviceInstanceEntity extends GenericEntity<String> implements Recor
}
return info;
}
public void addFeature(DeviceFeature... features) {
if (this.features == null) {
this.features = features;
}
if (features.length > 0) {
this.features = Stream
.concat(Stream.of(this.features), Stream.of(features))
.toArray(DeviceFeature[]::new);
}
}
public boolean hasFeature(DeviceFeature feature) {
if (this.features == null) {
return false;
}
return EnumDict.in(feature, DeviceFeature.values());
}
}

View File

@ -0,0 +1,22 @@
package org.jetlinks.community.device.enums;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.hswebframework.web.dict.Dict;
import org.hswebframework.web.dict.EnumDict;
@Dict("device-feature")
@Getter
@AllArgsConstructor
public enum DeviceFeature implements EnumDict<String> {
selfManageState("子设备自己管理状态")
;
private final String text;
@Override
public String getValue() {
return name();
}
}

View File

@ -2,6 +2,7 @@ package org.jetlinks.community.device.service;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.MapUtils;
import org.hswebframework.ezorm.rdb.mapping.ReactiveRepository;
import org.jetlinks.core.device.DeviceConfigKey;
import org.jetlinks.core.device.DeviceOperator;
@ -86,6 +87,16 @@ public class DeviceMessageBusinessHandler {
public Mono<Void> autoRegisterDevice(DeviceRegisterMessage message) {
return registry
.getDevice(message.getDeviceId())
.flatMap(device -> {
@SuppressWarnings("all")
Map<String, Object> config = message.getHeader("configuration").map(Map.class::cast).orElse(null);
if (MapUtils.isNotEmpty(config)) {
return device
.setConfigs(config)
.thenReturn(device);
}
return Mono.just(device);
})
.switchIfEmpty(Mono.defer(() -> {
//自动注册
return doAutoRegister(message);
@ -152,6 +163,15 @@ public class DeviceMessageBusinessHandler {
return Mono.empty();
}
@Subscribe("/device/*/*/unregister")
@Transactional(propagation = Propagation.NEVER)
public Mono<Void> unRegisterDevice(DeviceUnRegisterMessage message) {
//注销设备
return deviceService
.unregisterDevice(message.getDeviceId())
.then();
}
@Subscribe("/device/*/*/message/tags/update")
public Mono<Void> updateDeviceTag(UpdateTagMessage message) {
Map<String, Object> tags = message.getTags();

View File

@ -6,10 +6,12 @@ import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.hswebframework.ezorm.rdb.mapping.ReactiveRepository;
import org.hswebframework.ezorm.rdb.mapping.defaults.SaveResult;
import org.hswebframework.ezorm.rdb.operator.dml.Terms;
import org.hswebframework.web.crud.service.GenericReactiveCrudService;
import org.hswebframework.web.exception.BusinessException;
import org.hswebframework.web.id.IDGenerator;
import org.jetlinks.community.device.entity.*;
import org.jetlinks.community.device.enums.DeviceFeature;
import org.jetlinks.community.device.enums.DeviceState;
import org.jetlinks.community.device.response.DeviceDeployResult;
import org.jetlinks.community.device.response.DeviceDetail;
@ -350,6 +352,11 @@ public class LocalDeviceInstanceService extends GenericReactiveCrudService<Devic
.set(DeviceInstanceEntity::getState, state)
.where()
.in(DeviceInstanceEntity::getParentId, parents)
.nest()
/* */.accept(DeviceInstanceEntity::getFeatures, Terms.Enums.notInAny, DeviceFeature.selfManageState)
/* */.or()
/* */.isNull(DeviceInstanceEntity::getFeatures)
.end()
.execute())
.defaultIfEmpty(0)
)