|
|
|
@ -27,6 +27,7 @@ import org.springframework.util.StringUtils; |
|
|
|
import org.thingsboard.server.common.data.Device; |
|
|
|
import org.thingsboard.server.common.data.id.SessionId; |
|
|
|
import org.thingsboard.server.common.data.kv.BaseAttributeKvEntry; |
|
|
|
import org.thingsboard.server.common.data.relation.EntityRelation; |
|
|
|
import org.thingsboard.server.common.msg.core.*; |
|
|
|
import org.thingsboard.server.common.msg.session.BasicAdaptorToSessionActorMsg; |
|
|
|
import org.thingsboard.server.common.msg.session.BasicToDeviceActorSessionMsg; |
|
|
|
@ -36,6 +37,7 @@ import org.thingsboard.server.common.transport.adaptor.AdaptorException; |
|
|
|
import org.thingsboard.server.common.transport.adaptor.JsonConverter; |
|
|
|
import org.thingsboard.server.common.transport.auth.DeviceAuthService; |
|
|
|
import org.thingsboard.server.dao.device.DeviceService; |
|
|
|
import org.thingsboard.server.dao.relation.RelationService; |
|
|
|
import org.thingsboard.server.transport.mqtt.MqttTransportHandler; |
|
|
|
import org.thingsboard.server.transport.mqtt.adaptors.JsonMqttAdaptor; |
|
|
|
|
|
|
|
@ -58,28 +60,34 @@ public class GatewaySessionCtx { |
|
|
|
private final SessionMsgProcessor processor; |
|
|
|
private final DeviceService deviceService; |
|
|
|
private final DeviceAuthService authService; |
|
|
|
private final RelationService relationService; |
|
|
|
private final Map<String, GatewayDeviceSessionCtx> devices; |
|
|
|
private ChannelHandlerContext channel; |
|
|
|
|
|
|
|
public GatewaySessionCtx(SessionMsgProcessor processor, DeviceService deviceService, DeviceAuthService authService, DeviceSessionCtx gatewaySessionCtx) { |
|
|
|
public GatewaySessionCtx(SessionMsgProcessor processor, DeviceService deviceService, DeviceAuthService authService, RelationService relationService, DeviceSessionCtx gatewaySessionCtx) { |
|
|
|
this.processor = processor; |
|
|
|
this.deviceService = deviceService; |
|
|
|
this.authService = authService; |
|
|
|
this.relationService = relationService; |
|
|
|
this.gateway = gatewaySessionCtx.getDevice(); |
|
|
|
this.gatewaySessionId = gatewaySessionCtx.getSessionId(); |
|
|
|
this.devices = new HashMap<>(); |
|
|
|
} |
|
|
|
|
|
|
|
public void onDeviceConnect(MqttPublishMessage msg) throws AdaptorException { |
|
|
|
String deviceName = checkDeviceName(getDeviceName(msg)); |
|
|
|
JsonElement json = getJson(msg); |
|
|
|
String deviceName = checkDeviceName(getDeviceName(json)); |
|
|
|
String deviceType = getDeviceType(json); |
|
|
|
if (!devices.containsKey(deviceName)) { |
|
|
|
Optional<Device> deviceOpt = deviceService.findDeviceByTenantIdAndName(gateway.getTenantId(), deviceName); |
|
|
|
Device device = deviceOpt.orElseGet(() -> { |
|
|
|
Device newDevice = new Device(); |
|
|
|
newDevice.setTenantId(gateway.getTenantId()); |
|
|
|
newDevice.setName(deviceName); |
|
|
|
newDevice.setType("default"); |
|
|
|
return deviceService.saveDevice(newDevice); |
|
|
|
newDevice.setType(deviceType); |
|
|
|
newDevice = deviceService.saveDevice(newDevice); |
|
|
|
relationService.saveRelation(new EntityRelation(gateway.getId(), newDevice.getId(), "Created")); |
|
|
|
return newDevice; |
|
|
|
}); |
|
|
|
GatewayDeviceSessionCtx ctx = new GatewayDeviceSessionCtx(this, device); |
|
|
|
devices.put(deviceName, ctx); |
|
|
|
@ -91,7 +99,7 @@ public class GatewaySessionCtx { |
|
|
|
} |
|
|
|
|
|
|
|
public void onDeviceDisconnect(MqttPublishMessage msg) throws AdaptorException { |
|
|
|
String deviceName = checkDeviceName(getDeviceName(msg)); |
|
|
|
String deviceName = checkDeviceName(getDeviceName(getJson(msg))); |
|
|
|
GatewayDeviceSessionCtx deviceSessionCtx = devices.remove(deviceName); |
|
|
|
if (deviceSessionCtx != null) { |
|
|
|
processor.process(SessionCloseMsg.onDisconnect(deviceSessionCtx.getSessionId())); |
|
|
|
@ -211,11 +219,19 @@ public class GatewaySessionCtx { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private String getDeviceName(MqttPublishMessage mqttMsg) throws AdaptorException { |
|
|
|
JsonElement json = JsonMqttAdaptor.validateJsonPayload(gatewaySessionId, mqttMsg.payload()); |
|
|
|
private String getDeviceName(JsonElement json) throws AdaptorException { |
|
|
|
return json.getAsJsonObject().get("device").getAsString(); |
|
|
|
} |
|
|
|
|
|
|
|
private String getDeviceType(JsonElement json) throws AdaptorException { |
|
|
|
JsonElement type = json.getAsJsonObject().get("type"); |
|
|
|
return type == null ? "default" : type.getAsString(); |
|
|
|
} |
|
|
|
|
|
|
|
private JsonElement getJson(MqttPublishMessage mqttMsg) throws AdaptorException { |
|
|
|
return JsonMqttAdaptor.validateJsonPayload(gatewaySessionId, mqttMsg.payload()); |
|
|
|
} |
|
|
|
|
|
|
|
protected SessionMsgProcessor getProcessor() { |
|
|
|
return processor; |
|
|
|
} |
|
|
|
@ -229,7 +245,9 @@ public class GatewaySessionCtx { |
|
|
|
} |
|
|
|
|
|
|
|
private void ack(MqttPublishMessage msg) { |
|
|
|
writeAndFlush(MqttTransportHandler.createMqttPubAckMsg(msg.variableHeader().messageId())); |
|
|
|
if(msg.variableHeader().messageId() > 0) { |
|
|
|
writeAndFlush(MqttTransportHandler.createMqttPubAckMsg(msg.variableHeader().messageId())); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
protected void writeAndFlush(MqttMessage mqttMessage) { |
|
|
|
|