]> git.basschouten.com Git - openhab-addons.git/blob
5f4bedc1a3f316afdf85276a313b9548dc24e220
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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 DatahubPricelistRecords} into map of hourly tariffs.
32  * 
33  * @author Jacob Laursen - Initial contribution
34  */
35 @NonNullByDefault
36 public class PriceListParser {
37
38     private final Clock clock;
39
40     public PriceListParser() {
41         this(Clock.system(EnergiDataServiceBindingConstants.DATAHUB_TIMEZONE));
42     }
43
44     public PriceListParser(Clock clock) {
45         this.clock = clock;
46     }
47
48     public Map<Instant, BigDecimal> toHourly(Collection<DatahubPricelistRecord> records) {
49         Map<Instant, BigDecimal> totalMap = new ConcurrentHashMap<>(CacheManager.TARIFF_MAX_CACHE_SIZE);
50         records.stream().map(record -> record.chargeTypeCode()).distinct().forEach(chargeTypeCode -> {
51             Map<Instant, BigDecimal> currentMap = toHourly(records, chargeTypeCode);
52             for (Entry<Instant, BigDecimal> current : currentMap.entrySet()) {
53                 BigDecimal total = totalMap.get(current.getKey());
54                 if (total == null) {
55                     total = BigDecimal.ZERO;
56                 }
57                 totalMap.put(current.getKey(), total.add(current.getValue()));
58             }
59         });
60
61         return totalMap;
62     }
63
64     public Map<Instant, BigDecimal> toHourly(Collection<DatahubPricelistRecord> records, String chargeTypeCode) {
65         Map<Instant, BigDecimal> tariffMap = new ConcurrentHashMap<>(CacheManager.TARIFF_MAX_CACHE_SIZE);
66
67         Instant firstHourStart = Instant.now(clock).minus(CacheManager.NUMBER_OF_HISTORIC_HOURS, ChronoUnit.HOURS)
68                 .truncatedTo(ChronoUnit.HOURS);
69         Instant lastHourStart = Instant.now(clock).truncatedTo(ChronoUnit.HOURS).plus(2, ChronoUnit.DAYS)
70                 .truncatedTo(ChronoUnit.DAYS);
71
72         LocalDateTime previousValidFrom = LocalDateTime.MAX;
73         LocalDateTime previousValidTo = LocalDateTime.MIN;
74         Map<LocalTime, BigDecimal> tariffs = Map.of();
75         for (Instant hourStart = firstHourStart; hourStart
76                 .isBefore(lastHourStart); hourStart = hourStart.plus(1, ChronoUnit.HOURS)) {
77             LocalDateTime localDateTime = hourStart.atZone(EnergiDataServiceBindingConstants.DATAHUB_TIMEZONE)
78                     .toLocalDateTime();
79             if (localDateTime.compareTo(previousValidFrom) < 0 || localDateTime.compareTo(previousValidTo) >= 0) {
80                 DatahubPricelistRecord priceList = getTariffs(records, localDateTime, chargeTypeCode);
81                 if (priceList != null) {
82                     tariffs = priceList.getTariffMap();
83                     previousValidFrom = priceList.validFrom();
84                     previousValidTo = priceList.validTo();
85                 } else {
86                     tariffs = Map.of();
87                 }
88             }
89
90             LocalTime localTime = LocalTime
91                     .of(hourStart.atZone(EnergiDataServiceBindingConstants.DATAHUB_TIMEZONE).getHour(), 0);
92             BigDecimal tariff = tariffs.get(localTime);
93             if (tariff != null) {
94                 tariffMap.put(hourStart, tariff);
95             }
96         }
97
98         return tariffMap;
99     }
100
101     private @Nullable DatahubPricelistRecord getTariffs(Collection<DatahubPricelistRecord> records,
102             LocalDateTime localDateTime, String chargeTypeCode) {
103         return records.stream()
104                 .filter(record -> localDateTime.compareTo(record.validFrom()) >= 0
105                         && localDateTime.compareTo(record.validTo()) < 0
106                         && record.chargeTypeCode().equals(chargeTypeCode))
107                 .findFirst().orElse(null);
108     }
109 }