|
|
|
@ -1,12 +1,12 @@ |
|
|
|
/** |
|
|
|
* Copyright © 2016-2017 The Thingsboard Authors |
|
|
|
* <p> |
|
|
|
* |
|
|
|
* 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 |
|
|
|
* <p> |
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
* <p> |
|
|
|
* |
|
|
|
* 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. |
|
|
|
@ -15,9 +15,10 @@ |
|
|
|
*/ |
|
|
|
package org.thingsboard.server.transport.mqtt.session; |
|
|
|
|
|
|
|
import com.google.gson.*; |
|
|
|
import io.netty.buffer.ByteBufAllocator; |
|
|
|
import io.netty.buffer.UnpooledByteBufAllocator; |
|
|
|
import com.google.gson.JsonArray; |
|
|
|
import com.google.gson.JsonElement; |
|
|
|
import com.google.gson.JsonObject; |
|
|
|
import com.google.gson.JsonSyntaxException; |
|
|
|
import io.netty.channel.ChannelHandlerContext; |
|
|
|
import io.netty.handler.codec.mqtt.MqttMessage; |
|
|
|
import io.netty.handler.codec.mqtt.MqttPublishMessage; |
|
|
|
@ -27,28 +28,23 @@ 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.msg.core.*; |
|
|
|
import org.thingsboard.server.common.msg.session.AdaptorToSessionActorMsg; |
|
|
|
import org.thingsboard.server.common.msg.session.BasicAdaptorToSessionActorMsg; |
|
|
|
import org.thingsboard.server.common.msg.session.BasicToDeviceActorSessionMsg; |
|
|
|
import org.thingsboard.server.common.msg.session.FromDeviceMsg; |
|
|
|
import org.thingsboard.server.common.msg.session.ctrl.SessionCloseMsg; |
|
|
|
import org.thingsboard.server.common.transport.SessionMsgProcessor; |
|
|
|
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.common.transport.session.DeviceAwareSessionContext; |
|
|
|
import org.thingsboard.server.dao.device.DeviceService; |
|
|
|
import org.thingsboard.server.transport.mqtt.MqttTopics; |
|
|
|
import org.thingsboard.server.transport.mqtt.MqttTransportHandler; |
|
|
|
import org.thingsboard.server.transport.mqtt.adaptors.JsonMqttAdaptor; |
|
|
|
|
|
|
|
import java.nio.charset.Charset; |
|
|
|
import java.util.Collections; |
|
|
|
import java.util.HashMap; |
|
|
|
import java.util.Map; |
|
|
|
import java.util.Optional; |
|
|
|
import java.util.stream.Collectors; |
|
|
|
|
|
|
|
import static org.thingsboard.server.common.msg.session.MsgType.SUBSCRIBE_ATTRIBUTES_REQUEST; |
|
|
|
import static org.thingsboard.server.transport.mqtt.adaptors.JsonMqttAdaptor.validateJsonPayload; |
|
|
|
|
|
|
|
/** |
|
|
|
@ -165,6 +161,29 @@ public class GatewaySessionCtx { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public void onDeviceAttributesRequest(MqttPublishMessage mqttMsg) throws AdaptorException { |
|
|
|
JsonElement json = validateJsonPayload(gatewaySessionId, mqttMsg.payload()); |
|
|
|
if (json.isJsonObject()) { |
|
|
|
JsonObject jsonObj = json.getAsJsonObject(); |
|
|
|
int requestId = jsonObj.get("id").getAsInt(); |
|
|
|
String deviceName = jsonObj.get("device").getAsString(); |
|
|
|
boolean clientScope = jsonObj.get("client").getAsBoolean(); |
|
|
|
String key = jsonObj.get("key").getAsString(); |
|
|
|
|
|
|
|
BasicGetAttributesRequest request; |
|
|
|
if (clientScope) { |
|
|
|
request = new BasicGetAttributesRequest(requestId, Collections.singleton(key), null); |
|
|
|
} else { |
|
|
|
request = new BasicGetAttributesRequest(requestId, null, Collections.singleton(key)); |
|
|
|
} |
|
|
|
GatewayDeviceSessionCtx deviceSessionCtx = devices.get(deviceName); |
|
|
|
processor.process(new BasicToDeviceActorSessionMsg(deviceSessionCtx.getDevice(), |
|
|
|
new BasicAdaptorToSessionActorMsg(deviceSessionCtx, request))); |
|
|
|
} else { |
|
|
|
throw new JsonSyntaxException("Can't parse value: " + json); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private String checkDeviceConnected(String deviceName) { |
|
|
|
if (!devices.containsKey(deviceName)) { |
|
|
|
throw new RuntimeException("Device is not connected!"); |
|
|
|
|