Search in sources :

Example 1 with TransportPayloadType

use of org.thingsboard.server.common.data.TransportPayloadType in project thingsboard by thingsboard.

the class CoapTransportResource method processProvision.

private void processProvision(CoapExchange exchange) {
    exchange.accept();
    try {
        UUID sessionId = UUID.randomUUID();
        log.trace("[{}] Processing provision publish msg [{}]!", sessionId, exchange.advanced().getRequest());
        TransportProtos.ProvisionDeviceRequestMsg provisionRequestMsg;
        TransportPayloadType payloadType;
        try {
            provisionRequestMsg = transportContext.getJsonCoapAdaptor().convertToProvisionRequestMsg(sessionId, exchange.advanced().getRequest());
            payloadType = TransportPayloadType.JSON;
        } catch (Exception e) {
            if (e instanceof JsonParseException || (e.getCause() != null && e.getCause() instanceof JsonParseException)) {
                provisionRequestMsg = transportContext.getProtoCoapAdaptor().convertToProvisionRequestMsg(sessionId, exchange.advanced().getRequest());
                payloadType = TransportPayloadType.PROTOBUF;
            } else {
                throw new AdaptorException(e);
            }
        }
        transportService.process(provisionRequestMsg, new DeviceProvisionCallback(exchange, payloadType));
    } catch (AdaptorException e) {
        log.trace("Failed to decode message: ", e);
        exchange.respond(CoAP.ResponseCode.BAD_REQUEST);
    }
}
Also used : AdaptorException(org.thingsboard.server.common.transport.adaptor.AdaptorException) TransportPayloadType(org.thingsboard.server.common.data.TransportPayloadType) TransportProtos(org.thingsboard.server.gen.transport.TransportProtos) UUID(java.util.UUID) JsonParseException(com.google.gson.JsonParseException) JsonParseException(com.google.gson.JsonParseException) AdaptorException(org.thingsboard.server.common.transport.adaptor.AdaptorException)

Aggregations

JsonParseException (com.google.gson.JsonParseException)1 UUID (java.util.UUID)1 TransportPayloadType (org.thingsboard.server.common.data.TransportPayloadType)1 AdaptorException (org.thingsboard.server.common.transport.adaptor.AdaptorException)1 TransportProtos (org.thingsboard.server.gen.transport.TransportProtos)1