Commit a8dd25a7c87a14481ab484bb4ea6c38055a2ec57

Authored by Andrii Shvaika
2 parents c9bdce48 50209d91

merge with master

... ... @@ -247,7 +247,7 @@
247 247 "name": "Edit firmware",
248 248 "icon": "edit",
249 249 "type": "customPretty",
250   - "customHtml": "<form #editEntityForm=\"ngForm\" [formGroup]=\"editEntityFormGroup\"\n (ngSubmit)=\"save()\" class=\"edit-entity-form\">\n <mat-toolbar fxLayout=\"row\" color=\"primary\">\n <h2>Edit firmware {{entityName}}</h2>\n <span fxFlex></span>\n <button mat-icon-button (click)=\"cancel()\" type=\"button\">\n <mat-icon class=\"material-icons\">close</mat-icon>\n </button>\n </mat-toolbar>\n <mat-progress-bar color=\"warn\" mode=\"indeterminate\" *ngIf=\"isLoading$ | async\">\n </mat-progress-bar>\n <div style=\"height: 4px;\" *ngIf=\"!(isLoading$ | async)\"></div>\n <div mat-dialog-content fxLayout=\"column\">\n <tb-firmware-autocomplete\n [useFullEntityId]=\"true\"\n formControlName=\"firmwareId\">\n </tb-firmware-autocomplete>\n </div>\n <div mat-dialog-actions fxLayout=\"row\" fxLayoutAlign=\"end center\">\n <button mat-button color=\"primary\"\n type=\"button\"\n [disabled]=\"(isLoading$ | async)\"\n (click)=\"cancel()\" cdkFocusInitial>\n Cancel\n </button>\n <button mat-button mat-raised-button color=\"primary\"\n type=\"submit\"\n [disabled]=\"(isLoading$ | async) || editEntityForm.invalid || !editEntityForm.dirty\">\n Save\n </button>\n </div>\n</form>",
  250 + "customHtml": "<form #editEntityForm=\"ngForm\" [formGroup]=\"editEntityFormGroup\"\n (ngSubmit)=\"save()\" class=\"edit-entity-form\">\n <mat-toolbar fxLayout=\"row\" color=\"primary\">\n <h2>Edit firmware {{entityName}}</h2>\n <span fxFlex></span>\n <button mat-icon-button (click)=\"cancel()\" type=\"button\">\n <mat-icon class=\"material-icons\">close</mat-icon>\n </button>\n </mat-toolbar>\n <mat-progress-bar color=\"warn\" mode=\"indeterminate\" *ngIf=\"isLoading$ | async\">\n </mat-progress-bar>\n <div style=\"height: 4px;\" *ngIf=\"!(isLoading$ | async)\"></div>\n <div *ngIf=\"entity.deviceProfileId\" mat-dialog-content fxLayout=\"column\">\n <tb-firmware-autocomplete\n [useFullEntityId]=\"true\"\n [deviceProfileId]=\"entity.deviceProfileId.id\"\n formControlName=\"firmwareId\">\n </tb-firmware-autocomplete>\n </div>\n <div mat-dialog-actions fxLayout=\"row\" fxLayoutAlign=\"end center\">\n <button mat-button color=\"primary\"\n type=\"button\"\n [disabled]=\"(isLoading$ | async)\"\n (click)=\"cancel()\" cdkFocusInitial>\n Cancel\n </button>\n <button mat-button mat-raised-button color=\"primary\"\n type=\"submit\"\n [disabled]=\"(isLoading$ | async) || editEntityForm.invalid || !editEntityForm.dirty\">\n Save\n </button>\n </div>\n</form>",
