]> git.basschouten.com Git - openhab-addons.git/commitdiff
Fix tariff time series update during spot price unavailability (#16654)
authorJacob Laursen <jacob-github@vindvejr.dk>
Tue, 16 Apr 2024 06:21:26 +0000 (08:21 +0200)
committerGitHub <noreply@github.com>
Tue, 16 Apr 2024 06:21:26 +0000 (08:21 +0200)
Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
bundles/org.openhab.binding.energidataservice/src/main/java/org/openhab/binding/energidataservice/internal/handler/EnergiDataServiceHandler.java

index e2a9bb70e100882bececbd6d552c5a32c5f1fe5d..eac6f08be7bd7bb1529c0497b50362429806ff93 100644 (file)
@@ -25,7 +25,6 @@ import java.time.temporal.ChronoUnit;
 import java.util.Arrays;
 import java.util.Collection;
 import java.util.Currency;
-import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
@@ -496,53 +495,35 @@ public class EnergiDataServiceHandler extends BaseThingHandler {
     }
 
     private void updateTimeSeries() {
-        TimeSeries spotPriceTimeSeries = new TimeSeries(REPLACE);
-        Map<DatahubTariff, TimeSeries> datahubTimeSeriesMap = new HashMap<>();
-        Map<DatahubTariff, BigDecimal> datahubPreviousTariff = new HashMap<>();
+        updatePriceTimeSeries(CHANNEL_SPOT_PRICE, cacheManager.getSpotPrices(), config.getCurrency(), false);
+
         for (DatahubTariff datahubTariff : DatahubTariff.values()) {
-            datahubTimeSeriesMap.put(datahubTariff, new TimeSeries(REPLACE));
+            String channelId = datahubTariff.getChannelId();
+            updatePriceTimeSeries(channelId, cacheManager.getTariffs(datahubTariff), CURRENCY_DKK, true);
         }
+    }
 
-        Map<Instant, BigDecimal> spotPriceMap = cacheManager.getSpotPrices();
-        List<Entry<Instant, BigDecimal>> spotPrices = spotPriceMap.entrySet().stream()
-                .sorted(Map.Entry.comparingByKey()).toList();
-        for (Entry<Instant, BigDecimal> spotPrice : spotPrices) {
-            Instant hourStart = spotPrice.getKey();
-            if (isLinked(CHANNEL_SPOT_PRICE)) {
-                spotPriceTimeSeries.add(hourStart, getEnergyPrice(spotPrice.getValue(), config.getCurrency()));
-            }
-            for (Map.Entry<DatahubTariff, TimeSeries> entry : datahubTimeSeriesMap.entrySet()) {
-                DatahubTariff datahubTariff = entry.getKey();
-                String channelId = datahubTariff.getChannelId();
-                if (!isLinked(channelId)) {
-                    continue;
-                }
-                BigDecimal tariff = cacheManager.getTariff(datahubTariff, hourStart);
-                if (tariff != null) {
-                    BigDecimal previousTariff = datahubPreviousTariff.get(datahubTariff);
-                    if (previousTariff != null && tariff.equals(previousTariff)) {
-                        // Skip redundant states.
-                        continue;
-                    }
-                    TimeSeries timeSeries = entry.getValue();
-                    timeSeries.add(hourStart, getEnergyPrice(tariff, CURRENCY_DKK));
-                    datahubPreviousTariff.put(datahubTariff, tariff);
-                }
-            }
-        }
-        if (spotPriceTimeSeries.size() > 0) {
-            sendTimeSeries(CHANNEL_SPOT_PRICE, spotPriceTimeSeries);
+    private void updatePriceTimeSeries(String channelId, Map<Instant, BigDecimal> priceMap, Currency currency,
+            boolean deduplicate) {
+        if (!isLinked(channelId)) {
+            return;
         }
-        for (Map.Entry<DatahubTariff, TimeSeries> entry : datahubTimeSeriesMap.entrySet()) {
-            DatahubTariff datahubTariff = entry.getKey();
-            String channelId = datahubTariff.getChannelId();
-            if (!isLinked(channelId)) {
+        List<Entry<Instant, BigDecimal>> prices = priceMap.entrySet().stream().sorted(Map.Entry.comparingByKey())
+                .toList();
+        TimeSeries timeSeries = new TimeSeries(REPLACE);
+        BigDecimal previousTariff = null;
+        for (Entry<Instant, BigDecimal> price : prices) {
+            Instant hourStart = price.getKey();
+            BigDecimal priceValue = price.getValue();
+            if (deduplicate && priceValue.equals(previousTariff)) {
+                // Skip redundant states.
                 continue;
             }
-            TimeSeries timeSeries = entry.getValue();
-            if (timeSeries.size() > 0) {
-                sendTimeSeries(channelId, timeSeries);
-            }
+            timeSeries.add(hourStart, getEnergyPrice(priceValue, currency));
+            previousTariff = priceValue;
+        }
+        if (timeSeries.size() > 0) {
+            sendTimeSeries(channelId, timeSeries);
         }
     }