Skip to content

Commit

Permalink
fix(设备模块): 解决修改设备物模型后,设备物模型脱离产品物模型问题 (#387)
Browse files Browse the repository at this point in the history
* fix(设备模块): 解决修改设备物模型后,设备物模型脱离产品物模型问题
  • Loading branch information
tancongsir authored Aug 2, 2023
1 parent 943d073 commit b131922
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,67 +40,6 @@ public DefaultPropertyMetricManager(ThingsRegistry registry,
this.repository = repository;
}

public Flux<PropertyMetric> getPropertyMetrics(String thingType,
String thingId,
String property) {
return Mono
.zip(
//数据库中记录的
repository
.createQuery()
.where(PropertyMetricEntity::getThingType, thingType)
.and(PropertyMetricEntity::getThingId, thingId)
.and(PropertyMetricEntity::getProperty, property)
.fetch()
.map(PropertyMetricEntity::toMetric)
.collectMap(PropertyMetric::getId),
//物模型中配置的
registry
.getThing(thingType, thingId)
.flatMap(Thing::getTemplate)
.flatMap(ThingTemplate::getMetadata)
.flatMapIterable(metadata -> metadata
.getProperty(property)
.map(PropertyMetadataConstants.Metrics::getMetrics)
.orElse(Collections.emptyList()))
.collectMap(PropertyMetric::getId, Function.identity(), LinkedHashMap::new),
(exists, inMetadata) -> {
for (Map.Entry<String, PropertyMetric> entry : exists.entrySet()) {
String metric = entry.getKey();
PropertyMetric independent = entry.getValue();
PropertyMetric fromMetadata = inMetadata.get(metric);
if (fromMetadata == null) {
inMetadata.put(metric, independent);
continue;
}
fromMetadata.setValue(independent.getValue());
}
return Flux.fromIterable(inMetadata.values());
})
.flatMapMany(Function.identity());
}

public Mono<SaveResult> savePropertyMetrics(String thingType,
String thingId,
String property,
Flux<PropertyMetric> metrics) {
return metrics
.map(metric -> {
PropertyMetricEntity entity = new PropertyMetricEntity();
entity.setThingId(thingId);
entity.setThingType(thingType);
entity.setMetric(metric.getId());
entity.setMetricName(metric.getName());
entity.setProperty(property);
entity.setValue(String.valueOf(metric.getValue()));
entity.setRange(metric.isRange());
entity.genericId();
return entity;
})
.as(repository::save);

}

@Override
protected Mono<PropertyMetric> loadPropertyMetric(ThingId thingId,
String property,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,7 @@ public DeviceInfo toDeviceInfo(boolean includeConfiguration) {
info.addConfig(DeviceConfigKey.parentGatewayId, this.getParentId());
info.addConfig(PropertyConstants.deviceName, name);
info.addConfig(PropertyConstants.productName, productName);
info.addConfig(PropertyConstants.orgId, orgId);
info.addConfig(PropertyConstants.creatorId, creatorId);
info.addConfig(PropertyConstants.creatorId,creatorId);
if (hasFeature(DeviceFeature.selfManageState)) {
info.addConfig(DeviceConfigKey.selfManageState, true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,13 @@ public class DeviceDetail {
@Schema(description = "激活时间")
private long registerTime;

//设备元数据
//设备物模型
@Schema(description = "物模型")
private String metadata;

@Schema(description = "产品物模型")
private String productMetadata;

//是否为独立物模型
@Schema(description = "是否为独立物模型")
private boolean independentMetadata;
Expand Down Expand Up @@ -162,10 +165,30 @@ public DeviceDetail notActive() {
}

private DeviceMetadata decodeMetadata() {
if (StringUtils.isEmpty(metadata)) {
return null;
if (StringUtils.hasText(metadata)) {
return JetLinksDeviceMetadataCodec.getInstance().doDecode(metadata);
}
return JetLinksDeviceMetadataCodec.getInstance().doDecode(metadata);
return null;
}

private void mergeDeviceMetadata(String deviceMetadata) {
mergeDeviceMetadata(JetLinksDeviceMetadataCodec.getInstance().doDecode(deviceMetadata));
}

private void mergeDeviceMetadata(DeviceMetadata deviceMetadata) {
if (!StringUtils.hasText(productMetadata)) {
metadata = JetLinksDeviceMetadataCodec.getInstance().doEncode(deviceMetadata);
return;
}

//合并物模型
metadata = JetLinksDeviceMetadataCodec
.getInstance()
.doEncode(new CompositeDeviceMetadata(
JetLinksDeviceMetadataCodec.getInstance().doDecode(productMetadata),
deviceMetadata
));

}

private void initTags() {
Expand All @@ -189,27 +212,32 @@ public Mono<DeviceDetail> with(DeviceOperator operator, List<ConfigPropertyMetad
operator.getOnlineTime().defaultIfEmpty(0L),
//T3: 离线时间
operator.getOfflineTime().defaultIfEmpty(0L),
//T4: 物模型
operator.getMetadata().switchIfEmpty(Mono.fromSupplier(this::decodeMetadata)),
//T5: 真实的配置信息
//T4: 真实的配置信息
operator.getSelfConfigs(configs
.stream()
.map(ConfigPropertyMetadata::getProperty)
.collect(Collectors.toList()))
.defaultIfEmpty(Values.of(Collections.emptyMap()))
.collect(Collectors.toSet()))
.defaultIfEmpty(Values.of(Collections.emptyMap())),
//T5:设备物模型,单独保存物模型后有值
operator.getSelfConfig(DeviceConfigKey.metadata)
.defaultIfEmpty("")
)
.doOnNext(tp -> {
setOnlineTime(tp.getT2());
setOfflineTime(tp.getT3());
setAddress(tp.getT1());
with(tp.getT4()
.getTags()
.stream()
.map(DeviceTagEntity::of)
.collect(Collectors.toList()));
Map<String, Object> cachedConfigs = tp.getT5().getAllValues();

Map<String, Object> cachedConfigs = tp.getT4().getAllValues();
cachedConfiguration.putAll(cachedConfigs);
// cachedConfigs.forEach(configuration::putIfAbsent);

DeviceMetadata metadata_ = decodeMetadata();
if (null != metadata_) {
with(metadata_.getTags()
.stream()
.map(DeviceTagEntity::of)
.collect(Collectors.toList()));
}

})
.thenReturn(this);
}
Expand Down Expand Up @@ -258,9 +286,7 @@ public DeviceDetail with(DeviceProductEntity productEntity) {
if (productEntity == null) {
return this;
}
if (StringUtils.isEmpty(metadata)) {
setMetadata(productEntity.getMetadata());
}
setProductMetadata(productEntity.getMetadata());
if (CollectionUtils.isEmpty(configuration) && !CollectionUtils.isEmpty(productEntity.getConfiguration())) {
configuration.putAll(productEntity.getConfiguration());
}
Expand All @@ -284,7 +310,6 @@ public DeviceDetail with(DeviceInstanceEntity device) {
setId(device.getId());
setName(device.getName());
setState(device.getState());
setOrgId(device.getOrgId());
setParentId(device.getParentId());
setDescription(device.getDescribe());
if (device.getFeatures() != null) {
Expand All @@ -309,7 +334,7 @@ public DeviceDetail with(DeviceInstanceEntity device) {
configuration.putAll(device.getConfiguration());
}
if (StringUtils.hasText(device.getDeriveMetadata())) {
setMetadata(device.getDeriveMetadata());
mergeDeviceMetadata(device.getDeriveMetadata());
setIndependentMetadata(true);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,9 @@ public DeviceInstanceController(LocalDeviceInstanceService service,
@QueryAction
@Operation(summary = "获取指定ID设备详情")
public Mono<DeviceDetail> getDeviceDetailInfo(@PathVariable @Parameter(description = "设备ID") String id) {
return service.getDeviceDetail(id);
return service
.getDeviceDetail(id)
.switchIfEmpty(Mono.error(NotFoundException::new));
}

//读取设备属性
Expand Down

0 comments on commit b131922

Please sign in to comment.