Showing
9 changed files
with
63 additions
and
14 deletions
common/transport/coap/src/main/java/org/thingsboard/server/transport/coap/CoapTransportResource.java
@@ -67,8 +67,6 @@ public class CoapTransportResource extends AbstractCoapTransportResource { | @@ -67,8 +67,6 @@ public class CoapTransportResource extends AbstractCoapTransportResource { | ||
67 | private static final int REQUEST_ID_POSITION_CERTIFICATE_REQUEST = 4; | 67 | private static final int REQUEST_ID_POSITION_CERTIFICATE_REQUEST = 4; |
68 | private static final String DTLS_SESSION_ID_KEY = "DTLS_SESSION_ID"; | 68 | private static final String DTLS_SESSION_ID_KEY = "DTLS_SESSION_ID"; |
69 | 69 | ||
70 | - private final ConcurrentMap<TbCoapClientState, ObserveRelation> sessionInfoToObserveRelationMap = new ConcurrentHashMap<>(); | ||
71 | - | ||
72 | private final ConcurrentMap<String, TbCoapDtlsSessionInfo> dtlsSessionIdMap; | 70 | private final ConcurrentMap<String, TbCoapDtlsSessionInfo> dtlsSessionIdMap; |
73 | private final long timeout; | 71 | private final long timeout; |
74 | private final CoapClientContext clients; | 72 | private final CoapClientContext clients; |
@@ -50,4 +50,5 @@ public interface CoapTransportAdaptor { | @@ -50,4 +50,5 @@ public interface CoapTransportAdaptor { | ||
50 | ProvisionDeviceRequestMsg convertToProvisionRequestMsg(UUID sessionId, Request inbound) throws AdaptorException; | 50 | ProvisionDeviceRequestMsg convertToProvisionRequestMsg(UUID sessionId, Request inbound) throws AdaptorException; |
51 | 51 | ||
52 | int getContentFormat(); | 52 | int getContentFormat(); |
53 | + | ||
53 | } | 54 | } |
@@ -169,4 +169,5 @@ public class JsonCoapAdaptor implements CoapTransportAdaptor { | @@ -169,4 +169,5 @@ public class JsonCoapAdaptor implements CoapTransportAdaptor { | ||
169 | public int getContentFormat() { | 169 | public int getContentFormat() { |
170 | return MediaTypeRegistry.APPLICATION_JSON; | 170 | return MediaTypeRegistry.APPLICATION_JSON; |
171 | } | 171 | } |
172 | + | ||
172 | } | 173 | } |
@@ -24,6 +24,7 @@ import org.eclipse.californium.core.server.resources.CoapExchange; | @@ -24,6 +24,7 @@ import org.eclipse.californium.core.server.resources.CoapExchange; | ||
24 | import org.thingsboard.server.common.transport.SessionMsgListener; | 24 | import org.thingsboard.server.common.transport.SessionMsgListener; |
25 | import org.thingsboard.server.gen.transport.TransportProtos; | 25 | import org.thingsboard.server.gen.transport.TransportProtos; |
26 | import org.thingsboard.server.transport.coap.client.TbCoapClientState; | 26 | import org.thingsboard.server.transport.coap.client.TbCoapClientState; |
27 | +import org.thingsboard.server.transport.coap.client.TbCoapContentFormatUtil; | ||
27 | import org.thingsboard.server.transport.coap.client.TbCoapObservationState; | 28 | import org.thingsboard.server.transport.coap.client.TbCoapObservationState; |
28 | 29 | ||
29 | import java.util.UUID; | 30 | import java.util.UUID; |
@@ -74,9 +75,7 @@ public abstract class AbstractSyncSessionCallback implements SessionMsgListener | @@ -74,9 +75,7 @@ public abstract class AbstractSyncSessionCallback implements SessionMsgListener | ||
74 | } | 75 | } |
75 | 76 | ||
76 | protected void respond(Response response) { | 77 | protected void respond(Response response) { |
77 | - int contentFormat = exchange.getRequestOptions().getContentFormat(); | ||
78 | - contentFormat = contentFormat != MediaTypeRegistry.UNDEFINED ? contentFormat : state.getContentFormat(); | ||
79 | - response.getOptions().setContentFormat(contentFormat); | 78 | + response.getOptions().setContentFormat(TbCoapContentFormatUtil.getContentFormat(exchange.getRequestOptions().getContentFormat(), state.getContentFormat())); |
80 | exchange.respond(response); | 79 | exchange.respond(response); |
81 | } | 80 | } |
82 | 81 |
@@ -34,7 +34,7 @@ public class GetAttributesSyncSessionCallback extends AbstractSyncSessionCallbac | @@ -34,7 +34,7 @@ public class GetAttributesSyncSessionCallback extends AbstractSyncSessionCallbac | ||
34 | @Override | 34 | @Override |
35 | public void onGetAttributesResponse(TransportProtos.GetAttributeResponseMsg msg) { | 35 | public void onGetAttributesResponse(TransportProtos.GetAttributeResponseMsg msg) { |
36 | try { | 36 | try { |
37 | - respond(state.getAdaptor().convertToPublish(isConRequest(state.getAttrs()), msg)); | 37 | + respond(state.getAdaptor().convertToPublish(request.isConfirmable(), msg)); |
38 | } catch (AdaptorException e) { | 38 | } catch (AdaptorException e) { |
39 | log.trace("[{}] Failed to reply due to error", state.getDeviceId(), e); | 39 | log.trace("[{}] Failed to reply due to error", state.getDeviceId(), e); |
40 | exchange.respond(new Response(CoAP.ResponseCode.INTERNAL_SERVER_ERROR)); | 40 | exchange.respond(new Response(CoAP.ResponseCode.INTERNAL_SERVER_ERROR)); |
@@ -33,7 +33,7 @@ public class ToServerRpcSyncSessionCallback extends AbstractSyncSessionCallback | @@ -33,7 +33,7 @@ public class ToServerRpcSyncSessionCallback extends AbstractSyncSessionCallback | ||
33 | @Override | 33 | @Override |
34 | public void onToServerRpcResponse(TransportProtos.ToServerRpcResponseMsg toServerResponse) { | 34 | public void onToServerRpcResponse(TransportProtos.ToServerRpcResponseMsg toServerResponse) { |
35 | try { | 35 | try { |
36 | - respond(state.getAdaptor().convertToPublish(isConRequest(state.getRpc()), toServerResponse)); | 36 | + respond(state.getAdaptor().convertToPublish(request.isConfirmable(), toServerResponse)); |
37 | } catch (AdaptorException e) { | 37 | } catch (AdaptorException e) { |
38 | log.trace("Failed to reply due to error", e); | 38 | log.trace("Failed to reply due to error", e); |
39 | exchange.respond(CoAP.ResponseCode.INTERNAL_SERVER_ERROR); | 39 | exchange.respond(CoAP.ResponseCode.INTERNAL_SERVER_ERROR); |
@@ -329,9 +329,7 @@ public class DefaultCoapClientContext implements CoapClientContext { | @@ -329,9 +329,7 @@ public class DefaultCoapClientContext implements CoapClientContext { | ||
329 | state.lock(); | 329 | state.lock(); |
330 | try { | 330 | try { |
331 | if (state.getConfiguration() == null || state.getAdaptor() == null) { | 331 | if (state.getConfiguration() == null || state.getAdaptor() == null) { |
332 | - state.setConfiguration(getTransportConfigurationContainer(deviceProfile)); | ||
333 | - state.setAdaptor(getCoapTransportAdaptor(state.getConfiguration().isJsonPayload())); | ||
334 | - state.setContentFormat(state.getAdaptor().getContentFormat()); | 332 | + initStateAdaptor(deviceProfile, state); |
335 | } | 333 | } |
336 | if (state.getCredentials() == null) { | 334 | if (state.getCredentials() == null) { |
337 | state.init(deviceCredentials); | 335 | state.init(deviceCredentials); |
@@ -395,6 +393,12 @@ public class DefaultCoapClientContext implements CoapClientContext { | @@ -395,6 +393,12 @@ public class DefaultCoapClientContext implements CoapClientContext { | ||
395 | } | 393 | } |
396 | } | 394 | } |
397 | 395 | ||
396 | + private void initStateAdaptor(DeviceProfile deviceProfile, TbCoapClientState state) throws AdaptorException { | ||
397 | + state.setConfiguration(getTransportConfigurationContainer(deviceProfile)); | ||
398 | + state.setAdaptor(getCoapTransportAdaptor(state.getConfiguration().isJsonPayload())); | ||
399 | + state.setContentFormat(state.getAdaptor().getContentFormat()); | ||
400 | + } | ||
401 | + | ||
398 | private CoapTransportAdaptor getCoapTransportAdaptor(boolean jsonPayloadType) { | 402 | private CoapTransportAdaptor getCoapTransportAdaptor(boolean jsonPayloadType) { |
399 | return jsonPayloadType ? transportContext.getJsonCoapAdaptor() : transportContext.getProtoCoapAdaptor(); | 403 | return jsonPayloadType ? transportContext.getJsonCoapAdaptor() : transportContext.getProtoCoapAdaptor(); |
400 | } | 404 | } |
@@ -457,7 +461,23 @@ public class DefaultCoapClientContext implements CoapClientContext { | @@ -457,7 +461,23 @@ public class DefaultCoapClientContext implements CoapClientContext { | ||
457 | } | 461 | } |
458 | 462 | ||
459 | @Override | 463 | @Override |
464 | + public void onDeviceProfileUpdate(TransportProtos.SessionInfoProto newSessionInfo, DeviceProfile deviceProfile) { | ||
465 | + try { | ||
466 | + initStateAdaptor(deviceProfile, state); | ||
467 | + } catch (AdaptorException e) { | ||
468 | + log.warn("[{}] Failed to update device profile: ", deviceProfile.getId(), e); | ||
469 | + } | ||
470 | + } | ||
471 | + | ||
472 | + @Override | ||
460 | public void onDeviceUpdate(TransportProtos.SessionInfoProto sessionInfo, Device device, Optional<DeviceProfile> deviceProfileOpt) { | 473 | public void onDeviceUpdate(TransportProtos.SessionInfoProto sessionInfo, Device device, Optional<DeviceProfile> deviceProfileOpt) { |
474 | + if (deviceProfileOpt.isPresent()) { | ||
475 | + try { | ||
476 | + initStateAdaptor(deviceProfileOpt.get(), state); | ||
477 | + } catch (AdaptorException e) { | ||
478 | + log.warn("[{}] Failed to update device: ", device.getId(), e); | ||
479 | + } | ||
480 | + } | ||
461 | state.onDeviceUpdate(device); | 481 | state.onDeviceUpdate(device); |
462 | } | 482 | } |
463 | 483 | ||
@@ -709,9 +729,7 @@ public class DefaultCoapClientContext implements CoapClientContext { | @@ -709,9 +729,7 @@ public class DefaultCoapClientContext implements CoapClientContext { | ||
709 | } | 729 | } |
710 | 730 | ||
711 | private void respond(CoapExchange exchange, Response response, int defContentFormat) { | 731 | private void respond(CoapExchange exchange, Response response, int defContentFormat) { |
712 | - int contentFormat = exchange.getRequestOptions().getContentFormat(); | ||
713 | - contentFormat = contentFormat != MediaTypeRegistry.UNDEFINED ? contentFormat : defContentFormat; | ||
714 | - response.getOptions().setContentFormat(contentFormat); | 732 | + response.getOptions().setContentFormat(TbCoapContentFormatUtil.getContentFormat(exchange.getRequestOptions().getContentFormat(), defContentFormat)); |
715 | exchange.respond(response); | 733 | exchange.respond(response); |
716 | } | 734 | } |
717 | } | 735 | } |
1 | +/** | ||
2 | + * Copyright © 2016-2021 The Thingsboard Authors | ||
3 | + * | ||
4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | ||
5 | + * you may not use this file except in compliance with the License. | ||
6 | + * You may obtain a copy of the License at | ||
7 | + * | ||
8 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
9 | + * | ||
10 | + * Unless required by applicable law or agreed to in writing, software | ||
11 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
13 | + * See the License for the specific language governing permissions and | ||
14 | + * limitations under the License. | ||
15 | + */ | ||
16 | +package org.thingsboard.server.transport.coap.client; | ||
17 | + | ||
18 | +import org.eclipse.californium.core.coap.MediaTypeRegistry; | ||
19 | + | ||
20 | +public class TbCoapContentFormatUtil { | ||
21 | + | ||
22 | + public static int getContentFormat(int requestFormat, int adaptorFormat) { | ||
23 | + if (isStrict(adaptorFormat)) { | ||
24 | + return adaptorFormat; | ||
25 | + } else { | ||
26 | + return requestFormat != MediaTypeRegistry.UNDEFINED ? requestFormat : adaptorFormat; | ||
27 | + } | ||
28 | + } | ||
29 | + | ||
30 | + public static boolean isStrict(int contentFormat) { | ||
31 | + return contentFormat == MediaTypeRegistry.APPLICATION_OCTET_STREAM; | ||
32 | + } | ||
33 | +} |
@@ -317,7 +317,6 @@ public class DefaultLwM2MUplinkMsgHandler extends LwM2MExecutorAwareService impl | @@ -317,7 +317,6 @@ public class DefaultLwM2MUplinkMsgHandler extends LwM2MExecutorAwareService impl | ||
317 | this.updateResourcesValue(lwM2MClient, lwM2mResource, path); | 317 | this.updateResourcesValue(lwM2MClient, lwM2mResource, path); |
318 | } | 318 | } |
319 | } | 319 | } |
320 | - clientContext.update(lwM2MClient); | ||
321 | if (clientContext.awake(lwM2MClient)) { | 320 | if (clientContext.awake(lwM2MClient)) { |
322 | // clientContext.awake calls clientContext.update | 321 | // clientContext.awake calls clientContext.update |
323 | log.debug("[{}] Device is awake", lwM2MClient.getEndpoint()); | 322 | log.debug("[{}] Device is awake", lwM2MClient.getEndpoint()); |