From f4fada50361ed8056cc3cb9f37ffb012e18b6b6c Mon Sep 17 00:00:00 2001 From: Jacob Laursen Date: Sun, 14 Apr 2024 21:48:34 +0200 Subject: [PATCH] Adjust retry policy for extended spot price unavailability (#16653) Signed-off-by: Jacob Laursen --- .../internal/ApiController.java | 6 +--- .../handler/EnergiDataServiceHandler.java | 34 +++++++++++-------- .../internal/retry/RetryPolicyFactory.java | 11 ++---- 3 files changed, 24 insertions(+), 27 deletions(-) diff --git a/bundles/org.openhab.binding.energidataservice/src/main/java/org/openhab/binding/energidataservice/internal/ApiController.java b/bundles/org.openhab.binding.energidataservice/src/main/java/org/openhab/binding/energidataservice/internal/ApiController.java index 25588fa4650..1aaaf45be5d 100644 --- a/bundles/org.openhab.binding.energidataservice/src/main/java/org/openhab/binding/energidataservice/internal/ApiController.java +++ b/bundles/org.openhab.binding.energidataservice/src/main/java/org/openhab/binding/energidataservice/internal/ApiController.java @@ -122,14 +122,10 @@ public class ApiController { try { String responseContent = sendRequest(request, properties); ElspotpriceRecords records = gson.fromJson(responseContent, ElspotpriceRecords.class); - if (records == null) { + if (records == null || Objects.isNull(records.records())) { throw new DataServiceException("Error parsing response"); } - if (records.total() == 0 || Objects.isNull(records.records()) || records.records().length == 0) { - throw new DataServiceException("No records"); - } - return Arrays.stream(records.records()).filter(Objects::nonNull).toArray(ElspotpriceRecord[]::new); } catch (JsonSyntaxException e) { throw new DataServiceException("Error parsing response", e); diff --git a/bundles/org.openhab.binding.energidataservice/src/main/java/org/openhab/binding/energidataservice/internal/handler/EnergiDataServiceHandler.java b/bundles/org.openhab.binding.energidataservice/src/main/java/org/openhab/binding/energidataservice/internal/handler/EnergiDataServiceHandler.java index 414bde6993c..e2a9bb70e10 100644 --- a/bundles/org.openhab.binding.energidataservice/src/main/java/org/openhab/binding/energidataservice/internal/handler/EnergiDataServiceHandler.java +++ b/bundles/org.openhab.binding.energidataservice/src/main/java/org/openhab/binding/energidataservice/internal/handler/EnergiDataServiceHandler.java @@ -250,12 +250,15 @@ public class EnergiDataServiceHandler extends BaseThingHandler { updateTimeSeries(); if (isLinked(CHANNEL_SPOT_PRICE)) { - if (cacheManager.getNumberOfFutureSpotPrices() < 13) { - logger.warn("Spot prices are not yet available, retry scheduled (see details in Thing properties)"); - retryPolicy = RetryPolicyFactory.whenExpectedSpotPriceDataMissing(DAILY_REFRESH_TIME_CET, - NORD_POOL_TIMEZONE); - } else { + long numberOfFutureSpotPrices = cacheManager.getNumberOfFutureSpotPrices(); + LocalTime now = LocalTime.now(NORD_POOL_TIMEZONE); + + if (numberOfFutureSpotPrices >= 13 || (numberOfFutureSpotPrices == 12 + && now.isAfter(DAILY_REFRESH_TIME_CET.minusHours(1)) && now.isBefore(DAILY_REFRESH_TIME_CET))) { retryPolicy = RetryPolicyFactory.atFixedTime(DAILY_REFRESH_TIME_CET, NORD_POOL_TIMEZONE); + } else { + logger.warn("Spot prices are not available, retry scheduled (see details in Thing properties)"); + retryPolicy = RetryPolicyFactory.whenExpectedSpotPriceDataMissing(); } } else { retryPolicy = RetryPolicyFactory.atFixedTime(LocalTime.MIDNIGHT, timeZoneProvider.getTimeZone()); @@ -293,10 +296,13 @@ public class EnergiDataServiceHandler extends BaseThingHandler { Duration.ofHours(-CacheManager.NUMBER_OF_HISTORIC_HOURS)); } Map properties = editProperties(); - ElspotpriceRecord[] spotPriceRecords = apiController.getSpotPrices(config.priceArea, config.getCurrency(), - start, properties); - cacheManager.putSpotPrices(spotPriceRecords, config.getCurrency()); - updateProperties(properties); + try { + ElspotpriceRecord[] spotPriceRecords = apiController.getSpotPrices(config.priceArea, config.getCurrency(), + start, properties); + cacheManager.putSpotPrices(spotPriceRecords, config.getCurrency()); + } finally { + updateProperties(properties); + } } private void downloadTariffs(DatahubTariff datahubTariff) throws InterruptedException, DataServiceException { @@ -325,11 +331,11 @@ public class EnergiDataServiceHandler extends BaseThingHandler { private Collection downloadPriceLists(GlobalLocationNumber globalLocationNumber, DatahubTariffFilter filter) throws InterruptedException, DataServiceException { Map properties = editProperties(); - Collection records = apiController.getDatahubPriceLists(globalLocationNumber, - ChargeType.Tariff, filter, properties); - updateProperties(properties); - - return records; + try { + return apiController.getDatahubPriceLists(globalLocationNumber, ChargeType.Tariff, filter, properties); + } finally { + updateProperties(properties); + } } private DatahubTariffFilter getGridTariffFilter() { diff --git a/bundles/org.openhab.binding.energidataservice/src/main/java/org/openhab/binding/energidataservice/internal/retry/RetryPolicyFactory.java b/bundles/org.openhab.binding.energidataservice/src/main/java/org/openhab/binding/energidataservice/internal/retry/RetryPolicyFactory.java index effe7dc7df5..d5937f4f06e 100644 --- a/bundles/org.openhab.binding.energidataservice/src/main/java/org/openhab/binding/energidataservice/internal/retry/RetryPolicyFactory.java +++ b/bundles/org.openhab.binding.energidataservice/src/main/java/org/openhab/binding/energidataservice/internal/retry/RetryPolicyFactory.java @@ -76,15 +76,10 @@ public class RetryPolicyFactory { /** * Determine {@link RetryStrategy} when expected spot price data is missing. * - * @param localTime the time of daily data request - * @param zoneId time-zone * @return retry strategy */ - public static RetryStrategy whenExpectedSpotPriceDataMissing(LocalTime localTime, ZoneId zoneId) { - LocalTime now = LocalTime.now(zoneId); - if (now.isAfter(localTime)) { - return new ExponentialBackoff().withMinimum(Duration.ofMinutes(10)).withJitter(0.2); - } - return atFixedTime(localTime, zoneId); + public static RetryStrategy whenExpectedSpotPriceDataMissing() { + return new ExponentialBackoff().withMinimum(Duration.ofMinutes(10)).withMaximum(Duration.ofHours(1)) + .withJitter(0.2); } }