Added deduplication support for gateway

This commit is contained in:
Andrew Shvayka 2017-03-24 12:41:12 +02:00
parent 0bdf246383
commit ba348dd997

View File

@ -1,12 +1,12 @@
/** /**
* Copyright © 2016-2017 The Thingsboard Authors * Copyright © 2016-2017 The Thingsboard Authors
* * <p>
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* * <p>
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* * <p>
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * 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 { public void onDeviceConnect(MqttPublishMessage msg) throws AdaptorException {
String deviceName = checkDeviceName(getDeviceName(msg)); String deviceName = checkDeviceName(getDeviceName(msg));
Optional<Device> deviceOpt = deviceService.findDeviceByTenantIdAndName(gateway.getTenantId(), deviceName); if (!devices.containsKey(deviceName)) {
Device device = deviceOpt.orElseGet(() -> { Optional<Device> deviceOpt = deviceService.findDeviceByTenantIdAndName(gateway.getTenantId(), deviceName);
Device newDevice = new Device(); Device device = deviceOpt.orElseGet(() -> {
newDevice.setTenantId(gateway.getTenantId()); Device newDevice = new Device();
newDevice.setName(deviceName); newDevice.setTenantId(gateway.getTenantId());
return deviceService.saveDevice(newDevice); newDevice.setName(deviceName);
}); return deviceService.saveDevice(newDevice);
GatewayDeviceSessionCtx ctx = new GatewayDeviceSessionCtx(this, device); });
devices.put(deviceName, ctx); GatewayDeviceSessionCtx ctx = new GatewayDeviceSessionCtx(this, device);
processor.process(new BasicToDeviceActorSessionMsg(device, new BasicAdaptorToSessionActorMsg(ctx, new AttributesSubscribeMsg()))); devices.put(deviceName, ctx);
processor.process(new BasicToDeviceActorSessionMsg(device, new BasicAdaptorToSessionActorMsg(ctx, new RpcSubscribeMsg()))); processor.process(new BasicToDeviceActorSessionMsg(device, new BasicAdaptorToSessionActorMsg(ctx, new AttributesSubscribeMsg())));
processor.process(new BasicToDeviceActorSessionMsg(device, new BasicAdaptorToSessionActorMsg(ctx, new RpcSubscribeMsg())));
}
ack(msg); ack(msg);
} }