Commit 50209d91fe161c32e03d7fff8f20946b3feda52e
Committed by
Andrew Shvayka
1 parent
3fb993b4
Lwm2m: add Write_Update many
Showing
5 changed files
with
91 additions
and
32 deletions
... | ... | @@ -24,10 +24,12 @@ import com.google.gson.JsonObject; |
24 | 24 | import com.google.gson.reflect.TypeToken; |
25 | 25 | import lombok.extern.slf4j.Slf4j; |
26 | 26 | import org.eclipse.leshan.core.model.ResourceModel; |
27 | +import org.eclipse.leshan.core.node.LwM2mMultipleResource; | |
27 | 28 | import org.eclipse.leshan.core.node.LwM2mObject; |
28 | 29 | import org.eclipse.leshan.core.node.LwM2mObjectInstance; |
29 | 30 | import org.eclipse.leshan.core.node.LwM2mPath; |
30 | 31 | import org.eclipse.leshan.core.node.LwM2mResource; |
32 | +import org.eclipse.leshan.core.node.LwM2mSingleResource; | |
31 | 33 | import org.eclipse.leshan.core.observation.Observation; |
32 | 34 | import org.eclipse.leshan.core.request.ContentFormat; |
33 | 35 | import org.eclipse.leshan.core.request.WriteRequest; |
... | ... | @@ -54,6 +56,7 @@ import org.thingsboard.server.gen.transport.TransportProtos.SessionEvent; |
54 | 56 | import org.thingsboard.server.gen.transport.TransportProtos.SessionInfoProto; |
55 | 57 | import org.thingsboard.server.queue.util.TbLwM2mTransportComponent; |
56 | 58 | import org.thingsboard.server.transport.lwm2m.config.LwM2MTransportServerConfig; |
59 | +import org.thingsboard.server.transport.lwm2m.server.LwM2mTransportHandlerUtil.LwM2mTypeOper; | |
57 | 60 | import org.thingsboard.server.transport.lwm2m.server.client.LwM2mClient; |
58 | 61 | import org.thingsboard.server.transport.lwm2m.server.client.LwM2mClientContext; |
59 | 62 | import org.thingsboard.server.transport.lwm2m.server.client.LwM2mClientProfile; |
... | ... | @@ -92,7 +95,6 @@ import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportHandle |
92 | 95 | import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportHandlerUtil.LOG_LW2M_INFO; |
93 | 96 | import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportHandlerUtil.LOG_LW2M_VALUE; |
94 | 97 | import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportHandlerUtil.LWM2M_STRATEGY_2; |
95 | -import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportHandlerUtil.LwM2mTypeOper; | |
96 | 98 | import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportHandlerUtil.LwM2mTypeOper.DISCOVER; |
97 | 99 | import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportHandlerUtil.LwM2mTypeOper.EXECUTE; |
98 | 100 | import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportHandlerUtil.LwM2mTypeOper.OBSERVE; |
... | ... | @@ -294,23 +296,31 @@ public class DefaultLwM2MTransportMsgHandler implements LwM2mTransportMsgHandler |
294 | 296 | @Override |
295 | 297 | public void onUpdateValueAfterReadResponse(Registration registration, String path, ReadResponse response, Lwm2mClientRpcRequest rpcRequest) { |
296 | 298 | if (response.getContent() != null) { |
299 | + Object value = null; | |
297 | 300 | if (response.getContent() instanceof LwM2mObject) { |
298 | 301 | LwM2mObject lwM2mObject = (LwM2mObject) response.getContent(); |
302 | + if (rpcRequest != null) { | |
303 | + value = lwM2mObject.toString(); | |
304 | + } | |
299 | 305 | this.updateObjectResourceValue(registration, lwM2mObject, path); |
300 | 306 | } else if (response.getContent() instanceof LwM2mObjectInstance) { |
301 | 307 | LwM2mObjectInstance lwM2mObjectInstance = (LwM2mObjectInstance) response.getContent(); |
308 | + if (rpcRequest != null) { | |
309 | + value = lwM2mObjectInstance.toString(); | |
310 | + } | |
302 | 311 | this.updateObjectInstanceResourceValue(registration, lwM2mObjectInstance, path); |
303 | 312 | } else if (response.getContent() instanceof LwM2mResource) { |
304 | 313 | LwM2mResource lwM2mResource = (LwM2mResource) response.getContent(); |
305 | 314 | if (rpcRequest != null) { |
306 | - Object valueResp = lwM2mResource.isMultiInstances() ? lwM2mResource.getValues() : lwM2mResource.getValue(); | |
307 | - Object value = this.converter.convertValue(valueResp, lwM2mResource.getType(), ResourceModel.Type.STRING, | |
308 | - new LwM2mPath(convertPathFromIdVerToObjectId(path))); | |
309 | - rpcRequest.setValueMsg(String.format("%s", value)); | |
310 | - this.sentRpcRequest(rpcRequest, response.getCode().getName(), (String) value, LOG_LW2M_VALUE); | |
315 | + value = lwM2mResource.isMultiInstances() ? ((LwM2mMultipleResource) lwM2mResource).toString() : | |
316 | + ((LwM2mSingleResource) lwM2mResource).toString(); | |
311 | 317 | } |
312 | 318 | this.updateResourcesValue(registration, lwM2mResource, path); |
313 | 319 | } |
320 | + if (rpcRequest != null) { | |
321 | + rpcRequest.setValueMsg(String.format("%s", value)); | |
322 | + this.sentRpcRequest(rpcRequest, response.getCode().getName(), (String) value, LOG_LW2M_VALUE); | |
323 | + } | |
314 | 324 | } |
315 | 325 | } |
316 | 326 | |
... | ... | @@ -616,7 +626,7 @@ public class DefaultLwM2MTransportMsgHandler implements LwM2mTransportMsgHandler |
616 | 626 | public void sendLogsToThingsboard(String logMsg, String registrationId) { |
617 | 627 | SessionInfoProto sessionInfo = this.getValidateSessionInfo(registrationId); |
618 | 628 | if (logMsg != null && sessionInfo != null) { |
619 | - if(logMsg.length() > 1024){ | |
629 | + if (logMsg.length() > 1024) { | |
620 | 630 | logMsg = logMsg.substring(0, 1024); |
621 | 631 | } |
622 | 632 | this.helper.sendParametersOnThingsboardTelemetry(this.helper.getKvLogyToThingsboard(logMsg), sessionInfo); |
... | ... | @@ -656,10 +666,10 @@ public class DefaultLwM2MTransportMsgHandler implements LwM2mTransportMsgHandler |
656 | 666 | /** |
657 | 667 | * @param registration - |
658 | 668 | * @param lwM2mObject - |
659 | - * @param path - | |
669 | + * @param pathIdVer - | |
660 | 670 | */ |
661 | - private void updateObjectResourceValue(Registration registration, LwM2mObject lwM2mObject, String path) { | |
662 | - LwM2mPath pathIds = new LwM2mPath(path); | |
671 | + private void updateObjectResourceValue(Registration registration, LwM2mObject lwM2mObject, String pathIdVer) { | |
672 | + LwM2mPath pathIds = new LwM2mPath(convertPathFromIdVerToObjectId(pathIdVer)); | |
663 | 673 | lwM2mObject.getInstances().forEach((instanceId, instance) -> { |
664 | 674 | String pathInstance = pathIds.toString() + "/" + instanceId; |
665 | 675 | this.updateObjectInstanceResourceValue(registration, instance, pathInstance); |
... | ... | @@ -669,10 +679,10 @@ public class DefaultLwM2MTransportMsgHandler implements LwM2mTransportMsgHandler |
669 | 679 | /** |
670 | 680 | * @param registration - |
671 | 681 | * @param lwM2mObjectInstance - |
672 | - * @param path - | |
682 | + * @param pathIdVer - | |
673 | 683 | */ |
674 | - private void updateObjectInstanceResourceValue(Registration registration, LwM2mObjectInstance lwM2mObjectInstance, String path) { | |
675 | - LwM2mPath pathIds = new LwM2mPath(path); | |
684 | + private void updateObjectInstanceResourceValue(Registration registration, LwM2mObjectInstance lwM2mObjectInstance, String pathIdVer) { | |
685 | + LwM2mPath pathIds = new LwM2mPath(convertPathFromIdVerToObjectId(pathIdVer)); | |
676 | 686 | lwM2mObjectInstance.getResources().forEach((resourceId, resource) -> { |
677 | 687 | String pathRez = pathIds.toString() + "/" + resourceId; |
678 | 688 | this.updateResourcesValue(registration, resource, pathRez); |
... | ... | @@ -782,8 +792,8 @@ public class DefaultLwM2MTransportMsgHandler implements LwM2mTransportMsgHandler |
782 | 792 | lwM2MClient.getPendingReadRequests().addAll(pathSend); |
783 | 793 | ConcurrentHashMap<String, Object> finalParams = params; |
784 | 794 | pathSend.forEach(target -> { |
785 | - lwM2mTransportRequest.sendAllRequest(registration, target, typeOper, ContentFormat.TLV.getName(), | |
786 | - finalParams != null ? finalParams.get(target) : null, this.config.getTimeout(), null); | |
795 | + lwM2mTransportRequest.sendAllRequest(registration, target, typeOper, ContentFormat.TLV.getName(), | |
796 | + finalParams != null ? finalParams.get(target) : null, this.config.getTimeout(), null); | |
787 | 797 | }); |
788 | 798 | if (OBSERVE.equals(typeOper)) { |
789 | 799 | lwM2MClient.initReadValue(this, null); |
... | ... | @@ -932,10 +942,9 @@ public class DefaultLwM2MTransportMsgHandler implements LwM2mTransportMsgHandler |
932 | 942 | public void onWriteResponseOk(Registration registration, String path, WriteRequest request) { |
933 | 943 | if (request.getNode() instanceof LwM2mResource) { |
934 | 944 | this.updateResourcesValue(registration, ((LwM2mResource) request.getNode()), path); |
935 | - } | |
936 | - else if (request.getNode() instanceof LwM2mObjectInstance) { | |
945 | + } else if (request.getNode() instanceof LwM2mObjectInstance) { | |
937 | 946 | ((LwM2mObjectInstance) request.getNode()).getResources().forEach((resId, resource) -> { |
938 | - this.updateResourcesValue(registration, resource, path+ "/" + resId); | |
947 | + this.updateResourcesValue(registration, resource, path + "/" + resId); | |
939 | 948 | }); |
940 | 949 | } |
941 | 950 | |
... | ... | @@ -1421,7 +1430,6 @@ public class DefaultLwM2MTransportMsgHandler implements LwM2mTransportMsgHandler |
1421 | 1430 | } |
1422 | 1431 | |
1423 | 1432 | /** |
1424 | - * | |
1425 | 1433 | * @param lwM2MClient - |
1426 | 1434 | */ |
1427 | 1435 | public void updateFirmwareClient(LwM2mClient lwM2MClient) { | ... | ... |
... | ... | @@ -63,6 +63,13 @@ import static org.eclipse.leshan.core.attributes.Attribute.DIMENSION; |
63 | 63 | import static org.eclipse.leshan.core.attributes.Attribute.MAXIMUM_PERIOD; |
64 | 64 | import static org.eclipse.leshan.core.attributes.Attribute.MINIMUM_PERIOD; |
65 | 65 | import static org.eclipse.leshan.core.attributes.Attribute.OBJECT_VERSION; |
66 | +import static org.eclipse.leshan.core.model.ResourceModel.Type.BOOLEAN; | |
67 | +import static org.eclipse.leshan.core.model.ResourceModel.Type.FLOAT; | |
68 | +import static org.eclipse.leshan.core.model.ResourceModel.Type.INTEGER; | |
69 | +import static org.eclipse.leshan.core.model.ResourceModel.Type.OBJLNK; | |
70 | +import static org.eclipse.leshan.core.model.ResourceModel.Type.OPAQUE; | |
71 | +import static org.eclipse.leshan.core.model.ResourceModel.Type.STRING; | |
72 | +import static org.eclipse.leshan.core.model.ResourceModel.Type.TIME; | |
66 | 73 | import static org.thingsboard.server.common.data.lwm2m.LwM2mConstants.LWM2M_SEPARATOR_KEY; |
67 | 74 | import static org.thingsboard.server.common.data.lwm2m.LwM2mConstants.LWM2M_SEPARATOR_PATH; |
68 | 75 | |
... | ... | @@ -508,4 +515,24 @@ public class LwM2mTransportHandlerUtil { |
508 | 515 | return Sets.newConcurrentHashSet(attributeListOld); |
509 | 516 | } |
510 | 517 | |
518 | + public static ResourceModel.Type equalsResourceTypeGetSimpleName(Object value) { | |
519 | + switch (value.getClass().getSimpleName()) { | |
520 | + case "Double": | |
521 | + return FLOAT; | |
522 | + case "Integer": | |
523 | + return INTEGER; | |
524 | + case "String": | |
525 | + return STRING; | |
526 | + case "Boolean": | |
527 | + return BOOLEAN; | |
528 | + case "byte[]": | |
529 | + return OPAQUE; | |
530 | + case "Date": | |
531 | + return TIME; | |
532 | + case "ObjectLink": | |
533 | + return OBJLNK; | |
534 | + default: | |
535 | + return null; | |
536 | + } | |
537 | + } | |
511 | 538 | } | ... | ... |
... | ... | @@ -60,6 +60,7 @@ import java.util.Arrays; |
60 | 60 | import java.util.Collection; |
61 | 61 | import java.util.Date; |
62 | 62 | import java.util.Set; |
63 | +import java.util.concurrent.ConcurrentHashMap; | |
63 | 64 | import java.util.concurrent.ExecutorService; |
64 | 65 | import java.util.concurrent.Executors; |
65 | 66 | import java.util.stream.Collectors; |
... | ... | @@ -178,7 +179,7 @@ public class LwM2mTransportRequest { |
178 | 179 | * send request: path = '/3/0' node == wM2mObjectInstance |
179 | 180 | * with params == "\"resources\": {15: resource:{id:15. value:'+01'...}} |
180 | 181 | **/ |
181 | - Collection<LwM2mResource> resources = lwM2MClient.getNewResourcesForInstance( | |
182 | + Collection<LwM2mResource> resources = lwM2MClient.getNewOneResourceForInstance( | |
182 | 183 | targetIdVer, params, |
183 | 184 | this.config.getModelProvider(), |
184 | 185 | this.converter); |
... | ... | @@ -193,8 +194,14 @@ public class LwM2mTransportRequest { |
193 | 194 | */ |
194 | 195 | |
195 | 196 | else if (resultIds.isObjectInstance()) { |
196 | - String content = (String) params; | |
197 | -// node = Gson.fromJson((content, LwM2mNode.class); | |
197 | + if (((ConcurrentHashMap) params).size() > 0) { | |
198 | + Collection<LwM2mResource> resources = lwM2MClient.getNewManyResourcesForInstance( | |
199 | + targetIdVer, params, | |
200 | + this.config.getModelProvider(), | |
201 | + this.converter); | |
202 | + request = new WriteRequest(WriteRequest.Mode.UPDATE, contentFormat, resultIds.getObjectId(), | |
203 | + resultIds.getObjectInstanceId(), resources); | |
204 | + } | |
198 | 205 | } else if (resultIds.getObjectId() >= 0) { |
199 | 206 | request = new ObserveRequest(resultIds.getObjectId()); |
200 | 207 | } |
... | ... | @@ -264,9 +271,9 @@ public class LwM2mTransportRequest { |
264 | 271 | * @param timeoutInMs - |
265 | 272 | */ |
266 | 273 | |
267 | - @SuppressWarnings("unchecked") | |
268 | - private void sendRequest(Registration registration, LwM2mClient lwM2MClient, DownlinkRequest request, long timeoutInMs, Lwm2mClientRpcRequest rpcRequest) { | |
269 | - | |
274 | + @SuppressWarnings({"error sendRequest"}) | |
275 | + private void sendRequest(Registration registration, LwM2mClient lwM2MClient, DownlinkRequest request, | |
276 | + long timeoutInMs, Lwm2mClientRpcRequest rpcRequest) { | |
270 | 277 | context.getServer().send(registration, request, timeoutInMs, (ResponseCallback<?>) response -> { |
271 | 278 | if (!lwM2MClient.isInit()) { |
272 | 279 | lwM2MClient.initReadValue(this.serviceImpl, convertPathFromObjectIdToIdVer(request.getPath().toString(), registration)); | ... | ... |
... | ... | @@ -44,6 +44,7 @@ import java.util.stream.Collectors; |
44 | 44 | import static org.thingsboard.server.common.data.lwm2m.LwM2mConstants.LWM2M_SEPARATOR_PATH; |
45 | 45 | import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportHandlerUtil.TRANSPORT_DEFAULT_LWM2M_VERSION; |
46 | 46 | import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportHandlerUtil.convertPathFromIdVerToObjectId; |
47 | +import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportHandlerUtil.equalsResourceTypeGetSimpleName; | |
47 | 48 | import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportHandlerUtil.getVerFromPathIdVerOrId; |
48 | 49 | |
49 | 50 | @Slf4j |
... | ... | @@ -103,23 +104,40 @@ public class LwM2mClient implements Cloneable { |
103 | 104 | } |
104 | 105 | } |
105 | 106 | |
106 | - public ResourceModel getResourceModel(String pathRez, LwM2mModelProvider modelProvider) { | |
107 | - LwM2mPath pathIds = new LwM2mPath(convertPathFromIdVerToObjectId(pathRez)); | |
107 | + public ResourceModel getResourceModel(String pathIdVer, LwM2mModelProvider modelProvider) { | |
108 | + LwM2mPath pathIds = new LwM2mPath(convertPathFromIdVerToObjectId(pathIdVer)); | |
108 | 109 | String verSupportedObject = registration.getSupportedObject().get(pathIds.getObjectId()); |
109 | - String verRez = getVerFromPathIdVerOrId(pathRez); | |
110 | + String verRez = getVerFromPathIdVerOrId(pathIdVer); | |
110 | 111 | return verRez == null || verRez.equals(verSupportedObject) ? modelProvider.getObjectModel(registration) |
111 | 112 | .getResourceModel(pathIds.getObjectId(), pathIds.getResourceId()) : null; |
112 | 113 | } |
113 | 114 | |
114 | - public Collection<LwM2mResource> getNewResourcesForInstance(String pathRezIdVer, Object params, LwM2mModelProvider modelProvider, | |
115 | - LwM2mValueConverterImpl converter) { | |
115 | + public Collection<LwM2mResource> getNewOneResourceForInstance(String pathRezIdVer, Object params, LwM2mModelProvider modelProvider, | |
116 | + LwM2mValueConverterImpl converter) { | |
116 | 117 | LwM2mPath pathIds = new LwM2mPath(convertPathFromIdVerToObjectId(pathRezIdVer)); |
117 | 118 | Collection<LwM2mResource> resources = ConcurrentHashMap.newKeySet(); |
118 | 119 | Map<Integer, ResourceModel> resourceModels = modelProvider.getObjectModel(registration) |
119 | 120 | .getObjectModel(pathIds.getObjectId()).resources; |
120 | 121 | resourceModels.forEach((resId, resourceModel) -> { |
121 | 122 | if (resId == pathIds.getResourceId()) { |
122 | - resources.add(LwM2mSingleResource.newResource(resId, converter.convertValue(params, ResourceModel.Type.STRING, resourceModel.type, pathIds), resourceModel.type)); | |
123 | + resources.add(LwM2mSingleResource.newResource(resId, converter.convertValue(params, | |
124 | + equalsResourceTypeGetSimpleName(params), resourceModel.type, pathIds), resourceModel.type)); | |
125 | + | |
126 | + }}); | |
127 | + return resources; | |
128 | + } | |
129 | + | |
130 | + public Collection<LwM2mResource> getNewManyResourcesForInstance(String pathRezIdVer, Object params, LwM2mModelProvider modelProvider, | |
131 | + LwM2mValueConverterImpl converter) { | |
132 | + LwM2mPath pathIds = new LwM2mPath(convertPathFromIdVerToObjectId(pathRezIdVer)); | |
133 | + Collection<LwM2mResource> resources = ConcurrentHashMap.newKeySet(); | |
134 | + Map<Integer, ResourceModel> resourceModels = modelProvider.getObjectModel(registration) | |
135 | + .getObjectModel(pathIds.getObjectId()).resources; | |
136 | + resourceModels.forEach((resId, resourceModel) -> { | |
137 | + if (((ConcurrentHashMap) params).containsKey(String.valueOf(resId))) { | |
138 | + Object value = ((ConcurrentHashMap) params).get((String.valueOf(resId))); | |
139 | + resources.add(LwM2mSingleResource.newResource(resId, | |
140 | + converter.convertValue(value, equalsResourceTypeGetSimpleName(value), resourceModel.type, pathIds), resourceModel.type)); | |
123 | 141 | |
124 | 142 | }}); |
125 | 143 | return resources; | ... | ... |
... | ... | @@ -177,7 +177,6 @@ public class LwM2mValueConverterImpl implements LwM2mValueConverter { |
177 | 177 | } |
178 | 178 | default: |
179 | 179 | } |
180 | - | |
181 | 180 | throw new CodecException("Invalid value type for resource %s, expected %s, got %s", resourcePath, expectedType, |
182 | 181 | currentType); |
183 | 182 | } | ... | ... |