Commit a4e3049c279cb46f3181f6b2b1c8d507796039ae

Authored by nickAS21
1 parent a9481012

Lwm2m: content format for observe fx bug

... ... @@ -236,6 +236,12 @@ public class LwM2mClient implements Cloneable {
236 236 .getResourceModel(pathIds.getObjectId(), pathIds.getResourceId()) : null;
237 237 }
238 238
  239 + public boolean isResourceMultiInstances(String pathIdVer, LwM2mModelProvider modelProvider) {
  240 + ResourceModel resourceModel = getResourceModel(pathIdVer, modelProvider);
  241 + return resourceModel.multiple;
  242 +
  243 + }
  244 +
239 245 public ObjectModel getObjectModel(String pathIdVer, LwM2mModelProvider modelProvider) {
240 246 LwM2mPath pathIds = new LwM2mPath(fromVersionedIdToObjectId(pathIdVer));
241 247 String verSupportedObject = registration.getSupportedObject().get(pathIds.getObjectId());
... ... @@ -273,7 +279,6 @@ public class LwM2mClient implements Cloneable {
273 279 }
274 280
275 281 public String resourceToString(LwM2mResource lwM2mResource) {
276   -
277 282 return lwM2mResource.isMultiInstances() ? lwM2mResource.getInstances().toString() : lwM2mResource.getValue().toString();
278 283 }
279 284
... ...
... ... @@ -48,6 +48,7 @@ import org.eclipse.leshan.core.response.WriteAttributesResponse;
48 48 import org.eclipse.leshan.core.response.WriteCompositeResponse;
49 49 import org.eclipse.leshan.core.response.WriteResponse;
50 50 import org.eclipse.leshan.core.util.Hex;
  51 +import org.eclipse.leshan.server.model.LwM2mModelProvider;
51 52 import org.eclipse.leshan.server.registration.Registration;
52 53 import org.springframework.stereotype.Service;
53 54 import org.thingsboard.common.util.JacksonUtil;
... ... @@ -119,7 +120,7 @@ public class DefaultLwM2mDownlinkMsgHandler extends LwM2MExecutorAwareService im
119 120 @Override
120 121 public void sendReadRequest(LwM2mClient client, TbLwM2MReadRequest request, DownlinkRequestCallback<ReadRequest, ReadResponse> callback) {
121 122 validateVersionedId(client, request);
122   - ReadRequest downlink = new ReadRequest(getRequestContentFormat(client, request), request.getObjectId());
  123 + ReadRequest downlink = new ReadRequest(getRequestContentFormat(client, request, this.config.getModelProvider()), request.getObjectId());
123 124 sendRequest(client, downlink, request.getTimeout(), callback);
124 125 }
125 126
... ... @@ -155,7 +156,7 @@ public class DefaultLwM2mDownlinkMsgHandler extends LwM2MExecutorAwareService im
155 156 Set<Observation> observations = context.getServer().getObservationService().getObservations(client.getRegistration());
156 157 if (observations.stream().noneMatch(observation -> observation.getPath().equals(resultIds))) {
157 158 ObserveRequest downlink;
158   - ContentFormat contentFormat = getRequestContentFormat(client, request);
  159 + ContentFormat contentFormat = getRequestContentFormat(client, request, this.config.getModelProvider());
159 160 if (resultIds.isResource()) {
160 161 downlink = new ObserveRequest(contentFormat, resultIds.getObjectId(), resultIds.getObjectInstanceId(), resultIds.getResourceId());
161 162 } else if (resultIds.isObjectInstance()) {
... ... @@ -457,16 +458,23 @@ public class DefaultLwM2mDownlinkMsgHandler extends LwM2MExecutorAwareService im
457 458 throw new CodecException("Invalid ResourceModel_Type for %s ContentFormat.", type);
458 459 }
459 460
460   - private static ContentFormat getRequestContentFormat(LwM2mClient client, HasContentFormat request) {
  461 + private static ContentFormat getRequestContentFormat(LwM2mClient client, HasContentFormat request, LwM2mModelProvider modelProvider) {
461 462 if (request.getRequestContentFormat() != null) {
462 463 return request.getRequestContentFormat();
463 464 } else {
464   - String versionedId = fromVersionedIdToObjectId(((TbLwM2MReadRequest) request).getVersionedId());
465   - if (versionedId != null && (new LwM2mPath(versionedId).isObject() || new LwM2mPath(versionedId).isObjectInstance())) {
466   - return ContentFormat.DEFAULT;
467   - } else {
  465 + String versionedId = null;
  466 + if (request instanceof TbLwM2MReadRequest) {
  467 + versionedId = ((TbLwM2MReadRequest) request).getVersionedId();
  468 + } else if (request instanceof TbLwM2MObserveRequest) {
  469 + versionedId = ((TbLwM2MObserveRequest) request).getVersionedId();
  470 + }
  471 + String id = fromVersionedIdToObjectId(versionedId);
  472 + if (id != null && !client.isResourceMultiInstances(versionedId, modelProvider)) {
468 473 return client.getDefaultContentFormat();
469 474 }
  475 + else {
  476 + return ContentFormat.DEFAULT;
  477 + }
470 478 }
471 479 }
472 480 }
... ...