Fix tariff time series update during spot price unavailability (#16654)

Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
Signed-off-by: Ciprian Pascu <contact@ciprianpascu.ro>
This commit is contained in:
Jacob Laursen 2024-04-16 08:21:26 +02:00 committed by Ciprian Pascu
parent 5949c462c1
commit b44187c6bc

View File

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