Search in sources :

Example 6 with AdaptorException

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

the class JsonMqttAdaptor method convertResponseMsg.

private MqttMessage convertResponseMsg(DeviceSessionCtx ctx, ToDeviceMsg msg, ResponseMsg<?> responseMsg, Optional<Exception> responseError) throws AdaptorException {
    MqttMessage result = null;
    MsgType requestMsgType = responseMsg.getRequestMsgType();
    Integer requestId = responseMsg.getRequestId();
    if (requestId >= 0) {
        if (requestMsgType == MsgType.POST_ATTRIBUTES_REQUEST || requestMsgType == MsgType.POST_TELEMETRY_REQUEST) {
            result = MqttTransportHandler.createMqttPubAckMsg(requestId);
        } else if (requestMsgType == MsgType.GET_ATTRIBUTES_REQUEST) {
            GetAttributesResponse response = (GetAttributesResponse) msg;
            Optional<AttributesKVMsg> responseData = response.getData();
            if (response.isSuccess() && responseData.isPresent()) {
                result = createMqttPublishMsg(ctx, MqttTopics.DEVICE_ATTRIBUTES_RESPONSE_TOPIC_PREFIX + requestId, responseData.get(), true);
            } else {
                if (responseError.isPresent()) {
                    throw new AdaptorException(responseError.get());
                }
            }
        }
    }
    return result;
}
Also used : AdaptorException(org.thingsboard.server.common.transport.adaptor.AdaptorException) Optional(java.util.Optional)

Example 7 with AdaptorException

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

the class JsonMqttAdaptor method convertToGetAttributesRequest.

private FromDeviceMsg convertToGetAttributesRequest(DeviceSessionCtx ctx, MqttPublishMessage inbound) throws AdaptorException {
    String topicName = inbound.variableHeader().topicName();
    try {
        Integer requestId = Integer.valueOf(topicName.substring(MqttTopics.DEVICE_ATTRIBUTES_REQUEST_TOPIC_PREFIX.length()));
        String payload = inbound.payload().toString(UTF8);
        JsonElement requestBody = new JsonParser().parse(payload);
        Set<String> clientKeys = toStringSet(requestBody, "clientKeys");
        Set<String> sharedKeys = toStringSet(requestBody, "sharedKeys");
        if (clientKeys == null && sharedKeys == null) {
            return new BasicGetAttributesRequest(requestId);
        } else {
            return new BasicGetAttributesRequest(requestId, clientKeys, sharedKeys);
        }
    } 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) JsonElement(com.google.gson.JsonElement) JsonParser(com.google.gson.JsonParser)

Example 8 with AdaptorException

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

the class JsonCoapAdaptor method convertToDeviceRpcResponse.

private FromDeviceMsg convertToDeviceRpcResponse(CoapSessionCtx ctx, Request inbound) throws AdaptorException {
    Optional<Integer> requestId = CoapTransportResource.getRequestId(inbound);
    String payload = validatePayload(ctx, inbound);
    JsonObject response = new JsonParser().parse(payload).getAsJsonObject();
    return new ToDeviceRpcResponseMsg(requestId.orElseThrow(() -> new AdaptorException("Request id is missing!")), response.get("response").toString());
}
Also used : AdaptorException(org.thingsboard.server.common.transport.adaptor.AdaptorException) JsonObject(com.google.gson.JsonObject) JsonParser(com.google.gson.JsonParser)

Example 9 with AdaptorException

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

the class CoapTransportResource method processRequest.

private Optional<SessionId> processRequest(CoapExchange exchange, MsgType type) {
    log.trace("Processing {}", exchange.advanced().getRequest());
    exchange.accept();
    Exchange advanced = exchange.advanced();
    Request request = advanced.getRequest();
    Optional<DeviceCredentialsFilter> credentials = decodeCredentials(request);
    if (!credentials.isPresent()) {
        exchange.respond(ResponseCode.BAD_REQUEST);
        return Optional.empty();
    }
    CoapSessionCtx ctx = new CoapSessionCtx(exchange, adaptor, processor, authService, timeout);
    if (!ctx.login(credentials.get())) {
        exchange.respond(ResponseCode.UNAUTHORIZED);
        return Optional.empty();
    }
    AdaptorToSessionActorMsg msg;
    try {
        switch(type) {
            case GET_ATTRIBUTES_REQUEST:
            case POST_ATTRIBUTES_REQUEST:
            case POST_TELEMETRY_REQUEST:
            case TO_DEVICE_RPC_RESPONSE:
            case TO_SERVER_RPC_REQUEST:
                ctx.setSessionType(SessionType.SYNC);
                msg = adaptor.convertToActorMsg(ctx, type, request);
                break;
            case SUBSCRIBE_ATTRIBUTES_REQUEST:
            case SUBSCRIBE_RPC_COMMANDS_REQUEST:
                ExchangeObserver systemObserver = (ExchangeObserver) observerField.get(advanced);
                advanced.setObserver(new CoapExchangeObserverProxy(systemObserver, ctx));
                ctx.setSessionType(SessionType.ASYNC);
                msg = adaptor.convertToActorMsg(ctx, type, request);
                break;
            case UNSUBSCRIBE_ATTRIBUTES_REQUEST:
            case UNSUBSCRIBE_RPC_COMMANDS_REQUEST:
                ctx.setSessionType(SessionType.ASYNC);
                msg = adaptor.convertToActorMsg(ctx, type, request);
                break;
            default:
                log.trace("[{}] Unsupported msg type: {}", ctx.getSessionId(), type);
                throw new IllegalArgumentException("Unsupported msg type: " + type);
        }
        log.trace("Processing msg: {}", msg);
        processor.process(new BasicToDeviceActorSessionMsg(ctx.getDevice(), msg));
    } catch (AdaptorException e) {
        log.debug("Failed to decode payload {}", e);
        exchange.respond(ResponseCode.BAD_REQUEST, e.getMessage());
        return Optional.empty();
    } catch (IllegalArgumentException | IllegalAccessException e) {
        log.debug("Failed to process payload {}", e);
        exchange.respond(ResponseCode.INTERNAL_SERVER_ERROR, e.getMessage());
    }
    return Optional.of(ctx.getSessionId());
}
Also used : Request(org.eclipse.californium.core.coap.Request) CoapExchangeObserverProxy(org.thingsboard.server.transport.coap.session.CoapExchangeObserverProxy) CoapExchange(org.eclipse.californium.core.server.resources.CoapExchange) Exchange(org.eclipse.californium.core.network.Exchange) DeviceCredentialsFilter(org.thingsboard.server.common.data.security.DeviceCredentialsFilter) AdaptorException(org.thingsboard.server.common.transport.adaptor.AdaptorException) CoapSessionCtx(org.thingsboard.server.transport.coap.session.CoapSessionCtx) ExchangeObserver(org.eclipse.californium.core.network.ExchangeObserver)

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