]> git.basschouten.com Git - openhab-addons.git/blob
1cd6f03159bdec23bf4570fb5eb3e37d7021f90a
[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.structure.devices.deviceparameters.impl;
14
15 import java.text.DateFormat;
16 import java.text.ParseException;
17 import java.text.SimpleDateFormat;
18 import java.time.Instant;
19 import java.util.Date;
20 import java.util.Map;
21
22 import org.openhab.binding.digitalstrom.internal.lib.event.constants.EventResponseEnum;
23 import org.openhab.binding.digitalstrom.internal.lib.serverconnection.constants.JSONApiResponseKeysEnum;
24 import org.openhab.binding.digitalstrom.internal.lib.structure.devices.deviceparameters.constants.SensorEnum;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 import com.google.gson.JsonObject;
29
30 /**
31  * The {@link DeviceSensorValue} contains all needed information of a device sensor, e.g. the sensor type, to detect
32  * which kind of sensor it is (see {@link SensorEnum}), the sensor index to read out sensor at the digitalSTROM device
33  * by calling {@link DsAPI#getDeviceSensorValue(String, DSID, String, String, Short)} and as well as of course the value
34  * and
35  * timestamp of the last sensor update.
36  *
37  * @author Michael Ochel - Initial contribution
38  * @author Matthias Siegele - Initial contribution
39  */
40 public class DeviceSensorValue {
41
42     private final Logger logger = LoggerFactory.getLogger(DeviceSensorValue.class);
43
44     private SensorEnum sensorType;
45     private Short sensorIndex;
46
47     private Float floatValue;
48     private Integer dsValue;
49
50     private Date timestamp;
51     private boolean valid = false;
52
53     /**
54      * Creates a new {@link DeviceSensorValue} through the {@link JsonObject} of the digitalSTROM json response for a
55      * device.
56      *
57      * @param sensorValue must not be null
58      */
59     public DeviceSensorValue(JsonObject sensorValue) {
60         if (sensorValue.get(JSONApiResponseKeysEnum.TYPE.getKey()) != null) {
61             sensorType = SensorEnum.getSensor(sensorValue.get(JSONApiResponseKeysEnum.TYPE.getKey()).getAsShort());
62         }
63         if (sensorValue.get(JSONApiResponseKeysEnum.INDEX.getKey()) != null) {
64             sensorIndex = sensorValue.get(JSONApiResponseKeysEnum.INDEX.getKey()).getAsShort();
65         }
66         if (sensorValue.get(JSONApiResponseKeysEnum.VALID.getKey()) != null) {
67             valid = sensorValue.get(JSONApiResponseKeysEnum.VALID.getKey()).getAsBoolean();
68         }
69         if (sensorValue.get(JSONApiResponseKeysEnum.VALUE.getKey()) != null) {
70             floatValue = sensorValue.get(JSONApiResponseKeysEnum.VALUE.getKey()).getAsFloat();
71         }
72         if (sensorValue.get(JSONApiResponseKeysEnum.VALUE_DS.getKey()) != null) {
73             dsValue = sensorValue.get(JSONApiResponseKeysEnum.VALUE_DS.getKey()).getAsInt();
74         }
75         if (sensorValue.get(JSONApiResponseKeysEnum.TIMESTAMP.getKey()) != null) {
76             DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
77             try {
78                 timestamp = formatter.parse(sensorValue.get(JSONApiResponseKeysEnum.TIMESTAMP.getKey()).getAsString());
79             } catch (ParseException e) {
80                 logger.error("A ParseException occurred by parsing date string: {}",
81                         sensorValue.get(JSONApiResponseKeysEnum.TIMESTAMP.getKey()).getAsString(), e);
82             }
83         }
84     }
85
86     /**
87      * Creates a new {@link DeviceSensorValue} through the properties of a digitalSTROM
88      * {@link EventNames#DEVICE_SENSOR_VALUE} event.
89      *
90      * @param eventProperties must not be null
91      */
92     public DeviceSensorValue(Map<EventResponseEnum, String> eventProperties) {
93         String strVal = eventProperties.get(EventResponseEnum.SENSOR_VALUE_FLOAT);
94         if (strVal != null) {
95             floatValue = Float.parseFloat(strVal);
96         }
97         strVal = eventProperties.get(EventResponseEnum.SENSOR_TYPE);
98         if (strVal != null) {
99             sensorType = SensorEnum.getSensor(Short.parseShort(strVal));
100         }
101         strVal = eventProperties.get(EventResponseEnum.SENSOR_VALUE);
102         if (strVal != null) {
103             dsValue = Integer.parseInt(strVal);
104         }
105         strVal = eventProperties.get(EventResponseEnum.SENSOR_INDEX);
106         if (strVal != null) {
107             sensorIndex = Short.parseShort(strVal);
108         }
109         timestamp = Date.from(Instant.ofEpochMilli(System.currentTimeMillis()));
110         valid = true;
111     }
112
113     /**
114      * Creates a new {@link DeviceSensorValue} through the {@link SensorEnum} and the sensor index.
115      *
116      * @param sensorType must not be null
117      * @param sensorIndex must not be null
118      */
119     public DeviceSensorValue(SensorEnum sensorType, Short sensorIndex) {
120         this.sensorType = sensorType;
121         this.sensorIndex = sensorIndex;
122     }
123
124     /**
125      * Returns the {@link Float} value of this {@link DeviceSensorValue}.
126      *
127      * @return the floatValue
128      */
129     public Float getFloatValue() {
130         return floatValue;
131     }
132
133     /**
134      * Sets a new sensor value as {@link Float}. The internal digitalSTROM value will be changed through the resolution
135      * at {@link SensorEnum} automatically, too.
136      *
137      * @param floatValue the new float sensor value
138      * @return true, if set was successful
139      */
140     public boolean setFloatValue(Float floatValue) {
141         if (floatValue > -1) {
142             this.floatValue = floatValue;
143             if (sensorType.getResolution() != 800) {
144                 this.dsValue = (int) (floatValue / sensorType.getResolution());
145             } else {
146                 this.dsValue = (int) (800 * Math.log10(floatValue));
147             }
148             timestamp = Date.from(Instant.ofEpochMilli(System.currentTimeMillis()));
149             this.valid = true;
150             return true;
151         }
152         return false;
153     }
154
155     /**
156      * Returns the internal digitalSTROM value as {@link Integer}. The resolution can be found at {@link SensorEnum},
157      * but float sensor value will be changed through the resolution at {@link SensorEnum} automatically, too.
158      *
159      * @return the dsValue
160      */
161     public Integer getDsValue() {
162         return dsValue;
163     }
164
165     /**
166      * Sets a new internal digitalSTROM value as {@link Integer}.
167      *
168      * @param dsValue the internal digitalSTROM value to set
169      * @return true, if set was successful
170      */
171     public boolean setDsValue(Integer dsValue) {
172         if (dsValue > -1) {
173             this.dsValue = dsValue;
174             if (sensorType.getResolution() != 800) {
175                 this.floatValue = dsValue * sensorType.getResolution();
176             } else {
177                 this.floatValue = 10 * (dsValue / sensorType.getResolution());
178             }
179             timestamp = Date.from(Instant.ofEpochMilli(System.currentTimeMillis()));
180             this.valid = true;
181             return true;
182         }
183         return false;
184     }
185
186     /**
187      * Sets a new internal digitalSTROM value as {@link Integer} and a new sensor value as {@link Float}.
188      *
189      * @param floatValue must not be null
190      * @param dSvalue must not be null
191      * @return true, if set was successful
192      */
193     public boolean setValues(Float floatValue, Integer dSvalue) {
194         if (dsValue > -1 && floatValue > -1) {
195             this.floatValue = floatValue;
196             this.dsValue = dSvalue;
197             timestamp = Date.from(Instant.ofEpochMilli(System.currentTimeMillis()));
198             this.valid = true;
199             return true;
200         }
201         return false;
202     }
203
204     /**
205      * Returns the sensor type as {@link SensorEnum} of this {@link DeviceSensorValue}.
206      *
207      * @return the sensorType
208      */
209     public SensorEnum getSensorType() {
210         return sensorType;
211     }
212
213     /**
214      * Returns the sensor index to read the sensor value out though
215      * {@link DsAPI#getDeviceSensorValue(String, DSID, String, String, Short)}.
216      *
217      * @return the sensorIndex
218      */
219     public Short getSensorIndex() {
220         return sensorIndex;
221     }
222
223     /**
224      * Returns the timestamp of the last set value as {@link Date}.
225      *
226      * @return the timestamp
227      */
228     public Date getTimestamp() {
229         return timestamp;
230     }
231
232     /**
233      * Returns true if the sensor value is valid.
234      *
235      * @return the valid
236      */
237     public boolean getValid() {
238         return valid;
239     }
240
241     /*
242      * (non-Javadoc)
243      *
244      * @see java.lang.Object#toString()
245      */
246     @Override
247     public String toString() {
248         return "DeviceSensorValue [sensorType=" + sensorType + ", sensorIndex=" + sensorIndex + ", floatValue="
249                 + floatValue + ", dsValue=" + dsValue + ", timestamp=" + timestamp + ", valid=" + valid + "]";
250     }
251
252     /*
253      * (non-Javadoc)
254      *
255      * @see java.lang.Object#hashCode()
256      */
257     @Override
258     public int hashCode() {
259         final int prime = 31;
260         int result = 1;
261         result = prime * result + ((sensorType == null) ? 0 : sensorType.hashCode());
262         return result;
263     }
264
265     /*
266      * (non-Javadoc)
267      *
268      * @see java.lang.Object#equals(java.lang.Object)
269      */
270     @SuppressWarnings("PMD.SimplifyBooleanReturns")
271     @Override
272     public boolean equals(Object obj) {
273         if (this == obj) {
274             return true;
275         }
276         if (obj == null) {
277             return false;
278         }
279         if (obj.getClass() != getClass()) {
280             return false;
281         }
282         DeviceSensorValue other = (DeviceSensorValue) obj;
283         if (sensorType != other.sensorType) {
284             return false;
285         }
286         return true;
287     }
288 }