diff --git a/bundles/org.openhab.binding.energidataservice/src/main/java/org/openhab/binding/energidataservice/internal/CacheManager.java b/bundles/org.openhab.binding.energidataservice/src/main/java/org/openhab/binding/energidataservice/internal/CacheManager.java index d73ee12dd1d..3e95bf67e82 100644 --- a/bundles/org.openhab.binding.energidataservice/src/main/java/org/openhab/binding/energidataservice/internal/CacheManager.java +++ b/bundles/org.openhab.binding.energidataservice/src/main/java/org/openhab/binding/energidataservice/internal/CacheManager.java @@ -50,16 +50,11 @@ public class CacheManager { private final Clock clock; private final PriceListParser priceListParser = new PriceListParser(); - private Collection netTariffRecords = new ArrayList<>(); - private Collection systemTariffRecords = new ArrayList<>(); - private Collection electricityTaxRecords = new ArrayList<>(); - private Collection transmissionNetTariffRecords = new ArrayList<>(); + private Map> datahubRecordsMap = new HashMap<>(); private Map spotPriceMap = new ConcurrentHashMap<>(SPOT_PRICE_MAX_CACHE_SIZE); - private Map netTariffMap = new ConcurrentHashMap<>(TARIFF_MAX_CACHE_SIZE); - private Map systemTariffMap = new ConcurrentHashMap<>(TARIFF_MAX_CACHE_SIZE); - private Map electricityTaxMap = new ConcurrentHashMap<>(TARIFF_MAX_CACHE_SIZE); - private Map transmissionNetTariffMap = new ConcurrentHashMap<>(TARIFF_MAX_CACHE_SIZE); + + private Map> 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 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 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 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 records) { - putDatahubRecords(transmissionNetTariffRecords, records); - updateTransmissionNetTariffs(); + public void putTariffs(DatahubTariff datahubTariff, Collection records) { + Collection datahubRecords = datahubRecordsMap.get(datahubTariff); + if (datahubRecords == null) { + throw new IllegalStateException("Datahub records not initialized"); + } + putDatahubRecords(datahubRecords, records); + updateTariffs(datahubTariff); } private void putDatahubRecords(Collection 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 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 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 getNetTariffs() { - return new HashMap(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 getSystemTariffs() { - return new HashMap(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 getElectricityTaxes() { - return new HashMap(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 getTransmissionNetTariffs() { - return new HashMap(transmissionNetTariffMap); + public Map getTariffs(DatahubTariff datahubTariff) { + Map tariffs = tariffsMap.get(datahubTariff); + if (tariffs == null) { + throw new IllegalStateException("Tariffs not initialized"); + } + return new HashMap(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 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 tariffs : tariffsMap.values()) { + tariffs.entrySet().removeIf(entry -> entry.getKey().isBefore(firstHourStart)); + } } private boolean isValidNextDay(Collection records) { diff --git a/bundles/org.openhab.binding.energidataservice/src/main/java/org/openhab/binding/energidataservice/internal/DatahubTariff.java b/bundles/org.openhab.binding.energidataservice/src/main/java/org/openhab/binding/energidataservice/internal/DatahubTariff.java new file mode 100644 index 00000000000..fb257d00628 --- /dev/null +++ b/bundles/org.openhab.binding.energidataservice/src/main/java/org/openhab/binding/energidataservice/internal/DatahubTariff.java @@ -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; + } +} diff --git a/bundles/org.openhab.binding.energidataservice/src/main/java/org/openhab/binding/energidataservice/internal/action/EnergiDataServiceActions.java b/bundles/org.openhab.binding.energidataservice/src/main/java/org/openhab/binding/energidataservice/internal/action/EnergiDataServiceActions.java index 3e9a3e64271..52cba2cc14b 100644 --- a/bundles/org.openhab.binding.energidataservice/src/main/java/org/openhab/binding/energidataservice/internal/action/EnergiDataServiceActions.java +++ b/bundles/org.openhab.binding.energidataservice/src/main/java/org/openhab/binding/energidataservice/internal/action/EnergiDataServiceActions.java @@ -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 netTariffMap = handler.getNetTariffs(); + Map netTariffMap = handler.getTariffs(DatahubTariff.NET_TARIFF); mergeMaps(prices, netTariffMap, !spotPricesRequired); } if (priceElements.contains(PriceElement.SYSTEM_TARIFF)) { - Map systemTariffMap = handler.getSystemTariffs(); + Map systemTariffMap = handler.getTariffs(DatahubTariff.SYSTEM_TARIFF); mergeMaps(prices, systemTariffMap, !spotPricesRequired); } if (priceElements.contains(PriceElement.ELECTRICITY_TAX)) { - Map electricityTaxMap = handler.getElectricityTaxes(); + Map electricityTaxMap = handler.getTariffs(DatahubTariff.ELECTRICITY_TAX); mergeMaps(prices, electricityTaxMap, !spotPricesRequired); } if (priceElements.contains(PriceElement.TRANSMISSION_NET_TARIFF)) { - Map transmissionNetTariffMap = handler.getTransmissionNetTariffs(); + Map transmissionNetTariffMap = handler + .getTariffs(DatahubTariff.TRANSMISSION_NET_TARIFF); mergeMaps(prices, transmissionNetTariffMap, !spotPricesRequired); } diff --git a/bundles/org.openhab.binding.energidataservice/src/main/java/org/openhab/binding/energidataservice/internal/api/DatahubTariffFilter.java b/bundles/org.openhab.binding.energidataservice/src/main/java/org/openhab/binding/energidataservice/internal/api/DatahubTariffFilter.java index ecbec4a8bb9..2393c890e6d 100644 --- a/bundles/org.openhab.binding.energidataservice/src/main/java/org/openhab/binding/energidataservice/internal/api/DatahubTariffFilter.java +++ b/bundles/org.openhab.binding.energidataservice/src/main/java/org/openhab/binding/energidataservice/internal/api/DatahubTariffFilter.java @@ -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 */ 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 970d8a1da23..94a4f430eba 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 @@ -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 chargeTypeCodes = datahubPriceConfiguration.getChargeTypeCodes(); Set 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 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 getNetTariffs() { + public Map 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 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 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 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() { diff --git a/bundles/org.openhab.binding.energidataservice/src/test/java/org/openhab/binding/energidataservice/internal/action/EnergiDataServiceActionsTest.java b/bundles/org.openhab.binding.energidataservice/src/test/java/org/openhab/binding/energidataservice/internal/action/EnergiDataServiceActionsTest.java index 3170973a0b0..5679d3d7a4f 100644 --- a/bundles/org.openhab.binding.energidataservice/src/test/java/org/openhab/binding/energidataservice/internal/action/EnergiDataServiceActionsTest.java +++ b/bundles/org.openhab.binding.energidataservice/src/test/java/org/openhab/binding/energidataservice/internal/action/EnergiDataServiceActionsTest.java @@ -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); }