Skip to content

Commit

Permalink
Persist future prices
Browse files Browse the repository at this point in the history
Resolves #15863

Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
  • Loading branch information
jlaur committed Nov 7, 2023
1 parent 24f4234 commit 5204dcc
Showing 1 changed file with 47 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -180,6 +184,7 @@ private void refreshElectricityPrices() {

updateStatus(ThingStatus.ONLINE);
updatePrices();
updateTimeSeries();

if (isLinked(CHANNEL_SPOT_PRICE) || isLinked(CHANNEL_HOURLY_PRICES)) {
if (cacheManager.getNumberOfFutureSpotPrices() < 13) {
Expand Down Expand Up @@ -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<DatahubTariff, TimeSeries> datahubTimeSeriesMap = new HashMap<>();
for (DatahubTariff datahubTariff : DatahubTariff.values()) {
datahubTimeSeriesMap.put(datahubTariff, new TimeSeries(REPLACE));
}

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, new DecimalType(spotPrice.getValue()));
}
for (Map.Entry<DatahubTariff, TimeSeries> 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<DatahubTariff, TimeSeries> 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.
*
Expand Down

0 comments on commit 5204dcc

Please sign in to comment.