]> git.basschouten.com Git - openhab-addons.git/blob
1d1c1079c02b6e493be6e639fc1ee233a8a77670
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.smhi.internal;
14
15 import java.math.BigDecimal;
16 import java.time.temporal.ChronoUnit;
17 import java.util.ArrayList;
18 import java.util.List;
19 import java.util.Optional;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22
23 /**
24  * @author Anders Alfredsson - Initial contribution
25  */
26 @NonNullByDefault
27 public class ForecastAggregator {
28     /**
29      * Get the maximum value for the specified parameter for the n:th day after the forecast's reference time
30      *
31      * @param timeSeries
32      * @param dayOffset
33      * @param parameter
34      * @return
35      */
36     public static Optional<BigDecimal> max(TimeSeries timeSeries, int dayOffset, String parameter) {
37         List<Forecast> dayForecasts = timeSeries.getDay(dayOffset);
38         return dayForecasts.stream().map(forecast -> forecast.getParameter(parameter)).filter(Optional::isPresent)
39                 .map(Optional::get).max(BigDecimal::compareTo);
40     }
41
42     /**
43      * Get the minimum value for the specified parameter for the n:th day after the forecast's reference time
44      *
45      * @param timeSeries
46      * @param dayOffset
47      * @param parameter
48      * @return
49      */
50     public static Optional<BigDecimal> min(TimeSeries timeSeries, int dayOffset, String parameter) {
51         List<Forecast> dayForecasts = timeSeries.getDay(dayOffset);
52         return dayForecasts.stream().map(forecast -> forecast.getParameter(parameter)).filter(Optional::isPresent)
53                 .map(Optional::get).min(BigDecimal::compareTo);
54     }
55
56     /**
57      * Get the total value for the specified parameter for the n:th day after the forecast's reference time.
58      * If there aren't any values for every hour, the previous value is used for each empty slot.
59      *
60      * @param timeSeries
61      * @param dayOffset
62      * @param parameter
63      * @return
64      */
65     public static Optional<BigDecimal> total(TimeSeries timeSeries, int dayOffset, String parameter) {
66         List<Forecast> dayForecasts = timeSeries.getDay(dayOffset);
67         List<BigDecimal> values = new ArrayList<>();
68         for (int i = 0; i < dayForecasts.size(); i++) {
69             Forecast current = dayForecasts.get(i);
70             long hours;
71             if (i == 0) {
72                 hours = current.getValidTime().until(dayForecasts.get(i + 1).getValidTime(), ChronoUnit.HOURS);
73             } else {
74                 hours = dayForecasts.get(i - 1).getValidTime().until(current.getValidTime(), ChronoUnit.HOURS);
75             }
76             values.add(current.getParameter(parameter).map(value -> value.multiply(BigDecimal.valueOf(hours)))
77                     .orElse(BigDecimal.ZERO));
78         }
79         return values.stream().reduce(BigDecimal::add);
80     }
81
82     /**
83      * Get the value at 12:00 UTC for the specified parameter for the n:th day after the forecast's reference time.
84      * If that time is not included (should only happen for day 0 if after 12:00), get the first value for the day
85      * instead.
86      *
87      * @param timeSeries
88      * @param dayOffset
89      * @param parameter
90      * @return
91      */
92     public static Optional<BigDecimal> noonOrFirst(TimeSeries timeSeries, int dayOffset, String parameter) {
93         List<Forecast> dayForecasts = timeSeries.getDay(dayOffset);
94         return dayForecasts.stream().filter(forecast -> forecast.getValidTime().getHour() >= 12).findFirst()
95                 .flatMap(forecast -> forecast.getParameter(parameter));
96     }
97 }