coap: final version
This commit is contained in:
		
							parent
							
								
									cdc6daa7a9
								
							
						
					
					
						commit
						41adbe1003
					
				@ -25,6 +25,7 @@ import org.awaitility.Awaitility;
 | 
			
		||||
import org.eclipse.californium.core.CoapObserveRelation;
 | 
			
		||||
import org.eclipse.californium.core.CoapResponse;
 | 
			
		||||
import org.eclipse.californium.core.coap.CoAP;
 | 
			
		||||
import org.eclipse.californium.core.coap.CoAP.ResponseCode;
 | 
			
		||||
import org.springframework.beans.factory.annotation.Autowired;
 | 
			
		||||
import org.thingsboard.common.util.JacksonUtil;
 | 
			
		||||
import org.thingsboard.server.common.data.DynamicProtoUtils;
 | 
			
		||||
@ -238,7 +239,7 @@ public abstract class AbstractCoapAttributesIntegrationTest extends AbstractCoap
 | 
			
		||||
        String awaitAlias = "await Json Test Subscribe To AttributesUpdates (client.getObserveRelation)";
 | 
			
		||||
        await(awaitAlias)
 | 
			
		||||
                .atMost(DEFAULT_WAIT_TIMEOUT_SECONDS, TimeUnit.SECONDS)
 | 
			
		||||
                .until(() -> CoAP.ResponseCode.CONTENT.equals(callbackCoap.getResponseCode()) &&
 | 
			
		||||
                .until(() -> ResponseCode.VALID.equals(callbackCoap.getResponseCode()) &&
 | 
			
		||||
                        callbackCoap.getObserve() != null &&
 | 
			
		||||
                        0 == callbackCoap.getObserve().intValue());
 | 
			
		||||
        if (emptyCurrentStateNotification) {
 | 
			
		||||
@ -285,7 +286,7 @@ public abstract class AbstractCoapAttributesIntegrationTest extends AbstractCoap
 | 
			
		||||
        CoapObserveRelation observeRelation = client.getObserveRelation(callbackCoap);
 | 
			
		||||
        await(awaitAlias)
 | 
			
		||||
                .atMost(DEFAULT_WAIT_TIMEOUT_SECONDS, TimeUnit.SECONDS)
 | 
			
		||||
                .until(() -> CoAP.ResponseCode.CONTENT.equals(callbackCoap.getResponseCode()) &&
 | 
			
		||||
                .until(() -> ResponseCode.VALID.equals(callbackCoap.getResponseCode()) &&
 | 
			
		||||
                        callbackCoap.getObserve() != null &&
 | 
			
		||||
                        0 == callbackCoap.getObserve().intValue());
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -19,6 +19,7 @@ import lombok.extern.slf4j.Slf4j;
 | 
			
		||||
import org.eclipse.californium.core.server.resources.Resource;
 | 
			
		||||
import org.junit.After;
 | 
			
		||||
import org.junit.Before;
 | 
			
		||||
import org.junit.Ignore;
 | 
			
		||||
import org.junit.Test;
 | 
			
		||||
import org.springframework.beans.factory.annotation.Autowired;
 | 
			
		||||
import org.thingsboard.server.coapserver.DefaultCoapServerService;
 | 
			
		||||
@ -59,11 +60,15 @@ public class CoapAttributesUpdatesIntegrationTest extends AbstractCoapAttributes
 | 
			
		||||
        processAfterTest();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    @Ignore // Uncomment when Californium 3.11 is released with https://github.com/eclipse-californium/californium/pull/2215
 | 
			
		||||
    @Test
 | 
			
		||||
    public void testSubscribeToAttributesUpdatesFromTheServer() throws Exception {
 | 
			
		||||
        processJsonTestSubscribeToAttributesUpdates(false);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Ignore // Uncomment when Californium 3.11 is released with https://github.com/eclipse-californium/californium/pull/2215
 | 
			
		||||
    @Test
 | 
			
		||||
    public void testSubscribeToAttributesUpdatesFromTheServerWithEmptyCurrentStateNotification() throws Exception {
 | 
			
		||||
        processJsonTestSubscribeToAttributesUpdates(true);
 | 
			
		||||
 | 
			
		||||
@ -18,6 +18,7 @@ package org.thingsboard.server.transport.coap.attributes.updates;
 | 
			
		||||
import lombok.extern.slf4j.Slf4j;
 | 
			
		||||
import org.junit.After;
 | 
			
		||||
import org.junit.Before;
 | 
			
		||||
import org.junit.Ignore;
 | 
			
		||||
import org.junit.Test;
 | 
			
		||||
import org.thingsboard.server.common.data.CoapDeviceType;
 | 
			
		||||
import org.thingsboard.server.common.data.TransportPayloadType;
 | 
			
		||||
@ -44,11 +45,12 @@ public class CoapAttributesUpdatesJsonIntegrationTest extends AbstractCoapAttrib
 | 
			
		||||
        processAfterTest();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Ignore  // Uncomment when Californium 3.11 is released with https://github.com/eclipse-californium/californium/pull/2215
 | 
			
		||||
    @Test
 | 
			
		||||
    public void testSubscribeToAttributesUpdatesFromTheServer() throws Exception {
 | 
			
		||||
        processJsonTestSubscribeToAttributesUpdates(false);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Ignore // Uncomment when Californium 3.11 is released with https://github.com/eclipse-californium/californium/pull/2215
 | 
			
		||||
    @Test
 | 
			
		||||
    public void testSubscribeToAttributesUpdatesFromTheServerWithEmptyCurrentStateNotification() throws Exception {
 | 
			
		||||
        processJsonTestSubscribeToAttributesUpdates(true);
 | 
			
		||||
 | 
			
		||||
@ -18,6 +18,7 @@ package org.thingsboard.server.transport.coap.attributes.updates;
 | 
			
		||||
import lombok.extern.slf4j.Slf4j;
 | 
			
		||||
import org.junit.After;
 | 
			
		||||
import org.junit.Before;
 | 
			
		||||
import org.junit.Ignore;
 | 
			
		||||
import org.junit.Test;
 | 
			
		||||
import org.thingsboard.server.common.data.CoapDeviceType;
 | 
			
		||||
import org.thingsboard.server.common.data.TransportPayloadType;
 | 
			
		||||
@ -43,12 +44,12 @@ public class CoapAttributesUpdatesProtoIntegrationTest extends AbstractCoapAttri
 | 
			
		||||
    public void afterTest() throws Exception {
 | 
			
		||||
        processAfterTest();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Ignore // Uncomment when Californium 3.11 is released with https://github.com/eclipse-californium/californium/pull/2215
 | 
			
		||||
    @Test
 | 
			
		||||
    public void testSubscribeToAttributesUpdatesFromTheServer() throws Exception {
 | 
			
		||||
        processProtoTestSubscribeToAttributesUpdates(false);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Ignore // Uncomment when Californium 3.11 is released with https://github.com/eclipse-californium/californium/pull/2215
 | 
			
		||||
    @Test
 | 
			
		||||
    public void testSubscribeToAttributesUpdatesFromTheServerWithEmptyCurrentStateNotification() throws Exception {
 | 
			
		||||
        processProtoTestSubscribeToAttributesUpdates(true);
 | 
			
		||||
 | 
			
		||||
@ -26,6 +26,7 @@ import org.eclipse.californium.core.coap.CoAP;
 | 
			
		||||
import org.eclipse.californium.core.coap.MediaTypeRegistry;
 | 
			
		||||
import org.junit.After;
 | 
			
		||||
import org.junit.Before;
 | 
			
		||||
import org.junit.Ignore;
 | 
			
		||||
import org.junit.Test;
 | 
			
		||||
import org.thingsboard.common.util.JacksonUtil;
 | 
			
		||||
import org.thingsboard.server.common.data.id.DeviceId;
 | 
			
		||||
@ -82,6 +83,7 @@ public class CoapClientIntegrationTest extends AbstractCoapIntegrationTest {
 | 
			
		||||
        processAfterTest();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Ignore // Uncomment when Californium 3.11 is released with https://github.com/eclipse-californium/californium/pull/2215
 | 
			
		||||
    @Test
 | 
			
		||||
    public void testConfirmableRequests() throws Exception {
 | 
			
		||||
        boolean confirmable = true;
 | 
			
		||||
@ -90,6 +92,7 @@ public class CoapClientIntegrationTest extends AbstractCoapIntegrationTest {
 | 
			
		||||
        processTestRequestAttributesValuesFromTheServer(confirmable);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Ignore // Uncomment when Californium 3.11 is released with https://github.com/eclipse-californium/californium/pull/2215
 | 
			
		||||
    @Test
 | 
			
		||||
    public void testNonConfirmableRequests() throws Exception {
 | 
			
		||||
        boolean confirmable = false;
 | 
			
		||||
 | 
			
		||||
@ -82,15 +82,27 @@ public abstract class AbstractCoapServerSideRpcIntegrationTest extends AbstractC
 | 
			
		||||
                .until(() -> CoAP.ResponseCode.VALID.equals(callbackCoap.getResponseCode()) &&
 | 
			
		||||
                        callbackCoap.getObserve() != null && 0 == callbackCoap.getObserve());
 | 
			
		||||
        validateCurrentStateNotification(callbackCoap);
 | 
			
		||||
        int expectedObserveAfterRpcProcessed = callbackCoap.getObserve() + 1;
 | 
			
		||||
        String setGpioRequest = "{\"method\":\"setGpio\",\"params\":{\"pin\": \"23\",\"value\": 1}}";
 | 
			
		||||
 | 
			
		||||
        int expectedObserveAfterRpcProcessed1 = callbackCoap.getObserve() + 1;
 | 
			
		||||
        String setGpioRequest = "{\"method\":\"setGpio1\",\"params\":{\"pin\": \"21\",\"value\": 1}}";
 | 
			
		||||
        String deviceId = savedDevice.getId().getId().toString();
 | 
			
		||||
        String result = doPostAsync("/api/rpc/oneway/" + deviceId, setGpioRequest, String.class, status().isOk());
 | 
			
		||||
        awaitAlias = "await One Way Rpc setGpio(method, params, value)";
 | 
			
		||||
        await(awaitAlias)
 | 
			
		||||
                .atMost(DEFAULT_WAIT_TIMEOUT_SECONDS, TimeUnit.SECONDS)
 | 
			
		||||
                .until(() -> CoAP.ResponseCode.CONTENT.equals(callbackCoap.getResponseCode()) &&
 | 
			
		||||
                        callbackCoap.getObserve() != null && expectedObserveAfterRpcProcessed == callbackCoap.getObserve());
 | 
			
		||||
                        callbackCoap.getObserve() != null && expectedObserveAfterRpcProcessed1 == callbackCoap.getObserve());
 | 
			
		||||
        validateOneWayStateChangedNotification(callbackCoap, result);
 | 
			
		||||
 | 
			
		||||
        int expectedObserveAfterRpcProcessed2 = callbackCoap.getObserve() + 1;
 | 
			
		||||
        setGpioRequest = "{\"method\":\"setGpio2\",\"params\":{\"pin\": \"22\",\"value\": 2}}";
 | 
			
		||||
        deviceId = savedDevice.getId().getId().toString();
 | 
			
		||||
        result = doPostAsync("/api/rpc/oneway/" + deviceId, setGpioRequest, String.class, status().isOk());
 | 
			
		||||
        awaitAlias = "await One Way Rpc setGpio(method, params, value)";
 | 
			
		||||
        await(awaitAlias)
 | 
			
		||||
                .atMost(DEFAULT_WAIT_TIMEOUT_SECONDS, TimeUnit.SECONDS)
 | 
			
		||||
                .until(() -> CoAP.ResponseCode.CONTENT.equals(callbackCoap.getResponseCode()) &&
 | 
			
		||||
                        callbackCoap.getObserve() != null && expectedObserveAfterRpcProcessed2 == callbackCoap.getObserve());
 | 
			
		||||
        validateOneWayStateChangedNotification(callbackCoap, result);
 | 
			
		||||
 | 
			
		||||
        observeRelation.proactiveCancel();
 | 
			
		||||
 | 
			
		||||
@ -20,6 +20,7 @@ import lombok.extern.slf4j.Slf4j;
 | 
			
		||||
import org.junit.After;
 | 
			
		||||
import org.junit.Assert;
 | 
			
		||||
import org.junit.Before;
 | 
			
		||||
import org.junit.Ignore;
 | 
			
		||||
import org.junit.Test;
 | 
			
		||||
import org.thingsboard.server.dao.service.DaoSqlTest;
 | 
			
		||||
import org.thingsboard.server.service.security.AccessValidator;
 | 
			
		||||
@ -82,11 +83,13 @@ public class CoapServerSideRpcDefaultIntegrationTest extends AbstractCoapServerS
 | 
			
		||||
        Assert.assertEquals(AccessValidator.DEVICE_WITH_REQUESTED_ID_NOT_FOUND, result);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Ignore // Uncomment when Californium 3.11 is released with https://github.com/eclipse-californium/californium/pull/2215
 | 
			
		||||
    @Test
 | 
			
		||||
    public void testServerCoapOneWayRpc() throws Exception {
 | 
			
		||||
        processOneWayRpcTest(false);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Ignore // Uncomment when Californium 3.11 is released with https://github.com/eclipse-californium/californium/pull/2215
 | 
			
		||||
    @Test
 | 
			
		||||
    public void testServerCoapTwoWayRpc() throws Exception {
 | 
			
		||||
        processTwoWayRpcTest("{\"value1\":\"A\",\"value2\":\"B\"}", false);
 | 
			
		||||
 | 
			
		||||
@ -18,6 +18,7 @@ package org.thingsboard.server.transport.coap.rpc;
 | 
			
		||||
import lombok.extern.slf4j.Slf4j;
 | 
			
		||||
import org.junit.After;
 | 
			
		||||
import org.junit.Before;
 | 
			
		||||
import org.junit.Ignore;
 | 
			
		||||
import org.junit.Test;
 | 
			
		||||
import org.thingsboard.server.common.data.CoapDeviceType;
 | 
			
		||||
import org.thingsboard.server.common.data.TransportPayloadType;
 | 
			
		||||
@ -43,11 +44,13 @@ public class CoapServerSideRpcJsonIntegrationTest extends AbstractCoapServerSide
 | 
			
		||||
        processAfterTest();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Ignore // Uncomment when Californium 3.11 is released with https://github.com/eclipse-californium/californium/pull/2215
 | 
			
		||||
    @Test
 | 
			
		||||
    public void testServerCoapOneWayRpc() throws Exception {
 | 
			
		||||
        processOneWayRpcTest(false);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Ignore // Uncomment when Californium 3.11 is released with https://github.com/eclipse-californium/californium/pull/2215
 | 
			
		||||
    @Test
 | 
			
		||||
    public void testServerCoapTwoWayRpc() throws Exception {
 | 
			
		||||
        processTwoWayRpcTest("{\"value1\":\"A\",\"value2\":\"B\"}", false);
 | 
			
		||||
 | 
			
		||||
@ -18,6 +18,7 @@ package org.thingsboard.server.transport.coap.rpc;
 | 
			
		||||
import lombok.extern.slf4j.Slf4j;
 | 
			
		||||
import org.junit.After;
 | 
			
		||||
import org.junit.Before;
 | 
			
		||||
import org.junit.Ignore;
 | 
			
		||||
import org.junit.Test;
 | 
			
		||||
import org.thingsboard.server.common.data.CoapDeviceType;
 | 
			
		||||
import org.thingsboard.server.common.data.TransportPayloadType;
 | 
			
		||||
@ -44,11 +45,13 @@ public class CoapServerSideRpcProtoIntegrationTest extends AbstractCoapServerSid
 | 
			
		||||
        processAfterTest();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Ignore // Uncomment when Californium 3.11 is released with https://github.com/eclipse-californium/californium/pull/2215
 | 
			
		||||
    @Test
 | 
			
		||||
    public void testServerCoapOneWayRpc() throws Exception {
 | 
			
		||||
        processOneWayRpcTest(true);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Ignore // Uncomment when Californium 3.11 is released with https://github.com/eclipse-californium/californium/pull/2215
 | 
			
		||||
    @Test
 | 
			
		||||
    public void testServerCoapTwoWayRpc() throws Exception {
 | 
			
		||||
        processTwoWayRpcTest("{\"payload\":\"{\\\"value1\\\":\\\"A\\\",\\\"value2\\\":\\\"B\\\"}\"}", true);
 | 
			
		||||
 | 
			
		||||
@ -19,7 +19,6 @@ import com.google.gson.JsonParseException;
 | 
			
		||||
import lombok.extern.slf4j.Slf4j;
 | 
			
		||||
import org.eclipse.californium.core.coap.CoAP;
 | 
			
		||||
import org.eclipse.californium.core.coap.Request;
 | 
			
		||||
import org.eclipse.californium.core.coap.Response;
 | 
			
		||||
import org.eclipse.californium.core.network.Exchange;
 | 
			
		||||
import org.eclipse.californium.core.observe.ObserveRelation;
 | 
			
		||||
import org.eclipse.californium.core.server.resources.CoapExchange;
 | 
			
		||||
@ -27,6 +26,8 @@ import org.eclipse.californium.core.server.resources.Resource;
 | 
			
		||||
import org.eclipse.californium.core.server.resources.ResourceObserver;
 | 
			
		||||
import org.thingsboard.server.coapserver.CoapServerService;
 | 
			
		||||
import org.thingsboard.server.coapserver.TbCoapDtlsSessionInfo;
 | 
			
		||||
import org.thingsboard.server.common.adaptor.AdaptorException;
 | 
			
		||||
import org.thingsboard.server.common.adaptor.JsonConverter;
 | 
			
		||||
import org.thingsboard.server.common.data.DataConstants;
 | 
			
		||||
import org.thingsboard.server.common.data.DeviceProfile;
 | 
			
		||||
import org.thingsboard.server.common.data.DeviceTransportType;
 | 
			
		||||
@ -35,13 +36,11 @@ import org.thingsboard.server.common.data.TransportPayloadType;
 | 
			
		||||
import org.thingsboard.server.common.data.security.DeviceTokenCredentials;
 | 
			
		||||
import org.thingsboard.server.common.msg.session.FeatureType;
 | 
			
		||||
import org.thingsboard.server.common.transport.TransportServiceCallback;
 | 
			
		||||
import org.thingsboard.server.common.adaptor.AdaptorException;
 | 
			
		||||
import org.thingsboard.server.common.adaptor.JsonConverter;
 | 
			
		||||
import org.thingsboard.server.common.transport.auth.ValidateDeviceCredentialsResponse;
 | 
			
		||||
import org.thingsboard.server.gen.transport.TransportProtos;
 | 
			
		||||
import org.thingsboard.server.transport.coap.callback.CoapDeviceAuthCallback;
 | 
			
		||||
import org.thingsboard.server.transport.coap.callback.CoapNoOpCallback;
 | 
			
		||||
import org.thingsboard.server.transport.coap.callback.CoapOkCallback;
 | 
			
		||||
import org.thingsboard.server.transport.coap.callback.CoapResponseCodeCallback;
 | 
			
		||||
import org.thingsboard.server.transport.coap.callback.GetAttributesSyncSessionCallback;
 | 
			
		||||
import org.thingsboard.server.transport.coap.callback.ToServerRpcSyncSessionCallback;
 | 
			
		||||
import org.thingsboard.server.transport.coap.client.CoapClientContext;
 | 
			
		||||
@ -54,7 +53,6 @@ import java.util.Random;
 | 
			
		||||
import java.util.UUID;
 | 
			
		||||
import java.util.concurrent.ConcurrentMap;
 | 
			
		||||
import java.util.concurrent.TimeUnit;
 | 
			
		||||
import java.util.concurrent.atomic.AtomicInteger;
 | 
			
		||||
 | 
			
		||||
import static org.eclipse.californium.elements.DtlsEndpointContext.KEY_SESSION_ID;
 | 
			
		||||
 | 
			
		||||
@ -84,30 +82,6 @@ public class CoapTransportResource extends AbstractCoapTransportResource {
 | 
			
		||||
        ctx.getScheduler().scheduleAtFixedRate(clients::reportActivity, new Random().nextInt((int) sessionReportTimeout), sessionReportTimeout, TimeUnit.MILLISECONDS);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /*
 | 
			
		||||
     * Overwritten method from CoapResource to be able to manage our own observe notification counters.
 | 
			
		||||
     */
 | 
			
		||||
    @Override
 | 
			
		||||
    public void checkObserveRelation(Exchange exchange, Response response) {
 | 
			
		||||
        String token = getTokenFromRequest(exchange.getRequest());
 | 
			
		||||
        final ObserveRelation relation = exchange.getRelation();
 | 
			
		||||
        if (relation == null || relation.isCanceled()) {
 | 
			
		||||
            return; // because request did not try to establish a relation
 | 
			
		||||
        }
 | 
			
		||||
        if (response.getCode().isSuccess()) {
 | 
			
		||||
            if (!relation.isEstablished()) {
 | 
			
		||||
                relation.setEstablished();
 | 
			
		||||
                addObserveRelation(relation);
 | 
			
		||||
            }
 | 
			
		||||
            AtomicInteger state = clients.getNotificationCounterByToken(token);
 | 
			
		||||
            if (state != null) {
 | 
			
		||||
                response.getOptions().setObserve(state.getAndIncrement());
 | 
			
		||||
            } else {
 | 
			
		||||
                response.getOptions().removeObserve();
 | 
			
		||||
            }
 | 
			
		||||
        } // ObserveLayer takes care of the else case
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    protected void processHandleGet(CoapExchange exchange) {
 | 
			
		||||
        Optional<FeatureType> featureType = getFeatureType(exchange.advanced().getRequest());
 | 
			
		||||
@ -278,7 +252,7 @@ public class CoapTransportResource extends AbstractCoapTransportResource {
 | 
			
		||||
        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));
 | 
			
		||||
                new CoapResponseCodeCallback(exchange, CoAP.ResponseCode.CREATED, CoAP.ResponseCode.INTERNAL_SERVER_ERROR));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void handlePostTelemetryRequest(TbCoapClientState clientState, CoapExchange exchange, Request request) throws AdaptorException {
 | 
			
		||||
@ -286,7 +260,7 @@ public class CoapTransportResource extends AbstractCoapTransportResource {
 | 
			
		||||
        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));
 | 
			
		||||
                new CoapResponseCodeCallback(exchange, CoAP.ResponseCode.CREATED, CoAP.ResponseCode.INTERNAL_SERVER_ERROR));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void handleClaimRequest(TbCoapClientState clientState, CoapExchange exchange, Request request) throws AdaptorException {
 | 
			
		||||
@ -294,7 +268,7 @@ public class CoapTransportResource extends AbstractCoapTransportResource {
 | 
			
		||||
        UUID sessionId = toSessionId(sessionInfo);
 | 
			
		||||
        transportService.process(sessionInfo,
 | 
			
		||||
                clientState.getAdaptor().convertToClaimDevice(sessionId, request, sessionInfo),
 | 
			
		||||
                new CoapOkCallback(exchange, CoAP.ResponseCode.CREATED, CoAP.ResponseCode.INTERNAL_SERVER_ERROR));
 | 
			
		||||
                new CoapResponseCodeCallback(exchange, CoAP.ResponseCode.CREATED, CoAP.ResponseCode.INTERNAL_SERVER_ERROR));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void handleAttributeSubscribeRequest(TbCoapClientState clientState, CoapExchange exchange, Request request) {
 | 
			
		||||
@ -320,7 +294,7 @@ public class CoapTransportResource extends AbstractCoapTransportResource {
 | 
			
		||||
        UUID sessionId = toSessionId(session);
 | 
			
		||||
        transportService.process(session,
 | 
			
		||||
                clientState.getAdaptor().convertToDeviceRpcResponse(sessionId, request, clientState.getConfiguration().getRpcResponseMsgDescriptor()),
 | 
			
		||||
                new CoapOkCallback(exchange, CoAP.ResponseCode.CREATED, CoAP.ResponseCode.INTERNAL_SERVER_ERROR));
 | 
			
		||||
                new CoapResponseCodeCallback(exchange, CoAP.ResponseCode.CREATED, CoAP.ResponseCode.INTERNAL_SERVER_ERROR));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void handleRpcSubscribeRequest(TbCoapClientState clientState, CoapExchange exchange, Request request) {
 | 
			
		||||
 | 
			
		||||
@ -0,0 +1,54 @@
 | 
			
		||||
/**
 | 
			
		||||
 * Copyright © 2016-2024 The Thingsboard Authors
 | 
			
		||||
 *
 | 
			
		||||
 * Licensed under the Apache License, Version 2.0 (the "License");
 | 
			
		||||
 * you may not use this file except in compliance with the License.
 | 
			
		||||
 * You may obtain a copy of the License at
 | 
			
		||||
 *
 | 
			
		||||
 *     http://www.apache.org/licenses/LICENSE-2.0
 | 
			
		||||
 *
 | 
			
		||||
 * Unless required by applicable law or agreed to in writing, software
 | 
			
		||||
 * distributed under the License is distributed on an "AS IS" BASIS,
 | 
			
		||||
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 | 
			
		||||
 * See the License for the specific language governing permissions and
 | 
			
		||||
 * limitations under the License.
 | 
			
		||||
 */
 | 
			
		||||
package org.thingsboard.server.transport.coap.callback;
 | 
			
		||||
 | 
			
		||||
import org.eclipse.californium.core.coap.Response;
 | 
			
		||||
import org.eclipse.californium.core.server.resources.CoapExchange;
 | 
			
		||||
import org.thingsboard.server.common.transport.TransportServiceCallback;
 | 
			
		||||
 | 
			
		||||
public class CoapResponseCallback implements TransportServiceCallback<Void> {
 | 
			
		||||
 | 
			
		||||
    protected final CoapExchange exchange;
 | 
			
		||||
    protected final Response onSuccessResponse;
 | 
			
		||||
    protected final Response onFailureResponse;
 | 
			
		||||
 | 
			
		||||
    public CoapResponseCallback(CoapExchange exchange, Response onSuccessResponse, Response onFailureResponse) {
 | 
			
		||||
        this.exchange = exchange;
 | 
			
		||||
        this.onSuccessResponse = onSuccessResponse;
 | 
			
		||||
        this.onFailureResponse = onFailureResponse;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @param msg
 | 
			
		||||
     */
 | 
			
		||||
    @Override
 | 
			
		||||
    public void onSuccess(Void msg) {
 | 
			
		||||
        this.onSuccessResponse.setConfirmable(isConRequest());
 | 
			
		||||
        exchange.respond(this.onSuccessResponse);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @param e
 | 
			
		||||
     */
 | 
			
		||||
    @Override
 | 
			
		||||
    public void onError(Throwable e) {
 | 
			
		||||
        exchange.respond(onFailureResponse);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    protected boolean isConRequest() {
 | 
			
		||||
        return exchange.advanced().getRequest().isConfirmable();
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@ -20,13 +20,13 @@ import org.eclipse.californium.core.coap.Response;
 | 
			
		||||
import org.eclipse.californium.core.server.resources.CoapExchange;
 | 
			
		||||
import org.thingsboard.server.common.transport.TransportServiceCallback;
 | 
			
		||||
 | 
			
		||||
public class CoapOkCallback implements TransportServiceCallback<Void> {
 | 
			
		||||
public class CoapResponseCodeCallback implements TransportServiceCallback<Void> {
 | 
			
		||||
 | 
			
		||||
    protected final CoapExchange exchange;
 | 
			
		||||
    protected final CoAP.ResponseCode onSuccessResponse;
 | 
			
		||||
    protected final CoAP.ResponseCode onFailureResponse;
 | 
			
		||||
 | 
			
		||||
    public CoapOkCallback(CoapExchange exchange, CoAP.ResponseCode onSuccessResponse, CoAP.ResponseCode onFailureResponse) {
 | 
			
		||||
    public CoapResponseCodeCallback(CoapExchange exchange, CoAP.ResponseCode onSuccessResponse, CoAP.ResponseCode onFailureResponse) {
 | 
			
		||||
        this.exchange = exchange;
 | 
			
		||||
        this.onSuccessResponse = onSuccessResponse;
 | 
			
		||||
        this.onFailureResponse = onFailureResponse;
 | 
			
		||||
@ -18,6 +18,7 @@ package org.thingsboard.server.transport.coap.client;
 | 
			
		||||
import lombok.RequiredArgsConstructor;
 | 
			
		||||
import lombok.extern.slf4j.Slf4j;
 | 
			
		||||
import org.eclipse.californium.core.coap.CoAP;
 | 
			
		||||
import org.eclipse.californium.core.coap.CoAP.ResponseCode;
 | 
			
		||||
import org.eclipse.californium.core.coap.Response;
 | 
			
		||||
import org.eclipse.californium.core.observe.ObserveRelation;
 | 
			
		||||
import org.eclipse.californium.core.server.resources.CoapExchange;
 | 
			
		||||
@ -26,6 +27,7 @@ import org.springframework.context.annotation.Lazy;
 | 
			
		||||
import org.springframework.context.event.EventListener;
 | 
			
		||||
import org.springframework.stereotype.Service;
 | 
			
		||||
import org.thingsboard.server.coapserver.CoapServerContext;
 | 
			
		||||
import org.thingsboard.server.common.adaptor.AdaptorException;
 | 
			
		||||
import org.thingsboard.server.common.data.DataConstants;
 | 
			
		||||
import org.thingsboard.server.common.data.Device;
 | 
			
		||||
import org.thingsboard.server.common.data.DeviceProfile;
 | 
			
		||||
@ -45,7 +47,6 @@ import org.thingsboard.server.common.data.id.DeviceId;
 | 
			
		||||
import org.thingsboard.server.common.data.id.DeviceProfileId;
 | 
			
		||||
import org.thingsboard.server.common.data.rpc.RpcStatus;
 | 
			
		||||
import org.thingsboard.server.common.msg.session.FeatureType;
 | 
			
		||||
import org.thingsboard.server.transport.coap.CoapSessionMsgType;
 | 
			
		||||
import org.thingsboard.server.common.transport.DeviceDeletedEvent;
 | 
			
		||||
import org.thingsboard.server.common.transport.DeviceProfileUpdatedEvent;
 | 
			
		||||
import org.thingsboard.server.common.transport.DeviceUpdatedEvent;
 | 
			
		||||
@ -53,18 +54,19 @@ import org.thingsboard.server.common.transport.SessionMsgListener;
 | 
			
		||||
import org.thingsboard.server.common.transport.TransportDeviceProfileCache;
 | 
			
		||||
import org.thingsboard.server.common.transport.TransportService;
 | 
			
		||||
import org.thingsboard.server.common.transport.TransportServiceCallback;
 | 
			
		||||
import org.thingsboard.server.common.adaptor.AdaptorException;
 | 
			
		||||
import org.thingsboard.server.common.transport.auth.SessionInfoCreator;
 | 
			
		||||
import org.thingsboard.server.common.transport.auth.ValidateDeviceCredentialsResponse;
 | 
			
		||||
import org.thingsboard.server.gen.transport.TransportProtos;
 | 
			
		||||
import org.thingsboard.server.queue.discovery.PartitionService;
 | 
			
		||||
import org.thingsboard.server.transport.coap.CoapSessionMsgType;
 | 
			
		||||
import org.thingsboard.server.transport.coap.CoapTransportContext;
 | 
			
		||||
import org.thingsboard.server.transport.coap.TbCoapMessageObserver;
 | 
			
		||||
import org.thingsboard.server.transport.coap.TransportConfigurationContainer;
 | 
			
		||||
import org.thingsboard.server.transport.coap.adaptors.CoapTransportAdaptor;
 | 
			
		||||
import org.thingsboard.server.transport.coap.callback.AbstractSyncSessionCallback;
 | 
			
		||||
import org.thingsboard.server.transport.coap.callback.CoapNoOpCallback;
 | 
			
		||||
import org.thingsboard.server.transport.coap.callback.CoapOkCallback;
 | 
			
		||||
import org.thingsboard.server.transport.coap.callback.CoapResponseCallback;
 | 
			
		||||
import org.thingsboard.server.transport.coap.callback.CoapResponseCodeCallback;
 | 
			
		||||
 | 
			
		||||
import java.util.Optional;
 | 
			
		||||
import java.util.UUID;
 | 
			
		||||
@ -329,9 +331,14 @@ public class DefaultCoapClientContext implements CoapClientContext {
 | 
			
		||||
                            TransportProtos.GetAttributeRequestMsg.newBuilder().setOnlyShared(true).build(),
 | 
			
		||||
                            new CoapNoOpCallback(exchange));
 | 
			
		||||
                } else {
 | 
			
		||||
                    Response response = new Response(CoAP.ResponseCode.VALID);
 | 
			
		||||
                    if (state.getRpc() == null) {
 | 
			
		||||
                        state.setRpc(new TbCoapObservationState(exchange, token));
 | 
			
		||||
                    }
 | 
			
		||||
                    response.getOptions().setObserve(state.getRpc().getObserveCounter().getAndIncrement());
 | 
			
		||||
                    transportService.process(state.getSession(),
 | 
			
		||||
                            TransportProtos.SubscribeToRPCMsg.getDefaultInstance(),
 | 
			
		||||
                            new CoapOkCallback(exchange, CoAP.ResponseCode.VALID, CoAP.ResponseCode.INTERNAL_SERVER_ERROR)
 | 
			
		||||
                            new CoapResponseCallback(exchange, response,  new Response(CoAP.ResponseCode.INTERNAL_SERVER_ERROR))
 | 
			
		||||
                    );
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
@ -478,7 +485,13 @@ public class DefaultCoapClientContext implements CoapClientContext {
 | 
			
		||||
            TbCoapObservationState attrs = state.getAttrs();
 | 
			
		||||
            if (attrs != null) {
 | 
			
		||||
                try {
 | 
			
		||||
                    Response response = state.getAdaptor().convertToPublish(msg);
 | 
			
		||||
                    Response resp = state.getAdaptor().convertToPublish(msg);
 | 
			
		||||
                    Response response = new Response(ResponseCode.VALID);
 | 
			
		||||
                    response.setPayload(resp.getPayload());
 | 
			
		||||
                    if (state.getRpc() == null) {
 | 
			
		||||
                        state.setRpc(new TbCoapObservationState(attrs.getExchange(), attrs.getToken()));
 | 
			
		||||
                    }
 | 
			
		||||
                    response.getOptions().setObserve(state.getRpc().getObserveCounter().getAndIncrement());
 | 
			
		||||
                    respond(attrs.getExchange(), response, state.getContentFormat());
 | 
			
		||||
                } catch (AdaptorException e) {
 | 
			
		||||
                    log.trace("Failed to reply due to error", e);
 | 
			
		||||
@ -509,6 +522,7 @@ public class DefaultCoapClientContext implements CoapClientContext {
 | 
			
		||||
                    boolean conRequest = AbstractSyncSessionCallback.isConRequest(state.getAttrs());
 | 
			
		||||
                    int requestId = getNextMsgId();
 | 
			
		||||
                    Response response = state.getAdaptor().convertToPublish(msg);
 | 
			
		||||
                    response.getOptions().setObserve(state.getRpc().getObserveCounter().getAndIncrement());
 | 
			
		||||
                    response.setConfirmable(conRequest);
 | 
			
		||||
                    response.setMID(requestId);
 | 
			
		||||
                    if (conRequest) {
 | 
			
		||||
@ -573,6 +587,7 @@ public class DefaultCoapClientContext implements CoapClientContext {
 | 
			
		||||
            int requestId = getNextMsgId();
 | 
			
		||||
            try {
 | 
			
		||||
                Response response = state.getAdaptor().convertToPublish(msg, state.getConfiguration().getRpcRequestDynamicMessageBuilder());
 | 
			
		||||
                response.getOptions().setObserve(state.getRpc().getObserveCounter().getAndIncrement());
 | 
			
		||||
                response.setConfirmable(conRequest);
 | 
			
		||||
                response.setMID(requestId);
 | 
			
		||||
                if (conRequest) {
 | 
			
		||||
@ -808,7 +823,7 @@ public class DefaultCoapClientContext implements CoapClientContext {
 | 
			
		||||
            state.setRpc(null);
 | 
			
		||||
            transportService.process(state.getSession(),
 | 
			
		||||
                    TransportProtos.SubscribeToRPCMsg.newBuilder().setUnsubscribe(true).build(),
 | 
			
		||||
                    new CoapOkCallback(exchange, CoAP.ResponseCode.DELETED, CoAP.ResponseCode.INTERNAL_SERVER_ERROR));
 | 
			
		||||
                    new CoapResponseCodeCallback(exchange, CoAP.ResponseCode.DELETED, CoAP.ResponseCode.INTERNAL_SERVER_ERROR));
 | 
			
		||||
            if (state.getAttrs() == null) {
 | 
			
		||||
                closeAndCleanup(state);
 | 
			
		||||
            }
 | 
			
		||||
@ -822,7 +837,7 @@ public class DefaultCoapClientContext implements CoapClientContext {
 | 
			
		||||
            state.setAttrs(null);
 | 
			
		||||
            transportService.process(state.getSession(),
 | 
			
		||||
                    TransportProtos.SubscribeToAttributeUpdatesMsg.newBuilder().setUnsubscribe(true).build(),
 | 
			
		||||
                    new CoapOkCallback(exchange, CoAP.ResponseCode.DELETED, CoAP.ResponseCode.INTERNAL_SERVER_ERROR));
 | 
			
		||||
                    new CoapResponseCodeCallback(exchange, CoAP.ResponseCode.DELETED, CoAP.ResponseCode.INTERNAL_SERVER_ERROR));
 | 
			
		||||
            if (state.getRpc() == null) {
 | 
			
		||||
                closeAndCleanup(state);
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										2
									
								
								pom.xml
									
									
									
									
									
								
							
							
						
						
									
										2
									
								
								pom.xml
									
									
									
									
									
								
							@ -72,7 +72,7 @@
 | 
			
		||||
        <fasterxml-classmate.version>1.3.4</fasterxml-classmate.version>
 | 
			
		||||
        <auth0-jwt.version>4.2.1</auth0-jwt.version>
 | 
			
		||||
        <json-schema-validator.version>2.2.6</json-schema-validator.version>
 | 
			
		||||
        <californium.version>3.9.1</californium.version>
 | 
			
		||||
        <californium.version>3.10.0</californium.version>
 | 
			
		||||
        <leshan.version>2.0.0-M14</leshan.version>
 | 
			
		||||
        <gson.version>2.9.0</gson.version>
 | 
			
		||||
        <freemarker.version>2.3.30</freemarker.version>
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user