Search in sources :

Example 1 with AdaptorException

use of org.thingsboard.server.common.transport.adaptor.AdaptorException 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 AdaptorException

use of org.thingsboard.server.common.transport.adaptor.AdaptorException in project thingsboard by thingsboard.

the class JsonMqttAdaptor method convertToRpcCommandResponse.

private FromDeviceMsg convertToRpcCommandResponse(DeviceSessionCtx ctx, MqttPublishMessage inbound) throws AdaptorException {
    String topicName = inbound.variableHeader().topicName();
    try {
        Integer requestId = Integer.valueOf(topicName.substring(MqttTopics.DEVICE_RPC_RESPONSE_TOPIC.length()));
        String payload = inbound.payload().toString(UTF8);
        return new ToDeviceRpcResponseMsg(requestId, payload);
    } catch (RuntimeException e) {
        log.warn("Failed to decode get attributes request", e);
        throw new AdaptorException(e);
    }
}
Also used : AdaptorException(org.thingsboard.server.common.transport.adaptor.AdaptorException)

Example 3 with AdaptorException

use of org.thingsboard.server.common.transport.adaptor.AdaptorException 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 4 with AdaptorException

use of org.thingsboard.server.common.transport.adaptor.AdaptorException 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 5 with AdaptorException

use of org.thingsboard.server.common.transport.adaptor.AdaptorException in project thingsboard by thingsboard.

the class JsonMqttAdaptor method convertToServerRpcRequest.

private FromDeviceMsg convertToServerRpcRequest(DeviceSessionCtx ctx, MqttPublishMessage inbound) throws AdaptorException {
    String topicName = inbound.variableHeader().topicName();
    String payload = validatePayload(ctx.getSessionId(), inbound.payload());
    try {
        Integer requestId = Integer.valueOf(topicName.substring(MqttTopics.DEVICE_RPC_REQUESTS_TOPIC.length()));
        return JsonConverter.convertToServerRpcRequest(new JsonParser().parse(payload), requestId);
    } catch (IllegalStateException | JsonSyntaxException ex) {
        throw new AdaptorException(ex);
    }
}
Also used : JsonSyntaxException(com.google.gson.JsonSyntaxException) AdaptorException(org.thingsboard.server.common.transport.adaptor.AdaptorException) JsonParser(com.google.gson.JsonParser)

Aggregations

AdaptorException (org.thingsboard.server.common.transport.adaptor.AdaptorException)9 JsonParser (com.google.gson.JsonParser)3 AdaptorToSessionActorMsg (org.thingsboard.server.common.msg.session.AdaptorToSessionActorMsg)3 BasicToDeviceActorSessionMsg (org.thingsboard.server.common.msg.session.BasicToDeviceActorSessionMsg)3 JsonElement (com.google.gson.JsonElement)1 JsonObject (com.google.gson.JsonObject)1 JsonSyntaxException (com.google.gson.JsonSyntaxException)1 MqttQoS (io.netty.handler.codec.mqtt.MqttQoS)1 ArrayList (java.util.ArrayList)1 Optional (java.util.Optional)1 Request (org.eclipse.californium.core.coap.Request)1 Exchange (org.eclipse.californium.core.network.Exchange)1 ExchangeObserver (org.eclipse.californium.core.network.ExchangeObserver)1 CoapExchange (org.eclipse.californium.core.server.resources.CoapExchange)1 DeviceCredentialsFilter (org.thingsboard.server.common.data.security.DeviceCredentialsFilter)1 CoapExchangeObserverProxy (org.thingsboard.server.transport.coap.session.CoapExchangeObserverProxy)1 CoapSessionCtx (org.thingsboard.server.transport.coap.session.CoapSessionCtx)1