251 251 "customCss": "",
252 252 "customFunction": "let $injector = widgetContext.$scope.$injector;\nlet customDialog = $injector.get(widgetContext.servicesMap.get('customDialog'));\nlet entityService = $injector.get(widgetContext.servicesMap.get('entityService'));\nlet deviceService = $injector.get(widgetContext.servicesMap.get('deviceService'));\n\nopenEditEntityDialog();\n\nfunction openEditEntityDialog() {\n customDialog.customDialog(htmlTemplate, EditEntityDialogController).subscribe();\n}\n\nfunction EditEntityDialogController(instance) {\n let vm = instance;\n\n vm.entityName = entityName;\n vm.entity = {};\n\n vm.editEntityFormGroup = vm.fb.group({\n firmwareId: [null]\n });\n\n getEntityInfo();\n\n vm.cancel = function() {\n vm.dialogRef.close(null);\n };\n\n vm.save = function() {\n vm.editEntityFormGroup.markAsPristine();\n saveEntity().subscribe(\n function () {\n // widgetContext.updateAliases();\n vm.dialogRef.close(null);\n }\n );\n };\n\n\n function getEntityInfo() {\n entityService.getEntity(entityId.entityType, entityId.id).subscribe(\n function (data) {\n vm.entity = data;\n vm.editEntityFormGroup.patchValue({\n firmwareId: vm.entity.firmwareId\n }, {emitEvent: false});\n }\n );\n }\n\n function saveEntity() {\n const formValues = vm.editEntityFormGroup.value;\n vm.entity.firmwareId = formValues.firmwareId;\n return deviceService.saveDevice(vm.entity);\n }\n}",
253 253 "customResources": [],
... ...
... ... @@ -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;
... ... @@ -277,23 +279,31 @@ public class DefaultLwM2MTransportMsgHandler implements LwM2mTransportMsgHandler
277 279 @Override
278 280 public void onUpdateValueAfterReadResponse(Registration registration, String path, ReadResponse response, Lwm2mClientRpcRequest rpcRequest) {
279 281 if (response.getContent() != null) {
  282 + Object value = null;
280 283 if (response.getContent() instanceof LwM2mObject) {
281 284 LwM2mObject lwM2mObject = (LwM2mObject) response.getContent();
  285 + if (rpcRequest != null) {
  286 + value = lwM2mObject.toString();
  287 + }
282 288 this.updateObjectResourceValue(registration, lwM2mObject, path);
283 289 } else if (response.getContent() instanceof LwM2mObjectInstance) {
284 290 LwM2mObjectInstance lwM2mObjectInstance = (LwM2mObjectInstance) response.getContent();
  291 + if (rpcRequest != null) {
  292 + value = lwM2mObjectInstance.toString();
  293 + }
285 294 this.updateObjectInstanceResourceValue(registration, lwM2mObjectInstance, path);
286 295 } else if (response.getContent() instanceof LwM2mResource) {
287 296 LwM2mResource lwM2mResource = (LwM2mResource) response.getContent();
288 297 if (rpcRequest != null) {
289   - Object valueResp = lwM2mResource.isMultiInstances() ? lwM2mResource.getValues() : lwM2mResource.getValue();
290   - Object value = this.converter.convertValue(valueResp, lwM2mResource.getType(), ResourceModel.Type.STRING,
291   - new LwM2mPath(convertPathFromIdVerToObjectId(path)));
292   - rpcRequest.setValueMsg(String.format("%s", value));
293   - this.sentRpcRequest(rpcRequest, response.getCode().getName(), (String) value, LOG_LW2M_VALUE);
  298 + value = lwM2mResource.isMultiInstances() ? ((LwM2mMultipleResource) lwM2mResource).toString() :
  299 + ((LwM2mSingleResource) lwM2mResource).toString();
294 300 }
295 301 this.updateResourcesValue(registration, lwM2mResource, path);
296 302 }
  303 + if (rpcRequest != null) {
  304 + rpcRequest.setValueMsg(String.format("%s", value));
  305 + this.sentRpcRequest(rpcRequest, response.getCode().getName(), (String) value, LOG_LW2M_VALUE);
  306 + }
297 307 }
298 308 }
299 309
... ... @@ -600,10 +610,10 @@ public class DefaultLwM2MTransportMsgHandler implements LwM2mTransportMsgHandler
600 610 /**
601 611 * @param registration -
602 612 * @param lwM2mObject -
603   - * @param path -
  613 + * @param pathIdVer -
604 614 */
605   - private void updateObjectResourceValue(Registration registration, LwM2mObject lwM2mObject, String path) {
606   - LwM2mPath pathIds = new LwM2mPath(path);
  615 + private void updateObjectResourceValue(Registration registration, LwM2mObject lwM2mObject, String pathIdVer) {
  616 + LwM2mPath pathIds = new LwM2mPath(convertPathFromIdVerToObjectId(pathIdVer));
607 617 lwM2mObject.getInstances().forEach((instanceId, instance) -> {
608 618 String pathInstance = pathIds.toString() + "/" + instanceId;
609 619 this.updateObjectInstanceResourceValue(registration, instance, pathInstance);
... ... @@ -613,10 +623,10 @@ public class DefaultLwM2MTransportMsgHandler implements LwM2mTransportMsgHandler
613 623 /**
614 624 * @param registration -
615 625 * @param lwM2mObjectInstance -
616   - * @param path -
  626 + * @param pathIdVer -
617 627 */
618   - private void updateObjectInstanceResourceValue(Registration registration, LwM2mObjectInstance lwM2mObjectInstance, String path) {
619   - LwM2mPath pathIds = new LwM2mPath(path);
  628 + private void updateObjectInstanceResourceValue(Registration registration, LwM2mObjectInstance lwM2mObjectInstance, String pathIdVer) {
  629 + LwM2mPath pathIds = new LwM2mPath(convertPathFromIdVerToObjectId(pathIdVer));
620 630 lwM2mObjectInstance.getResources().forEach((resourceId, resource) -> {
621 631 String pathRez = pathIds.toString() + "/" + resourceId;
622 632 this.updateResourcesValue(registration, resource, pathRez);
... ... @@ -862,10 +872,9 @@ public class DefaultLwM2MTransportMsgHandler implements LwM2mTransportMsgHandler
862 872 public void onWriteResponseOk(Registration registration, String path, WriteRequest request) {
863 873 if (request.getNode() instanceof LwM2mResource) {
864 874 this.updateResourcesValue(registration, ((LwM2mResource) request.getNode()), path);
865   - }
866   - else if (request.getNode() instanceof LwM2mObjectInstance) {
  875 + } else if (request.getNode() instanceof LwM2mObjectInstance) {
867 876 ((LwM2mObjectInstance) request.getNode()).getResources().forEach((resId, resource) -> {
868   - this.updateResourcesValue(registration, resource, path+ "/" + resId);
  877 + this.updateResourcesValue(registration, resource, path + "/" + resId);
869 878 });
870 879 }
871 880
... ...
... ... @@ -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;
... ... @@ -176,7 +177,7 @@ public class LwM2mTransportRequest {
176 177 * send request: path = '/3/0' node == wM2mObjectInstance
177 178 * with params == "\"resources\": {15: resource:{id:15. value:'+01'...}}
178 179 **/
179   - Collection<LwM2mResource> resources = lwM2MClient.getNewResourcesForInstance(
  180 + Collection<LwM2mResource> resources = lwM2MClient.getNewOneResourceForInstance(
180 181 targetIdVer, params,
181 182 this.config.getModelProvider(),
182 183 this.converter);
... ... @@ -191,8 +192,14 @@ public class LwM2mTransportRequest {
191 192 */
192 193
193 194 else if (resultIds.isObjectInstance()) {
194   - String content = (String) params;
195   -// node = Gson.fromJson((content, LwM2mNode.class);
  195 + if (((ConcurrentHashMap) params).size() > 0) {
  196 + Collection<LwM2mResource> resources = lwM2MClient.getNewManyResourcesForInstance(
  197 + targetIdVer, params,
  198 + this.config.getModelProvider(),
  199 + this.converter);
  200 + request = new WriteRequest(WriteRequest.Mode.UPDATE, contentFormat, resultIds.getObjectId(),
  201 + resultIds.getObjectInstanceId(), resources);
  202 + }
196 203 } else if (resultIds.getObjectId() >= 0) {
197 204 request = new ObserveRequest(resultIds.getObjectId());
198 205 }
... ... @@ -262,9 +269,9 @@ public class LwM2mTransportRequest {
262 269 * @param timeoutInMs -
263 270 */
264 271
265   - @SuppressWarnings("unchecked")
266   - private void sendRequest(Registration registration, LwM2mClient lwM2MClient, DownlinkRequest request, long timeoutInMs, Lwm2mClientRpcRequest rpcRequest) {
267   -
  272 + @SuppressWarnings({"error sendRequest"})
  273 + private void sendRequest(Registration registration, LwM2mClient lwM2MClient, DownlinkRequest request,
  274 + long timeoutInMs, Lwm2mClientRpcRequest rpcRequest) {
268 275 context.getServer().send(registration, request, timeoutInMs, (ResponseCallback<?>) response -> {
269 276 if (!lwM2MClient.isInit()) {
270 277 lwM2MClient.initReadValue(this.serviceImpl, convertPathFromObjectIdToIdVer(request.getPath().toString(), registration));
... ...
... ... @@ -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
... ... @@ -507,4 +514,24 @@ public class LwM2mTransportUtil {
507 514 return Sets.newConcurrentHashSet(attributeListOld);
508 515 }
509 516
  517 + public static ResourceModel.Type equalsResourceTypeGetSimpleName(Object value) {
  518 + switch (value.getClass().getSimpleName()) {
  519 + case "Double":
  520 + return FLOAT;
  521 + case "Integer":
  522 + return INTEGER;
  523 + case "String":
  524 + return STRING;
  525 + case "Boolean":
  526 + return BOOLEAN;
  527 + case "byte[]":
  528 + return OPAQUE;
  529 + case "Date":
  530 + return TIME;
  531 + case "ObjectLink":
  532 + return OBJLNK;
  533 + default:
  534 + return null;
  535 + }
  536 + }
510 537 }
... ...
... ... @@ -50,6 +50,7 @@ import static org.thingsboard.server.common.data.lwm2m.LwM2mConstants.LWM2M_SEPA
50 50 import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportUtil.TRANSPORT_DEFAULT_LWM2M_VERSION;
51 51 import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportUtil.convertPathFromIdVerToObjectId;
52 52 import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportUtil.getVerFromPathIdVerOrId;
  53 +import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportUtil.equalsResourceTypeGetSimpleName;
53 54
54 55 @Slf4j
55 56 public class LwM2mClient implements Cloneable {
... ... @@ -179,23 +180,40 @@ public class LwM2mClient implements Cloneable {
179 180 }
180 181 }
181 182
182   - public ResourceModel getResourceModel(String pathRez, LwM2mModelProvider modelProvider) {
183   - LwM2mPath pathIds = new LwM2mPath(convertPathFromIdVerToObjectId(pathRez));
  183 + public ResourceModel getResourceModel(String pathIdVer, LwM2mModelProvider modelProvider) {
  184 + LwM2mPath pathIds = new LwM2mPath(convertPathFromIdVerToObjectId(pathIdVer));
184 185 String verSupportedObject = registration.getSupportedObject().get(pathIds.getObjectId());
185   - String verRez = getVerFromPathIdVerOrId(pathRez);
  186 + String verRez = getVerFromPathIdVerOrId(pathIdVer);
186 187 return verRez == null || verRez.equals(verSupportedObject) ? modelProvider.getObjectModel(registration)
187 188 .getResourceModel(pathIds.getObjectId(), pathIds.getResourceId()) : null;
188 189 }
189 190
190   - public Collection<LwM2mResource> getNewResourcesForInstance(String pathRezIdVer, Object params, LwM2mModelProvider modelProvider,
191   - LwM2mValueConverterImpl converter) {
  191 + public Collection<LwM2mResource> getNewOneResourceForInstance(String pathRezIdVer, Object params, LwM2mModelProvider modelProvider,
  192 + LwM2mValueConverterImpl converter) {
192 193 LwM2mPath pathIds = new LwM2mPath(convertPathFromIdVerToObjectId(pathRezIdVer));
193 194 Collection<LwM2mResource> resources = ConcurrentHashMap.newKeySet();
194 195 Map<Integer, ResourceModel> resourceModels = modelProvider.getObjectModel(registration)
195 196 .getObjectModel(pathIds.getObjectId()).resources;
196 197 resourceModels.forEach((resId, resourceModel) -> {
197 198 if (resId == pathIds.getResourceId()) {
198   - resources.add(LwM2mSingleResource.newResource(resId, converter.convertValue(params, ResourceModel.Type.STRING, resourceModel.type, pathIds), resourceModel.type));
  199 + resources.add(LwM2mSingleResource.newResource(resId, converter.convertValue(params,
  200 + equalsResourceTypeGetSimpleName(params), resourceModel.type, pathIds), resourceModel.type));
  201 +
  202 + }});
  203 + return resources;
  204 + }
  205 +
  206 + public Collection<LwM2mResource> getNewManyResourcesForInstance(String pathRezIdVer, Object params, LwM2mModelProvider modelProvider,
  207 + LwM2mValueConverterImpl converter) {
  208 + LwM2mPath pathIds = new LwM2mPath(convertPathFromIdVerToObjectId(pathRezIdVer));
  209 + Collection<LwM2mResource> resources = ConcurrentHashMap.newKeySet();
  210 + Map<Integer, ResourceModel> resourceModels = modelProvider.getObjectModel(registration)
  211 + .getObjectModel(pathIds.getObjectId()).resources;
  212 + resourceModels.forEach((resId, resourceModel) -> {
  213 + if (((ConcurrentHashMap) params).containsKey(String.valueOf(resId))) {
  214 + Object value = ((ConcurrentHashMap) params).get((String.valueOf(resId)));
  215 + resources.add(LwM2mSingleResource.newResource(resId,
  216 + converter.convertValue(value, equalsResourceTypeGetSimpleName(value), resourceModel.type, pathIds), resourceModel.type));
199 217
200 218 }});
201 219 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 }
... ...