Search in sources :

Example 1 with BasicToDeviceActorSessionMsg

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

the class MqttTransportHandler method processUnsubscribe.

private void processUnsubscribe(ChannelHandlerContext ctx, MqttUnsubscribeMessage mqttMsg) {
    if (!checkConnected(ctx)) {
        return;
    }
    log.trace("[{}] Processing subscription [{}]!", sessionId, mqttMsg.variableHeader().messageId());
    for (String topicName : mqttMsg.payload().topics()) {
        try {
            if (topicName.equals(DEVICE_ATTRIBUTES_TOPIC)) {
                AdaptorToSessionActorMsg msg = adaptor.convertToActorMsg(deviceSessionCtx, UNSUBSCRIBE_ATTRIBUTES_REQUEST, mqttMsg);
                processor.process(new BasicToDeviceActorSessionMsg(deviceSessionCtx.getDevice(), msg));
            } else if (topicName.equals(DEVICE_RPC_REQUESTS_SUB_TOPIC)) {
                AdaptorToSessionActorMsg msg = adaptor.convertToActorMsg(deviceSessionCtx, UNSUBSCRIBE_RPC_COMMANDS_REQUEST, mqttMsg);
                processor.process(new BasicToDeviceActorSessionMsg(deviceSessionCtx.getDevice(), msg));
            } else if (topicName.equals(DEVICE_ATTRIBUTES_RESPONSES_TOPIC)) {
                deviceSessionCtx.setDisallowAttributeResponses();
            }
        } catch (AdaptorException e) {
            log.warn("[{}] Failed to process unsubscription [{}] to [{}]", sessionId, mqttMsg.variableHeader().messageId(), topicName);
        }
    }
    ctx.writeAndFlush(createUnSubAckMessage(mqttMsg.variableHeader().messageId()));
}
Also used : AdaptorException(org.thingsboard.server.common.transport.adaptor.AdaptorException) AdaptorToSessionActorMsg(org.thingsboard.server.common.msg.session.AdaptorToSessionActorMsg) BasicToDeviceActorSessionMsg(org.thingsboard.server.common.msg.session.BasicToDeviceActorSessionMsg)

Example 2 with BasicToDeviceActorSessionMsg

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

the class GatewaySessionCtx method onDeviceConnect.

private void onDeviceConnect(String deviceName, String deviceType) {
    if (!devices.containsKey(deviceName)) {
        Device device = deviceService.findDeviceByTenantIdAndName(gateway.getTenantId(), deviceName);
        if (device == null) {
            device = new Device();
            device.setTenantId(gateway.getTenantId());
            device.setName(deviceName);
            device.setType(deviceType);
            device = deviceService.saveDevice(device);
            relationService.saveRelationAsync(new EntityRelation(gateway.getId(), device.getId(), "Created"));
        }
        GatewayDeviceSessionCtx ctx = new GatewayDeviceSessionCtx(this, device);
        devices.put(deviceName, ctx);
        log.debug("[{}] Added device [{}] to the gateway session", gatewaySessionId, deviceName);
        processor.process(new BasicToDeviceActorSessionMsg(device, new BasicAdaptorToSessionActorMsg(ctx, new AttributesSubscribeMsg())));
        processor.process(new BasicToDeviceActorSessionMsg(device, new BasicAdaptorToSessionActorMsg(ctx, new RpcSubscribeMsg())));
    }
}
Also used : EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) Device(org.thingsboard.server.common.data.Device) BasicToDeviceActorSessionMsg(org.thingsboard.server.common.msg.session.BasicToDeviceActorSessionMsg) BasicAdaptorToSessionActorMsg(org.thingsboard.server.common.msg.session.BasicAdaptorToSessionActorMsg)

Example 3 with BasicToDeviceActorSessionMsg

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

the class GatewaySessionCtx method onDeviceRpcResponse.

