]> git.basschouten.com Git - openhab-addons.git/blob
7519355a262bcb259813737c00e8901d22f815ff
[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.climate.datatypes;
14
15 import java.text.DateFormat;
16 import java.text.ParseException;
17 import java.text.SimpleDateFormat;
18 import java.util.Date;
19
20 import org.openhab.binding.digitalstrom.internal.lib.structure.devices.deviceparameters.constants.SensorEnum;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 /**
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.
27  *
28  * @author Michael Ochel - Initial contribution
29  * @author Matthias Siegele - Initial contribution
30  */
31 public class CachedSensorValue {
32
33     private final Logger logger = LoggerFactory.getLogger(CachedSensorValue.class);
34
35     private final SensorEnum sensorType;
36     private final Float sensorValue;
37     private final String timestamp;
38
39     /**
40      * Create a new {@link CachedSensorValue}.
41      *
42      * @param sensorType must not be null
43      * @param sensorValue must not be null
44      * @param timestamp must not be null
45      */
46     public CachedSensorValue(SensorEnum sensorType, Float sensorValue, String timestamp) {
47         this.sensorType = sensorType;
48         this.sensorValue = sensorValue;
49         this.timestamp = timestamp;
50     }
51
52     /**
53      * Returns the sensor type as {@link SensorEnum}.
54      *
55      * @return the sensorType
56      */
57     public SensorEnum getSensorType() {
58         return sensorType;
59     }
60
61     /**
62      * Returns the sensor value.
63      *
64      * @return the sensorValue
65      */
66     public Float getSensorValue() {
67         return sensorValue;
68     }
69
70     /**
71      * Returns the timestamp as {@link String}.
72      *
73      * @return the timestamp
74      */
75     public String getTimestamp() {
76         return timestamp;
77     }
78
79     /**
80      * Returns the time stamp as {@link Date}.
81      *
82      * @return the timeStamp
83      */
84     public Date getTimestampAsDate() {
85         DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SS");
86         try {
87             return formatter.parse(timestamp);
88         } catch (ParseException e) {
89             logger.error("A ParseException occurred by parsing date string: {}", timestamp, e);
90         }
91         return null;
92     }
93
94     /*
95      * (non-Javadoc)
96      *
97      * @see java.lang.Object#toString()
98      */
99     @Override
100     public String toString() {
101         return "CachedSensorValue [SENSOR_TYPE=" + sensorType + ", SENSOR_VALUE=" + sensorValue + ", TIMESTAMP="
102                 + timestamp + "]";
103     }
104
105     /*
106      * (non-Javadoc)
107      * 
108      * @see java.lang.Object#hashCode()
109      */
110     @Override
111     public int hashCode() {
112         final int prime = 31;
113         int result = 1;
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());
117         return result;
118     }
119
120     /*
121      * (non-Javadoc)
122      * 
123      * @see java.lang.Object#equals(java.lang.Object)
124      */
125     @Override
126     public boolean equals(Object obj) {
127         if (this == obj) {
128             return true;
129         }
130         if (obj == null) {
131             return false;
132         }
133         if (!(obj instanceof CachedSensorValue)) {
134             return false;
135         }
136         CachedSensorValue other = (CachedSensorValue) obj;
137         if (sensorType != other.sensorType) {
138             return false;
139         }
140         if (sensorValue == null) {
141             if (other.sensorValue != null) {
142                 return false;
143             }
144         } else if (!sensorValue.equals(other.sensorValue)) {
145             return false;
146         }
147         if (timestamp == null) {
148             if (other.timestamp != null) {
149                 return false;
150             }
151         } else if (!timestamp.equals(other.timestamp)) {
152             return false;
153         }
154         return true;
155     }
156 }