]> git.basschouten.com Git - openhab-addons.git/blob
d0c512854ae18dd148a1f49e7f373314af13777c
[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     @Override
87     public ZonedDateTime getUpdatedDate() {
88         return updatedDate;
89     }
90
91     @Override
92     public int compareDateWith(Log compareTo) {
93         if (compareTo == null) {
94             return -1;
95         }
96         ZonedDateTime compareToDate = compareTo.getMeasurementDate();
97         ZonedDateTime compareFromDate = this.getMeasurementDate();
98         if (compareFromDate == null) {
99             return -1;
100         } else if (compareToDate == null) {
101             return 1;
102         } else {
103             return compareFromDate.compareTo(compareToDate);
104         }
105     }
106
107     @Override
108     public boolean isNewerThan(Log hasModifiedDate) {
109         return compareDateWith(hasModifiedDate) > 0;
110     }
111
112     @Override
113     public boolean isOlderThan(Log hasModifiedDate) {
114         return compareDateWith(hasModifiedDate) < 0;
115     }
116 }