diff --git a/common/transport/mqtt/src/main/java/org/thingsboard/server/transport/mqtt/session/AbstractGatewaySessionHandler.java b/common/transport/mqtt/src/main/java/org/thingsboard/server/transport/mqtt/session/AbstractGatewaySessionHandler.java index 8ea6480142..cbb805731e 100644 --- a/common/transport/mqtt/src/main/java/org/thingsboard/server/transport/mqtt/session/AbstractGatewaySessionHandler.java +++ b/common/transport/mqtt/src/main/java/org/thingsboard/server/transport/mqtt/session/AbstractGatewaySessionHandler.java @@ -68,6 +68,7 @@ import org.thingsboard.server.transport.mqtt.adaptors.MqttTransportAdaptor; import org.thingsboard.server.transport.mqtt.adaptors.ProtoMqttAdaptor; import org.thingsboard.server.transport.mqtt.gateway.GatewayMetricsService; import org.thingsboard.server.transport.mqtt.util.sparkplug.SparkplugConnectionState; +import org.thingsboard.server.transport.mqtt.util.sparkplug.SparkplugTopic; import java.util.ArrayList; import java.util.Collections; @@ -296,6 +297,15 @@ public abstract class AbstractGatewaySessionHandler onDeviceConnectSparkplug(SparkplugTopic topic, String deviceType) { + T result = devices.get(topic.getNodeDeviceName()); + if (result == null) { + return onDeviceConnect(topic.getNodeDeviceNameAllPath(), deviceType); + } else { + return Futures.immediateFuture(result); + } + } + private ListenableFuture getDeviceCreationFuture(String deviceName, String deviceType) { final SettableFuture futureToSet = SettableFuture.create(); ListenableFuture future = deviceFutures.putIfAbsent(deviceName, futureToSet); @@ -844,7 +854,7 @@ public abstract class AbstractGatewaySessionHandler contextListenableFuture; + TransportProtos.SessionInfoProto sessionInfo = this.deviceSessionCtx.getSessionInfo(); if (topic.isNode()) { if (topic.isType(NBIRTH)) { - sendSparkplugStateOnTelemetry(this.deviceSessionCtx.getSessionInfo(), deviceName, ONLINE, + sendSparkplugStateOnTelemetry(sessionInfo, deviceName, ONLINE, sparkplugBProto.getTimestamp()); setNodeBirthMetrics(sparkplugBProto.getMetricsList()); } contextListenableFuture = Futures.immediateFuture(this.deviceSessionCtx); } else { - ListenableFuture deviceCtx = onDeviceConnectProto(topic); - contextListenableFuture = Futures.transform(deviceCtx, ctx -> { - if (topic.isType(DBIRTH)) { - sendSparkplugStateOnTelemetry(ctx.getSessionInfo(), deviceName, ONLINE, - sparkplugBProto.getTimestamp()); - try { - ctx.setDeviceBirthMetrics(sparkplugBProto.getMetricsList()); - } catch (IllegalArgumentException | DuplicateKeyException e) { - throw new RuntimeException(e); + try { + ListenableFuture deviceCtx = this.onDeviceConnectProto(topic); + deviceName = checkDeviceName(deviceCtx.get().getDeviceInfo().getDeviceName()); + String finalDeviceName = deviceName; + contextListenableFuture = Futures.transform(deviceCtx, ctx -> { + if (topic.isType(DBIRTH)) { + sendSparkplugStateOnTelemetry(sessionInfo, finalDeviceName, ONLINE, + sparkplugBProto.getTimestamp()); + ctx.setDeviceBirthMetrics(sparkplugBProto.getMetricsList()); } - } - return ctx; - }, MoreExecutors.directExecutor()); + return ctx; + }, MoreExecutors.directExecutor()); + } catch (IllegalArgumentException | DuplicateKeyException | ExecutionException | InterruptedException e) { + throw new RuntimeException(e); + } } Set attributesMetricNames = ((MqttDeviceProfileTransportConfiguration) deviceSessionCtx .getDeviceProfile().getProfileData().getTransportConfiguration()).getSparkplugAttributesMetricNames(); @@ -222,7 +223,7 @@ public class SparkplugNodeSessionHandler extends AbstractGatewaySessionHandler