Skip to content

Commit

Permalink
Synchronized Weather cache update requests (openhab#16572)
Browse files Browse the repository at this point in the history
Signed-off-by: clinique <gael@lhopital.org>
  • Loading branch information
clinique authored and matchews committed Oct 18, 2024
1 parent cea0d5f commit 66d82ac
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ protected List<NAObject> updateReadings(AircareApi api) {
try {
return List.of(api.getHomeCoach(handler.getId()));
} catch (NetatmoException e) {
logger.warn("Error retrieving home-coach data '{}' : {}", handler.getId(), e.getMessage());
logger.warn("Error retrieving home-coach data '{}': {}", handler.getId(), e.getMessage());
}
return List.of();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,16 @@ public CacheWeatherCapability(CommonInterface handler, Duration validity) {
}

@Override
protected List<NAObject> updateReadings(WeatherApi api) {
protected synchronized List<NAObject> updateReadings(WeatherApi api) {
Instant now = Instant.now();

if (requestTS.plus(validity).isBefore(now)) {
logger.debug("Requesting fresh data");
lastResult = getFreshData(api);
requestTS = now;
logger.debug("{} requesting fresh data for {}", getClass().getSimpleName(), thingUID);
List<NAObject> result = getFreshData(api);
if (!result.isEmpty()) {
lastResult = result;
requestTS = now;
}
}

return lastResult;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ protected List<NAObject> getFreshData(WeatherApi api) {
try {
return List.of(owned ? api.getOwnedStationData(handler.getId()) : api.getStationData(handler.getId()));
} catch (NetatmoException e) {
logger.warn("Error retrieving weather data '{}' : {}", handler.getId(), e.getMessage());
logger.warn("Error retrieving weather data '{}': {}", handler.getId(), e.getMessage());
}
return List.of();
}
Expand Down

0 comments on commit 66d82ac

Please sign in to comment.