forked from openhab/openhab-addons
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[smhi] Add aggregated channels for daily forecast. (openhab#9387)
* Add aggregated channels for daily forecast. Also updates to use Optionals instead of @nullables, and add unit tests * Revert unsing explicit unit definition Signed-off-by: Anders Alfredsson <[email protected]>
- Loading branch information
Showing
10 changed files
with
342 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
...nhab.binding.smhi/src/main/java/org/openhab/binding/smhi/internal/ForecastAggregator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/** | ||
* Copyright (c) 2010-2020 Contributors to the openHAB project | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
|
||
package org.openhab.binding.smhi.internal; | ||
|
||
import java.math.BigDecimal; | ||
import java.math.RoundingMode; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
import org.eclipse.jdt.annotation.NonNullByDefault; | ||
|
||
/** | ||
* @author Anders Alfredsson - Initial contribution | ||
*/ | ||
@NonNullByDefault | ||
public class ForecastAggregator { | ||
public static Optional<BigDecimal> max(TimeSeries timeSeries, int dayOffset, String parameter) { | ||
List<Forecast> dayForecasts = timeSeries.getDay(dayOffset); | ||
return dayForecasts.stream().map(forecast -> forecast.getParameter(parameter)).filter(Optional::isPresent) | ||
.map(Optional::get).max(BigDecimal::compareTo); | ||
} | ||
|
||
public static Optional<BigDecimal> min(TimeSeries timeSeries, int dayOffset, String parameter) { | ||
List<Forecast> dayForecasts = timeSeries.getDay(dayOffset); | ||
return dayForecasts.stream().map(forecast -> forecast.getParameter(parameter)).filter(Optional::isPresent) | ||
.map(Optional::get).min(BigDecimal::compareTo); | ||
} | ||
|
||
public static Optional<BigDecimal> total(TimeSeries timeSeries, int dayOffset, String parameter) { | ||
List<Forecast> dayForecasts = timeSeries.getDay(dayOffset); | ||
BigDecimal sum = dayForecasts.stream().map(forecast -> forecast.getParameter(parameter)) | ||
.filter(Optional::isPresent).map(Optional::get).reduce(BigDecimal::add).orElse(BigDecimal.ZERO); | ||
BigDecimal mean = sum.divide(BigDecimal.valueOf(dayForecasts.size()), RoundingMode.HALF_UP); | ||
return Optional.of(mean.multiply(BigDecimal.valueOf(24))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.