diff --git a/transport/mqtt/src/main/java/org/thingsboard/server/transport/mqtt/session/GatewaySessionCtx.java b/transport/mqtt/src/main/java/org/thingsboard/server/transport/mqtt/session/GatewaySessionCtx.java index 675aec8df2..66f475231d 100644 --- a/transport/mqtt/src/main/java/org/thingsboard/server/transport/mqtt/session/GatewaySessionCtx.java +++ b/transport/mqtt/src/main/java/org/thingsboard/server/transport/mqtt/session/GatewaySessionCtx.java @@ -1,12 +1,12 @@ /** * Copyright © 2016-2017 The Thingsboard Authors - * + *

* Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

* Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -72,17 +72,19 @@ public class GatewaySessionCtx { public void onDeviceConnect(MqttPublishMessage msg) throws AdaptorException { String deviceName = checkDeviceName(getDeviceName(msg)); - Optional deviceOpt = deviceService.findDeviceByTenantIdAndName(gateway.getTenantId(), deviceName); - Device device = deviceOpt.orElseGet(() -> { - Device newDevice = new Device(); - newDevice.setTenantId(gateway.getTenantId()); - newDevice.setName(deviceName); - return deviceService.saveDevice(newDevice); - }); - GatewayDeviceSessionCtx ctx = new GatewayDeviceSessionCtx(this, device); - devices.put(deviceName, ctx); - processor.process(new BasicToDeviceActorSessionMsg(device, new BasicAdaptorToSessionActorMsg(ctx, new AttributesSubscribeMsg()))); - processor.process(new BasicToDeviceActorSessionMsg(device, new BasicAdaptorToSessionActorMsg(ctx, new RpcSubscribeMsg()))); + if (!devices.containsKey(deviceName)) { + Optional deviceOpt = deviceService.findDeviceByTenantIdAndName(gateway.getTenantId(), deviceName); + Device device = deviceOpt.orElseGet(() -> { + Device newDevice = new Device(); + newDevice.setTenantId(gateway.getTenantId()); + newDevice.setName(deviceName); + return deviceService.saveDevice(newDevice); + }); + GatewayDeviceSessionCtx ctx = new GatewayDeviceSessionCtx(this, device); + devices.put(deviceName, ctx); + processor.process(new BasicToDeviceActorSessionMsg(device, new BasicAdaptorToSessionActorMsg(ctx, new AttributesSubscribeMsg()))); + processor.process(new BasicToDeviceActorSessionMsg(device, new BasicAdaptorToSessionActorMsg(ctx, new RpcSubscribeMsg()))); + } ack(msg); }