[aWATTar] shedule API update more then once per day (#17068)

Signed-off-by: Thomas Leber <thomas@tl-photography.at>
Signed-off-by: Ciprian Pascu <contact@ciprianpascu.ro>
This commit is contained in:
tl-photography 2024-07-22 08:35:24 +02:00 committed by Ciprian Pascu
parent b44a248e87
commit 4a52fd5780
2 changed files with 44 additions and 3 deletions

View 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.

View 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() {