]> git.basschouten.com Git - openhab-addons.git/blob
f8b3656183b4ef9cf7112e14ec6b9f77225563cf
[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.digitalstrom.internal.lib.climate.jsonresponsecontainer;
14
15 import java.util.LinkedList;
16 import java.util.List;
17
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;
21
22 import com.google.gson.JsonObject;
23
24 /**
25  * The {@link BaseSensorValues} is a base implementation of sensor response of the digitalSTROM-json-API.
26  *
27  * @author Michael Ochel - Initial contribution
28  * @author Matthias Siegele - Initial contribution
29  */
30 public abstract class BaseSensorValues {
31
32     private List<CachedSensorValue> sensorValues;
33
34     /**
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).
37      *
38      * @param jObject must not be null
39      * @param outdoor (true = outdoor; false = indoor)
40      */
41     protected void addSensorValue(JsonObject jObject, boolean outdoor) {
42         if (jObject.get(JSONApiResponseKeysEnum.TEMPERATION_VALUE.getKey()) != null) {
43             SensorEnum sensorType = SensorEnum.TEMPERATURE_INDOORS;
44             if (outdoor) {
45                 sensorType = SensorEnum.TEMPERATURE_OUTDOORS;
46             }
47             addSensorValue(new CachedSensorValue(sensorType,
48                     jObject.get(JSONApiResponseKeysEnum.TEMPERATION_VALUE.getKey()).getAsFloat(),
49                     jObject.get(JSONApiResponseKeysEnum.TEMPERATION_VALUE_TIME.getKey()).getAsString()));
50         }
51         if (jObject.get(JSONApiResponseKeysEnum.HUMIDITY_VALUE.getKey()) != null) {
52             SensorEnum sensorType = SensorEnum.RELATIVE_HUMIDITY_INDOORS;
53             if (outdoor) {
54                 sensorType = SensorEnum.RELATIVE_HUMIDITY_OUTDOORS;
55             }
56             addSensorValue(new CachedSensorValue(sensorType,
57                     jObject.get(JSONApiResponseKeysEnum.HUMIDITY_VALUE.getKey()).getAsFloat(),
58                     jObject.get(JSONApiResponseKeysEnum.HUMIDITY_VALUE_TIME.getKey()).getAsString()));
59         }
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()));
64         }
65         if (jObject.get(JSONApiResponseKeysEnum.BRIGHTNESS_VALUE.getKey()) != null) {
66             SensorEnum sensorType = SensorEnum.BRIGHTNESS_INDOORS;
67             if (outdoor) {
68                 sensorType = SensorEnum.BRIGHTNESS_OUTDOORS;
69             }
70             addSensorValue(new CachedSensorValue(sensorType,
71                     jObject.get(JSONApiResponseKeysEnum.BRIGHTNESS_VALUE.getKey()).getAsFloat(),
72                     jObject.get(JSONApiResponseKeysEnum.BRIGHTNESS_VALUE_TIME.getKey()).getAsString()));
73         }
74     }
75
76     private void addSensorValue(CachedSensorValue cachedSensorValue) {
77         if (sensorValues == null) {
78             sensorValues = new LinkedList<>();
79             sensorValues.add(cachedSensorValue);
80         } else {
81             sensorValues.add(cachedSensorValue);
82         }
83     }
84
85     /**
86      * Returns the available sensor types.
87      *
88      * @return available sensor types
89      */
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());
95             }
96         }
97         return sensorTypes;
98     }
99
100     /**
101      * Returns the {@link CachedSensorValue}'s as {@link List}.
102      *
103      * @return list of {@link CachedSensorValue}'s
104      */
105     public List<CachedSensorValue> getCachedSensorValues() {
106         return sensorValues;
107     }
108
109     /**
110      * Returns the {@link CachedSensorValue} of the given sensor type or null, if no {@link CachedSensorValue} for the
111      * given sensor type exists.
112      *
113      * @param sensorType can be null
114      * @return the {@link CachedSensorValue} of the given sensorType or null
115      */
116     public CachedSensorValue getCachedSensorValue(SensorEnum sensorType) {
117         if (sensorType != null && sensorValues != null) {
118             for (CachedSensorValue cSensorValue : sensorValues) {
119                 if (cSensorValue.getSensorType().equals(sensorType)) {
120                     return cSensorValue;
121                 }
122             }
123         }
124         return null;
125     }
126
127     /**
128      * Returns true, if the given sensor type exist, otherwise false.
129      *
130      * @param sensorType can be null
131      * @return true, if the given sensor type exist, otherwise false
132      */
133     public boolean existSensorValue(SensorEnum sensorType) {
134         return getCachedSensorValue(sensorType) != null;
135     }
136
137     /**
138      * Returns true, is sensor values exists, otherwise false.
139      *
140      * @return true, if sensor values exists, otherwise false
141      */
142     public boolean existSensorValues() {
143         return sensorValues != null;
144     }
145
146     /*
147      * (non-Javadoc)
148      *
149      * @see java.lang.Object#toString()
150      */
151     @Override
152     public String toString() {
153         return "SensorValues [sensorValues=" + sensorValues + "]";
154     }
155 }