]> git.basschouten.com Git - openhab-addons.git/blob
cad7b95a4ecaa9b693a429e56c23a0d6087b42da
[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.plugwiseha.internal.api.model.dto;
14
15 import java.time.ZonedDateTime;
16 import java.util.Optional;
17
18 import com.thoughtworks.xstream.annotations.XStreamAlias;
19
20 /**
21  * @author B. van Wetten - Initial contribution
22  * @author Leo Siepel - finish initial contribution
23  */
24 @XStreamAlias("point_log")
25 public class Log extends PlugwiseBaseModel implements PlugwiseComparableDate<Log> {
26
27     private String type;
28
29     private String unit;
30
31     private String measurement;
32
33     @XStreamAlias("measurement_date")
34     private ZonedDateTime measurementDate;
35
36     @XStreamAlias("updated_date")
37     private ZonedDateTime updatedDate;
38
39     public String getType() {
40         return type;
41     }
42
43     public String getUnit() {
44         return unit;
45     }
46
47     public Optional<String> getMeasurement() {
48         return Optional.ofNullable(measurement);
49     }
50
51     public Optional<Boolean> getMeasurementAsBoolean() {
52         if (measurement != null) {
53             switch (measurement.toLowerCase()) {
54                 case "on":
55                     return Optional.of(true);
56                 case "off":
57                     return Optional.of(false);
58                 default:
59                     return Optional.empty();
60             }
61         } else {
62             return Optional.empty();
63         }
64     }
65
66     public Optional<Double> getMeasurementAsDouble() {
67         try {
68             if (measurement != null) {
69                 return Optional.of(Double.parseDouble(measurement));
70             } else {
71                 return Optional.empty();
72             }
73         } catch (NumberFormatException e) {
74             return Optional.empty();
75         }
76     }
77
78     public Optional<String> getMeasurementUnit() {
79         return Optional.ofNullable(unit);
80     }
81
82     public ZonedDateTime getMeasurementDate() {
83         return measurementDate;
84     }
85
86     public ZonedDateTime getUpdatedDate() {
87         return updatedDate;
88     }
89
90     @Override
91     public int compareDateWith(Log compareTo) {
92         if (compareTo == null) {
93             return -1;
94         }
95         ZonedDateTime compareToDate = compareTo.getMeasurementDate();
96         ZonedDateTime compareFromDate = this.getMeasurementDate();
97         if (compareFromDate == null) {
98             return -1;
99         } else if (compareToDate == null) {
100             return 1;
101         } else {
102             return compareFromDate.compareTo(compareToDate);
103         }
104     }
105
106     @Override
107     public boolean isNewerThan(Log hasModifiedDate) {
108         return compareDateWith(hasModifiedDate) > 0;
109     }
110
111     @Override
112     public boolean isOlderThan(Log hasModifiedDate) {
113         return compareDateWith(hasModifiedDate) < 0;
114     }
115 }