Search in sources :

Example 1 with CoapNoOpCallback

use of org.thingsboard.server.transport.coap.callback.CoapNoOpCallback 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 2 with CoapNoOpCallback

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

the class CoapTransportResource method handleToServerRpcRequest.

private void handleToServerRpcRequest(TbCoapClientState clientState, CoapExchange exchange, Request request) throws AdaptorException {
    TransportProtos.SessionInfoProto sessionInfo = clients.getNewSyncSession(clientState);
    UUID sessionId = toSessionId(sessionInfo);
    transportService.registerSyncSession(sessionInfo, new ToServerRpcSyncSessionCallback(clientState, exchange, request), timeout);
    transportService.process(sessionInfo, clientState.getAdaptor().convertToServerRpcRequest(sessionId, request), new CoapNoOpCallback(exchange));
}
Also used : ToServerRpcSyncSessionCallback(org.thingsboard.server.transport.coap.callback.ToServerRpcSyncSessionCallback) CoapNoOpCallback(org.thingsboard.server.transport.coap.callback.CoapNoOpCallback) TransportProtos(org.thingsboard.server.gen.transport.TransportProtos) UUID(java.util.UUID)

Example 3 with CoapNoOpCallback

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

the class CoapTransportResource method handleGetAttributesRequest.

private void handleGetAttributesRequest(TbCoapClientState clientState, CoapExchange exchange, Request request) throws AdaptorException {
    TransportProtos.SessionInfoProto sessionInfo = clients.getNewSyncSession(clientState);
    UUID sessionId = toSessionId(sessionInfo);
    transportService.registerSyncSession(sessionInfo, new GetAttributesSyncSessionCallback(clientState, exchange, request), timeout);
    transportService.process(sessionInfo, clientState.getAdaptor().convertToGetAttributes(sessionId, request), new CoapNoOpCallback(exchange));
}
Also used : CoapNoOpCallback(org.thingsboard.server.transport.coap.callback.CoapNoOpCallback) GetAttributesSyncSessionCallback(org.thingsboard.server.transport.coap.callback.GetAttributesSyncSessionCallback) TransportProtos(org.thingsboard.server.gen.transport.TransportProtos) UUID(java.util.UUID)

Aggregations

TransportProtos (org.thingsboard.server.gen.transport.TransportProtos)3 CoapNoOpCallback (org.thingsboard.server.transport.coap.callback.CoapNoOpCallback)3 UUID (java.util.UUID)2 CoapOkCallback (org.thingsboard.server.transport.coap.callback.CoapOkCallback)1 GetAttributesSyncSessionCallback (org.thingsboard.server.transport.coap.callback.GetAttributesSyncSessionCallback)1 ToServerRpcSyncSessionCallback (org.thingsboard.server.transport.coap.callback.ToServerRpcSyncSessionCallback)1