]> git.basschouten.com Git - openhab-addons.git/blob
44844a6db2516d83bcddfe50e4e485dcfc91de89
[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.solarforecast.internal.utils;
14
15 import java.time.Instant;
16 import java.util.Iterator;
17 import java.util.List;
18 import java.util.TreeMap;
19
20 import javax.measure.MetricPrefix;
21 import javax.measure.quantity.Energy;
22 import javax.measure.quantity.Power;
23
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.openhab.binding.solarforecast.internal.actions.SolarForecast;
26 import org.openhab.core.library.types.QuantityType;
27 import org.openhab.core.library.unit.Units;
28 import org.openhab.core.types.TimeSeries.Entry;
29
30 /**
31  * The {@link Utils} Helpers for Solcast and ForecastSolar
32  *
33  * @author Bernd Weymann - Initial contribution
34  */
35 @NonNullByDefault
36 public class Utils {
37     public static QuantityType<Energy> getEnergyState(double d) {
38         if (d < 0) {
39             return QuantityType.valueOf(-1, Units.KILOWATT_HOUR);
40         }
41         return QuantityType.valueOf(Math.round(d * 1000) / 1000.0, Units.KILOWATT_HOUR);
42     }
43
44     public static QuantityType<Power> getPowerState(double d) {
45         if (d < 0) {
46             return QuantityType.valueOf(-1, MetricPrefix.KILO(Units.WATT));
47         }
48         return QuantityType.valueOf(Math.round(d * 1000) / 1000.0, MetricPrefix.KILO(Units.WATT));
49     }
50
51     public static void addState(TreeMap<Instant, QuantityType<?>> map, Entry entry) {
52         Instant timestamp = entry.timestamp();
53         QuantityType<?> qt1 = map.get(timestamp);
54         if (qt1 != null) {
55             QuantityType<?> qt2 = (QuantityType<?>) entry.state();
56             double combinedValue = qt1.doubleValue() + qt2.doubleValue();
57             map.put(timestamp, QuantityType.valueOf(combinedValue, qt2.getUnit()));
58         } else {
59             map.put(timestamp, (QuantityType<?>) entry.state());
60         }
61     }
62
63     public static boolean isBeforeOrEqual(Instant query, Instant reference) {
64         return !query.isAfter(reference);
65     }
66
67     public static boolean isAfterOrEqual(Instant query, Instant reference) {
68         return !query.isBefore(reference);
69     }
70
71     public static Instant getCommonStartTime(List<SolarForecast> forecastObjects) {
72         if (forecastObjects.isEmpty()) {
73             return Instant.MAX;
74         }
75         Instant start = Instant.MIN;
76         for (Iterator<SolarForecast> iterator = forecastObjects.iterator(); iterator.hasNext();) {
77             SolarForecast sf = iterator.next();
78             // if start is maximum there's no forecast data available - return immediately
79             if (sf.getForecastBegin().equals(Instant.MAX)) {
80                 return Instant.MAX;
81             } else if (sf.getForecastBegin().isAfter(start)) {
82                 // take latest timestamp from all forecasts
83                 start = sf.getForecastBegin();
84             }
85         }
86         return start;
87     }
88
89     public static Instant getCommonEndTime(List<SolarForecast> forecastObjects) {
90         if (forecastObjects.isEmpty()) {
91             return Instant.MIN;
92         }
93         Instant end = Instant.MAX;
94         for (Iterator<SolarForecast> iterator = forecastObjects.iterator(); iterator.hasNext();) {
95             SolarForecast sf = iterator.next();
96             // if end is minimum there's no forecast data available - return immediately
97             if (sf.getForecastEnd().equals(Instant.MIN)) {
98                 return Instant.MIN;
99             } else if (sf.getForecastEnd().isBefore(end)) {
100                 // take earliest timestamp from all forecast
101                 end = sf.getForecastEnd();
102             }
103         }
104         return end;
105     }
106 }