]> git.basschouten.com Git - openhab-addons.git/blob
26f4fdeb4f30019c64932bbbbd3017f002a2a98b
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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 an 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 contributer
38  * @author Matthias Siegele - initial contributer
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 eventPropertie must not be null
91      */
92     public DeviceSensorValue(Map<EventResponseEnum, String> eventPropertie) {
93         if (eventPropertie.get(EventResponseEnum.SENSOR_VALUE_FLOAT) != null) {
94             floatValue = Float.parseFloat(eventPropertie.get(EventResponseEnum.SENSOR_VALUE_FLOAT));
95         }
96         if (eventPropertie.get(EventResponseEnum.SENSOR_TYPE) != null) {
97             sensorType = SensorEnum.getSensor(Short.parseShort(eventPropertie.get(EventResponseEnum.SENSOR_TYPE)));
98         }
99         if (eventPropertie.get(EventResponseEnum.SENSOR_VALUE) != null) {
100             dsValue = Integer.parseInt(eventPropertie.get(EventResponseEnum.SENSOR_VALUE));
101         }
102         if (eventPropertie.get(EventResponseEnum.SENSOR_INDEX) != null) {
103             sensorIndex = Short.parseShort(eventPropertie.get(EventResponseEnum.SENSOR_INDEX));
104         }
105         timestamp = Date.from(Instant.ofEpochMilli(System.currentTimeMillis()));
106         valid = true;
107     }
108
109     /**
110      * Creates a new {@link DeviceSensorValue} through the {@link SensorEnum} and the sensor index.
111      *
112      * @param sensorType must not be null
113      * @param sensorIndex must not be null
114      */
115     public DeviceSensorValue(SensorEnum sensorType, Short sensorIndex) {
116         this.sensorType = sensorType;
117         this.sensorIndex = sensorIndex;
118     }
119
120     /**
121      * Returns the {@link Float} value of this {@link DeviceSensorValue}.
122      *
123      * @return the floatValue
124      */
125     public Float getFloatValue() {
126         return floatValue;
127     }
128
129     /**
130      * Sets a new sensor value as {@link Float}. The internal digitalSTROM value will be changed through the resolution
131      * at {@link SensorEnum} automatically, too.
132      *
133      * @param floatValue the new float sensor value
134      * @return true, if set was successful
135      */
136     public boolean setFloatValue(Float floatValue) {
137         if (floatValue > -1) {
138             this.floatValue = floatValue;
139             if (sensorType.getResolution() != 800) {
140                 this.dsValue = (int) (floatValue / sensorType.getResolution());
141             } else {
142                 this.dsValue = (int) (800 * Math.log10(floatValue));
143             }
144             timestamp = Date.from(Instant.ofEpochMilli(System.currentTimeMillis()));
145             this.valid = true;
146             return true;
147         }
148         return false;
149     }
150
151     /**
152      * Returns the internal digitalSTROM value as {@link Integer}. The resolution can be found at {@link SensorEnum},
153      * but float sensor value will be changed through the resolution at {@link SensorEnum} automatically, too.
154      *
155      * @return the dsValue
156      */
157     public Integer getDsValue() {
158         return dsValue;
159     }
160
161     /**
162      * Sets a new internal digitalSTROM value as {@link Integer}.
163      *
164      * @param dsValue the internal digitalSTROM value to set
165      * @return true, if set was successful
166      */
167     public boolean setDsValue(Integer dsValue) {
168         if (dsValue > -1) {
169             this.dsValue = dsValue;
170             if (sensorType.getResolution() != 800) {
171                 this.floatValue = dsValue * sensorType.getResolution();
172             } else {
173                 this.floatValue = 10 * (dsValue / sensorType.getResolution());
174             }
175             timestamp = Date.from(Instant.ofEpochMilli(System.currentTimeMillis()));
176             this.valid = true;
177             return true;
178         }
179         return false;
180     }
181
182     /**
183      * Sets a new internal digitalSTROM value as {@link Integer} and a new sensor value as {@link Float}.
184      *
185      * @param floatValue must not be null
186      * @param dSvalue must not be null
187      * @return true, if set was successful
188      */
189     public boolean setValues(Float floatValue, Integer dSvalue) {
190         if (dsValue > -1 && floatValue > -1) {
191             this.floatValue = floatValue;
192             this.dsValue = dSvalue;
193             timestamp = Date.from(Instant.ofEpochMilli(System.currentTimeMillis()));
194             this.valid = true;
195             return true;
196         }
197         return false;
198     }
199
200     /**
201      * Returns the sensor type as {@link SensorEnum} of this {@link DeviceSensorValue}.
202      *
203      * @return the sensorType
204      */
205     public SensorEnum getSensorType() {
206         return sensorType;
207     }
208
209     /**
210      * Returns the sensor index to read the sensor value out though
211      * {@link DsAPI#getDeviceSensorValue(String, DSID, String, String, Short)}.
212      *
213      * @return the sensorIndex
214      */
215     public Short getSensorIndex() {
216         return sensorIndex;
217     }
218
219     /**
220      * Returns the timestamp of the last set value as {@link Date}.
221      *
222      * @return the timestamp
223      */
224     public Date getTimestamp() {
225         return timestamp;
226     }
227
228     /**
229      * Returns true if the sensor value is valid.
230      *
231      * @return the valid
232      */
233     public boolean getValid() {
234         return valid;
235     }
236
237     /*
238      * (non-Javadoc)
239      *
240      * @see java.lang.Object#toString()
241      */
242     @Override
243     public String toString() {
244         return "DeviceSensorValue [sensorType=" + sensorType + ", sensorIndex=" + sensorIndex + ", floatValue="
245                 + floatValue + ", dsValue=" + dsValue + ", timestamp=" + timestamp + ", valid=" + valid + "]";
246     }
247
248     /*
249      * (non-Javadoc)
250      *
251      * @see java.lang.Object#hashCode()
252      */
253     @Override
254     public int hashCode() {
255         final int prime = 31;
256         int result = 1;
257         result = prime * result + ((sensorType == null) ? 0 : sensorType.hashCode());
258         return result;
259     }
260
261     /*
262      * (non-Javadoc)
263      *
264      * @see java.lang.Object#equals(java.lang.Object)
265      */
266     @Override
267     public boolean equals(Object obj) {
268         if (this == obj) {
269             return true;
270         }
271         if (obj == null) {
272             return false;
273         }
274         if (obj.getClass() != getClass()) {
275             return false;
276         }
277         DeviceSensorValue other = (DeviceSensorValue) obj;
278         if (sensorType != other.sensorType) {
279             return false;
280         }
281         return true;
282     }
283 }