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.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 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
34 * {@link org.openhab.binding.digitalstrom.internal.lib.serverconnection.DsAPI#getDeviceSensorValue(String, DSID, String, String, Short)}
35 * and as well as of course the value and timestamp of the last sensor update.
37 * @author Michael Ochel - Initial contribution
38 * @author Matthias Siegele - Initial contribution
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 org.openhab.binding.digitalstrom.internal.lib.event.constants.EventNames#DEVICE_SENSOR_VALUE} event.
90 * @param eventProperties must not be null
92 public DeviceSensorValue(Map<EventResponseEnum, String> eventProperties) {
93 String strVal = eventProperties.get(EventResponseEnum.SENSOR_VALUE_FLOAT);
95 floatValue = Float.parseFloat(strVal);
97 strVal = eventProperties.get(EventResponseEnum.SENSOR_TYPE);
99 sensorType = SensorEnum.getSensor(Short.parseShort(strVal));
101 strVal = eventProperties.get(EventResponseEnum.SENSOR_VALUE);
102 if (strVal != null) {
103 dsValue = Integer.parseInt(strVal);
105 strVal = eventProperties.get(EventResponseEnum.SENSOR_INDEX);
106 if (strVal != null) {
107 sensorIndex = Short.parseShort(strVal);
109 timestamp = Date.from(Instant.ofEpochMilli(System.currentTimeMillis()));
114 * Creates a new {@link DeviceSensorValue} through the {@link SensorEnum} and the sensor index.
116 * @param sensorType must not be null
117 * @param sensorIndex must not be null
119 public DeviceSensorValue(SensorEnum sensorType, Short sensorIndex) {
120 this.sensorType = sensorType;
121 this.sensorIndex = sensorIndex;
125 * Returns the {@link Float} value of this {@link DeviceSensorValue}.
127 * @return the floatValue
129 public Float getFloatValue() {
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.
137 * @param floatValue the new float sensor value
138 * @return true, if set was successful
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());
146 this.dsValue = (int) (800 * Math.log10(floatValue));
148 timestamp = Date.from(Instant.ofEpochMilli(System.currentTimeMillis()));
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.
159 * @return the dsValue
161 public Integer getDsValue() {
166 * Sets a new internal digitalSTROM value as {@link Integer}.
168 * @param dsValue the internal digitalSTROM value to set
169 * @return true, if set was successful
171 public boolean setDsValue(Integer dsValue) {
173 this.dsValue = dsValue;
174 if (sensorType.getResolution() != 800) {
175 this.floatValue = dsValue * sensorType.getResolution();
177 this.floatValue = 10 * (dsValue / sensorType.getResolution());
179 timestamp = Date.from(Instant.ofEpochMilli(System.currentTimeMillis()));
187 * Sets a new internal digitalSTROM value as {@link Integer} and a new sensor value as {@link Float}.
189 * @param floatValue must not be null
190 * @param dSvalue must not be null
191 * @return true, if set was successful
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()));
205 * Returns the sensor type as {@link SensorEnum} of this {@link DeviceSensorValue}.
207 * @return the sensorType
209 public SensorEnum getSensorType() {
214 * Returns the sensor index to read the sensor value out though
215 * {@link org.openhab.binding.digitalstrom.internal.lib.serverconnection.DsAPI#getDeviceSensorValue(String, DSID, String, String, Short)}.
217 * @return the sensorIndex
219 public Short getSensorIndex() {
224 * Returns the timestamp of the last set value as {@link Date}.
226 * @return the timestamp
228 public Date getTimestamp() {
233 * Returns true if the sensor value is valid.
237 public boolean getValid() {
244 * @see java.lang.Object#toString()
247 public String toString() {
248 return "DeviceSensorValue [sensorType=" + sensorType + ", sensorIndex=" + sensorIndex + ", floatValue="
249 + floatValue + ", dsValue=" + dsValue + ", timestamp=" + timestamp + ", valid=" + valid + "]";
255 * @see java.lang.Object#hashCode()
258 public int hashCode() {
259 final int prime = 31;
261 result = prime * result + ((sensorType == null) ? 0 : sensorType.hashCode());
268 * @see java.lang.Object#equals(java.lang.Object)
270 @SuppressWarnings("PMD.SimplifyBooleanReturns")
272 public boolean equals(Object obj) {
279 if (obj.getClass() != getClass()) {
282 DeviceSensorValue other = (DeviceSensorValue) obj;
283 if (sensorType != other.sensorType) {