]> git.basschouten.com Git - openhab-addons.git/blob
ef36fbdd11152daa585a69dcdbd7bdfd8dca24a9
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.energidataservice.internal;
14
15 import java.math.BigDecimal;
16 import java.time.Clock;
17 import java.time.Instant;
18 import java.time.LocalDateTime;
19 import java.time.LocalTime;
20 import java.time.temporal.ChronoUnit;
21 import java.util.Collection;
22 import java.util.Map;
23 import java.util.Map.Entry;
24 import java.util.concurrent.ConcurrentHashMap;
25
26 import org.eclipse.jdt.annotation.NonNullByDefault;
27 import org.eclipse.jdt.annotation.Nullable;
28 import org.openhab.binding.energidataservice.internal.api.dto.DatahubPricelistRecord;
29
30 /**
31  * Parses results from {@link org.openhab.binding.energidataservice.internal.api.dto.DatahubPricelistRecords}
32  * into map of hourly tariffs.
33  * 
34  * @author Jacob Laursen - Initial contribution
35  */
36 @NonNullByDefault
37 public class PriceListParser {
38
39     private final Clock clock;
40
41     public PriceListParser() {
42         this(Clock.system(EnergiDataServiceBindingConstants.DATAHUB_TIMEZONE));
43     }
44
45     public PriceListParser(Clock clock) {
46         this.clock = clock;
47     }
48
49     public Map<Instant, BigDecimal> toHourly(Collection<DatahubPricelistRecord> records) {
50         Instant firstHourStart = Instant.now(clock).minus(CacheManager.NUMBER_OF_HISTORIC_HOURS, ChronoUnit.HOURS)
51                 .truncatedTo(ChronoUnit.HOURS);
52         Instant lastHourStart = Instant.now(clock).truncatedTo(ChronoUnit.HOURS).plus(2, ChronoUnit.DAYS)
53                 .truncatedTo(ChronoUnit.DAYS);
54
55         return toHourly(records, firstHourStart, lastHourStart);
56     }
57
58     public Map<Instant, BigDecimal> toHourly(Collection<DatahubPricelistRecord> records, Instant firstHourStart,
59             Instant lastHourStart) {
60         Map<Instant, BigDecimal> totalMap = new ConcurrentHashMap<>(CacheManager.TARIFF_MAX_CACHE_SIZE);
61         records.stream().map(record -> record.chargeTypeCode()).distinct().forEach(chargeTypeCode -> {
62             Map<Instant, BigDecimal> currentMap = toHourly(records, chargeTypeCode, firstHourStart, lastHourStart);
63             for (Entry<Instant, BigDecimal> current : currentMap.entrySet()) {
64                 BigDecimal total = totalMap.get(current.getKey());
65                 if (total == null) {
66                     total = BigDecimal.ZERO;
67                 }
68                 totalMap.put(current.getKey(), total.add(current.getValue()));
69             }
70         });
71
72         return totalMap;
73     }
74
75     private Map<Instant, BigDecimal> toHourly(Collection<DatahubPricelistRecord> records, String chargeTypeCode,
76             Instant firstHourStart, Instant lastHourStart) {
77         Map<Instant, BigDecimal> tariffMap = new ConcurrentHashMap<>(CacheManager.TARIFF_MAX_CACHE_SIZE);
78
79         LocalDateTime previousValidFrom = LocalDateTime.MAX;
80         LocalDateTime previousValidTo = LocalDateTime.MIN;
81         Map<LocalTime, BigDecimal> tariffs = Map.of();
82         for (Instant hourStart = firstHourStart; hourStart
83                 .isBefore(lastHourStart); hourStart = hourStart.plus(1, ChronoUnit.HOURS)) {
84             LocalDateTime localDateTime = hourStart.atZone(EnergiDataServiceBindingConstants.DATAHUB_TIMEZONE)
85                     .toLocalDateTime();
86             if (localDateTime.compareTo(previousValidFrom) < 0 || localDateTime.compareTo(previousValidTo) >= 0) {
87                 DatahubPricelistRecord priceList = getTariffs(records, localDateTime, chargeTypeCode);
88                 if (priceList != null) {
89                     tariffs = priceList.getTariffMap();
90                     previousValidFrom = priceList.validFrom();
91                     previousValidTo = priceList.validTo();
92                 } else {
93                     tariffs = Map.of();
94                 }
95             }
96
97             LocalTime localTime = LocalTime
98                     .of(hourStart.atZone(EnergiDataServiceBindingConstants.DATAHUB_TIMEZONE).getHour(), 0);
99             BigDecimal tariff = tariffs.get(localTime);
100             if (tariff != null) {
101                 tariffMap.put(hourStart, tariff);
102             }
103         }
104
105         return tariffMap;
106     }
107
108     private @Nullable DatahubPricelistRecord getTariffs(Collection<DatahubPricelistRecord> records,
109             LocalDateTime localDateTime, String chargeTypeCode) {
110         return records.stream()
111                 .filter(record -> localDateTime.compareTo(record.validFrom()) >= 0
112                         && localDateTime.compareTo(record.validTo()) < 0
113                         && record.chargeTypeCode().equals(chargeTypeCode))
114                 .findFirst().orElse(null);
115     }
116 }