Synchronized Weather cache update requests (#16572)

Signed-off-by: clinique <gael@lhopital.org>
Signed-off-by: Ciprian Pascu <contact@ciprianpascu.ro>
This commit is contained in:
Gaël L'hopital 2024-03-25 09:06:50 +01:00 committed by Ciprian Pascu
parent f99ff75b9d
commit 4206dd811a
3 changed files with 9 additions and 6 deletions

View File

@ -41,7 +41,7 @@ public class AirCareCapability extends RestCapability<AircareApi> {
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();
}

View File

@ -43,13 +43,16 @@ public abstract class CacheWeatherCapability extends RestCapability<WeatherApi>
}
@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;

View File

@ -42,7 +42,7 @@ public class WeatherCapability extends CacheWeatherCapability {
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();
}