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 dd9edd2909..f7e4a4a5cc 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 @@ -37,7 +37,6 @@ import org.thingsboard.server.queue.util.TbLwM2mTransportComponent; import org.thingsboard.server.transport.lwm2m.config.LwM2MTransportServerConfig; import org.thingsboard.server.transport.lwm2m.secure.TbLwM2MSecurityInfo; import org.thingsboard.server.transport.lwm2m.server.LwM2mTransportContext; -import org.thingsboard.server.transport.lwm2m.server.LwM2mVersionedModelProvider; import org.thingsboard.server.transport.lwm2m.server.ota.LwM2MOtaUpdateService; import org.thingsboard.server.transport.lwm2m.server.session.LwM2MSessionManager; import org.thingsboard.server.transport.lwm2m.server.store.TbLwM2MClientStore; @@ -71,7 +70,6 @@ public class LwM2mClientContextImpl implements LwM2mClientContext { private final TbLwM2MClientStore clientStore; private final LwM2MSessionManager sessionManager; private final TransportDeviceProfileCache deviceProfileCache; - private final LwM2mVersionedModelProvider modelProvider; @Autowired @Lazy @@ -229,10 +227,7 @@ public class LwM2mClientContextImpl implements LwM2mClientContext { throw new LwM2MClientStateException(client.getState(), "Client is in invalid state."); } client.setRegistration(registration); - onUplink(client); - if (compareAndSetSleepFlag(client, false)) { - sendMsgsAfterSleeping(client); - } else { + if (!awake(client)) { clientStore.put(client); } } finally { @@ -419,7 +414,7 @@ public class LwM2mClientContextImpl implements LwM2mClientContext { powerMode = PowerMode.DRX; } } - if (PowerMode.DRX.equals(powerMode)) { + if (PowerMode.DRX.equals(powerMode) || otaUpdateService.isOtaDownloading(client)) { return true; } client.lock(); @@ -503,7 +498,7 @@ public class LwM2mClientContextImpl implements LwM2mClientContext { sleepTask.cancel(false); } Future task = context.getScheduler().schedule(() -> { - if (uplinkTime == client.getLastUplinkTime()) { + if (uplinkTime == client.getLastUplinkTime() && !otaUpdateService.isOtaDownloading(client)) { asleep(client); } return null; 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 5fda8f87a9..1ebd2e75f8 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 @@ -182,9 +182,9 @@ public class DefaultLwM2MOtaUpdateService extends LwM2MExecutorAwareService impl } var clientSettings = clientContext.getProfile(client.getProfileId()).getClientLwM2mSettings(); - onFirmwareStrategyUpdate(client, clientSettings); + initFwStrategy(client, clientSettings); onCurrentSoftwareStrategyUpdate(client, clientSettings); - + if (!attributesToFetch.isEmpty()) { var future = attributesService.getSharedAttributes(client, attributesToFetch); DonAsynchron.withCallback(future, attrs -> { @@ -193,7 +193,7 @@ public class DefaultLwM2MOtaUpdateService extends LwM2MExecutorAwareService impl Optional newFwVersion = getAttributeValue(attrs, FIRMWARE_VERSION); Optional newFwTag = getAttributeValue(attrs, FIRMWARE_TAG); Optional newFwUrl = getAttributeValue(attrs, FIRMWARE_URL); - if (newFwTitle.isPresent() && newFwVersion.isPresent()) { + if (newFwTitle.isPresent() && newFwVersion.isPresent() && !isOtaDownloading(client) && !UPDATING.equals(fwInfo.status)) { onTargetFirmwareUpdate(client, newFwTitle.get(), newFwVersion.get(), newFwUrl, newFwTag); } } @@ -245,10 +245,14 @@ public class DefaultLwM2MOtaUpdateService extends LwM2MExecutorAwareService impl @Override public void onFirmwareStrategyUpdate(LwM2mClient client, OtherConfiguration configuration) { log.debug("[{}] Current fw strategy: {}", client.getEndpoint(), configuration.getFwUpdateStrategy()); + startFirmwareUpdateIfNeeded(client, initFwStrategy(client, configuration)); + } + + private LwM2MClientFwOtaInfo initFwStrategy(LwM2mClient client, OtherConfiguration configuration) { LwM2MClientFwOtaInfo fwInfo = getOrInitFwInfo(client); fwInfo.setStrategy(LwM2MFirmwareUpdateStrategy.fromStrategyFwByCode(configuration.getFwUpdateStrategy())); fwInfo.setBaseUrl(configuration.getFwUpdateResource()); - startFirmwareUpdateIfNeeded(client, fwInfo); + return fwInfo; } @Override @@ -308,6 +312,7 @@ public class DefaultLwM2MOtaUpdateService extends LwM2MExecutorAwareService impl if (FirmwareUpdateResult.INITIAL.equals(result) && OtaPackageUpdateStatus.UPDATING.equals(fwInfo.getStatus())) { status = Optional.of(UPDATED); fwInfo.setRetryAttempts(0); + fwInfo.setFailedPackageId(null); } status.ifPresent(otaStatus -> { @@ -388,6 +393,22 @@ public class DefaultLwM2MOtaUpdateService extends LwM2MExecutorAwareService impl startSoftwareUpdateIfNeeded(client, fwInfo); } + @Override + public boolean isOtaDownloading(LwM2mClient client) { + String endpoint = client.getEndpoint(); + LwM2MClientFwOtaInfo fwInfo = fwStates.get(endpoint); + LwM2MClientSwOtaInfo swInfo = swStates.get(endpoint); + + if (fwInfo != null && (DOWNLOADING.equals(fwInfo.getStatus()))) { + return true; + } + if (swInfo != null && (DOWNLOADING.equals(swInfo.getStatus()))) { + return true; + } + + return false; + } + private void startFirmwareUpdateIfNeeded(LwM2mClient client, LwM2MClientFwOtaInfo fwInfo) { try { if (!fwInfo.isSupported() && fwInfo.isAssigned()) { @@ -401,7 +422,7 @@ public class DefaultLwM2MOtaUpdateService extends LwM2MExecutorAwareService impl log.debug("[{}] Starting update to [{}{}] using binary", client.getEndpoint(), fwInfo.getTargetName(), fwInfo.getTargetVersion()); startUpdateUsingBinary(client, fwInfo); } - } else if (fwInfo.getResult() != null && fwInfo.getResult().getCode() > UPDATE_SUCCESSFULLY.getCode()) { + } else if (fwInfo.getResult() != null && fwInfo.getResult().getCode() > UPDATE_SUCCESSFULLY.getCode()) { log.trace("[{}] Previous update failed. [{}]", client.getEndpoint(), fwInfo); logService.log(client, "Previous update firmware failed. Result: " + fwInfo.getResult().name()); } diff --git a/common/transport/lwm2m/src/main/java/org/thingsboard/server/transport/lwm2m/server/ota/LwM2MOtaUpdateService.java b/common/transport/lwm2m/src/main/java/org/thingsboard/server/transport/lwm2m/server/ota/LwM2MOtaUpdateService.java index b23924b906..4aa3776682 100644 --- a/common/transport/lwm2m/src/main/java/org/thingsboard/server/transport/lwm2m/server/ota/LwM2MOtaUpdateService.java +++ b/common/transport/lwm2m/src/main/java/org/thingsboard/server/transport/lwm2m/server/ota/LwM2MOtaUpdateService.java @@ -55,4 +55,6 @@ public interface LwM2MOtaUpdateService { void onCurrentSoftwareStateUpdate(LwM2mClient lwM2MClient, Long value); void onCurrentSoftwareResultUpdate(LwM2mClient client, Long result); + + boolean isOtaDownloading(LwM2mClient client); }