public void onDeviceRpcResponse(MqttPublishMessage mqttMsg) throws AdaptorException {
    JsonElement json = validateJsonPayload(gatewaySessionId, mqttMsg.payload());
    if (json.isJsonObject()) {
        JsonObject jsonObj = json.getAsJsonObject();
        String deviceName = checkDeviceConnected(jsonObj.get(DEVICE_PROPERTY).getAsString());
        Integer requestId = jsonObj.get("id").getAsInt();
        String data = jsonObj.get("data").toString();
        GatewayDeviceSessionCtx deviceSessionCtx = devices.get(deviceName);
        processor.process(new BasicToDeviceActorSessionMsg(deviceSessionCtx.getDevice(), new BasicAdaptorToSessionActorMsg(deviceSessionCtx, new ToDeviceRpcResponseMsg(requestId, data))));
    } else {
        throw new JsonSyntaxException(CAN_T_PARSE_VALUE + json);
    }
}
Also used : JsonSyntaxException(com.google.gson.JsonSyntaxException) JsonElement(com.google.gson.JsonElement) BasicToDeviceActorSessionMsg(org.thingsboard.server.common.msg.session.BasicToDeviceActorSessionMsg) JsonObject(com.google.gson.JsonObject) BasicAdaptorToSessionActorMsg(org.thingsboard.server.common.msg.session.BasicAdaptorToSessionActorMsg)

Example 4 with BasicToDeviceActorSessionMsg

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

the class MqttTransportHandler method processDevicePublish.

private void processDevicePublish(ChannelHandlerContext ctx, MqttPublishMessage mqttMsg, String topicName, int msgId) {
    AdaptorToSessionActorMsg msg = null;
    try {
        if (topicName.equals(DEVICE_TELEMETRY_TOPIC)) {
            msg = adaptor.convertToActorMsg(deviceSessionCtx, POST_TELEMETRY_REQUEST, mqttMsg);
        } else if (topicName.equals(DEVICE_ATTRIBUTES_TOPIC)) {
            msg = adaptor.convertToActorMsg(deviceSessionCtx, POST_ATTRIBUTES_REQUEST, mqttMsg);
        } else if (topicName.startsWith(DEVICE_ATTRIBUTES_REQUEST_TOPIC_PREFIX)) {
            msg = adaptor.convertToActorMsg(deviceSessionCtx, GET_ATTRIBUTES_REQUEST, mqttMsg);
            if (msgId >= 0) {
                ctx.writeAndFlush(createMqttPubAckMsg(msgId));
            }
        } else if (topicName.startsWith(DEVICE_RPC_RESPONSE_TOPIC)) {
            msg = adaptor.convertToActorMsg(deviceSessionCtx, TO_DEVICE_RPC_RESPONSE, mqttMsg);
            if (msgId >= 0) {
                ctx.writeAndFlush(createMqttPubAckMsg(msgId));
            }
        } else if (topicName.startsWith(DEVICE_RPC_REQUESTS_TOPIC)) {
            msg = adaptor.convertToActorMsg(deviceSessionCtx, TO_SERVER_RPC_REQUEST, mqttMsg);
            if (msgId >= 0) {
                ctx.writeAndFlush(createMqttPubAckMsg(msgId));
            }
        }
    } catch (AdaptorException e) {
        log.warn("[{}] Failed to process publish msg [{}][{}]", sessionId, topicName, msgId, e);
    }
    if (msg != null) {
        processor.process(new BasicToDeviceActorSessionMsg(deviceSessionCtx.getDevice(), msg));
    } else {
        log.info("[{}] Closing current session due to invalid publish msg [{}][{}]", sessionId, topicName, msgId);
        ctx.close();
    }
}
Also used : AdaptorException(org.thingsboard.server.common.transport.adaptor.AdaptorException) AdaptorToSessionActorMsg(org.thingsboard.server.common.msg.session.AdaptorToSessionActorMsg) BasicToDeviceActorSessionMsg(org.thingsboard.server.common.msg.session.BasicToDeviceActorSessionMsg)

Example 5 with BasicToDeviceActorSessionMsg

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

the class MqttTransportHandler method processSubscribe.

