Commit 5c8618c3ccdf49f47ec2ecc07893955320a7c719
Committed by
Andrew Shvayka
1 parent
1636c54c
added content format for the CoAP responses
Showing
9 changed files
with
43 additions
and
17 deletions
... | ... | @@ -23,6 +23,7 @@ import com.google.protobuf.Descriptors; |
23 | 23 | import com.google.protobuf.DynamicMessage; |
24 | 24 | import lombok.extern.slf4j.Slf4j; |
25 | 25 | import org.eclipse.californium.core.coap.CoAP; |
26 | +import org.eclipse.californium.core.coap.MediaTypeRegistry; | |
26 | 27 | import org.eclipse.californium.core.coap.Request; |
27 | 28 | import org.eclipse.californium.core.coap.Response; |
28 | 29 | import org.springframework.stereotype.Component; |
... | ... | @@ -164,4 +165,8 @@ public class JsonCoapAdaptor implements CoapTransportAdaptor { |
164 | 165 | return payload; |
165 | 166 | } |
166 | 167 | |
168 | + @Override | |
169 | + public int getContentFormat() { | |
170 | + return MediaTypeRegistry.APPLICATION_JSON; | |
171 | + } | |
167 | 172 | } | ... | ... |
... | ... | @@ -23,6 +23,7 @@ import com.google.protobuf.InvalidProtocolBufferException; |
23 | 23 | import com.google.protobuf.util.JsonFormat; |
24 | 24 | import lombok.extern.slf4j.Slf4j; |
25 | 25 | import org.eclipse.californium.core.coap.CoAP; |
26 | +import org.eclipse.californium.core.coap.MediaTypeRegistry; | |
26 | 27 | import org.eclipse.californium.core.coap.Request; |
27 | 28 | import org.eclipse.californium.core.coap.Response; |
28 | 29 | import org.springframework.stereotype.Component; |
... | ... | @@ -162,4 +163,8 @@ public class ProtoCoapAdaptor implements CoapTransportAdaptor { |
162 | 163 | return JsonFormat.printer().includingDefaultValueFields().print(dynamicMessage); |
163 | 164 | } |
164 | 165 | |
166 | + @Override | |
167 | + public int getContentFormat() { | |
168 | + return MediaTypeRegistry.APPLICATION_OCTET_STREAM; | |
169 | + } | |
165 | 170 | } | ... | ... |
... | ... | @@ -17,7 +17,9 @@ package org.thingsboard.server.transport.coap.callback; |
17 | 17 | |
18 | 18 | import lombok.RequiredArgsConstructor; |
19 | 19 | import lombok.extern.slf4j.Slf4j; |
20 | +import org.eclipse.californium.core.coap.MediaTypeRegistry; | |
20 | 21 | import org.eclipse.californium.core.coap.Request; |
22 | +import org.eclipse.californium.core.coap.Response; | |
21 | 23 | import org.eclipse.californium.core.server.resources.CoapExchange; |
22 | 24 | import org.thingsboard.server.common.transport.SessionMsgListener; |
23 | 25 | import org.thingsboard.server.gen.transport.TransportProtos; |
... | ... | @@ -71,4 +73,11 @@ public abstract class AbstractSyncSessionCallback implements SessionMsgListener |
71 | 73 | } |
72 | 74 | } |
73 | 75 | |
76 | + protected void respond(Response response) { | |
77 | + int contentFormat = exchange.getRequestOptions().getContentFormat(); | |
78 | + contentFormat = contentFormat != MediaTypeRegistry.UNDEFINED ? contentFormat : state.getContentFormat(); | |
79 | + response.getOptions().setContentFormat(contentFormat); | |
80 | + exchange.respond(response); | |
81 | + } | |
82 | + | |
74 | 83 | } | ... | ... |
... | ... | @@ -34,7 +34,7 @@ public class GetAttributesSyncSessionCallback extends AbstractSyncSessionCallbac |
34 | 34 | @Override |
35 | 35 | public void onGetAttributesResponse(TransportProtos.GetAttributeResponseMsg msg) { |
36 | 36 | try { |
37 | - exchange.respond(state.getAdaptor().convertToPublish(AbstractSyncSessionCallback.isConRequest(state.getAttrs()), msg)); | |
37 | + respond(state.getAdaptor().convertToPublish(isConRequest(state.getAttrs()), msg)); | |
38 | 38 | } catch (AdaptorException e) { |
39 | 39 | log.trace("[{}] Failed to reply due to error", state.getDeviceId(), e); |
40 | 40 | exchange.respond(new Response(CoAP.ResponseCode.INTERNAL_SERVER_ERROR)); | ... | ... |
... | ... | @@ -33,7 +33,7 @@ public class ToServerRpcSyncSessionCallback extends AbstractSyncSessionCallback |
33 | 33 | @Override |
34 | 34 | public void onToServerRpcResponse(TransportProtos.ToServerRpcResponseMsg toServerResponse) { |
35 | 35 | try { |
36 | - exchange.respond(state.getAdaptor().convertToPublish(isConRequest(state.getRpc()), toServerResponse)); | |
36 | + respond(state.getAdaptor().convertToPublish(isConRequest(state.getRpc()), toServerResponse)); | |
37 | 37 | } catch (AdaptorException e) { |
38 | 38 | log.trace("Failed to reply due to error", e); |
39 | 39 | exchange.respond(CoAP.ResponseCode.INTERNAL_SERVER_ERROR); | ... | ... |
... | ... | @@ -18,6 +18,7 @@ package org.thingsboard.server.transport.coap.client; |
18 | 18 | import lombok.RequiredArgsConstructor; |
19 | 19 | import lombok.extern.slf4j.Slf4j; |
20 | 20 | import org.eclipse.californium.core.coap.CoAP; |
21 | +import org.eclipse.californium.core.coap.MediaTypeRegistry; | |
21 | 22 | import org.eclipse.californium.core.coap.Response; |
22 | 23 | import org.eclipse.californium.core.observe.ObserveRelation; |
23 | 24 | import org.eclipse.californium.core.server.resources.CoapExchange; |
... | ... | @@ -330,6 +331,7 @@ public class DefaultCoapClientContext implements CoapClientContext { |
330 | 331 | if (state.getConfiguration() == null || state.getAdaptor() == null) { |
331 | 332 | state.setConfiguration(getTransportConfigurationContainer(deviceProfile)); |
332 | 333 | state.setAdaptor(getCoapTransportAdaptor(state.getConfiguration().isJsonPayload())); |
334 | + state.setContentFormat(state.getAdaptor().getContentFormat()); | |
333 | 335 | } |
334 | 336 | if (state.getCredentials() == null) { |
335 | 337 | state.init(deviceCredentials); |
... | ... | @@ -409,7 +411,7 @@ public class DefaultCoapClientContext implements CoapClientContext { |
409 | 411 | try { |
410 | 412 | boolean conRequest = AbstractSyncSessionCallback.isConRequest(state.getAttrs()); |
411 | 413 | Response response = state.getAdaptor().convertToPublish(conRequest, msg); |
412 | - attrs.getExchange().respond(response); | |
414 | + respond(attrs.getExchange(), response, state.getContentFormat()); | |
413 | 415 | } catch (AdaptorException e) { |
414 | 416 | log.trace("Failed to reply due to error", e); |
415 | 417 | cancelObserveRelation(attrs); |
... | ... | @@ -440,10 +442,10 @@ public class DefaultCoapClientContext implements CoapClientContext { |
440 | 442 | int requestId = getNextMsgId(); |
441 | 443 | Response response = state.getAdaptor().convertToPublish(conRequest, msg); |
442 | 444 | response.setMID(requestId); |
443 | - attrs.getExchange().respond(response); | |
444 | 445 | if (conRequest) { |
445 | 446 | response.addMessageObserver(new TbCoapMessageObserver(requestId, id -> awake(state), id -> asleep(state))); |
446 | 447 | } |
448 | + respond(attrs.getExchange(), response, state.getContentFormat()); | |
447 | 449 | } catch (AdaptorException e) { |
448 | 450 | log.trace("[{}] Failed to reply due to error", state.getDeviceId(), e); |
449 | 451 | cancelObserveRelation(attrs); |
... | ... | @@ -503,7 +505,7 @@ public class DefaultCoapClientContext implements CoapClientContext { |
503 | 505 | if (conRequest) { |
504 | 506 | response.addMessageObserver(new TbCoapMessageObserver(requestId, id -> awake(state), id -> asleep(state))); |
505 | 507 | } |
506 | - state.getRpc().getExchange().respond(response); | |
508 | + respond(state.getRpc().getExchange(), response, state.getContentFormat()); | |
507 | 509 | sent = true; |
508 | 510 | } catch (AdaptorException e) { |
509 | 511 | log.trace("Failed to reply due to error", e); |
... | ... | @@ -705,4 +707,11 @@ public class DefaultCoapClientContext implements CoapClientContext { |
705 | 707 | state.setAdaptor(null); |
706 | 708 | //TODO: add optimistic lock check that the client was already deleted and cleanup "clients" map. |
707 | 709 | } |
710 | + | |
711 | + 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); | |
715 | + exchange.respond(response); | |
716 | + } | |
708 | 717 | } | ... | ... |
... | ... | @@ -18,26 +18,21 @@ package org.thingsboard.server.transport.coap.client; |
18 | 18 | import lombok.Data; |
19 | 19 | import lombok.Getter; |
20 | 20 | import lombok.Setter; |
21 | -import org.eclipse.leshan.server.registration.Registration; | |
22 | 21 | import org.thingsboard.server.common.data.Device; |
23 | 22 | import org.thingsboard.server.common.data.DeviceTransportType; |
24 | 23 | import org.thingsboard.server.common.data.device.data.CoapDeviceTransportConfiguration; |
25 | 24 | import org.thingsboard.server.common.data.device.data.PowerMode; |
26 | 25 | import org.thingsboard.server.common.data.id.DeviceId; |
27 | 26 | import org.thingsboard.server.common.data.id.DeviceProfileId; |
28 | -import org.thingsboard.server.common.data.id.TenantId; | |
29 | 27 | import org.thingsboard.server.common.transport.auth.ValidateDeviceCredentialsResponse; |
30 | 28 | import org.thingsboard.server.gen.transport.TransportProtos; |
31 | 29 | import org.thingsboard.server.transport.coap.TransportConfigurationContainer; |
32 | 30 | import org.thingsboard.server.transport.coap.adaptors.CoapTransportAdaptor; |
33 | 31 | |
34 | -import java.util.ArrayList; | |
35 | 32 | import java.util.HashMap; |
36 | 33 | import java.util.HashSet; |
37 | -import java.util.List; | |
38 | 34 | import java.util.Map; |
39 | 35 | import java.util.Set; |
40 | -import java.util.UUID; | |
41 | 36 | import java.util.concurrent.Future; |
42 | 37 | import java.util.concurrent.locks.Lock; |
43 | 38 | import java.util.concurrent.locks.ReentrantLock; |
... | ... | @@ -55,6 +50,8 @@ public class TbCoapClientState { |
55 | 50 | private volatile DefaultCoapClientContext.CoapSessionListener listener; |
56 | 51 | private volatile TbCoapObservationState attrs; |
57 | 52 | private volatile TbCoapObservationState rpc; |
53 | + private volatile int contentFormat; | |
54 | + | |
58 | 55 | private TransportProtos.AttributeUpdateNotificationMsg missedAttributeUpdates; |
59 | 56 | |
60 | 57 | private DeviceProfileId profileId; | ... | ... |
... | ... | @@ -137,17 +137,17 @@ public class LwM2mTransportCoapResource extends AbstractLwM2mTransportResource { |
137 | 137 | ); |
138 | 138 | UUID currentId = UUID.fromString(idStr); |
139 | 139 | Response response = new Response(CoAP.ResponseCode.CONTENT); |
140 | - byte[] fwData = this.getOtaData(currentId); | |
141 | - log.debug("Read softWare data (length): [{}]", fwData.length); | |
142 | - if (fwData != null && fwData.length > 0) { | |
143 | - response.setPayload(fwData); | |
140 | + byte[] otaData = this.getOtaData(currentId); | |
141 | + log.debug("Read ota data (length): [{}]", otaData.length); | |
142 | + if (otaData.length > 0) { | |
143 | + response.setPayload(otaData); | |
144 | 144 | if (exchange.getRequestOptions().getBlock2() != null) { |
145 | 145 | int chunkSize = exchange.getRequestOptions().getBlock2().getSzx(); |
146 | - boolean lastFlag = fwData.length <= chunkSize; | |
146 | + boolean lastFlag = otaData.length <= chunkSize; | |
147 | 147 | response.getOptions().setBlock2(chunkSize, lastFlag, 0); |
148 | - log.trace("With block2 Send currentId: [{}], length: [{}], chunkSize [{}], moreFlag [{}]", currentId.toString(), fwData.length, chunkSize, lastFlag); | |
148 | + log.trace("With block2 Send currentId: [{}], length: [{}], chunkSize [{}], moreFlag [{}]", currentId.toString(), otaData.length, chunkSize, lastFlag); | |
149 | 149 | } else { |
150 | - log.trace("With block1 Send currentId: [{}], length: [{}], ", currentId.toString(), fwData.length); | |
150 | + log.trace("With block1 Send currentId: [{}], length: [{}], ", currentId.toString(), otaData.length); | |
151 | 151 | } |
152 | 152 | exchange.respond(response); |
153 | 153 | } | ... | ... |