diff --git a/common/transport/lwm2m/src/main/java/org/thingsboard/server/transport/lwm2m/server/client/LwM2mClientContextImpl.java b/common/transport/lwm2m/src/main/java/org/thingsboard/server/transport/lwm2m/server/client/LwM2mClientContextImpl.java index 36cf1639db..7b3102a55c 100644 --- a/common/transport/lwm2m/src/main/java/org/thingsboard/server/transport/lwm2m/server/client/LwM2mClientContextImpl.java +++ b/common/transport/lwm2m/src/main/java/org/thingsboard/server/transport/lwm2m/server/client/LwM2mClientContextImpl.java @@ -289,6 +289,7 @@ public class LwM2mClientContextImpl implements LwM2mClientContext { @Override public String getObjectIdByKeyNameFromProfile(LwM2mClient client, String keyName) { Lwm2mDeviceProfileTransportConfiguration profile = getProfile(client.getRegistration()); + if (profile == null) throw new IllegalArgumentException(keyName + " is not configured in the device profile! Device profile is null"); for (Map.Entry entry : profile.getObserveAttr().getKeyName().entrySet()) { String k = entry.getKey(); String v = entry.getValue(); @@ -347,6 +348,7 @@ public class LwM2mClientContextImpl implements LwM2mClientContext { PowerMode powerMode = lwM2MClient.getPowerMode(); if (powerMode == null) { Lwm2mDeviceProfileTransportConfiguration deviceProfile = getProfile(lwM2MClient.getRegistration()); + if (deviceProfile == null) return null; powerMode = deviceProfile.getClientLwM2mSettings().getPowerMode(); } return powerMode; @@ -377,8 +379,8 @@ public class LwM2mClientContextImpl implements LwM2mClientContext { } } } else { - log.warn("Device profile not found! The device profile ID is null. Return Lwm2mDeviceProfileTransportConfiguration with default."); - result = new Lwm2mDeviceProfileTransportConfiguration(); + log.warn("Device profile not found! The device profile ID is null. Return Lwm2mDeviceProfileTransportConfiguration with null."); + result = null; } return result; } @@ -413,6 +415,7 @@ public class LwM2mClientContextImpl implements LwM2mClientContext { OtherConfiguration profileSettings = null; if (powerMode == null && client.getProfileId() != null) { var clientProfile = getProfile(client.getRegistration()); + if (clientProfile == null) return true; profileSettings = clientProfile.getClientLwM2mSettings(); powerMode = profileSettings.getPowerMode(); } @@ -459,8 +462,10 @@ public class LwM2mClientContextImpl implements LwM2mClientContext { OtherConfiguration profileSettings = null; if (powerMode == null && client.getProfileId() != null) { var clientProfile = getProfile(client.getRegistration()); - profileSettings = clientProfile.getClientLwM2mSettings(); - powerMode = profileSettings.getPowerMode(); + if (clientProfile != null) { + profileSettings = clientProfile.getClientLwM2mSettings(); + powerMode = profileSettings.getPowerMode(); + } } if (powerMode == null || PowerMode.DRX.equals(powerMode)) { client.updateLastUplinkTime(); @@ -515,9 +520,11 @@ public class LwM2mClientContextImpl implements LwM2mClientContext { timeout = client.getEdrxCycle(); } else { var clientProfile = getProfile(client.getRegistration()); - OtherConfiguration clientLwM2mSettings = clientProfile.getClientLwM2mSettings(); - if (PowerMode.E_DRX.equals(clientLwM2mSettings.getPowerMode())) { - timeout = clientLwM2mSettings.getEdrxCycle(); + if (clientProfile != null) { + OtherConfiguration clientLwM2mSettings = clientProfile.getClientLwM2mSettings(); + if (PowerMode.E_DRX.equals(clientLwM2mSettings.getPowerMode())) { + timeout = clientLwM2mSettings.getEdrxCycle(); + } } } if (timeout == null || timeout == 0L) { diff --git a/common/transport/lwm2m/src/main/java/org/thingsboard/server/transport/lwm2m/server/ota/DefaultLwM2MOtaUpdateService.java b/common/transport/lwm2m/src/main/java/org/thingsboard/server/transport/lwm2m/server/ota/DefaultLwM2MOtaUpdateService.java index 325f1e2280..cc89fb8fd8 100644 --- a/common/transport/lwm2m/src/main/java/org/thingsboard/server/transport/lwm2m/server/ota/DefaultLwM2MOtaUpdateService.java +++ b/common/transport/lwm2m/src/main/java/org/thingsboard/server/transport/lwm2m/server/ota/DefaultLwM2MOtaUpdateService.java @@ -200,7 +200,8 @@ public class DefaultLwM2MOtaUpdateService extends LwM2MExecutorAwareService impl attributesToFetch.add(SOFTWARE_URL); } - var clientSettings = clientContext.getProfile(client.getRegistration()).getClientLwM2mSettings(); + var clientProfile = clientContext.getProfile(client.getRegistration()); + var clientSettings = clientProfile != null ? clientProfile.getClientLwM2mSettings() : null; if (clientSettings != null) { initFwStrategy(client, clientSettings); initSwStrategy(client, clientSettings); @@ -532,7 +533,8 @@ public class DefaultLwM2MOtaUpdateService extends LwM2MExecutorAwareService impl } else { strategy = info.getDeliveryMethod() == FirmwareDeliveryMethod.PULL.code ? LwM2MFirmwareUpdateStrategy.OBJ_5_TEMP_URL : LwM2MFirmwareUpdateStrategy.OBJ_5_BINARY; } - Boolean useObject19ForOtaInfo = clientContext.getProfile(client.getRegistration()).getClientLwM2mSettings().getUseObject19ForOtaInfo(); + var clientProfile = clientContext.getProfile(client.getRegistration()); + Boolean useObject19ForOtaInfo = clientProfile != null ? clientProfile.getClientLwM2mSettings().getUseObject19ForOtaInfo() : null; if (useObject19ForOtaInfo != null && useObject19ForOtaInfo){ sendInfoToObject19ForOta(client, FW_INFO_19_INSTANCE_ID, response, otaPackageId); } @@ -558,7 +560,8 @@ public class DefaultLwM2MOtaUpdateService extends LwM2MExecutorAwareService impl if (TransportProtos.ResponseStatus.SUCCESS.equals(response.getResponseStatus())) { UUID otaPackageId = new UUID(response.getOtaPackageIdMSB(), response.getOtaPackageIdLSB()); LwM2MSoftwareUpdateStrategy strategy = info.getStrategy(); - Boolean useObject19ForOtaInfo = clientContext.getProfile(client.getRegistration()).getClientLwM2mSettings().getUseObject19ForOtaInfo(); + var clientProfile = clientContext.getProfile(client.getRegistration()); + Boolean useObject19ForOtaInfo = clientProfile != null ? clientProfile.getClientLwM2mSettings().getUseObject19ForOtaInfo() : null; if (useObject19ForOtaInfo != null && useObject19ForOtaInfo){ sendInfoToObject19ForOta(client, SW_INFO_19_INSTANCE_ID, response, otaPackageId); } @@ -647,8 +650,9 @@ public class DefaultLwM2MOtaUpdateService extends LwM2MExecutorAwareService impl LwM2MClientSwOtaInfo info = otaInfoStore.getSw(endpoint); if (info == null) { var profile = clientContext.getProfile(client.getRegistration()); - info = new LwM2MClientSwOtaInfo(endpoint, profile.getClientLwM2mSettings().getSwUpdateResource(), - LwM2MSoftwareUpdateStrategy.fromStrategySwByCode(profile.getClientLwM2mSettings().getSwUpdateStrategy())); + OtherConfiguration clientLwM2mSettings = profile == null ? new OtherConfiguration() : profile.getClientLwM2mSettings(); + info = new LwM2MClientSwOtaInfo(endpoint, clientLwM2mSettings.getSwUpdateResource(), + LwM2MSoftwareUpdateStrategy.fromStrategySwByCode(clientLwM2mSettings.getSwUpdateStrategy())); update(info); } return info; 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 5e27b936f3..26eb6e83c4 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 @@ -418,7 +418,7 @@ public class DefaultLwM2mUplinkMsgHandler extends LwM2MExecutorAwareService impl }); if (!clients.isEmpty()) { var oldProfile = clientContext.getProfile(clients.get(0).getRegistration()); - this.onDeviceProfileUpdate(clients, oldProfile, deviceProfile); + if (oldProfile != null) this.onDeviceProfileUpdate(clients, oldProfile, deviceProfile); } } catch (Exception e) { log.warn("[{}] failed to update profile: {} [{}]", deviceProfile.getId(), e.getMessage(), deviceProfile); @@ -714,8 +714,10 @@ public class DefaultLwM2mUplinkMsgHandler extends LwM2MExecutorAwareService impl private void onDeviceUpdate(LwM2mClient lwM2MClient, Device device, Optional deviceProfileOpt) { var oldProfile = clientContext.getProfile(lwM2MClient.getRegistration()); - deviceProfileOpt.ifPresent(deviceProfile -> this.onDeviceProfileUpdate(Collections.singletonList(lwM2MClient), oldProfile, deviceProfile)); - lwM2MClient.onDeviceUpdate(device, deviceProfileOpt); + if (oldProfile != null) { + deviceProfileOpt.ifPresent(deviceProfile -> this.onDeviceProfileUpdate(Collections.singletonList(lwM2MClient), oldProfile, deviceProfile)); + lwM2MClient.onDeviceUpdate(device, deviceProfileOpt); + } } /** @@ -729,45 +731,12 @@ public class DefaultLwM2mUplinkMsgHandler extends LwM2MExecutorAwareService impl Set paths = updateResource.getPaths(); ResultsAddKeyValueProto results = new ResultsAddKeyValueProto(); var profile = clientContext.getProfile(registration); - List resultAttributes = new ArrayList<>(); - Set attributes = profile.getObserveAttr().getAttribute().stream() - .filter(paths::contains) - .collect(Collectors.toSet()); - if (!attributes.isEmpty()){ - attributes.stream() - .map(attr -> this.getKvToThingsBoard(attr, registration)) - .filter(Objects::nonNull) - .forEach(resultAttributes::add); - } - List resultTelemetries = new ArrayList<>(); - Set telemetries = profile.getObserveAttr().getTelemetry().stream() - .filter(paths::contains) - .collect(Collectors.toSet()); - if (!telemetries.isEmpty()){ - telemetries.stream() - .map(telemetry -> this.getKvToThingsBoard(telemetry, registration)) - .filter(Objects::nonNull) - .forEach(resultTelemetries::add); - } - if (resultAttributes.size() > 0) { - results.setResultAttributes(resultAttributes); - } - if (resultTelemetries.size() > 0) { - results.setResultTelemetries(resultTelemetries); - } - return results; - } - - private ResultsAddKeyValueProto getParametersFromProfile(Registration registration, Set path) { - if (!path.isEmpty()) { - ResultsAddKeyValueProto results = new ResultsAddKeyValueProto(); - var profile = clientContext.getProfile(registration); + if (profile != null) { List resultAttributes = new ArrayList<>(); Set attributes = profile.getObserveAttr().getAttribute().stream() - .map(LwM2MTransportUtil::fromVersionedIdToObjectId) - .filter(path::contains) + .filter(paths::contains) .collect(Collectors.toSet()); - if (!attributes.isEmpty()){ + if (!attributes.isEmpty()) { attributes.stream() .map(attr -> this.getKvToThingsBoard(attr, registration)) .filter(Objects::nonNull) @@ -775,10 +744,9 @@ public class DefaultLwM2mUplinkMsgHandler extends LwM2MExecutorAwareService impl } List resultTelemetries = new ArrayList<>(); Set telemetries = profile.getObserveAttr().getTelemetry().stream() - .map(LwM2MTransportUtil::fromVersionedIdToObjectId) - .filter(path::contains) + .filter(paths::contains) .collect(Collectors.toSet()); - if (!telemetries.isEmpty()){ + if (!telemetries.isEmpty()) { telemetries.stream() .map(telemetry -> this.getKvToThingsBoard(telemetry, registration)) .filter(Objects::nonNull) @@ -790,14 +758,15 @@ public class DefaultLwM2mUplinkMsgHandler extends LwM2MExecutorAwareService impl if (resultTelemetries.size() > 0) { results.setResultTelemetries(resultTelemetries); } - return results; } - return null; + return results; } private TransportProtos.KeyValueProto getKvToThingsBoard(String pathIdVer, Registration registration) { LwM2mClient lwM2MClient = this.clientContext.getClientByEndpoint(registration.getEndpoint()); - Map names = clientContext.getProfile(lwM2MClient.getRegistration()).getObserveAttr().getKeyName(); + var clientProfile = clientContext.getProfile(lwM2MClient.getRegistration()); + if (clientProfile == null) return null; + Map names = clientProfile.getObserveAttr().getKeyName(); if (names != null && names.containsKey(pathIdVer)) { String resourceName = names.get(pathIdVer); if (resourceName != null && !resourceName.isEmpty()) { @@ -887,10 +856,12 @@ public class DefaultLwM2mUplinkMsgHandler extends LwM2MExecutorAwareService impl private void onDeviceProfileUpdate(List clients, Lwm2mDeviceProfileTransportConfiguration oldProfileTransportConfiguration, DeviceProfile deviceProfile) { if (clientContext.profileUpdate(deviceProfile) != null) { var newProfileTransportConfiguration = clientContext.getProfile(clients.get(0).getRegistration()); - ParametersUpdateAnalyzeResult parametersUpdate = getParametersUpdate(oldProfileTransportConfiguration, newProfileTransportConfiguration); - ParametersObserveAnalyzeResult parametersObserve = getParametersObserve(oldProfileTransportConfiguration.getObserveAttr(), newProfileTransportConfiguration.getObserveAttr(), deviceProfile.getId().getId()); - compareAndSetWriteAttributesObservations(clients, parametersUpdate, parametersObserve); - updateValueOta(clients, newProfileTransportConfiguration, oldProfileTransportConfiguration); + if (newProfileTransportConfiguration != null) { + ParametersUpdateAnalyzeResult parametersUpdate = getParametersUpdate(oldProfileTransportConfiguration, newProfileTransportConfiguration); + ParametersObserveAnalyzeResult parametersObserve = getParametersObserve(oldProfileTransportConfiguration.getObserveAttr(), newProfileTransportConfiguration.getObserveAttr(), deviceProfile.getId().getId()); + compareAndSetWriteAttributesObservations(clients, parametersUpdate, parametersObserve); + updateValueOta(clients, newProfileTransportConfiguration, oldProfileTransportConfiguration); + } } }