Search in sources :

Example 6 with BasicAdaptorToSessionActorMsg

use of org.thingsboard.server.common.msg.session.BasicAdaptorToSessionActorMsg in project thingsboard by thingsboard.

the class GatewaySessionCtx method onDeviceAttributes.

public void onDeviceAttributes(MqttPublishMessage mqttMsg) throws AdaptorException {
    JsonElement json = validateJsonPayload(gatewaySessionId, mqttMsg.payload());
    int requestId = mqttMsg.variableHeader().messageId();
    if (json.isJsonObject()) {
        JsonObject jsonObj = json.getAsJsonObject();
        for (Map.Entry<String, JsonElement> deviceEntry : jsonObj.entrySet()) {
            String deviceName = checkDeviceConnected(deviceEntry.getKey());
            if (!deviceEntry.getValue().isJsonObject()) {
                throw new JsonSyntaxException(CAN_T_PARSE_VALUE + json);
            }
            long ts = System.currentTimeMillis();
            BasicUpdateAttributesRequest request = new BasicUpdateAttributesRequest(requestId);
            JsonObject deviceData = deviceEntry.getValue().getAsJsonObject();
            request.add(JsonConverter.parseValues(deviceData).stream().map(kv -> new BaseAttributeKvEntry(kv, ts)).collect(Collectors.toList()));
            GatewayDeviceSessionCtx deviceSessionCtx = devices.get(deviceName);
            processor.process(new BasicToDeviceActorSessionMsg(deviceSessionCtx.getDevice(), new BasicAdaptorToSessionActorMsg(deviceSessionCtx, request)));
        }
    } else {
        throw new JsonSyntaxException(CAN_T_PARSE_VALUE + json);
    }
}
Also used : JsonObject(com.google.gson.JsonObject) BasicAdaptorToSessionActorMsg(org.thingsboard.server.common.msg.session.BasicAdaptorToSessionActorMsg) JsonSyntaxException(com.google.gson.JsonSyntaxException) JsonElement(com.google.gson.JsonElement) BasicToDeviceActorSessionMsg(org.thingsboard.server.common.msg.session.BasicToDeviceActorSessionMsg) BaseAttributeKvEntry(org.thingsboard.server.common.data.kv.BaseAttributeKvEntry)

Aggregations

BasicAdaptorToSessionActorMsg (org.thingsboard.server.common.msg.session.BasicAdaptorToSessionActorMsg)6 BasicToDeviceActorSessionMsg (org.thingsboard.server.common.msg.session.BasicToDeviceActorSessionMsg)6 JsonElement (com.google.gson.JsonElement)4 JsonObject (com.google.gson.JsonObject)4 JsonSyntaxException (com.google.gson.JsonSyntaxException)4 JsonArray (com.google.gson.JsonArray)2 Device (org.thingsboard.server.common.data.Device)1 BaseAttributeKvEntry (org.thingsboard.server.common.data.kv.BaseAttributeKvEntry)1 EntityRelation (org.thingsboard.server.common.data.relation.EntityRelation)1 AdaptorToSessionActorMsg (org.thingsboard.server.common.msg.session.AdaptorToSessionActorMsg)1