]> git.basschouten.com Git - openhab-addons.git/blob
07a6e849e012fc46fa4e26750216ebd7dd611579
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.ambientweather.internal.util;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18
19 /**
20  * The {@link PressureTrend} is responsible for determining the 3 hour
21  * barometric pressure trend. All calculations are in inches of Mercury.
22  *
23  * @author Mark Hilbush - Initial contribution
24  */
25 @NonNullByDefault
26 public class PressureTrend extends SlidingTimeWindow<Double> {
27     private final Logger logger = LoggerFactory.getLogger(PressureTrend.class);
28
29     // Pressure trend is established after 3 hours
30     private static final long PRESSURE_TREND_PERIOD = 1000 * 60 * 60 * 3;
31
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;
35
36     // Pressure trends
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";
43
44     public PressureTrend() {
45         super(PRESSURE_TREND_PERIOD);
46     }
47
48     public String getPressureTrend() {
49         long firstTime;
50         long lastTime;
51         double firstValue;
52         double lastValue;
53
54         if (storage.isEmpty()) {
55             return UNKNOWN;
56         }
57
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();
63         }
64         if (lastTime - firstTime < period * 0.99) {
65             // Not within 1% of time period
66             return UNKNOWN;
67         }
68
69         double pressureDifference = lastValue - firstValue;
70         String pressureTrend;
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;
79         } else {
80             pressureTrend = FALLING_RAPIDLY;
81         }
82         removeOldEntries();
83         return pressureTrend;
84     }
85 }