Search in sources :

Example 1 with CoapOkCallback

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

the class DefaultCoapClientContext method cancelRpcSubscription.

private void cancelRpcSubscription(TbCoapClientState state) {
    if (state.getRpc() != null) {
        clientsByToken.remove(state.getRpc().getToken());
        CoapExchange exchange = state.getRpc().getExchange();
        state.setRpc(null);
        transportService.process(state.getSession(), TransportProtos.SubscribeToRPCMsg.newBuilder().setUnsubscribe(true).build(), new CoapOkCallback(exchange, CoAP.ResponseCode.DELETED, CoAP.ResponseCode.INTERNAL_SERVER_ERROR));
        if (state.getAttrs() == null) {
            closeAndCleanup(state);
        }
    }
}
Also used : CoapOkCallback(org.thingsboard.server.transport.coap.callback.CoapOkCallback) CoapExchange(org.eclipse.californium.core.server.resources.CoapExchange)

Example 2 with CoapOkCallback

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

the class DefaultCoapClientContext method registerFeatureObservation.

private boolean registerFeatureObservation(TbCoapClientState state, String token, CoapExchange exchange, FeatureType featureType) {
    state.lock();
    try {
        boolean newObservation;
        if (FeatureType.ATTRIBUTES.equals(featureType)) {
            if (state.getAttrs() == null) {
                newObservation = true;
                state.setAttrs(new TbCoapObservationState(exchange, token));
            } else {
                newObservation = !state.getAttrs().getToken().equals(token);
                if (newObservation) {
                    TbCoapObservationState old = state.getAttrs();
                    state.setAttrs(new TbCoapObservationState(exchange, token));
                    old.getExchange().respond(CoAP.ResponseCode.DELETED);
                }
            }
        } else {
            if (state.getRpc() == null) {
                newObservation = true;
                state.setRpc(new TbCoapObservationState(exchange, token));
            } else {
                newObservation = !state.getRpc().getToken().equals(token);
                if (newObservation) {
                    TbCoapObservationState old = state.getRpc();
                    state.setRpc(new TbCoapObservationState(exchange, token));
                    old.getExchange().respond(CoAP.ResponseCode.DELETED);
                }
            }
        }
        if (newObservation) {
            clientsByToken.put(token, state);
            if (state.getSession() == null) {
                TransportProtos.SessionInfoProto session = SessionInfoCreator.create(state.getCredentials(), transportContext, UUID.randomUUID());
                state.setSession(session);
                CoapSessionListener listener = new CoapSessionListener(state);
                state.setListener(listener);
                transportService.registerAsyncSession(session, state.getListener());
                transportService.process(session, getSessionEventMsg(TransportProtos.SessionEvent.OPEN), null);
            }
            if (FeatureType.ATTRIBUTES.equals(featureType)) {
                transportService.process(state.getSession(), TransportProtos.SubscribeToAttributeUpdatesMsg.getDefaultInstance(), new CoapNoOpCallback(exchange));
                transportService.process(state.getSession(), TransportProtos.GetAttributeRequestMsg.newBuilder().setOnlyShared(true).build(), new CoapNoOpCallback(exchange));
            } else {
                transportService.process(state.getSession(), TransportProtos.SubscribeToRPCMsg.getDefaultInstance(), new CoapOkCallback(exchange, CoAP.ResponseCode.VALID, CoAP.ResponseCode.INTERNAL_SERVER_ERROR));
            }
        }
        return newObservation;
    } finally {
        state.unlock();
    }
}
Also used : CoapNoOpCallback(org.thingsboard.server.transport.coap.callback.CoapNoOpCallback) CoapOkCallback(org.thingsboard.server.transport.coap.callback.CoapOkCallback) TransportProtos(org.thingsboard.server.gen.transport.TransportProtos)

Example 3 with CoapOkCallback

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

the class CoapTransportResource method handlePostAttributesRequest.

private void handlePostAttributesRequest(TbCoapClientState clientState, CoapExchange exchange, Request request) throws AdaptorException {
    TransportProtos.SessionInfoProto sessionInfo = clients.getNewSyncSession(clientState);
    UUID sessionId = toSessionId(sessionInfo);
    transportService.process(sessionInfo, clientState.getAdaptor().convertToPostAttributes(sessionId, request, clientState.getConfiguration().getAttributesMsgDescriptor()), new CoapOkCallback(exchange, CoAP.ResponseCode.CREATED, CoAP.ResponseCode.INTERNAL_SERVER_ERROR));
}
Also used : CoapOkCallback(org.thingsboard.server.transport.coap.callback.CoapOkCallback) TransportProtos(org.thingsboard.server.gen.transport.TransportProtos) UUID(java.util.UUID)

Example 4 with CoapOkCallback

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

the class CoapTransportResource method handlePostTelemetryRequest.

private void handlePostTelemetryRequest(TbCoapClientState clientState, CoapExchange exchange, Request request) throws AdaptorException {
    TransportProtos.SessionInfoProto sessionInfo = clients.getNewSyncSession(clientState);
    UUID sessionId = toSessionId(sessionInfo);
    transportService.process(sessionInfo, clientState.getAdaptor().convertToPostTelemetry(sessionId, request, clientState.getConfiguration().getTelemetryMsgDescriptor()), new CoapOkCallback(exchange, CoAP.ResponseCode.CREATED, CoAP.ResponseCode.INTERNAL_SERVER_ERROR));
}
Also used : CoapOkCallback(org.thingsboard.server.transport.coap.callback.CoapOkCallback) TransportProtos(org.thingsboard.server.gen.transport.TransportProtos) UUID(java.util.UUID)

Example 5 with CoapOkCallback

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

the class CoapTransportResource method handleClaimRequest.

private void handleClaimRequest(TbCoapClientState clientState, CoapExchange exchange, Request request) throws AdaptorException {
    TransportProtos.SessionInfoProto sessionInfo = clients.getNewSyncSession(clientState);
    UUID sessionId = toSessionId(sessionInfo);
    transportService.process(sessionInfo, clientState.getAdaptor().convertToClaimDevice(sessionId, request, sessionInfo), new CoapOkCallback(exchange, CoAP.ResponseCode.CREATED, CoAP.ResponseCode.INTERNAL_SERVER_ERROR));
}
Also used : CoapOkCallback(org.thingsboard.server.transport.coap.callback.CoapOkCallback) TransportProtos(org.thingsboard.server.gen.transport.TransportProtos) UUID(java.util.UUID)

Aggregations

CoapOkCallback (org.thingsboard.server.transport.coap.callback.CoapOkCallback)7 TransportProtos (org.thingsboard.server.gen.transport.TransportProtos)5 UUID (java.util.UUID)4 CoapExchange (org.eclipse.californium.core.server.resources.CoapExchange)2 CoapNoOpCallback (org.thingsboard.server.transport.coap.callback.CoapNoOpCallback)1