2 * Copyright (c) 2010-2023 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.ambientweather.internal.util;
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
20 * The {@link PressureTrend} is responsible for determining the 3 hour
21 * barometric pressure trend. All calculations are in inches of Mercury.
23 * @author Mark Hilbush - Initial contribution
26 public class PressureTrend extends SlidingTimeWindow<Double> {
27 private final Logger logger = LoggerFactory.getLogger(PressureTrend.class);
29 // Pressure trend is established after 3 hours
30 private static final long PRESSURE_TREND_PERIOD = 1000 * 60 * 60 * 3;
32 // Thresholds used to determine pressure trends
33 private static final double RAPIDLY_THRESHOLD = 0.06;
34 private static final double STEADY_THRESHOLD = 0.02;
37 private static final String RISING_RAPIDLY = "RISING RAPIDLY";
38 private static final String RISING = "RISING";
39 private static final String FALLING_RAPIDLY = "FALLING RAPIDLY";
40 private static final String FALLING = "FALLING";
41 private static final String STEADY = "STEADY";
42 private static final String UNKNOWN = "UNKNOWN";
44 public PressureTrend() {
45 super(PRESSURE_TREND_PERIOD);
48 public String getPressureTrend() {
54 if (storage.isEmpty()) {
58 synchronized (storage) {
59 firstTime = storage.firstKey();
60 lastTime = storage.lastKey();
61 firstValue = storage.get(storage.firstKey()).doubleValue();
62 lastValue = storage.get(storage.lastKey()).doubleValue();
64 if (lastTime - firstTime < period * 0.99) {
65 // Not within 1% of time period
69 double pressureDifference = lastValue - firstValue;
71 if (pressureDifference > RAPIDLY_THRESHOLD) {
72 pressureTrend = RISING_RAPIDLY;
73 } else if (pressureDifference > STEADY_THRESHOLD && pressureDifference <= RAPIDLY_THRESHOLD) {
74 pressureTrend = RISING;
75 } else if (pressureDifference > -STEADY_THRESHOLD && pressureDifference <= STEADY_THRESHOLD) {
76 pressureTrend = STEADY;
77 } else if (pressureDifference > -RAPIDLY_THRESHOLD && pressureDifference <= -STEADY_THRESHOLD) {
78 pressureTrend = FALLING;
80 pressureTrend = FALLING_RAPIDLY;