2 * Copyright (c) 2010-2020 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.processor;
15 import static org.openhab.binding.ambientweather.internal.AmbientWeatherBindingConstants.*;
17 import java.io.IOException;
18 import java.io.StringReader;
19 import java.util.TreeMap;
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;
29 import com.google.gson.stream.JsonReader;
32 * The {@link RemoteSensor} is responsible for updating the remote sensor
33 * channels for the remote sensors attached to a weather station.
35 * @author Mark Hilbush - Initial contribution
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;
42 private final Logger logger = LoggerFactory.getLogger(RemoteSensor.class);
44 private final TreeMap<Double, String> soilMoistureMap = new TreeMap<>();
46 private int numberOfSensors;
48 public RemoteSensor() {
49 initializeSoilMoistureMap();
52 public void setNumberOfSensors(int numberOfSensors) {
53 if (numberOfSensors < 1 || numberOfSensors > MAX_SENSORS) {
54 throw new IllegalArgumentException("Invalid number of sensors");
56 this.numberOfSensors = numberOfSensors;
59 public void updateChannels(AmbientWeatherStationHandler handler, final @Nullable String jsonData) {
60 if (jsonData == null) {
61 throw new IllegalArgumentException("Json data is null");
63 if (numberOfSensors < 1 || numberOfSensors > MAX_SENSORS) {
64 throw new IllegalStateException("Number of sensors has not been set");
66 for (int sensorNumber = 1; sensorNumber <= numberOfSensors; sensorNumber++) {
67 updateSensorChannels(handler, sensorNumber, jsonData);
72 * Iterate through the JSON object and update the channels for which
73 * there are remote sensor values
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);
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());
110 } catch (IOException e) {
111 logger.debug("IOException from JsonReader: {}", e.getMessage(), e);
115 stringReader.close();
116 } catch (IOException e) {
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");
131 * Convert the soil moisture to a string representation
133 private String convertSoilMoistureToString(double soilMoisture) {
134 Double key = soilMoistureMap.ceilingKey(soilMoisture);
135 return key == null ? "UNKNOWN" : soilMoistureMap.getOrDefault(key, "UNKNOWN");