From 8e20fd88007a662b53bcf3dc31e900cae966d2e5 Mon Sep 17 00:00:00 2001 From: Andrii Shvaika Date: Thu, 27 Apr 2023 17:08:34 +0300 Subject: [PATCH] Metric wildcards and default attributes --- .../session/SparkplugNodeSessionHandler.java | 16 ++++++++++++++-- ui-ngx/src/app/shared/models/device.models.ts | 1 + 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/common/transport/mqtt/src/main/java/org/thingsboard/server/transport/mqtt/session/SparkplugNodeSessionHandler.java b/common/transport/mqtt/src/main/java/org/thingsboard/server/transport/mqtt/session/SparkplugNodeSessionHandler.java index 9cf3495273..6b107683e0 100644 --- a/common/transport/mqtt/src/main/java/org/thingsboard/server/transport/mqtt/session/SparkplugNodeSessionHandler.java +++ b/common/transport/mqtt/src/main/java/org/thingsboard/server/transport/mqtt/session/SparkplugNodeSessionHandler.java @@ -233,7 +233,7 @@ public class SparkplugNodeSessionHandler extends AbstractGatewaySessionHandler msgs = new ArrayList<>(); for (SparkplugBProto.Payload.Metric protoMetric : sparkplugBProto.getMetricsList()) { - if (attributesMetricNames == null || !attributesMetricNames.contains(protoMetric.getName())) { + if (attributesMetricNames == null || !matches(attributesMetricNames, protoMetric)) { long ts = protoMetric.getTimestamp(); String key = "bdSeq".equals(protoMetric.getName()) ? topicTypeName + " " + protoMetric.getName() : protoMetric.getName(); @@ -264,7 +264,7 @@ public class SparkplugNodeSessionHandler extends AbstractGatewaySessionHandler msgs = new ArrayList<>(); for (SparkplugBProto.Payload.Metric protoMetric : sparkplugBProto.getMetricsList()) { - if (attributesMetricNames.contains(protoMetric.getName())) { + if (matches(attributesMetricNames, protoMetric)) { TransportApiProtos.AttributesMsg.Builder deviceAttributesMsgBuilder = TransportApiProtos.AttributesMsg.newBuilder(); Optional msgOpt = getPostAttributeMsg(protoMetric); if (msgOpt.isPresent()) { @@ -281,6 +281,18 @@ public class SparkplugNodeSessionHandler extends AbstractGatewaySessionHandler attributesMetricNames, SparkplugBProto.Payload.Metric protoMetric) { + String metricName = protoMetric.getName(); + for (String attributeMetricFilter : attributesMetricNames) { + if (metricName.equals(attributeMetricFilter) || + (attributeMetricFilter.endsWith("*") && metricName.startsWith( + attributeMetricFilter.substring(0, attributeMetricFilter.length() - 1)))) { + return true; + } + } + return false; + } + private Optional getPostAttributeMsg(SparkplugBProto.Payload.Metric protoMetric) throws ThingsboardException { Optional keyValueProtoOpt = fromSparkplugBMetricToKeyValueProto(protoMetric.getName(), protoMetric); if (keyValueProtoOpt.isPresent()) { diff --git a/ui-ngx/src/app/shared/models/device.models.ts b/ui-ngx/src/app/shared/models/device.models.ts index 1cf06e9ec6..b975d511ff 100644 --- a/ui-ngx/src/app/shared/models/device.models.ts +++ b/ui-ngx/src/app/shared/models/device.models.ts @@ -368,6 +368,7 @@ export function createDeviceProfileTransportConfiguration(type: DeviceTransportT deviceAttributesTopic: 'v1/devices/me/attributes', deviceAttributesSubscribeTopic: 'v1/devices/me/attributes', sparkplug: false, + sparkplugAttributesMetricNames: ['Node Control/*', 'Device Control/*', 'Properties/*'], sendAckOnValidationException: false, transportPayloadTypeConfiguration: { transportPayloadType: TransportPayloadType.JSON,