2 * Copyright (c) 2010-2024 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.solarforecast.internal.utils;
15 import java.time.Instant;
16 import java.util.Iterator;
17 import java.util.List;
18 import java.util.TreeMap;
20 import javax.measure.MetricPrefix;
21 import javax.measure.quantity.Energy;
22 import javax.measure.quantity.Power;
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;
31 * The {@link Utils} Helpers for Solcast and ForecastSolar
33 * @author Bernd Weymann - Initial contribution
37 public static QuantityType<Energy> getEnergyState(double d) {
39 return QuantityType.valueOf(-1, Units.KILOWATT_HOUR);
41 return QuantityType.valueOf(Math.round(d * 1000) / 1000.0, Units.KILOWATT_HOUR);
44 public static QuantityType<Power> getPowerState(double d) {
46 return QuantityType.valueOf(-1, MetricPrefix.KILO(Units.WATT));
48 return QuantityType.valueOf(Math.round(d * 1000) / 1000.0, MetricPrefix.KILO(Units.WATT));
51 public static void addState(TreeMap<Instant, QuantityType<?>> map, Entry entry) {
52 Instant timestamp = entry.timestamp();
53 QuantityType<?> qt1 = map.get(timestamp);
55 QuantityType<?> qt2 = (QuantityType<?>) entry.state();
56 double combinedValue = qt1.doubleValue() + qt2.doubleValue();
57 map.put(timestamp, QuantityType.valueOf(combinedValue, qt2.getUnit()));
59 map.put(timestamp, (QuantityType<?>) entry.state());
63 public static boolean isBeforeOrEqual(Instant query, Instant reference) {
64 return !query.isAfter(reference);
67 public static boolean isAfterOrEqual(Instant query, Instant reference) {
68 return !query.isBefore(reference);
71 public static Instant getCommonStartTime(List<SolarForecast> forecastObjects) {
72 if (forecastObjects.isEmpty()) {
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)) {
81 } else if (sf.getForecastBegin().isAfter(start)) {
82 // take latest timestamp from all forecasts
83 start = sf.getForecastBegin();
89 public static Instant getCommonEndTime(List<SolarForecast> forecastObjects) {
90 if (forecastObjects.isEmpty()) {
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)) {
99 } else if (sf.getForecastEnd().isBefore(end)) {
100 // take earliest timestamp from all forecast
101 end = sf.getForecastEnd();