From 9a795fd21399458b1aab836e47026ed62c9fae78 Mon Sep 17 00:00:00 2001 From: Jacob Laursen Date: Tue, 7 Nov 2023 22:23:53 +0100 Subject: [PATCH] Persist future prices Resolves #15863 Signed-off-by: Jacob Laursen --- .../handler/EnergiDataServiceHandler.java | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) 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 3241685f3d9c3..472983b7c1a23 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 @@ -13,6 +13,7 @@ package org.openhab.binding.energidataservice.internal.handler; import static org.openhab.binding.energidataservice.internal.EnergiDataServiceBindingConstants.*; +import static org.openhab.core.types.TimeSeries.Policy.REPLACE; import java.math.BigDecimal; import java.time.Duration; @@ -21,9 +22,11 @@ import java.time.LocalTime; import java.time.format.DateTimeFormatter; import java.time.temporal.ChronoUnit; +import java.util.ArrayList; 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; @@ -65,6 +68,7 @@ import org.openhab.core.thing.binding.ThingHandlerService; import org.openhab.core.types.Command; import org.openhab.core.types.RefreshType; +import org.openhab.core.types.TimeSeries; import org.openhab.core.types.UnDefType; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -306,6 +310,7 @@ private void updatePrices() { Arrays.stream(DatahubTariff.values()) .forEach(tariff -> updateCurrentTariff(tariff.getChannelId(), cacheManager.getTariff(tariff))); updateHourlyPrices(); + updateTimeSeries(); reschedulePriceUpdateJob(); } @@ -348,6 +353,48 @@ private void updateHourlyPrices() { updateState(CHANNEL_HOURLY_PRICES, new StringType(gson.toJson(targetPrices))); } + private void updateTimeSeries() { + TimeSeries spotPriceTimeSeries = new TimeSeries(REPLACE); + Map datahubTimeSeriesMap = new HashMap<>(); + for (DatahubTariff datahubTariff : DatahubTariff.values()) { + datahubTimeSeriesMap.put(datahubTariff, new TimeSeries(REPLACE)); + } + + Map spotPriceMap = cacheManager.getSpotPrices(); + List> spotPrices = spotPriceMap.entrySet().stream() + .sorted(Map.Entry.comparingByKey()).toList(); + for (Entry spotPrice : spotPrices) { + Instant hourStart = spotPrice.getKey(); + if (isLinked(CHANNEL_SPOT_PRICE)) { + spotPriceTimeSeries.add(hourStart, new DecimalType(spotPrice.getValue())); + } + for (Map.Entry entry : datahubTimeSeriesMap.entrySet()) { + DatahubTariff datahubTariff = entry.getKey(); + String channelId = datahubTariff.getChannelId(); + if (!isLinked(channelId)) { + continue; + } + TimeSeries timeSeries = entry.getValue(); + timeSeries.add(hourStart, + new DecimalType(cacheManager.getTariff(datahubTariff, hourStart))); + } + } + if (spotPriceTimeSeries.size() > 0) { + sendTimeSeries(CHANNEL_SPOT_PRICE, spotPriceTimeSeries); + } + for (Map.Entry entry : datahubTimeSeriesMap.entrySet()) { + DatahubTariff datahubTariff = entry.getKey(); + String channelId = datahubTariff.getChannelId(); + if (!isLinked(channelId)) { + continue; + } + TimeSeries timeSeries = entry.getValue(); + if (timeSeries.size() > 0) { + sendTimeSeries(channelId, timeSeries); + } + } + } + /** * Get the configured {@link Currency} for spot prices. *