优化设备事件查询
This commit is contained in:
parent
6c082000fa
commit
f1b0bb26f3
|
|
@ -22,9 +22,11 @@ import org.jetlinks.core.device.DeviceOperator;
|
|||
import org.jetlinks.core.device.DeviceRegistry;
|
||||
import org.jetlinks.core.message.DeviceOfflineMessage;
|
||||
import org.jetlinks.core.message.DeviceOnlineMessage;
|
||||
import org.jetlinks.core.metadata.DataType;
|
||||
import org.jetlinks.core.metadata.DeviceMetadata;
|
||||
import org.jetlinks.core.metadata.EventMetadata;
|
||||
import org.jetlinks.core.metadata.Metadata;
|
||||
import org.jetlinks.core.metadata.types.ObjectType;
|
||||
import org.jetlinks.core.utils.FluxUtils;
|
||||
import org.jetlinks.community.device.entity.DeviceInstanceEntity;
|
||||
import org.jetlinks.community.device.entity.DeviceProductEntity;
|
||||
|
|
@ -60,6 +62,7 @@ import java.net.URLEncoder;
|
|||
import java.nio.charset.StandardCharsets;
|
||||
import java.time.Duration;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
|
@ -247,6 +250,34 @@ public class LocalDeviceInstanceService extends GenericReactiveCrudService<Devic
|
|||
.defaultIfEmpty(PagerResult.empty());
|
||||
}
|
||||
|
||||
public Mono<PagerResult<Map<String, Object>>> queryDeviceEvent(String deviceId, String eventId, QueryParamEntity entity, boolean format) {
|
||||
return registry
|
||||
.getDevice(deviceId)
|
||||
.flatMap(operator -> operator.getSelfConfig(DeviceConfigKey.productId).zipWith(operator.getMetadata()))
|
||||
.flatMap(tp -> timeSeriesManager
|
||||
.getService(DeviceTimeSeriesMetric.deviceEventMetric(tp.getT1(), eventId))
|
||||
.queryPager(entity.and("deviceId", TermType.eq, deviceId), data -> {
|
||||
if (!format) {
|
||||
return data.getData();
|
||||
}
|
||||
Map<String, Object> formatData = new HashMap<>(data.getData());
|
||||
tp.getT2()
|
||||
.getEvent(eventId)
|
||||
.ifPresent(eventMetadata -> {
|
||||
DataType type = eventMetadata.getType();
|
||||
if (type instanceof ObjectType) {
|
||||
@SuppressWarnings("all")
|
||||
Map<String, Object> val = (Map<String, Object>) type.format(formatData);
|
||||
val.forEach((k, v) -> formatData.put(k + "_format", v));
|
||||
} else {
|
||||
formatData.put("value_format", type.format(data.get("value")));
|
||||
}
|
||||
});
|
||||
return formatData;
|
||||
})
|
||||
.defaultIfEmpty(PagerResult.empty()));
|
||||
}
|
||||
|
||||
public Mono<DevicePropertiesEntity> getDeviceLatestProperty(String deviceId, String property) {
|
||||
return registry
|
||||
.getDevice(deviceId)
|
||||
|
|
|
|||
|
|
@ -33,6 +33,7 @@ import reactor.core.publisher.Flux;
|
|||
import reactor.core.publisher.Mono;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
|
||||
@RestController
|
||||
@RequestMapping({"/device-instance", "/device/instance"})
|
||||
|
|
@ -156,6 +157,15 @@ public class DeviceInstanceController implements
|
|||
return service.getDeviceLatestProperty(deviceId, property);
|
||||
}
|
||||
|
||||
@GetMapping("/{deviceId:.+}/event/{eventId}")
|
||||
@QueryAction
|
||||
public Mono<PagerResult<Map<String, Object>>> queryPagerByDeviceEvent(QueryParamEntity queryParam,
|
||||
@RequestParam(defaultValue = "false") boolean format,
|
||||
@PathVariable String deviceId,
|
||||
@PathVariable String eventId) {
|
||||
return service.queryDeviceEvent(deviceId, eventId, queryParam, format);
|
||||
}
|
||||
|
||||
@GetMapping("/{deviceId:.+}/properties/_query")
|
||||
@QueryAction
|
||||
public Mono<PagerResult<DevicePropertiesEntity>> queryDeviceProperties(@PathVariable String deviceId, QueryParamEntity entity) {
|
||||
|
|
|
|||
|
|
@ -18,11 +18,14 @@ import org.jetlinks.core.metadata.unit.ValueUnit;
|
|||
import org.jetlinks.core.metadata.unit.ValueUnits;
|
||||
import org.jetlinks.community.device.entity.ProtocolSupportEntity;
|
||||
import org.jetlinks.community.device.service.LocalProtocolSupportService;
|
||||
import org.jetlinks.supports.protocol.management.ProtocolSupportLoaderProvider;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/protocol")
|
||||
@Authorize
|
||||
|
|
@ -37,6 +40,9 @@ public class ProtocolSupportController implements
|
|||
@Autowired
|
||||
private ProtocolSupports protocolSupports;
|
||||
|
||||
@Autowired
|
||||
private List<ProtocolSupportLoaderProvider> providers;
|
||||
|
||||
@PostMapping("/{id}/_deploy")
|
||||
@SaveAction
|
||||
public Mono<Boolean> deploy(@PathVariable String id) {
|
||||
|
|
@ -49,6 +55,14 @@ public class ProtocolSupportController implements
|
|||
return service.unDeploy(id);
|
||||
}
|
||||
|
||||
//获取支持的协议类型
|
||||
@GetMapping("/providers")
|
||||
@Authorize(merge = false)
|
||||
public Flux<String> getProviders() {
|
||||
return Flux.fromIterable(providers)
|
||||
.map(ProtocolSupportLoaderProvider::getProvider);
|
||||
}
|
||||
|
||||
@GetMapping("/supports")
|
||||
@Authorize(merge = false)
|
||||
public Flux<ProtocolInfo> allProtocols() {
|
||||
|
|
|
|||
Loading…
Reference in New Issue