2 * Copyright (c) 2010-2023 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.Units;
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 if (jsonData == null) {
79 String sensorNumber = String.valueOf(i);
80 StringReader stringReader = new StringReader(jsonData);
81 JsonReader reader = new JsonReader(stringReader);
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(),
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,
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());
113 } catch (IOException e) {
114 logger.debug("IOException from JsonReader: {}", e.getMessage(), e);
118 stringReader.close();
119 } catch (IOException e) {
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");
134 * Convert the soil moisture to a string representation
136 private String convertSoilMoistureToString(double soilMoisture) {
137 Double key = soilMoistureMap.ceilingKey(soilMoisture);
138 return key == null ? "UNKNOWN" : soilMoistureMap.getOrDefault(key, "UNKNOWN");