Search in sources :

Example 1 with AdaptorToSessionActorMsg

use of org.thingsboard.server.common.msg.session.AdaptorToSessionActorMsg 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 AdaptorToSessionActorMsg

use of org.thingsboard.server.common.msg.session.AdaptorToSessionActorMsg 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 3 with AdaptorToSessionActorMsg

use of org.thingsboard.server.common.msg.session.AdaptorToSessionActorMsg 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)

Example 4 with AdaptorToSessionActorMsg

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

the class DeviceApiController method process.

private void process(HttpSessionCtx ctx, FromDeviceMsg request) {
    AdaptorToSessionActorMsg msg = new BasicAdaptorToSessionActorMsg(ctx, request);
    processor.process(new BasicToDeviceActorSessionMsg(ctx.getDevice(), msg));
}
Also used : BasicAdaptorToSessionActorMsg(org.thingsboard.server.common.msg.session.BasicAdaptorToSessionActorMsg) AdaptorToSessionActorMsg(org.thingsboard.server.common.msg.session.AdaptorToSessionActorMsg) BasicToDeviceActorSessionMsg(org.thingsboard.server.common.msg.session.BasicToDeviceActorSessionMsg) BasicAdaptorToSessionActorMsg(org.thingsboard.server.common.msg.session.BasicAdaptorToSessionActorMsg)

Aggregations

AdaptorToSessionActorMsg (org.thingsboard.server.common.msg.session.AdaptorToSessionActorMsg)4 BasicToDeviceActorSessionMsg (org.thingsboard.server.common.msg.session.BasicToDeviceActorSessionMsg)4 AdaptorException (org.thingsboard.server.common.transport.adaptor.AdaptorException)3 MqttQoS (io.netty.handler.codec.mqtt.MqttQoS)1 ArrayList (java.util.ArrayList)1 BasicAdaptorToSessionActorMsg (org.thingsboard.server.common.msg.session.BasicAdaptorToSessionActorMsg)1