refactor(设备模块): 添加指标查询和保存指标功能 (#403)

This commit is contained in:
tancong 2023-08-09 11:57:58 +08:00 committed by GitHub
parent 7ffcf4bc4b
commit 20276d0c19
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 104 additions and 3 deletions

View File

@ -40,6 +40,67 @@ public class DefaultPropertyMetricManager extends AbstractPropertyMetricManager
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,

View File

@ -25,6 +25,7 @@ import org.hswebframework.web.exception.NotFoundException;
import org.hswebframework.web.exception.ValidationException;
import org.hswebframework.web.i18n.LocaleUtils;
import org.hswebframework.web.id.IDGenerator;
import org.jetlinks.community.PropertyMetric;
import org.jetlinks.community.device.entity.*;
import org.jetlinks.community.device.enums.DeviceState;
import org.jetlinks.community.device.response.DeviceDeployResult;
@ -50,6 +51,7 @@ import org.jetlinks.community.io.utils.FileUtils;
import org.jetlinks.community.relation.RelationObjectProvider;
import org.jetlinks.community.relation.service.RelationService;
import org.jetlinks.community.relation.service.request.SaveRelationRequest;
import org.jetlinks.community.things.impl.metric.DefaultPropertyMetricManager;
import org.jetlinks.community.timeseries.query.AggregationData;
import org.jetlinks.community.web.response.ValidationResult;
import org.jetlinks.core.Values;
@ -127,6 +129,8 @@ public class DeviceInstanceController implements
private final DeviceExcelFilterColumns filterColumns;
private final DefaultPropertyMetricManager metricManager;
@SuppressWarnings("all")
public DeviceInstanceController(LocalDeviceInstanceService service,
DeviceRegistry registry,
@ -139,7 +143,8 @@ public class DeviceInstanceController implements
TransactionalOperator transactionalOperator,
FileManager fileManager,
WebClient.Builder builder,
DeviceExcelFilterColumns filterColumns) {
DeviceExcelFilterColumns filterColumns,
DefaultPropertyMetricManager metricManager) {
this.service = service;
this.registry = registry;
this.productService = productService;
@ -152,6 +157,7 @@ public class DeviceInstanceController implements
this.fileManager = fileManager;
this.webClient = builder.build();
this.filterColumns = filterColumns;
this.metricManager = metricManager;
}
@ -1104,4 +1110,26 @@ public class DeviceInstanceController implements
}
@PatchMapping("/{deviceId}/metric/property/{property}")
@Operation(summary = "保存设备属性指标数据")
@SaveAction
public Mono<Void> savePropertyMetric(@PathVariable String deviceId,
@PathVariable String property,
@RequestBody Flux<PropertyMetric> metricFlux) {
return metricManager
.savePropertyMetrics(DeviceThingType.device.getId(),
deviceId,
property,
metricFlux)
.then();
}
@GetMapping("/{deviceId}/metric/property/{property}")
@Operation(summary = "获取设备属性指标数据")
@SaveAction
public Flux<PropertyMetric> getPropertyMetric(@PathVariable String deviceId,
@PathVariable String property) {
return metricManager
.getPropertyMetrics(DeviceThingType.device.getId(), deviceId, property);
}
}

View File

@ -100,8 +100,20 @@ public class ProtocolSupportController
@GetMapping("/supports")
@Authorize(merge = false)
@Operation(summary = "获取当前支持的协议")
public Flux<ProtocolInfo> allProtocols() {
return protocolSupports.getProtocols().map(ProtocolInfo::of);
public Flux<ProtocolInfo> allProtocols(@Parameter(hidden = true) QueryParamEntity query) {
return protocolSupports
.getProtocols()
.collectMap(ProtocolSupport::getId)
.flatMapMany(protocols -> service.createQuery()
.setParam(query)
.fetch()
.index()
.flatMap(tp2 -> Mono
.justOrEmpty(protocols.get(tp2.getT2().getId()))
.map(ProtocolInfo::of)
.map(protocolInfo -> Tuples.of(tp2.getT1(), protocolInfo))))
.sort(Comparator.comparingLong(Tuple2::getT1))
.map(Tuple2::getT2);
}
@GetMapping("/{id}/{transport}/configuration")