]> git.basschouten.com Git - openhab-addons.git/blob
f2e2dccd3b4350f03706b0b05bda9687cf74621a
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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 static org.openhab.binding.ambientweather.internal.AmbientWeatherBindingConstants.*;
16
17 import java.io.IOException;
18 import java.io.StringReader;
19 import java.util.TreeMap;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.openhab.binding.ambientweather.internal.handler.AmbientWeatherStationHandler;
24 import org.openhab.core.library.unit.ImperialUnits;
25 import org.openhab.core.library.unit.SmartHomeUnits;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 import com.google.gson.stream.JsonReader;
30
31 /**
32  * The {@link RemoteSensor} is responsible for updating the remote sensor
33  * channels for the remote sensors attached to a weather station.
34  *
35  * @author Mark Hilbush - Initial contribution
36  */
37 @NonNullByDefault
38 public class RemoteSensor {
39     // Maximum number of remote sensors that can be supported by a station
40     private static final int MAX_SENSORS = 10;
41
42     private final Logger logger = LoggerFactory.getLogger(RemoteSensor.class);
43
44     private final TreeMap<Double, String> soilMoistureMap = new TreeMap<>();
45
46     private int numberOfSensors;
47
48     public RemoteSensor() {
49         initializeSoilMoistureMap();
50     }
51
52     public void setNumberOfSensors(int numberOfSensors) {
53         if (numberOfSensors < 1 || numberOfSensors > MAX_SENSORS) {
54             throw new IllegalArgumentException("Invalid number of sensors");
55         }
56         this.numberOfSensors = numberOfSensors;
57     }
58
59     public void updateChannels(AmbientWeatherStationHandler handler, final @Nullable String jsonData) {
60         if (jsonData == null) {
61             throw new IllegalArgumentException("Json data is null");
62         }
63         if (numberOfSensors < 1 || numberOfSensors > MAX_SENSORS) {
64             throw new IllegalStateException("Number of sensors has not been set");
65         }
66         for (int sensorNumber = 1; sensorNumber <= numberOfSensors; sensorNumber++) {
67             updateSensorChannels(handler, sensorNumber, jsonData);
68         }
69     }
70
71     /*
72      * Iterate through the JSON object and update the channels for which
73      * there are remote sensor values
74      */
75     private void updateSensorChannels(AmbientWeatherStationHandler handler, int i, final @Nullable String jsonData) {
76         String sensorNumber = String.valueOf(i);
77         StringReader stringReader = new StringReader(jsonData);
78         JsonReader reader = new JsonReader(stringReader);
79         try {
80             reader.beginObject();
81             while (reader.hasNext()) {
82                 String name = reader.nextName();
83                 if (("temp" + sensorNumber + "f").equals(name)) {
84                     handler.updateQuantity(CHGRP_REMOTE_SENSOR + sensorNumber, CH_TEMPERATURE, reader.nextDouble(),
85                             ImperialUnits.FAHRENHEIT);
86                 } else if (("dewPoint" + sensorNumber).equals(name)) {
87                     handler.updateQuantity(CHGRP_REMOTE_SENSOR + sensorNumber, CH_DEW_POINT, reader.nextDouble(),
88                             ImperialUnits.FAHRENHEIT);
89                 } else if (("feelsLike" + sensorNumber).equals(name)) {
90                     handler.updateQuantity(CHGRP_REMOTE_SENSOR + sensorNumber, CH_FEELING_TEMPERATURE,
91                             reader.nextDouble(), ImperialUnits.FAHRENHEIT);
92                 } else if (("humidity" + sensorNumber).equals(name)) {
93                     handler.updateQuantity(CHGRP_REMOTE_SENSOR + sensorNumber, CH_HUMIDITY, reader.nextDouble(),
94                             SmartHomeUnits.PERCENT);
95                 } else if (("soiltemp" + sensorNumber).equals(name)) {
96                     handler.updateQuantity(CHGRP_REMOTE_SENSOR + sensorNumber, CH_SOIL_TEMPERATURE, reader.nextDouble(),
97                             ImperialUnits.FAHRENHEIT);
98                 } else if (("soilhum" + sensorNumber).equals(name)) {
99                     Double soilMoisture = reader.nextDouble();
100                     handler.updateQuantity(CHGRP_REMOTE_SENSOR + sensorNumber, CH_SOIL_MOISTURE, soilMoisture,
101                             SmartHomeUnits.PERCENT);
102                     handler.updateString(CHGRP_REMOTE_SENSOR + sensorNumber, CH_SOIL_MOISTURE_LEVEL,
103                             convertSoilMoistureToString(soilMoisture));
104                 } else if (("batt" + sensorNumber).equals(name)) {
105                     handler.updateString(CHGRP_REMOTE_SENSOR + sensorNumber, CH_BATTERY_INDICATOR, reader.nextString());
106                 } else {
107                     reader.skipValue();
108                 }
109             }
110         } catch (IOException e) {
111             logger.debug("IOException from JsonReader: {}", e.getMessage(), e);
112         } finally {
113             try {
114                 reader.close();
115                 stringReader.close();
116             } catch (IOException e) {
117                 // Eat the exception
118             }
119         }
120     }
121
122     private void initializeSoilMoistureMap() {
123         soilMoistureMap.put(33.0, "VERY DRY");
124         soilMoistureMap.put(60.0, "DRY");
125         soilMoistureMap.put(80.0, "MOIST");
126         soilMoistureMap.put(93.0, "WET");
127         soilMoistureMap.put(100.0, "VERY WET");
128     }
129
130     /*
131      * Convert the soil moisture to a string representation
132      */
133     private String convertSoilMoistureToString(double soilMoisture) {
134         Double key = soilMoistureMap.ceilingKey(soilMoisture);
135         return key == null ? "UNKNOWN" : soilMoistureMap.getOrDefault(key, "UNKNOWN");
136     }
137 }