mirror of
https://github.com/openhab/openhab-addons.git
synced 2025-01-10 15:11:59 +01:00
[energidataservice] Refactor to eliminate code duplication (#15651)
Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
This commit is contained in:
parent
54e128fee6
commit
86f8bc991d
@ -50,16 +50,11 @@ public class CacheManager {
|
||||
private final Clock clock;
|
||||
private final PriceListParser priceListParser = new PriceListParser();
|
||||
|
||||
private Collection<DatahubPricelistRecord> netTariffRecords = new ArrayList<>();
|
||||
private Collection<DatahubPricelistRecord> systemTariffRecords = new ArrayList<>();
|
||||
private Collection<DatahubPricelistRecord> electricityTaxRecords = new ArrayList<>();
|
||||
private Collection<DatahubPricelistRecord> transmissionNetTariffRecords = new ArrayList<>();
|
||||
private Map<DatahubTariff, Collection<DatahubPricelistRecord>> datahubRecordsMap = new HashMap<>();
|
||||
|
||||
private Map<Instant, BigDecimal> spotPriceMap = new ConcurrentHashMap<>(SPOT_PRICE_MAX_CACHE_SIZE);
|
||||
private Map<Instant, BigDecimal> netTariffMap = new ConcurrentHashMap<>(TARIFF_MAX_CACHE_SIZE);
|
||||
private Map<Instant, BigDecimal> systemTariffMap = new ConcurrentHashMap<>(TARIFF_MAX_CACHE_SIZE);
|
||||
private Map<Instant, BigDecimal> electricityTaxMap = new ConcurrentHashMap<>(TARIFF_MAX_CACHE_SIZE);
|
||||
private Map<Instant, BigDecimal> transmissionNetTariffMap = new ConcurrentHashMap<>(TARIFF_MAX_CACHE_SIZE);
|
||||
|
||||
private Map<DatahubTariff, Map<Instant, BigDecimal>> tariffsMap = new ConcurrentHashMap<>();
|
||||
|
||||
public CacheManager() {
|
||||
this(Clock.systemDefaultZone());
|
||||
@ -67,22 +62,20 @@ public class CacheManager {
|
||||
|
||||
public CacheManager(Clock clock) {
|
||||
this.clock = clock.withZone(NORD_POOL_TIMEZONE);
|
||||
|
||||
for (DatahubTariff tariff : DatahubTariff.values()) {
|
||||
datahubRecordsMap.put(tariff, new ArrayList<>());
|
||||
tariffsMap.put(tariff, new ConcurrentHashMap<>(TARIFF_MAX_CACHE_SIZE));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear all cached data.
|
||||
*/
|
||||
public void clear() {
|
||||
netTariffRecords.clear();
|
||||
systemTariffRecords.clear();
|
||||
electricityTaxRecords.clear();
|
||||
transmissionNetTariffRecords.clear();
|
||||
|
||||
datahubRecordsMap.clear();
|
||||
spotPriceMap.clear();
|
||||
netTariffMap.clear();
|
||||
systemTariffMap.clear();
|
||||
electricityTaxMap.clear();
|
||||
transmissionNetTariffMap.clear();
|
||||
tariffsMap.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -101,47 +94,18 @@ public class CacheManager {
|
||||
}
|
||||
|
||||
/**
|
||||
* Replace current "raw"/unprocessed net tariff records in cache.
|
||||
* Replace current "raw"/unprocessed tariff records in cache.
|
||||
* Map of hourly tariffs will be updated automatically.
|
||||
*
|
||||
* @param records to cache
|
||||
*/
|
||||
public void putNetTariffs(Collection<DatahubPricelistRecord> records) {
|
||||
putDatahubRecords(netTariffRecords, records);
|
||||
updateNetTariffs();
|
||||
}
|
||||
|
||||
/**
|
||||
* Replace current "raw"/unprocessed system tariff records in cache.
|
||||
* Map of hourly tariffs will be updated automatically.
|
||||
*
|
||||
* @param records to cache
|
||||
*/
|
||||
public void putSystemTariffs(Collection<DatahubPricelistRecord> records) {
|
||||
putDatahubRecords(systemTariffRecords, records);
|
||||
updateSystemTariffs();
|
||||
}
|
||||
|
||||
/**
|
||||
* Replace current "raw"/unprocessed electricity tax records in cache.
|
||||
* Map of hourly taxes will be updated automatically.
|
||||
*
|
||||
* @param records to cache
|
||||
*/
|
||||
public void putElectricityTaxes(Collection<DatahubPricelistRecord> records) {
|
||||
putDatahubRecords(electricityTaxRecords, records);
|
||||
updateElectricityTaxes();
|
||||
}
|
||||
|
||||
/**
|
||||
* Replace current "raw"/unprocessed transmission net tariff records in cache.
|
||||
* Map of hourly tariffs will be updated automatically.
|
||||
*
|
||||
* @param records to cache
|
||||
*/
|
||||
public void putTransmissionNetTariffs(Collection<DatahubPricelistRecord> records) {
|
||||
putDatahubRecords(transmissionNetTariffRecords, records);
|
||||
updateTransmissionNetTariffs();
|
||||
public void putTariffs(DatahubTariff datahubTariff, Collection<DatahubPricelistRecord> records) {
|
||||
Collection<DatahubPricelistRecord> datahubRecords = datahubRecordsMap.get(datahubTariff);
|
||||
if (datahubRecords == null) {
|
||||
throw new IllegalStateException("Datahub records not initialized");
|
||||
}
|
||||
putDatahubRecords(datahubRecords, records);
|
||||
updateTariffs(datahubTariff);
|
||||
}
|
||||
|
||||
private void putDatahubRecords(Collection<DatahubPricelistRecord> destination,
|
||||
@ -154,34 +118,14 @@ public class CacheManager {
|
||||
}
|
||||
|
||||
/**
|
||||
* Update map of hourly net tariffs from internal cache.
|
||||
* Update map of hourly tariffs from internal cache.
|
||||
*/
|
||||
public void updateNetTariffs() {
|
||||
netTariffMap = priceListParser.toHourly(netTariffRecords);
|
||||
cleanup();
|
||||
}
|
||||
|
||||
/**
|
||||
* Update map of system tariffs from internal cache.
|
||||
*/
|
||||
public void updateSystemTariffs() {
|
||||
systemTariffMap = priceListParser.toHourly(systemTariffRecords);
|
||||
cleanup();
|
||||
}
|
||||
|
||||
/**
|
||||
* Update map of electricity taxes from internal cache.
|
||||
*/
|
||||
public void updateElectricityTaxes() {
|
||||
electricityTaxMap = priceListParser.toHourly(electricityTaxRecords);
|
||||
cleanup();
|
||||
}
|
||||
|
||||
/**
|
||||
* Update map of hourly transmission net tariffs from internal cache.
|
||||
*/
|
||||
public void updateTransmissionNetTariffs() {
|
||||
transmissionNetTariffMap = priceListParser.toHourly(transmissionNetTariffRecords);
|
||||
public void updateTariffs(DatahubTariff datahubTariff) {
|
||||
Collection<DatahubPricelistRecord> datahubRecords = datahubRecordsMap.get(datahubTariff);
|
||||
if (datahubRecords == null) {
|
||||
throw new IllegalStateException("Datahub records not initialized");
|
||||
}
|
||||
tariffsMap.put(datahubTariff, priceListParser.toHourly(datahubRecords));
|
||||
cleanup();
|
||||
}
|
||||
|
||||
@ -214,115 +158,39 @@ public class CacheManager {
|
||||
}
|
||||
|
||||
/**
|
||||
* Get current net tariff.
|
||||
* Get current tariff.
|
||||
*
|
||||
* @return net tariff currently valid
|
||||
* @return tariff currently valid
|
||||
*/
|
||||
public @Nullable BigDecimal getNetTariff() {
|
||||
return getNetTariff(Instant.now(clock));
|
||||
public @Nullable BigDecimal getTariff(DatahubTariff datahubTariff) {
|
||||
return getTariff(datahubTariff, Instant.now(clock));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get net tariff valid at provided instant.
|
||||
* Get tariff valid at provided instant.
|
||||
*
|
||||
* @param time {@link Instant} for which to get the net tariff
|
||||
* @return net tariff at given time or null if not available
|
||||
* @param time {@link Instant} for which to get the tariff
|
||||
* @return tariff at given time or null if not available
|
||||
*/
|
||||
public @Nullable BigDecimal getNetTariff(Instant time) {
|
||||
return netTariffMap.get(getHourStart(time));
|
||||
public @Nullable BigDecimal getTariff(DatahubTariff datahubTariff, Instant time) {
|
||||
Map<Instant, BigDecimal> tariffs = tariffsMap.get(datahubTariff);
|
||||
if (tariffs == null) {
|
||||
throw new IllegalStateException("Tariffs not initialized");
|
||||
}
|
||||
return tariffs.get(getHourStart(time));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get map of all cached net tariffs.
|
||||
* Get map of all cached tariffs.
|
||||
*
|
||||
* @return net tariffs currently available, {@link #NUMBER_OF_HISTORIC_HOURS} back
|
||||
* @return tariffs currently available, {@link #NUMBER_OF_HISTORIC_HOURS} back
|
||||
*/
|
||||
public Map<Instant, BigDecimal> getNetTariffs() {
|
||||
return new HashMap<Instant, BigDecimal>(netTariffMap);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get current system tariff.
|
||||
*
|
||||
* @return system tariff currently valid
|
||||
*/
|
||||
public @Nullable BigDecimal getSystemTariff() {
|
||||
return getSystemTariff(Instant.now(clock));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get system tariff valid at provided instant.
|
||||
*
|
||||
* @param time {@link Instant} for which to get the system tariff
|
||||
* @return system tariff at given time or null if not available
|
||||
*/
|
||||
public @Nullable BigDecimal getSystemTariff(Instant time) {
|
||||
return systemTariffMap.get(getHourStart(time));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get map of all cached system tariffs.
|
||||
*
|
||||
* @return system tariffs currently available, {@link #NUMBER_OF_HISTORIC_HOURS} back
|
||||
*/
|
||||
public Map<Instant, BigDecimal> getSystemTariffs() {
|
||||
return new HashMap<Instant, BigDecimal>(systemTariffMap);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get current electricity tax.
|
||||
*
|
||||
* @return electricity tax currently valid
|
||||
*/
|
||||
public @Nullable BigDecimal getElectricityTax() {
|
||||
return getElectricityTax(Instant.now(clock));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get electricity tax valid at provided instant.
|
||||
*
|
||||
* @param time {@link Instant} for which to get the electricity tax
|
||||
* @return electricity tax at given time or null if not available
|
||||
*/
|
||||
public @Nullable BigDecimal getElectricityTax(Instant time) {
|
||||
return electricityTaxMap.get(getHourStart(time));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get map of all cached electricity taxes.
|
||||
*
|
||||
* @return electricity taxes currently available, {@link #NUMBER_OF_HISTORIC_HOURS} back
|
||||
*/
|
||||
public Map<Instant, BigDecimal> getElectricityTaxes() {
|
||||
return new HashMap<Instant, BigDecimal>(electricityTaxMap);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get current transmission net tariff.
|
||||
*
|
||||
* @return transmission net tariff currently valid
|
||||
*/
|
||||
public @Nullable BigDecimal getTransmissionNetTariff() {
|
||||
return getTransmissionNetTariff(Instant.now(clock));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get transmission net tariff valid at provided instant.
|
||||
*
|
||||
* @param time {@link Instant} for which to get the transmission net tariff
|
||||
* @return transmission net tariff at given time or null if not available
|
||||
*/
|
||||
public @Nullable BigDecimal getTransmissionNetTariff(Instant time) {
|
||||
return transmissionNetTariffMap.get(getHourStart(time));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get map of all cached transmission net tariffs.
|
||||
*
|
||||
* @return transmission net tariffs currently available, {@link #NUMBER_OF_HISTORIC_HOURS} back
|
||||
*/
|
||||
public Map<Instant, BigDecimal> getTransmissionNetTariffs() {
|
||||
return new HashMap<Instant, BigDecimal>(transmissionNetTariffMap);
|
||||
public Map<Instant, BigDecimal> getTariffs(DatahubTariff datahubTariff) {
|
||||
Map<Instant, BigDecimal> tariffs = tariffsMap.get(datahubTariff);
|
||||
if (tariffs == null) {
|
||||
throw new IllegalStateException("Tariffs not initialized");
|
||||
}
|
||||
return new HashMap<Instant, BigDecimal>(tariffs);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -373,39 +241,16 @@ public class CacheManager {
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if we have "raw" net tariff records cached which are valid tomorrow.
|
||||
* Check if we have "raw" tariff records cached which are valid tomorrow.
|
||||
*
|
||||
* @return true if net tariff records for tomorrow are cached
|
||||
* @return true if tariff records for tomorrow are cached
|
||||
*/
|
||||
public boolean areNetTariffsValidTomorrow() {
|
||||
return isValidNextDay(netTariffRecords);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if we have "raw" system tariff records cached which are valid tomorrow.
|
||||
*
|
||||
* @return true if system tariff records for tomorrow are cached
|
||||
*/
|
||||
public boolean areSystemTariffsValidTomorrow() {
|
||||
return isValidNextDay(systemTariffRecords);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if we have "raw" electricity tax records cached which are valid tomorrow.
|
||||
*
|
||||
* @return true if electricity tax records for tomorrow are cached
|
||||
*/
|
||||
public boolean areElectricityTaxesValidTomorrow() {
|
||||
return isValidNextDay(electricityTaxRecords);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if we have "raw" transmission net tariff records cached which are valid tomorrow.
|
||||
*
|
||||
* @return true if transmission net tariff records for tomorrow are cached
|
||||
*/
|
||||
public boolean areTransmissionNetTariffsValidTomorrow() {
|
||||
return isValidNextDay(transmissionNetTariffRecords);
|
||||
public boolean areTariffsValidTomorrow(DatahubTariff datahubTariff) {
|
||||
Collection<DatahubPricelistRecord> datahubRecords = datahubRecordsMap.get(datahubTariff);
|
||||
if (datahubRecords == null) {
|
||||
throw new IllegalStateException("Datahub records not initialized");
|
||||
}
|
||||
return isValidNextDay(datahubRecords);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -415,10 +260,10 @@ public class CacheManager {
|
||||
Instant firstHourStart = getFirstHourStart();
|
||||
|
||||
spotPriceMap.entrySet().removeIf(entry -> entry.getKey().isBefore(firstHourStart));
|
||||
netTariffMap.entrySet().removeIf(entry -> entry.getKey().isBefore(firstHourStart));
|
||||
systemTariffMap.entrySet().removeIf(entry -> entry.getKey().isBefore(firstHourStart));
|
||||
electricityTaxMap.entrySet().removeIf(entry -> entry.getKey().isBefore(firstHourStart));
|
||||
transmissionNetTariffMap.entrySet().removeIf(entry -> entry.getKey().isBefore(firstHourStart));
|
||||
|
||||
for (Map<Instant, BigDecimal> tariffs : tariffsMap.values()) {
|
||||
tariffs.entrySet().removeIf(entry -> entry.getKey().isBefore(firstHourStart));
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isValidNextDay(Collection<DatahubPricelistRecord> records) {
|
||||
|
@ -0,0 +1,40 @@
|
||||
/**
|
||||
* Copyright (c) 2010-2023 Contributors to the openHAB project
|
||||
*
|
||||
* See the NOTICE file(s) distributed with this work for additional
|
||||
* information.
|
||||
*
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*/
|
||||
package org.openhab.binding.energidataservice.internal;
|
||||
|
||||
import static org.openhab.binding.energidataservice.internal.EnergiDataServiceBindingConstants.*;
|
||||
|
||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||
|
||||
/**
|
||||
* {@link DatahubTariff} maps pricelists from the DatahubPricelist dataset to related channels.
|
||||
*
|
||||
* @author Jacob Laursen - Initial contribution
|
||||
*/
|
||||
@NonNullByDefault
|
||||
public enum DatahubTariff {
|
||||
NET_TARIFF(CHANNEL_NET_TARIFF),
|
||||
SYSTEM_TARIFF(CHANNEL_SYSTEM_TARIFF),
|
||||
ELECTRICITY_TAX(CHANNEL_ELECTRICITY_TAX),
|
||||
TRANSMISSION_NET_TARIFF(CHANNEL_TRANSMISSION_NET_TARIFF);
|
||||
|
||||
String channelId;
|
||||
|
||||
DatahubTariff(String channelId) {
|
||||
this.channelId = channelId;
|
||||
}
|
||||
|
||||
public String getChannelId() {
|
||||
return channelId;
|
||||
}
|
||||
}
|
@ -33,6 +33,7 @@ import javax.measure.quantity.Power;
|
||||
|
||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||
import org.eclipse.jdt.annotation.Nullable;
|
||||
import org.openhab.binding.energidataservice.internal.DatahubTariff;
|
||||
import org.openhab.binding.energidataservice.internal.PriceCalculator;
|
||||
import org.openhab.binding.energidataservice.internal.exception.MissingPriceException;
|
||||
import org.openhab.binding.energidataservice.internal.handler.EnergiDataServiceHandler;
|
||||
@ -236,22 +237,23 @@ public class EnergiDataServiceActions implements ThingActions {
|
||||
}
|
||||
|
||||
if (priceElements.contains(PriceElement.NET_TARIFF)) {
|
||||
Map<Instant, BigDecimal> netTariffMap = handler.getNetTariffs();
|
||||
Map<Instant, BigDecimal> netTariffMap = handler.getTariffs(DatahubTariff.NET_TARIFF);
|
||||
mergeMaps(prices, netTariffMap, !spotPricesRequired);
|
||||
}
|
||||
|
||||
if (priceElements.contains(PriceElement.SYSTEM_TARIFF)) {
|
||||
Map<Instant, BigDecimal> systemTariffMap = handler.getSystemTariffs();
|
||||
Map<Instant, BigDecimal> systemTariffMap = handler.getTariffs(DatahubTariff.SYSTEM_TARIFF);
|
||||
mergeMaps(prices, systemTariffMap, !spotPricesRequired);
|
||||
}
|
||||
|
||||
if (priceElements.contains(PriceElement.ELECTRICITY_TAX)) {
|
||||
Map<Instant, BigDecimal> electricityTaxMap = handler.getElectricityTaxes();
|
||||
Map<Instant, BigDecimal> electricityTaxMap = handler.getTariffs(DatahubTariff.ELECTRICITY_TAX);
|
||||
mergeMaps(prices, electricityTaxMap, !spotPricesRequired);
|
||||
}
|
||||
|
||||
if (priceElements.contains(PriceElement.TRANSMISSION_NET_TARIFF)) {
|
||||
Map<Instant, BigDecimal> transmissionNetTariffMap = handler.getTransmissionNetTariffs();
|
||||
Map<Instant, BigDecimal> transmissionNetTariffMap = handler
|
||||
.getTariffs(DatahubTariff.TRANSMISSION_NET_TARIFF);
|
||||
mergeMaps(prices, transmissionNetTariffMap, !spotPricesRequired);
|
||||
}
|
||||
|
||||
|
@ -18,7 +18,7 @@ import java.util.Set;
|
||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||
|
||||
/**
|
||||
* Filter for the {@link DatahubPricelist} dataset.
|
||||
* Filter for the DatahubPricelist dataset.
|
||||
*
|
||||
* @author Jacob Laursen - Initial contribution
|
||||
*/
|
||||
|
@ -21,6 +21,7 @@ import java.time.LocalDateTime;
|
||||
import java.time.LocalTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Currency;
|
||||
import java.util.List;
|
||||
@ -36,6 +37,7 @@ import org.eclipse.jetty.client.HttpClient;
|
||||
import org.eclipse.jetty.http.HttpStatus;
|
||||
import org.openhab.binding.energidataservice.internal.ApiController;
|
||||
import org.openhab.binding.energidataservice.internal.CacheManager;
|
||||
import org.openhab.binding.energidataservice.internal.DatahubTariff;
|
||||
import org.openhab.binding.energidataservice.internal.action.EnergiDataServiceActions;
|
||||
import org.openhab.binding.energidataservice.internal.api.ChargeType;
|
||||
import org.openhab.binding.energidataservice.internal.api.ChargeTypeCode;
|
||||
@ -170,20 +172,10 @@ public class EnergiDataServiceHandler extends BaseThingHandler {
|
||||
downloadSpotPrices();
|
||||
}
|
||||
|
||||
if (isLinked(CHANNEL_NET_TARIFF) || isLinked(CHANNEL_HOURLY_PRICES)) {
|
||||
downloadNetTariffs();
|
||||
}
|
||||
|
||||
if (isLinked(CHANNEL_SYSTEM_TARIFF) || isLinked(CHANNEL_HOURLY_PRICES)) {
|
||||
downloadSystemTariffs();
|
||||
}
|
||||
|
||||
if (isLinked(CHANNEL_ELECTRICITY_TAX) || isLinked(CHANNEL_HOURLY_PRICES)) {
|
||||
downloadElectricityTaxes();
|
||||
}
|
||||
|
||||
if (isLinked(CHANNEL_TRANSMISSION_NET_TARIFF) || isLinked(CHANNEL_HOURLY_PRICES)) {
|
||||
downloadTransmissionNetTariffs();
|
||||
for (DatahubTariff datahubTariff : DatahubTariff.values()) {
|
||||
if (isLinked(datahubTariff.getChannelId()) || isLinked(CHANNEL_HOURLY_PRICES)) {
|
||||
downloadTariffs(datahubTariff);
|
||||
}
|
||||
}
|
||||
|
||||
updateStatus(ThingStatus.ONLINE);
|
||||
@ -238,60 +230,25 @@ public class EnergiDataServiceHandler extends BaseThingHandler {
|
||||
updateProperties(properties);
|
||||
}
|
||||
|
||||
private void downloadNetTariffs() throws InterruptedException, DataServiceException {
|
||||
if (config.getGridCompanyGLN().isEmpty()) {
|
||||
return;
|
||||
}
|
||||
if (cacheManager.areNetTariffsValidTomorrow()) {
|
||||
logger.debug("Cached net tariffs still valid, skipping download.");
|
||||
cacheManager.updateNetTariffs();
|
||||
} else {
|
||||
DatahubTariffFilter filter = getNetTariffFilter();
|
||||
cacheManager.putNetTariffs(downloadPriceLists(config.getGridCompanyGLN(),
|
||||
new DatahubTariffFilter(filter, DateQueryParameter.of(filter.getDateQueryParameter(),
|
||||
Duration.ofHours(-CacheManager.NUMBER_OF_HISTORIC_HOURS)))));
|
||||
}
|
||||
}
|
||||
|
||||
private void downloadSystemTariffs() throws InterruptedException, DataServiceException {
|
||||
GlobalLocationNumber globalLocationNumber = config.getEnerginetGLN();
|
||||
private void downloadTariffs(DatahubTariff datahubTariff) throws InterruptedException, DataServiceException {
|
||||
GlobalLocationNumber globalLocationNumber = switch (datahubTariff) {
|
||||
case NET_TARIFF -> config.getGridCompanyGLN();
|
||||
default -> config.getEnerginetGLN();
|
||||
};
|
||||
if (globalLocationNumber.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
if (cacheManager.areSystemTariffsValidTomorrow()) {
|
||||
logger.debug("Cached system tariffs still valid, skipping download.");
|
||||
cacheManager.updateSystemTariffs();
|
||||
if (cacheManager.areTariffsValidTomorrow(datahubTariff)) {
|
||||
logger.debug("Cached tariffs of type {} still valid, skipping download.", datahubTariff);
|
||||
cacheManager.updateTariffs(datahubTariff);
|
||||
} else {
|
||||
cacheManager.putSystemTariffs(
|
||||
downloadPriceLists(globalLocationNumber, DatahubTariffFilterFactory.getSystemTariff()));
|
||||
}
|
||||
}
|
||||
|
||||
private void downloadElectricityTaxes() throws InterruptedException, DataServiceException {
|
||||
GlobalLocationNumber globalLocationNumber = config.getEnerginetGLN();
|
||||
if (globalLocationNumber.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
if (cacheManager.areElectricityTaxesValidTomorrow()) {
|
||||
logger.debug("Cached electricity taxes still valid, skipping download.");
|
||||
cacheManager.updateElectricityTaxes();
|
||||
} else {
|
||||
cacheManager.putElectricityTaxes(
|
||||
downloadPriceLists(globalLocationNumber, DatahubTariffFilterFactory.getElectricityTax()));
|
||||
}
|
||||
}
|
||||
|
||||
private void downloadTransmissionNetTariffs() throws InterruptedException, DataServiceException {
|
||||
GlobalLocationNumber globalLocationNumber = config.getEnerginetGLN();
|
||||
if (globalLocationNumber.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
if (cacheManager.areTransmissionNetTariffsValidTomorrow()) {
|
||||
logger.debug("Cached transmission net tariffs still valid, skipping download.");
|
||||
cacheManager.updateTransmissionNetTariffs();
|
||||
} else {
|
||||
cacheManager.putTransmissionNetTariffs(
|
||||
downloadPriceLists(globalLocationNumber, DatahubTariffFilterFactory.getTransmissionNetTariff()));
|
||||
DatahubTariffFilter filter = switch (datahubTariff) {
|
||||
case NET_TARIFF -> getNetTariffFilter();
|
||||
case SYSTEM_TARIFF -> DatahubTariffFilterFactory.getSystemTariff();
|
||||
case ELECTRICITY_TAX -> DatahubTariffFilterFactory.getElectricityTax();
|
||||
case TRANSMISSION_NET_TARIFF -> DatahubTariffFilterFactory.getTransmissionNetTariff();
|
||||
};
|
||||
cacheManager.putTariffs(datahubTariff, downloadPriceLists(globalLocationNumber, filter));
|
||||
}
|
||||
}
|
||||
|
||||
@ -327,23 +284,26 @@ public class EnergiDataServiceHandler extends BaseThingHandler {
|
||||
|
||||
Set<ChargeTypeCode> chargeTypeCodes = datahubPriceConfiguration.getChargeTypeCodes();
|
||||
Set<String> notes = datahubPriceConfiguration.getNotes();
|
||||
DatahubTariffFilter filter;
|
||||
if (!chargeTypeCodes.isEmpty() || !notes.isEmpty()) {
|
||||
// Completely override filter.
|
||||
return new DatahubTariffFilter(chargeTypeCodes, notes, start);
|
||||
filter = new DatahubTariffFilter(chargeTypeCodes, notes, start);
|
||||
} else {
|
||||
// Only override start date in pre-configured filter.
|
||||
return new DatahubTariffFilter(DatahubTariffFilterFactory.getNetTariffByGLN(config.gridCompanyGLN), start);
|
||||
filter = new DatahubTariffFilter(DatahubTariffFilterFactory.getNetTariffByGLN(config.gridCompanyGLN),
|
||||
start);
|
||||
}
|
||||
|
||||
return new DatahubTariffFilter(filter, DateQueryParameter.of(filter.getDateQueryParameter(),
|
||||
Duration.ofHours(-CacheManager.NUMBER_OF_HISTORIC_HOURS)));
|
||||
}
|
||||
|
||||
private void updatePrices() {
|
||||
cacheManager.cleanup();
|
||||
|
||||
updateCurrentSpotPrice();
|
||||
updateCurrentTariff(CHANNEL_NET_TARIFF, cacheManager.getNetTariff());
|
||||
updateCurrentTariff(CHANNEL_SYSTEM_TARIFF, cacheManager.getSystemTariff());
|
||||
updateCurrentTariff(CHANNEL_ELECTRICITY_TAX, cacheManager.getElectricityTax());
|
||||
updateCurrentTariff(CHANNEL_TRANSMISSION_NET_TARIFF, cacheManager.getTransmissionNetTariff());
|
||||
Arrays.stream(DatahubTariff.values())
|
||||
.forEach(tariff -> updateCurrentTariff(tariff.getChannelId(), cacheManager.getTariff(tariff)));
|
||||
updateHourlyPrices();
|
||||
|
||||
reschedulePriceUpdateJob();
|
||||
@ -376,10 +336,10 @@ public class EnergiDataServiceHandler extends BaseThingHandler {
|
||||
int i = 0;
|
||||
for (Entry<Instant, BigDecimal> sourcePrice : sourcePrices) {
|
||||
Instant hourStart = sourcePrice.getKey();
|
||||
BigDecimal netTariff = cacheManager.getNetTariff(hourStart);
|
||||
BigDecimal systemTariff = cacheManager.getSystemTariff(hourStart);
|
||||
BigDecimal electricityTax = cacheManager.getElectricityTax(hourStart);
|
||||
BigDecimal transmissionNetTariff = cacheManager.getTransmissionNetTariff(hourStart);
|
||||
BigDecimal netTariff = cacheManager.getTariff(DatahubTariff.NET_TARIFF, hourStart);
|
||||
BigDecimal systemTariff = cacheManager.getTariff(DatahubTariff.SYSTEM_TARIFF, hourStart);
|
||||
BigDecimal electricityTax = cacheManager.getTariff(DatahubTariff.ELECTRICITY_TAX, hourStart);
|
||||
BigDecimal transmissionNetTariff = cacheManager.getTariff(DatahubTariff.TRANSMISSION_NET_TARIFF, hourStart);
|
||||
targetPrices[i++] = new Price(hourStart.toString(), sourcePrice.getValue(), config.currencyCode, netTariff,
|
||||
systemTariff, electricityTax, transmissionNetTariff);
|
||||
}
|
||||
@ -418,91 +378,25 @@ public class EnergiDataServiceHandler extends BaseThingHandler {
|
||||
}
|
||||
|
||||
/**
|
||||
* Get cached net tariffs or try once to download them if not cached
|
||||
* Return cached tariffs or try once to download them if not cached
|
||||
* (usually if no items are linked).
|
||||
*
|
||||
* @return Map of future net tariffs
|
||||
* @return Map of future tariffs
|
||||
*/
|
||||
public Map<Instant, BigDecimal> getNetTariffs() {
|
||||
public Map<Instant, BigDecimal> getTariffs(DatahubTariff datahubTariff) {
|
||||
try {
|
||||
downloadNetTariffs();
|
||||
downloadTariffs(datahubTariff);
|
||||
} catch (DataServiceException e) {
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.warn("Error retrieving net tariffs", e);
|
||||
logger.warn("Error retrieving tariffs", e);
|
||||
} else {
|
||||
logger.warn("Error retrieving net tariffs: {}", e.getMessage());
|
||||
logger.warn("Error retrieving tariffs of type {}: {}", datahubTariff, e.getMessage());
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
|
||||
return cacheManager.getNetTariffs();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get cached system tariffs or try once to download them if not cached
|
||||
* (usually if no items are linked).
|
||||
*
|
||||
* @return Map of future system tariffs
|
||||
*/
|
||||
public Map<Instant, BigDecimal> getSystemTariffs() {
|
||||
try {
|
||||
downloadSystemTariffs();
|
||||
} catch (DataServiceException e) {
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.warn("Error retrieving system tariffs", e);
|
||||
} else {
|
||||
logger.warn("Error retrieving system tariffs: {}", e.getMessage());
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
|
||||
return cacheManager.getSystemTariffs();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get cached electricity taxes or try once to download them if not cached
|
||||
* (usually if no items are linked).
|
||||
*
|
||||
* @return Map of future electricity taxes
|
||||
*/
|
||||
public Map<Instant, BigDecimal> getElectricityTaxes() {
|
||||
try {
|
||||
downloadElectricityTaxes();
|
||||
} catch (DataServiceException e) {
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.warn("Error retrieving electricity taxes", e);
|
||||
} else {
|
||||
logger.warn("Error retrieving electricity taxes: {}", e.getMessage());
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
|
||||
return cacheManager.getElectricityTaxes();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return cached transmission net tariffs or try once to download them if not cached
|
||||
* (usually if no items are linked).
|
||||
*
|
||||
* @return Map of future transmissions net tariffs
|
||||
*/
|
||||
public Map<Instant, BigDecimal> getTransmissionNetTariffs() {
|
||||
try {
|
||||
downloadTransmissionNetTariffs();
|
||||
} catch (DataServiceException e) {
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.warn("Error retrieving transmission net tariffs", e);
|
||||
} else {
|
||||
logger.warn("Error retrieving transmission net tariffs: {}", e.getMessage());
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
|
||||
return cacheManager.getTransmissionNetTariffs();
|
||||
return cacheManager.getTariffs(datahubTariff);
|
||||
}
|
||||
|
||||
private void reschedulePriceUpdateJob() {
|
||||
|
@ -40,6 +40,7 @@ import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import org.mockito.junit.jupiter.MockitoSettings;
|
||||
import org.mockito.quality.Strictness;
|
||||
import org.openhab.binding.energidataservice.internal.DatahubTariff;
|
||||
import org.openhab.binding.energidataservice.internal.EnergiDataServiceBindingConstants;
|
||||
import org.openhab.binding.energidataservice.internal.PriceListParser;
|
||||
import org.openhab.binding.energidataservice.internal.api.dto.DatahubPricelistRecords;
|
||||
@ -393,10 +394,10 @@ public class EnergiDataServiceActionsTest {
|
||||
.toHourly(Arrays.stream(datahubRecords.records()).toList());
|
||||
|
||||
when(handler.getSpotPrices()).thenReturn(spotPrices);
|
||||
when(handler.getNetTariffs()).thenReturn(netTariffs);
|
||||
when(handler.getSystemTariffs()).thenReturn(systemTariffs);
|
||||
when(handler.getElectricityTaxes()).thenReturn(electricityTaxes);
|
||||
when(handler.getTransmissionNetTariffs()).thenReturn(transmissionNetTariffs);
|
||||
when(handler.getTariffs(DatahubTariff.NET_TARIFF)).thenReturn(netTariffs);
|
||||
when(handler.getTariffs(DatahubTariff.SYSTEM_TARIFF)).thenReturn(systemTariffs);
|
||||
when(handler.getTariffs(DatahubTariff.ELECTRICITY_TAX)).thenReturn(electricityTaxes);
|
||||
when(handler.getTariffs(DatahubTariff.TRANSMISSION_NET_TARIFF)).thenReturn(transmissionNetTariffs);
|
||||
when(handler.getCurrency()).thenReturn(EnergiDataServiceBindingConstants.CURRENCY_DKK);
|
||||
actions.setThingHandler(handler);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user