]> git.basschouten.com Git - openhab-addons.git/commitdiff
[aWATTar] shedule API update more then once per day (#17068)
authortl-photography <thomas@tl-photography.at>
Mon, 22 Jul 2024 06:35:24 +0000 (08:35 +0200)
committerGitHub <noreply@github.com>
Mon, 22 Jul 2024 06:35:24 +0000 (08:35 +0200)
Signed-off-by: Thomas Leber <thomas@tl-photography.at>
bundles/org.openhab.binding.awattar/README.md
bundles/org.openhab.binding.awattar/src/main/java/org/openhab/binding/awattar/internal/handler/AwattarBridgeHandler.java

index 104342a007f102d8f6a25d7a806951e6fff8a700..e0c227f45fdd02aee30be933ab019430cb3b118b 100644 (file)
@@ -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.
index 8bd65306efb92b56a647a219a583ef1d76a2790c..92e0ac08a1f5b5a1d88264c15f916d902ecbd3bb 100644 (file)
@@ -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<AwattarPrice> 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() {