From b58580a566f4051042b45325d06b7e0be60a14bd Mon Sep 17 00:00:00 2001 From: tl-photography Date: Mon, 22 Jul 2024 08:35:24 +0200 Subject: [PATCH] [aWATTar] shedule API update more then once per day (#17068) Signed-off-by: Thomas Leber --- bundles/org.openhab.binding.awattar/README.md | 4 ++ .../handler/AwattarBridgeHandler.java | 43 +++++++++++++++++-- 2 files changed, 44 insertions(+), 3 deletions(-) diff --git a/bundles/org.openhab.binding.awattar/README.md b/bundles/org.openhab.binding.awattar/README.md index 104342a007f..e0c227f45fd 100644 --- a/bundles/org.openhab.binding.awattar/README.md +++ b/bundles/org.openhab.binding.awattar/README.md @@ -18,6 +18,10 @@ The `prices` Thing provides todays and (after 14:00) tomorrows net and gross pri The `bestprice` Thing identifies the hours with the cheapest prices based on the given parameters. +Note: The Thing will schedule updates of the aWATTar API at 15:00, 18:00 and 21:00. +If late updates occur, e.g. after 21:00, there is a chance that consecutive best prices will be rescheduled. +As a consequence, a time schedule spanning over an update slot might be interrupted and rescheduled. + ## Discovery Auto discovery is not supported. diff --git a/bundles/org.openhab.binding.awattar/src/main/java/org/openhab/binding/awattar/internal/handler/AwattarBridgeHandler.java b/bundles/org.openhab.binding.awattar/src/main/java/org/openhab/binding/awattar/internal/handler/AwattarBridgeHandler.java index 8bd65306efb..92e0ac08a1f 100644 --- a/bundles/org.openhab.binding.awattar/src/main/java/org/openhab/binding/awattar/internal/handler/AwattarBridgeHandler.java +++ b/bundles/org.openhab.binding.awattar/src/main/java/org/openhab/binding/awattar/internal/handler/AwattarBridgeHandler.java @@ -67,6 +67,7 @@ public class AwattarBridgeHandler extends BaseBridgeHandler { private final Logger logger = LoggerFactory.getLogger(AwattarBridgeHandler.class); private final HttpClient httpClient; private @Nullable ScheduledFuture dataRefresher; + private Instant lastRefresh = Instant.EPOCH; private static final String URLDE = "https://api.awattar.de/v1/marketdata"; private static final String URLAT = "https://api.awattar.at/v1/marketdata"; @@ -179,13 +180,49 @@ public class AwattarBridgeHandler extends BaseBridgeHandler { } } + /** + * Check if the data needs to be refreshed. + * + * The data is refreshed if: + * - the thing is offline + * - the local cache is empty + * - the current time is after 15:00 and the last refresh was more than an hour ago + * - the current time is after 18:00 and the last refresh was more than an hour ago + * - the current time is after 21:00 and the last refresh was more than an hour ago + * + * @return true if the data needs to be refreshed + */ private boolean needRefresh() { + // if the thing is offline, we need to refresh if (getThing().getStatus() != ThingStatus.ONLINE) { return true; } - SortedSet localPrices = prices; - return localPrices == null - || localPrices.last().timerange().start() < Instant.now().toEpochMilli() + 9 * 3600 * 1000; + + // if the local cache is empty, we need to refresh + if (prices != null) { + return true; + } + + // Note: all this magic is made to avoid refreshing the data too often, since the API is rate-limited + // to 100 requests per day. + + // do not refresh before 15:00, since the prices for the next day are available only after 14:00 + ZonedDateTime now = ZonedDateTime.now(zone); + if (now.getHour() < 15) { + return false; + } + + // refresh then every 3 hours, if the last refresh was more than an hour ago + if (now.getHour() % 3 == 0 && lastRefresh.getEpochSecond() < now.minusHours(1).toEpochSecond()) { + + // update the last refresh time + lastRefresh = Instant.now(); + + // return true to indicate an update is needed + return true; + } + + return false; } public ZoneId getTimeZone() {