Browse Source

fixed double sending ota update after sleeping

pull/5775/head
YevhenBondarenko 5 years ago
parent
commit
95e3f3c05b
  1. 11
      common/transport/lwm2m/src/main/java/org/thingsboard/server/transport/lwm2m/server/client/LwM2mClientContextImpl.java
  2. 31
      common/transport/lwm2m/src/main/java/org/thingsboard/server/transport/lwm2m/server/ota/DefaultLwM2MOtaUpdateService.java
  3. 2
      common/transport/lwm2m/src/main/java/org/thingsboard/server/transport/lwm2m/server/ota/LwM2MOtaUpdateService.java

11
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.config.LwM2MTransportServerConfig;
import org.thingsboard.server.transport.lwm2m.secure.TbLwM2MSecurityInfo; import org.thingsboard.server.transport.lwm2m.secure.TbLwM2MSecurityInfo;
import org.thingsboard.server.transport.lwm2m.server.LwM2mTransportContext; 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.ota.LwM2MOtaUpdateService;
import org.thingsboard.server.transport.lwm2m.server.session.LwM2MSessionManager; import org.thingsboard.server.transport.lwm2m.server.session.LwM2MSessionManager;
import org.thingsboard.server.transport.lwm2m.server.store.TbLwM2MClientStore; import org.thingsboard.server.transport.lwm2m.server.store.TbLwM2MClientStore;
@ -71,7 +70,6 @@ public class LwM2mClientContextImpl implements LwM2mClientContext {
private final TbLwM2MClientStore clientStore; private final TbLwM2MClientStore clientStore;
private final LwM2MSessionManager sessionManager; private final LwM2MSessionManager sessionManager;
private final TransportDeviceProfileCache deviceProfileCache; private final TransportDeviceProfileCache deviceProfileCache;
private final LwM2mVersionedModelProvider modelProvider;
@Autowired @Autowired
@Lazy @Lazy
@ -229,10 +227,7 @@ public class LwM2mClientContextImpl implements LwM2mClientContext {
throw new LwM2MClientStateException(client.getState(), "Client is in invalid state."); throw new LwM2MClientStateException(client.getState(), "Client is in invalid state.");
} }
client.setRegistration(registration); client.setRegistration(registration);
onUplink(client); if (!awake(client)) {
if (compareAndSetSleepFlag(client, false)) {
sendMsgsAfterSleeping(client);
} else {
clientStore.put(client); clientStore.put(client);
} }
} finally { } finally {
@ -419,7 +414,7 @@ public class LwM2mClientContextImpl implements LwM2mClientContext {
powerMode = PowerMode.DRX; powerMode = PowerMode.DRX;
} }
} }
if (PowerMode.DRX.equals(powerMode)) { if (PowerMode.DRX.equals(powerMode) || otaUpdateService.isOtaDownloading(client)) {
return true; return true;
} }
client.lock(); client.lock();
@ -503,7 +498,7 @@ public class LwM2mClientContextImpl implements LwM2mClientContext {
sleepTask.cancel(false); sleepTask.cancel(false);
} }
Future<Void> task = context.getScheduler().schedule(() -> { Future<Void> task = context.getScheduler().schedule(() -> {
if (uplinkTime == client.getLastUplinkTime()) { if (uplinkTime == client.getLastUplinkTime() && !otaUpdateService.isOtaDownloading(client)) {
asleep(client); asleep(client);
} }
return null; return null;

31
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(); var clientSettings = clientContext.getProfile(client.getProfileId()).getClientLwM2mSettings();
onFirmwareStrategyUpdate(client, clientSettings); initFwStrategy(client, clientSettings);
onCurrentSoftwareStrategyUpdate(client, clientSettings); onCurrentSoftwareStrategyUpdate(client, clientSettings);
if (!attributesToFetch.isEmpty()) { if (!attributesToFetch.isEmpty()) {
var future = attributesService.getSharedAttributes(client, attributesToFetch); var future = attributesService.getSharedAttributes(client, attributesToFetch);
DonAsynchron.withCallback(future, attrs -> { DonAsynchron.withCallback(future, attrs -> {
@ -193,7 +193,7 @@ public class DefaultLwM2MOtaUpdateService extends LwM2MExecutorAwareService impl
Optional<String> newFwVersion = getAttributeValue(attrs, FIRMWARE_VERSION); Optional<String> newFwVersion = getAttributeValue(attrs, FIRMWARE_VERSION);
Optional<String> newFwTag = getAttributeValue(attrs, FIRMWARE_TAG); Optional<String> newFwTag = getAttributeValue(attrs, FIRMWARE_TAG);
Optional<String> newFwUrl = getAttributeValue(attrs, FIRMWARE_URL); Optional<String> 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); onTargetFirmwareUpdate(client, newFwTitle.get(), newFwVersion.get(), newFwUrl, newFwTag);
} }
} }
@ -245,10 +245,14 @@ public class DefaultLwM2MOtaUpdateService extends LwM2MExecutorAwareService impl
@Override @Override
public void onFirmwareStrategyUpdate(LwM2mClient client, OtherConfiguration configuration) { public void onFirmwareStrategyUpdate(LwM2mClient client, OtherConfiguration configuration) {
log.debug("[{}] Current fw strategy: {}", client.getEndpoint(), configuration.getFwUpdateStrategy()); 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); LwM2MClientFwOtaInfo fwInfo = getOrInitFwInfo(client);
fwInfo.setStrategy(LwM2MFirmwareUpdateStrategy.fromStrategyFwByCode(configuration.getFwUpdateStrategy())); fwInfo.setStrategy(LwM2MFirmwareUpdateStrategy.fromStrategyFwByCode(configuration.getFwUpdateStrategy()));
fwInfo.setBaseUrl(configuration.getFwUpdateResource()); fwInfo.setBaseUrl(configuration.getFwUpdateResource());
startFirmwareUpdateIfNeeded(client, fwInfo); return fwInfo;
} }
@Override @Override
@ -308,6 +312,7 @@ public class DefaultLwM2MOtaUpdateService extends LwM2MExecutorAwareService impl
if (FirmwareUpdateResult.INITIAL.equals(result) && OtaPackageUpdateStatus.UPDATING.equals(fwInfo.getStatus())) { if (FirmwareUpdateResult.INITIAL.equals(result) && OtaPackageUpdateStatus.UPDATING.equals(fwInfo.getStatus())) {
status = Optional.of(UPDATED); status = Optional.of(UPDATED);
fwInfo.setRetryAttempts(0); fwInfo.setRetryAttempts(0);
fwInfo.setFailedPackageId(null);
} }
status.ifPresent(otaStatus -> { status.ifPresent(otaStatus -> {
@ -388,6 +393,22 @@ public class DefaultLwM2MOtaUpdateService extends LwM2MExecutorAwareService impl
startSoftwareUpdateIfNeeded(client, fwInfo); 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) { private void startFirmwareUpdateIfNeeded(LwM2mClient client, LwM2MClientFwOtaInfo fwInfo) {
try { try {
if (!fwInfo.isSupported() && fwInfo.isAssigned()) { 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()); log.debug("[{}] Starting update to [{}{}] using binary", client.getEndpoint(), fwInfo.getTargetName(), fwInfo.getTargetVersion());
startUpdateUsingBinary(client, fwInfo); 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); log.trace("[{}] Previous update failed. [{}]", client.getEndpoint(), fwInfo);
logService.log(client, "Previous update firmware failed. Result: " + fwInfo.getResult().name()); logService.log(client, "Previous update firmware failed. Result: " + fwInfo.getResult().name());
} }

2
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 onCurrentSoftwareStateUpdate(LwM2mClient lwM2MClient, Long value);
void onCurrentSoftwareResultUpdate(LwM2mClient client, Long result); void onCurrentSoftwareResultUpdate(LwM2mClient client, Long result);
boolean isOtaDownloading(LwM2mClient client);
} }

Loading…
Cancel
Save