]> git.basschouten.com Git - openhab-addons.git/blob
b08adfad5778e1417a4ac0ba964e4cb3d8a3fe6f
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.processor;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.openhab.binding.ambientweather.internal.model.EventDataJson;
18 import org.openhab.binding.ambientweather.internal.util.PressureTrend;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 import com.google.gson.JsonSyntaxException;
23
24 /**
25  * The {@link AbstractProcessor} is the generic/error processor
26  * for info and weather updates from weather stations that are currently
27  * not supported by this binding.
28  *
29  * @author Mark Hilbush - Initial contribution
30  */
31 @NonNullByDefault
32 public abstract class AbstractProcessor implements ProcessorInterface {
33     // @formatter:off
34     private static final String[] UV_INDEX = {
35         "LOW",
36         "LOW",
37         "LOW",
38         "MODERATE",
39         "MODERATE",
40         "MODERATE",
41         "HIGH",
42         "HIGH",
43         "VERY HIGH",
44         "VERY HIGH",
45         "VERY HIGH",
46         "EXTREME",
47         "EXTREME",
48         "EXTREME",
49         "EXTREME",
50         "EXTREME"
51     };
52     // @formatter:on
53
54     private static final String[] WIND_DIRECTIONS = new String[] { "N", "NNE", "NE", "ENE", "E", "ESE", "SE", "SSE",
55             "S", "SSW", "SW", "WSW", "W", "WNW", "NW", "NNW" };
56
57     protected static final String NOT_APPLICABLE = "N/A";
58
59     protected final Logger logger = LoggerFactory.getLogger(getClass());
60
61     // Used to calculate barometric pressure trend
62     protected PressureTrend pressureTrend = new PressureTrend();
63
64     /*
65      * The channel group Id for this processor
66      */
67     protected String channelGroupId = "";
68
69     /*
70      * Used to extract remote sensor data from the data event Json
71      */
72     protected RemoteSensor remoteSensor = new RemoteSensor();
73
74     /*
75      * Parse the event data json string
76      */
77     protected @Nullable EventDataJson parseEventData(String station, String jsonData) {
78         EventDataJson data = null;
79         try {
80             logger.debug("Station {}: Parsing weather data event json", station);
81             data = ProcessorFactory.getGson().fromJson(jsonData, EventDataJson.class);
82         } catch (JsonSyntaxException e) {
83             logger.info("Station {}: Data event cannot be parsed: {}", station, e.getMessage());
84         }
85         return data;
86     }
87
88     /*
89      * Convert the UV Index integer value to a string representation
90      */
91     protected String convertUVIndexToString(int uvIndex) {
92         if (uvIndex < 0 || uvIndex >= UV_INDEX.length) {
93             return "UNKNOWN";
94         }
95         return UV_INDEX[uvIndex];
96     }
97
98     /*
99      * Convert the wind directions in degrees to a string representation
100      */
101     protected String convertWindDirectionToString(double windDirectionDegrees) {
102         double step = 360.0 / WIND_DIRECTIONS.length;
103         double b = Math.floor((windDirectionDegrees + (step / 2.0)) / step);
104         return WIND_DIRECTIONS[(int) (b % WIND_DIRECTIONS.length)];
105     }
106 }