]> git.basschouten.com Git - openhab-addons.git/blob
74429b6b06594e9e18b0394267aba19d146a8555
[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 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.Units;
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         if (jsonData == null) {
77             return;
78         }
79         String sensorNumber = String.valueOf(i);
80         StringReader stringReader = new StringReader(jsonData);
81         JsonReader reader = new JsonReader(stringReader);
82         try {
83             reader.beginObject();
84             while (reader.hasNext()) {
85                 String name = reader.nextName();
86                 if (("temp" + sensorNumber + "f").equals(name)) {
87                     handler.updateQuantity(CHGRP_REMOTE_SENSOR + sensorNumber, CH_TEMPERATURE, reader.nextDouble(),
88                             ImperialUnits.FAHRENHEIT);
89                 } else if (("dewPoint" + sensorNumber).equals(name)) {
90                     handler.updateQuantity(CHGRP_REMOTE_SENSOR + sensorNumber, CH_DEW_POINT, reader.nextDouble(),
91                             ImperialUnits.FAHRENHEIT);
92                 } else if (("feelsLike" + sensorNumber).equals(name)) {
93                     handler.updateQuantity(CHGRP_REMOTE_SENSOR + sensorNumber, CH_FEELING_TEMPERATURE,
94                             reader.nextDouble(), ImperialUnits.FAHRENHEIT);
95                 } else if (("humidity" + sensorNumber).equals(name)) {
96                     handler.updateQuantity(CHGRP_REMOTE_SENSOR + sensorNumber, CH_HUMIDITY, reader.nextDouble(),
97                             Units.PERCENT);
98                 } else if (("soiltemp" + sensorNumber).equals(name)) {
99                     handler.updateQuantity(CHGRP_REMOTE_SENSOR + sensorNumber, CH_SOIL_TEMPERATURE, reader.nextDouble(),
100                             ImperialUnits.FAHRENHEIT);
101                 } else if (("soilhum" + sensorNumber).equals(name)) {
102                     Double soilMoisture = reader.nextDouble();
103                     handler.updateQuantity(CHGRP_REMOTE_SENSOR + sensorNumber, CH_SOIL_MOISTURE, soilMoisture,
104                             Units.PERCENT);
105                     handler.updateString(CHGRP_REMOTE_SENSOR + sensorNumber, CH_SOIL_MOISTURE_LEVEL,
106                             convertSoilMoistureToString(soilMoisture));
107                 } else if (("batt" + sensorNumber).equals(name)) {
108                     handler.updateString(CHGRP_REMOTE_SENSOR + sensorNumber, CH_BATTERY_INDICATOR, reader.nextString());
109                 } else {
110                     reader.skipValue();
111                 }
112             }
113         } catch (IOException e) {
114             logger.debug("IOException from JsonReader: {}", e.getMessage(), e);
115         } finally {
116             try {
117                 reader.close();
118                 stringReader.close();
119             } catch (IOException e) {
120                 // Eat the exception
121             }
122         }
123     }
124
125     private void initializeSoilMoistureMap() {
126         soilMoistureMap.put(33.0, "VERY DRY");
127         soilMoistureMap.put(60.0, "DRY");
128         soilMoistureMap.put(80.0, "MOIST");
129         soilMoistureMap.put(93.0, "WET");
130         soilMoistureMap.put(100.0, "VERY WET");
131     }
132
133     /*
134      * Convert the soil moisture to a string representation
135      */
136     private String convertSoilMoistureToString(double soilMoisture) {
137         Double key = soilMoistureMap.ceilingKey(soilMoisture);
138         return key == null ? "UNKNOWN" : soilMoistureMap.getOrDefault(key, "UNKNOWN");
139     }
140 }