From 61e7dc4a90a104d6c04dd7649c10ed2167ae3961 Mon Sep 17 00:00:00 2001 From: YevhenBondarenko Date: Tue, 23 Nov 2021 16:08:00 +0200 Subject: [PATCH 1/2] fixed lwm2m device or profile update notifications --- .../uplink/DefaultLwM2MUplinkMsgHandler.java | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/common/transport/lwm2m/src/main/java/org/thingsboard/server/transport/lwm2m/server/uplink/DefaultLwM2MUplinkMsgHandler.java b/common/transport/lwm2m/src/main/java/org/thingsboard/server/transport/lwm2m/server/uplink/DefaultLwM2MUplinkMsgHandler.java index 7a2d27d548..dc96b1a9d2 100644 --- a/common/transport/lwm2m/src/main/java/org/thingsboard/server/transport/lwm2m/server/uplink/DefaultLwM2MUplinkMsgHandler.java +++ b/common/transport/lwm2m/src/main/java/org/thingsboard/server/transport/lwm2m/server/uplink/DefaultLwM2MUplinkMsgHandler.java @@ -378,9 +378,13 @@ public class DefaultLwM2MUplinkMsgHandler extends LwM2MExecutorAwareService impl List clients = clientContext.getLwM2mClients() .stream().filter(e -> e.getProfileId() != null) .filter(e -> e.getProfileId().equals(deviceProfile.getUuidId())).collect(Collectors.toList()); - clients.forEach(client -> client.onDeviceProfileUpdate(deviceProfile)); + clients.forEach(client -> { + this.securityStore.remove(client.getEndpoint(), client.getRegistration().getId()); + client.onDeviceProfileUpdate(deviceProfile); + }); if (clients.size() > 0) { - this.onDeviceProfileUpdate(clients, deviceProfile); + var oldProfile = clientContext.getProfile(deviceProfile.getUuidId()); + this.onDeviceProfileUpdate(clients, oldProfile, deviceProfile); } } catch (Exception e) { log.warn("[{}] failed to update profile: {}", deviceProfile.getId(), deviceProfile); @@ -392,6 +396,7 @@ public class DefaultLwM2MUplinkMsgHandler extends LwM2MExecutorAwareService impl try { LwM2mClient client = clientContext.getClientByDeviceId(device.getUuidId()); if (client != null) { + this.securityStore.remove(client.getEndpoint(), client.getRegistration().getId()); this.onDeviceUpdate(client, device, deviceProfileOpt); } } catch (Exception e) { @@ -645,7 +650,8 @@ public class DefaultLwM2MUplinkMsgHandler extends LwM2MExecutorAwareService impl } private void onDeviceUpdate(LwM2mClient lwM2MClient, Device device, Optional deviceProfileOpt) { - deviceProfileOpt.ifPresent(deviceProfile -> this.onDeviceProfileUpdate(Collections.singletonList(lwM2MClient), deviceProfile)); + var oldProfile = clientContext.getProfile(lwM2MClient.getProfileId()); + deviceProfileOpt.ifPresent(deviceProfile -> this.onDeviceProfileUpdate(Collections.singletonList(lwM2MClient), oldProfile, deviceProfile)); lwM2MClient.onDeviceUpdate(device, deviceProfileOpt); } @@ -754,8 +760,7 @@ public class DefaultLwM2MUplinkMsgHandler extends LwM2MExecutorAwareService impl } //TODO: review and optimize the logic to minimize number of the requests to device. - private void onDeviceProfileUpdate(List clients, DeviceProfile deviceProfile) { - var oldProfile = clientContext.getProfile(deviceProfile.getUuidId()); + private void onDeviceProfileUpdate(List clients, Lwm2mDeviceProfileTransportConfiguration oldProfile, DeviceProfile deviceProfile) { if (clientContext.profileUpdate(deviceProfile) != null) { // #1 TelemetryMappingConfiguration oldTelemetryParams = oldProfile.getObserveAttr(); From d024c69a0cfcd37c2d39d2e7c0e30cee53dc038c Mon Sep 17 00:00:00 2001 From: YevhenBondarenko Date: Tue, 30 Nov 2021 12:50:02 +0200 Subject: [PATCH 2/2] improvements (remove security if a new device profile has been set) --- .../lwm2m/server/uplink/DefaultLwM2MUplinkMsgHandler.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/common/transport/lwm2m/src/main/java/org/thingsboard/server/transport/lwm2m/server/uplink/DefaultLwM2MUplinkMsgHandler.java b/common/transport/lwm2m/src/main/java/org/thingsboard/server/transport/lwm2m/server/uplink/DefaultLwM2MUplinkMsgHandler.java index dc96b1a9d2..3a24a19d9b 100644 --- a/common/transport/lwm2m/src/main/java/org/thingsboard/server/transport/lwm2m/server/uplink/DefaultLwM2MUplinkMsgHandler.java +++ b/common/transport/lwm2m/src/main/java/org/thingsboard/server/transport/lwm2m/server/uplink/DefaultLwM2MUplinkMsgHandler.java @@ -392,12 +392,14 @@ public class DefaultLwM2MUplinkMsgHandler extends LwM2MExecutorAwareService impl } @Override - public void onDeviceUpdate(SessionInfoProto sessionInfo, Device device, Optional deviceProfileOpt) { + public void onDeviceUpdate(SessionInfoProto sessionInfo, Device device, Optional newDeviceProfileOpt) { try { LwM2mClient client = clientContext.getClientByDeviceId(device.getUuidId()); if (client != null) { - this.securityStore.remove(client.getEndpoint(), client.getRegistration().getId()); - this.onDeviceUpdate(client, device, deviceProfileOpt); + if (newDeviceProfileOpt.isPresent()) { + this.securityStore.remove(client.getEndpoint(), client.getRegistration().getId()); + } + this.onDeviceUpdate(client, device, newDeviceProfileOpt); } } catch (Exception e) { log.warn("[{}] failed to update device: {}", device.getId(), device);