2 * Copyright (c) 2010-2020 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.structure.devices.deviceparameters.impl;
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;
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;
28 import com.google.gson.JsonObject;
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
35 * timestamp of the last sensor update.
37 * @author Michael Ochel - initial contributer
38 * @author Matthias Siegele - initial contributer
40 public class DeviceSensorValue {
42 private final Logger logger = LoggerFactory.getLogger(DeviceSensorValue.class);
44 private SensorEnum sensorType;
45 private Short sensorIndex;
47 private Float floatValue;
48 private Integer dsValue;
50 private Date timestamp;
51 private boolean valid = false;
54 * Creates a new {@link DeviceSensorValue} through the {@link JsonObject} of the digitalSTROM json response for a
57 * @param sensorValue must not be null
59 public DeviceSensorValue(JsonObject sensorValue) {
60 if (sensorValue.get(JSONApiResponseKeysEnum.TYPE.getKey()) != null) {
61 sensorType = SensorEnum.getSensor(sensorValue.get(JSONApiResponseKeysEnum.TYPE.getKey()).getAsShort());
63 if (sensorValue.get(JSONApiResponseKeysEnum.INDEX.getKey()) != null) {
64 sensorIndex = sensorValue.get(JSONApiResponseKeysEnum.INDEX.getKey()).getAsShort();
66 if (sensorValue.get(JSONApiResponseKeysEnum.VALID.getKey()) != null) {
67 valid = sensorValue.get(JSONApiResponseKeysEnum.VALID.getKey()).getAsBoolean();
69 if (sensorValue.get(JSONApiResponseKeysEnum.VALUE.getKey()) != null) {
70 floatValue = sensorValue.get(JSONApiResponseKeysEnum.VALUE.getKey()).getAsFloat();
72 if (sensorValue.get(JSONApiResponseKeysEnum.VALUE_DS.getKey()) != null) {
73 dsValue = sensorValue.get(JSONApiResponseKeysEnum.VALUE_DS.getKey()).getAsInt();
75 if (sensorValue.get(JSONApiResponseKeysEnum.TIMESTAMP.getKey()) != null) {
76 DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
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);
87 * Creates a new {@link DeviceSensorValue} through the properties of a digitalSTROM
88 * {@link EventNames#DEVICE_SENSOR_VALUE} event.
90 * @param eventPropertie must not be null
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));
96 if (eventPropertie.get(EventResponseEnum.SENSOR_TYPE) != null) {
97 sensorType = SensorEnum.getSensor(Short.parseShort(eventPropertie.get(EventResponseEnum.SENSOR_TYPE)));
99 if (eventPropertie.get(EventResponseEnum.SENSOR_VALUE) != null) {
100 dsValue = Integer.parseInt(eventPropertie.get(EventResponseEnum.SENSOR_VALUE));
102 if (eventPropertie.get(EventResponseEnum.SENSOR_INDEX) != null) {
103 sensorIndex = Short.parseShort(eventPropertie.get(EventResponseEnum.SENSOR_INDEX));
105 timestamp = Date.from(Instant.ofEpochMilli(System.currentTimeMillis()));
110 * Creates a new {@link DeviceSensorValue} through the {@link SensorEnum} and the sensor index.
112 * @param sensorType must not be null
113 * @param sensorIndex must not be null
115 public DeviceSensorValue(SensorEnum sensorType, Short sensorIndex) {
116 this.sensorType = sensorType;
117 this.sensorIndex = sensorIndex;
121 * Returns the {@link Float} value of this {@link DeviceSensorValue}.
123 * @return the floatValue
125 public Float getFloatValue() {
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.
133 * @param floatValue the new float sensor value
134 * @return true, if set was successful
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());
142 this.dsValue = (int) (800 * Math.log10(floatValue));
144 timestamp = Date.from(Instant.ofEpochMilli(System.currentTimeMillis()));
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.
155 * @return the dsValue
157 public Integer getDsValue() {
162 * Sets a new internal digitalSTROM value as {@link Integer}.
164 * @param dsValue the internal digitalSTROM value to set
165 * @return true, if set was successful
167 public boolean setDsValue(Integer dsValue) {
169 this.dsValue = dsValue;
170 if (sensorType.getResolution() != 800) {
171 this.floatValue = dsValue * sensorType.getResolution();
173 this.floatValue = 10 * (dsValue / sensorType.getResolution());
175 timestamp = Date.from(Instant.ofEpochMilli(System.currentTimeMillis()));
183 * Sets a new internal digitalSTROM value as {@link Integer} and a new sensor value as {@link Float}.
185 * @param floatValue must not be null
186 * @param dSvalue must not be null
187 * @return true, if set was successful
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()));
201 * Returns the sensor type as {@link SensorEnum} of this {@link DeviceSensorValue}.
203 * @return the sensorType
205 public SensorEnum getSensorType() {
210 * Returns the sensor index to read the sensor value out though
211 * {@link DsAPI#getDeviceSensorValue(String, DSID, String, String, Short)}.
213 * @return the sensorIndex
215 public Short getSensorIndex() {
220 * Returns the timestamp of the last set value as {@link Date}.
222 * @return the timestamp
224 public Date getTimestamp() {
229 * Returns true if the sensor value is valid.
233 public boolean getValid() {
240 * @see java.lang.Object#toString()
243 public String toString() {
244 return "DeviceSensorValue [sensorType=" + sensorType + ", sensorIndex=" + sensorIndex + ", floatValue="
245 + floatValue + ", dsValue=" + dsValue + ", timestamp=" + timestamp + ", valid=" + valid + "]";
251 * @see java.lang.Object#hashCode()
254 public int hashCode() {
255 final int prime = 31;
257 result = prime * result + ((sensorType == null) ? 0 : sensorType.hashCode());
264 * @see java.lang.Object#equals(java.lang.Object)
267 public boolean equals(Object obj) {
274 if (obj.getClass() != getClass()) {
277 DeviceSensorValue other = (DeviceSensorValue) obj;
278 if (sensorType != other.sensorType) {