Search in sources :

Example 1 with CoapEfentoCallback

use of org.thingsboard.server.transport.coap.callback.CoapEfentoCallback in project thingsboard by thingsboard.

the class CoapEfentoTransportResource method processMeasurementsRequest.

private void processMeasurementsRequest(CoapExchange exchange, Request request) {
    byte[] bytes = request.getPayload();
    try {
        MeasurementsProtos.ProtoMeasurements protoMeasurements = MeasurementsProtos.ProtoMeasurements.parseFrom(bytes);
        log.trace("Successfully parsed Efento ProtoMeasurements: [{}]", protoMeasurements.getCloudToken());
        String token = protoMeasurements.getCloudToken();
        transportService.process(DeviceTransportType.COAP, TransportProtos.ValidateDeviceTokenRequestMsg.newBuilder().setToken(token).build(), new CoapDeviceAuthCallback(exchange, (msg, deviceProfile) -> {
            TransportProtos.SessionInfoProto sessionInfo = SessionInfoCreator.create(msg, transportContext, UUID.randomUUID());
            UUID sessionId = new UUID(sessionInfo.getSessionIdMSB(), sessionInfo.getSessionIdLSB());
            try {
                validateEfentoTransportConfiguration(deviceProfile);
                List<EfentoMeasurements> efentoMeasurements = getEfentoMeasurements(protoMeasurements, sessionId);
                transportService.process(sessionInfo, transportContext.getEfentoCoapAdaptor().convertToPostTelemetry(sessionId, efentoMeasurements), new CoapEfentoCallback(exchange, CoAP.ResponseCode.CREATED, CoAP.ResponseCode.INTERNAL_SERVER_ERROR));
                reportSubscriptionInfo(sessionInfo, false, false);
            } catch (AdaptorException e) {
                log.error("[{}] Failed to decode Efento ProtoMeasurements: ", sessionId, e);
                exchange.respond(CoAP.ResponseCode.BAD_REQUEST);
            }
        }));
    } catch (Exception e) {
        log.error("Failed to decode Efento ProtoMeasurements: ", e);
        exchange.respond(CoAP.ResponseCode.INTERNAL_SERVER_ERROR);
    }
}
Also used : CONFIGURATION(org.thingsboard.server.transport.coap.CoapTransportService.CONFIGURATION) JsonObject(com.google.gson.JsonObject) CoapExchange(org.eclipse.californium.core.server.resources.CoapExchange) Response(org.eclipse.californium.core.coap.Response) Exchange(org.eclipse.californium.core.network.Exchange) CoapDeviceAuthCallback(org.thingsboard.server.transport.coap.callback.CoapDeviceAuthCallback) MeasurementTypeProtos(org.thingsboard.server.gen.transport.coap.MeasurementTypeProtos) EfentoCoapDeviceTypeConfiguration(org.thingsboard.server.common.data.device.profile.EfentoCoapDeviceTypeConfiguration) ByteBuffer(java.nio.ByteBuffer) ArrayList(java.util.ArrayList) CoapEfentoUtils(org.thingsboard.server.transport.coap.efento.utils.CoapEfentoUtils) Resource(org.eclipse.californium.core.server.resources.Resource) CURRENT_TIMESTAMP(org.thingsboard.server.transport.coap.CoapTransportService.CURRENT_TIMESTAMP) DEVICE_INFO(org.thingsboard.server.transport.coap.CoapTransportService.DEVICE_INFO) AbstractCoapTransportResource(org.thingsboard.server.transport.coap.AbstractCoapTransportResource) Map(java.util.Map) MEASUREMENTS(org.thingsboard.server.transport.coap.CoapTransportService.MEASUREMENTS) DeviceTransportType(org.thingsboard.server.common.data.DeviceTransportType) MeasurementsProtos(org.thingsboard.server.gen.transport.coap.MeasurementsProtos) CoapOkCallback(org.thingsboard.server.transport.coap.callback.CoapOkCallback) DeviceProfile(org.thingsboard.server.common.data.DeviceProfile) CoapTransportContext(org.thingsboard.server.transport.coap.CoapTransportContext) UUID(java.util.UUID) TimeUnit(java.util.concurrent.TimeUnit) Slf4j(lombok.extern.slf4j.Slf4j) AdaptorException(org.thingsboard.server.common.transport.adaptor.AdaptorException) List(java.util.List) Request(org.eclipse.californium.core.coap.Request) CoapEfentoCallback(org.thingsboard.server.transport.coap.callback.CoapEfentoCallback) TreeMap(java.util.TreeMap) CoAP(org.eclipse.californium.core.coap.CoAP) CoapDeviceProfileTransportConfiguration(org.thingsboard.server.common.data.device.profile.CoapDeviceProfileTransportConfiguration) CollectionUtils(org.springframework.util.CollectionUtils) Data(lombok.Data) DeviceProfileTransportConfiguration(org.thingsboard.server.common.data.device.profile.DeviceProfileTransportConfiguration) TransportProtos(org.thingsboard.server.gen.transport.TransportProtos) AllArgsConstructor(lombok.AllArgsConstructor) SessionInfoCreator(org.thingsboard.server.common.transport.auth.SessionInfoCreator) AdaptorException(org.thingsboard.server.common.transport.adaptor.AdaptorException) ArrayList(java.util.ArrayList) List(java.util.List) UUID(java.util.UUID) CoapEfentoCallback(org.thingsboard.server.transport.coap.callback.CoapEfentoCallback) MeasurementsProtos(org.thingsboard.server.gen.transport.coap.MeasurementsProtos) CoapDeviceAuthCallback(org.thingsboard.server.transport.coap.callback.CoapDeviceAuthCallback) AdaptorException(org.thingsboard.server.common.transport.adaptor.AdaptorException)

Aggregations

JsonObject (com.google.gson.JsonObject)1 ByteBuffer (java.nio.ByteBuffer)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 UUID (java.util.UUID)1 TimeUnit (java.util.concurrent.TimeUnit)1 AllArgsConstructor (lombok.AllArgsConstructor)1 Data (lombok.Data)1 Slf4j (lombok.extern.slf4j.Slf4j)1 CoAP (org.eclipse.californium.core.coap.CoAP)1 Request (org.eclipse.californium.core.coap.Request)1 Response (org.eclipse.californium.core.coap.Response)1 Exchange (org.eclipse.californium.core.network.Exchange)1 CoapExchange (org.eclipse.californium.core.server.resources.CoapExchange)1 Resource (org.eclipse.californium.core.server.resources.Resource)1 CollectionUtils (org.springframework.util.CollectionUtils)1 DeviceProfile (org.thingsboard.server.common.data.DeviceProfile)1 DeviceTransportType (org.thingsboard.server.common.data.DeviceTransportType)1