Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[netatmo] Avoid multiple device data reading at startup #8101

Merged
merged 1 commit into from
Jul 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,8 @@ public void expireData() {
ZonedDateTime now = ZonedDateTime.now().minus(this.dataValidityPeriod, ChronoUnit.MILLIS);
dataTimeStamp = now.toInstant().toEpochMilli();
}

public boolean isSearchingRefreshInterval() {
return searchRefreshInterval && dataTimestamp0 != null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public abstract class NetatmoDeviceHandler<DEVICE> extends AbstractNetatmoThingH
private static final int DEFAULT_REFRESH_INTERVAL = 300000;

private final Logger logger = LoggerFactory.getLogger(NetatmoDeviceHandler.class);
private final Object updateLock = new Object();
private @Nullable ScheduledFuture<?> refreshJob;
private @Nullable RefreshStrategy refreshStrategy;
private @Nullable DEVICE device;
Expand All @@ -79,7 +80,7 @@ private void scheduleRefreshJob() {
long delay = strategy.nextRunDelayInS();
logger.debug("Scheduling update channel thread in {} s", delay);
refreshJob = scheduler.schedule(() -> {
updateChannels();
updateChannels(false);
ScheduledFuture<?> job = refreshJob;
if (job != null) {
job.cancel(false);
Expand All @@ -106,53 +107,62 @@ protected void updateProperties(DEVICE deviceData) {

@Override
protected void updateChannels() {
RefreshStrategy strategy = refreshStrategy;
if (strategy != null) {
logger.debug("Data aged of {} s", strategy.dataAge() / 1000);
if (strategy.isDataOutdated()) {
logger.debug("Trying to update channels on device {}", getId());
childs.clear();

Optional<DEVICE> newDeviceReading = Optional.empty();
try {
newDeviceReading = updateReadings();
} catch (RetrofitError e) {
if (logger.isDebugEnabled()) {
// we also attach the stack trace
logger.error("Unable to connect Netatmo API : {}", e.getMessage(), e);
updateChannels(true);
}

private void updateChannels(boolean requireDefinedRefreshInterval) {
// Avoid concurrent data readings
synchronized (updateLock) {
RefreshStrategy strategy = refreshStrategy;
if (strategy != null) {
logger.debug("Data aged of {} s", strategy.dataAge() / 1000);
boolean dataOutdated = (requireDefinedRefreshInterval && strategy.isSearchingRefreshInterval()) ? false
: strategy.isDataOutdated();
if (dataOutdated) {
logger.debug("Trying to update channels on device {}", getId());
childs.clear();

Optional<DEVICE> newDeviceReading = Optional.empty();
try {
newDeviceReading = updateReadings();
} catch (RetrofitError e) {
if (logger.isDebugEnabled()) {
// we also attach the stack trace
logger.error("Unable to connect Netatmo API : {}", e.getMessage(), e);
} else {
logger.error("Unable to connect Netatmo API : {}", e.getMessage());
}
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
"Unable to connect Netatmo API : " + e.getLocalizedMessage());
}
if (newDeviceReading.isPresent()) {
updateStatus(ThingStatus.ONLINE);
logger.debug("Successfully updated device {} readings! Now updating channels", getId());
DEVICE theDevice = newDeviceReading.get();
this.device = theDevice;
updateProperties(theDevice);
getDataTimestamp().ifPresent(dataTimeStamp -> {
strategy.setDataTimeStamp(dataTimeStamp, timeZoneProvider.getTimeZone());
});
getRadioHelper().ifPresent(helper -> helper.setModule(theDevice));
getBridgeHandler().ifPresent(handler -> {
handler.checkForNewThings(theDevice);
});
} else {
logger.error("Unable to connect Netatmo API : {}", e.getMessage());
logger.debug("Failed to update device {} readings! Skip updating channels", getId());
}
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
"Unable to connect Netatmo API : " + e.getLocalizedMessage());
}
if (newDeviceReading.isPresent()) {
updateStatus(ThingStatus.ONLINE);
logger.debug("Successfully updated device {} readings! Now updating channels", getId());
DEVICE theDevice = newDeviceReading.get();
this.device = theDevice;
updateProperties(theDevice);
getDataTimestamp().ifPresent(dataTimeStamp -> {
strategy.setDataTimeStamp(dataTimeStamp, timeZoneProvider.getTimeZone());
});
getRadioHelper().ifPresent(helper -> helper.setModule(theDevice));
getBridgeHandler().ifPresent(handler -> {
handler.checkForNewThings(theDevice);
// Be sure that all channels for the modules will be updated with refreshed data
childs.forEach((childId, moduleData) -> {
findNAThing(childId).map(NetatmoModuleHandler.class::cast).ifPresent(naChildModule -> {
naChildModule.setRefreshRequired(true);
});
});
} else {
logger.debug("Failed to update device {} readings! Skip updating channels", getId());
logger.debug("Data still valid for device {}", getId());
}
// Be sure that all channels for the modules will be updated with refreshed data
childs.forEach((childId, moduleData) -> {
findNAThing(childId).map(NetatmoModuleHandler.class::cast).ifPresent(naChildModule -> {
naChildModule.setRefreshRequired(true);
});
});
} else {
logger.debug("Data still valid for device {}", getId());
super.updateChannels();
updateChildModules();
}
super.updateChannels();
updateChildModules();
}
}

Expand Down