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.climate.datatypes;
15 import java.text.DateFormat;
16 import java.text.ParseException;
17 import java.text.SimpleDateFormat;
18 import java.util.Date;
20 import org.openhab.binding.digitalstrom.internal.lib.structure.devices.deviceparameters.constants.SensorEnum;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
25 * The {@link CachedSensorValue} holds a read sensor value. For that the {@link CachedSensorValue} includes the sensor
26 * type, sensor value and a the timestamp.
28 * @author Michael Ochel - Initial contribution
29 * @author Matthias Siegele - Initial contribution
31 public class CachedSensorValue {
33 private final Logger logger = LoggerFactory.getLogger(CachedSensorValue.class);
35 private final SensorEnum sensorType;
36 private final Float sensorValue;
37 private final String timestamp;
40 * Create a new {@link CachedSensorValue}.
42 * @param sensorType must not be null
43 * @param sensorValue must not be null
44 * @param timestamp must not be null
46 public CachedSensorValue(SensorEnum sensorType, Float sensorValue, String timestamp) {
47 this.sensorType = sensorType;
48 this.sensorValue = sensorValue;
49 this.timestamp = timestamp;
53 * Returns the sensor type as {@link SensorEnum}.
55 * @return the sensorType
57 public SensorEnum getSensorType() {
62 * Returns the sensor value.
64 * @return the sensorValue
66 public Float getSensorValue() {
71 * Returns the timestamp as {@link String}.
73 * @return the timestamp
75 public String getTimestamp() {
80 * Returns the time stamp as {@link Date}.
82 * @return the timeStamp
84 public Date getTimestampAsDate() {
85 DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SS");
87 return formatter.parse(timestamp);
88 } catch (ParseException e) {
89 logger.error("A ParseException occurred by parsing date string: {}", timestamp, e);
97 * @see java.lang.Object#toString()
100 public String toString() {
101 return "CachedSensorValue [SENSOR_TYPE=" + sensorType + ", SENSOR_VALUE=" + sensorValue + ", TIMESTAMP="
108 * @see java.lang.Object#hashCode()
111 public int hashCode() {
112 final int prime = 31;
114 result = prime * result + ((sensorType == null) ? 0 : sensorType.hashCode());
115 result = prime * result + ((sensorValue == null) ? 0 : sensorValue.hashCode());
116 result = prime * result + ((timestamp == null) ? 0 : timestamp.hashCode());
123 * @see java.lang.Object#equals(java.lang.Object)
126 public boolean equals(Object obj) {
133 if (!(obj instanceof CachedSensorValue)) {
136 CachedSensorValue other = (CachedSensorValue) obj;
137 if (sensorType != other.sensorType) {
140 if (sensorValue == null) {
141 if (other.sensorValue != null) {
144 } else if (!sensorValue.equals(other.sensorValue)) {
147 if (timestamp == null) {
148 if (other.timestamp != null) {
151 } else if (!timestamp.equals(other.timestamp)) {