@@ -67,6 +67,7 @@ public class AwattarBridgeHandler extends BaseBridgeHandler {
67
67
private final Logger logger = LoggerFactory .getLogger (AwattarBridgeHandler .class );
68
68
private final HttpClient httpClient ;
69
69
private @ Nullable ScheduledFuture <?> dataRefresher ;
70
+ private Instant lastRefresh = Instant .EPOCH ;
70
71
71
72
private static final String URLDE = "https://api.awattar.de/v1/marketdata" ;
72
73
private static final String URLAT = "https://api.awattar.at/v1/marketdata" ;
@@ -179,13 +180,49 @@ private void refresh() {
179
180
}
180
181
}
181
182
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
+ */
182
195
private boolean needRefresh () {
196
+ // if the thing is offline, we need to refresh
183
197
if (getThing ().getStatus () != ThingStatus .ONLINE ) {
184
198
return true ;
185
199
}
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 ;
189
226
}
190
227
191
228
public ZoneId getTimeZone () {
0 commit comments