Skip to content

Commit b58580a

Browse files
[aWATTar] shedule API update more then once per day (openhab#17068)
Signed-off-by: Thomas Leber <[email protected]>
1 parent 31dcc27 commit b58580a

File tree

2 files changed

+44
-3
lines changed

2 files changed

+44
-3
lines changed

bundles/org.openhab.binding.awattar/README.md

+4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ The `prices` Thing provides todays and (after 14:00) tomorrows net and gross pri
1818

1919
The `bestprice` Thing identifies the hours with the cheapest prices based on the given parameters.
2020

21+
Note: The Thing will schedule updates of the aWATTar API at 15:00, 18:00 and 21:00.
22+
If late updates occur, e.g. after 21:00, there is a chance that consecutive best prices will be rescheduled.
23+
As a consequence, a time schedule spanning over an update slot might be interrupted and rescheduled.
24+
2125
## Discovery
2226

2327
Auto discovery is not supported.

bundles/org.openhab.binding.awattar/src/main/java/org/openhab/binding/awattar/internal/handler/AwattarBridgeHandler.java

+40-3
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ public class AwattarBridgeHandler extends BaseBridgeHandler {
6767
private final Logger logger = LoggerFactory.getLogger(AwattarBridgeHandler.class);
6868
private final HttpClient httpClient;
6969
private @Nullable ScheduledFuture<?> dataRefresher;
70+
private Instant lastRefresh = Instant.EPOCH;
7071

7172
private static final String URLDE = "https://api.awattar.de/v1/marketdata";
7273
private static final String URLAT = "https://api.awattar.at/v1/marketdata";
@@ -179,13 +180,49 @@ private void refresh() {
179180
}
180181
}
181182

183+
/**
184+
* Check if the data needs to be refreshed.
185+
*
186+
* The data is refreshed if:
187+
* - the thing is offline
188+
* - the local cache is empty
189+
* - the current time is after 15:00 and the last refresh was more than an hour ago
190+
* - the current time is after 18:00 and the last refresh was more than an hour ago
191+
* - the current time is after 21:00 and the last refresh was more than an hour ago
192+
*
193+
* @return true if the data needs to be refreshed
194+
*/
182195
private boolean needRefresh() {
196+
// if the thing is offline, we need to refresh
183197
if (getThing().getStatus() != ThingStatus.ONLINE) {
184198
return true;
185199
}
186-
SortedSet<AwattarPrice> localPrices = prices;
187-
return localPrices == null
188-
|| localPrices.last().timerange().start() < Instant.now().toEpochMilli() + 9 * 3600 * 1000;
200+
201+
// if the local cache is empty, we need to refresh
202+
if (prices != null) {
203+
return true;
204+
}
205+
206+
// Note: all this magic is made to avoid refreshing the data too often, since the API is rate-limited
207+
// to 100 requests per day.
208+
209+
// do not refresh before 15:00, since the prices for the next day are available only after 14:00
210+
ZonedDateTime now = ZonedDateTime.now(zone);
211+
if (now.getHour() < 15) {
212+
return false;
213+
}
214+
215+
// refresh then every 3 hours, if the last refresh was more than an hour ago
216+
if (now.getHour() % 3 == 0 && lastRefresh.getEpochSecond() < now.minusHours(1).toEpochSecond()) {
217+
218+
// update the last refresh time
219+
lastRefresh = Instant.now();
220+
221+
// return true to indicate an update is needed
222+
return true;
223+
}
224+
225+
return false;
189226
}
190227

191228
public ZoneId getTimeZone() {

0 commit comments

Comments
 (0)