private void processSubscribe(ChannelHandlerContext ctx, MqttSubscribeMessage mqttMsg) {
    if (!checkConnected(ctx)) {
        return;
    }
    log.trace("[{}] Processing subscription [{}]!", sessionId, mqttMsg.variableHeader().messageId());
    List<Integer> grantedQoSList = new ArrayList<>();
    for (MqttTopicSubscription subscription : mqttMsg.payload().topicSubscriptions()) {
        String topicName = subscription.topicName();
        // TODO: handle this qos level.
        MqttQoS reqQoS = subscription.qualityOfService();
        try {
            if (topicName.equals(DEVICE_ATTRIBUTES_TOPIC)) {
                AdaptorToSessionActorMsg msg = adaptor.convertToActorMsg(deviceSessionCtx, SUBSCRIBE_ATTRIBUTES_REQUEST, mqttMsg);
                processor.process(new BasicToDeviceActorSessionMsg(deviceSessionCtx.getDevice(), msg));
                grantedQoSList.add(getMinSupportedQos(reqQoS));
            } else if (topicName.equals(DEVICE_RPC_REQUESTS_SUB_TOPIC)) {
                AdaptorToSessionActorMsg msg = adaptor.convertToActorMsg(deviceSessionCtx, SUBSCRIBE_RPC_COMMANDS_REQUEST, mqttMsg);
                processor.process(new BasicToDeviceActorSessionMsg(deviceSessionCtx.getDevice(), msg));
                grantedQoSList.add(getMinSupportedQos(reqQoS));
            } else if (topicName.equals(DEVICE_RPC_RESPONSE_SUB_TOPIC)) {
                grantedQoSList.add(getMinSupportedQos(reqQoS));
            } else if (topicName.equals(DEVICE_ATTRIBUTES_RESPONSES_TOPIC)) {
                deviceSessionCtx.setAllowAttributeResponses();
                grantedQoSList.add(getMinSupportedQos(reqQoS));
            } else if (topicName.equals(GATEWAY_ATTRIBUTES_TOPIC)) {
                grantedQoSList.add(getMinSupportedQos(reqQoS));
            } else {
                log.warn("[{}] Failed to subscribe to [{}][{}]", sessionId, topicName, reqQoS);
                grantedQoSList.add(FAILURE.value());
            }
        } catch (AdaptorException e) {
            log.warn("[{}] Failed to subscribe to [{}][{}]", sessionId, topicName, reqQoS);
            grantedQoSList.add(FAILURE.value());
        }
    }
    ctx.writeAndFlush(createSubAckMessage(mqttMsg.variableHeader().messageId(), grantedQoSList));
}
Also used : AdaptorException(org.thingsboard.server.common.transport.adaptor.AdaptorException) AdaptorToSessionActorMsg(org.thingsboard.server.common.msg.session.AdaptorToSessionActorMsg) BasicToDeviceActorSessionMsg(org.thingsboard.server.common.msg.session.BasicToDeviceActorSessionMsg) ArrayList(java.util.ArrayList) MqttQoS(io.netty.handler.codec.mqtt.MqttQoS)

Aggregations

BasicToDeviceActorSessionMsg (org.thingsboard.server.common.msg.session.BasicToDeviceActorSessionMsg)9 BasicAdaptorToSessionActorMsg (org.thingsboard.server.common.msg.session.BasicAdaptorToSessionActorMsg)6 JsonElement (com.google.gson.JsonElement)4 JsonObject (com.google.gson.JsonObject)4 JsonSyntaxException (com.google.gson.JsonSyntaxException)4 AdaptorToSessionActorMsg (org.thingsboard.server.common.msg.session.AdaptorToSessionActorMsg)4 AdaptorException (org.thingsboard.server.common.transport.adaptor.AdaptorException)3 JsonArray (com.google.gson.JsonArray)2 MqttQoS (io.netty.handler.codec.mqtt.MqttQoS)1 ArrayList (java.util.ArrayList)1 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