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.digitalstrom.internal.lib.climate.jsonresponsecontainer;
15 import java.util.LinkedList;
16 import java.util.List;
18 import org.openhab.binding.digitalstrom.internal.lib.climate.datatypes.CachedSensorValue;
19 import org.openhab.binding.digitalstrom.internal.lib.serverconnection.constants.JSONApiResponseKeysEnum;
20 import org.openhab.binding.digitalstrom.internal.lib.structure.devices.deviceparameters.constants.SensorEnum;
22 import com.google.gson.JsonObject;
25 * The {@link BaseSensorValues} is a base implementation of sensor response of the digitalSTROM-json-API.
27 * @author Michael Ochel - Initial contribution
28 * @author Matthias Siegele - Initial contribution
30 public abstract class BaseSensorValues {
32 private List<CachedSensorValue> sensorValues;
35 * Adds a sensor value through the digitalSTROM-API response as {@link JsonObject}. The boolean outdoor has to be
36 * set to indicate that it is an outdoor or indoor value (needed to choose the right sensor type).
38 * @param jObject must not be null
39 * @param outdoor (true = outdoor; false = indoor)
41 protected void addSensorValue(JsonObject jObject, boolean outdoor) {
42 if (jObject.get(JSONApiResponseKeysEnum.TEMPERATION_VALUE.getKey()) != null) {
43 SensorEnum sensorType = SensorEnum.TEMPERATURE_INDOORS;
45 sensorType = SensorEnum.TEMPERATURE_OUTDOORS;
47 addSensorValue(new CachedSensorValue(sensorType,
48 jObject.get(JSONApiResponseKeysEnum.TEMPERATION_VALUE.getKey()).getAsFloat(),
49 jObject.get(JSONApiResponseKeysEnum.TEMPERATION_VALUE_TIME.getKey()).getAsString()));
51 if (jObject.get(JSONApiResponseKeysEnum.HUMIDITY_VALUE.getKey()) != null) {
52 SensorEnum sensorType = SensorEnum.RELATIVE_HUMIDITY_INDOORS;
54 sensorType = SensorEnum.RELATIVE_HUMIDITY_OUTDOORS;
56 addSensorValue(new CachedSensorValue(sensorType,
57 jObject.get(JSONApiResponseKeysEnum.HUMIDITY_VALUE.getKey()).getAsFloat(),
58 jObject.get(JSONApiResponseKeysEnum.HUMIDITY_VALUE_TIME.getKey()).getAsString()));
60 if (jObject.get(JSONApiResponseKeysEnum.CO2_CONCENTRATION_VALUE.getKey()) != null) {
61 addSensorValue(new CachedSensorValue(SensorEnum.CARBON_DIOXIDE,
62 jObject.get(JSONApiResponseKeysEnum.CO2_CONCENTRATION_VALUE.getKey()).getAsFloat(),
63 jObject.get(JSONApiResponseKeysEnum.CO2_CONCENTRATION_VALUE_TIME.getKey()).getAsString()));
65 if (jObject.get(JSONApiResponseKeysEnum.BRIGHTNESS_VALUE.getKey()) != null) {
66 SensorEnum sensorType = SensorEnum.BRIGHTNESS_INDOORS;
68 sensorType = SensorEnum.BRIGHTNESS_OUTDOORS;
70 addSensorValue(new CachedSensorValue(sensorType,
71 jObject.get(JSONApiResponseKeysEnum.BRIGHTNESS_VALUE.getKey()).getAsFloat(),
72 jObject.get(JSONApiResponseKeysEnum.BRIGHTNESS_VALUE_TIME.getKey()).getAsString()));
76 private void addSensorValue(CachedSensorValue cachedSensorValue) {
77 if (sensorValues == null) {
78 sensorValues = new LinkedList<>();
79 sensorValues.add(cachedSensorValue);
81 sensorValues.add(cachedSensorValue);
86 * Returns the available sensor types.
88 * @return available sensor types
90 public List<SensorEnum> getAvailableSensorTypes() {
91 List<SensorEnum> sensorTypes = new LinkedList<>();
92 if (sensorValues != null) {
93 for (CachedSensorValue cSensorValue : sensorValues) {
94 sensorTypes.add(cSensorValue.getSensorType());
101 * Returns the {@link CachedSensorValue}'s as {@link List}.
103 * @return list of {@link CachedSensorValue}'s
105 public List<CachedSensorValue> getCachedSensorValues() {
110 * Returns the {@link CachedSensorValue} of the given sensor type or null, if no {@link CachedSensorValue} for the
111 * given sensor type exists.
113 * @param sensorType can be null
114 * @return the {@link CachedSensorValue} of the given sensorType or null
116 public CachedSensorValue getCachedSensorValue(SensorEnum sensorType) {
117 if (sensorType != null && sensorValues != null) {
118 for (CachedSensorValue cSensorValue : sensorValues) {
119 if (cSensorValue.getSensorType().equals(sensorType)) {
128 * Returns true, if the given sensor type exist, otherwise false.
130 * @param sensorType can be null
131 * @return true, if the given sensor type exist, otherwise false
133 public boolean existSensorValue(SensorEnum sensorType) {
134 return getCachedSensorValue(sensorType) != null;
138 * Returns true, is sensor values exists, otherwise false.
140 * @return true, if sensor values exists, otherwise false
142 public boolean existSensorValues() {
143 return sensorValues != null;
149 * @see java.lang.Object#toString()
152 public String toString() {
153 return "SensorValues [sensorValues=" + sensorValues + "]";