]> git.basschouten.com Git - openhab-addons.git/blob
6520ea31c7b6849f928f0121c7bf3855268c5238
[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.bticinosmarther.internal.api.dto;
14
15 import static org.openhab.binding.bticinosmarther.internal.SmartherBindingConstants.*;
16
17 import java.time.ZonedDateTime;
18 import java.time.format.DateTimeParseException;
19 import java.util.Collections;
20 import java.util.List;
21
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.openhab.binding.bticinosmarther.internal.api.dto.Enums.LoadState;
24 import org.openhab.binding.bticinosmarther.internal.api.dto.Enums.MeasureUnit;
25 import org.openhab.binding.bticinosmarther.internal.api.exception.SmartherIllegalPropertyValueException;
26 import org.openhab.binding.bticinosmarther.internal.util.DateUtil;
27
28 import com.google.gson.annotations.SerializedName;
29
30 /**
31  * The {@code Chronothermostat} class defines the dto for Smarther API chronothermostat object.
32  *
33  * @author Fabio Possieri - Initial contribution
34  */
35 public class Chronothermostat {
36
37     private static final String TIME_FOREVER = "Forever";
38
39     private String function;
40     private String mode;
41     @SerializedName("setPoint")
42     private Measure setPointTemperature;
43     private List<Program> programs;
44     @SerializedName("temperatureFormat")
45     private String temperatureFormat;
46     @SerializedName("loadState")
47     private String loadState;
48     @SerializedName("activationTime")
49     private String activationTime;
50     private String time;
51     private Sensor thermometer;
52     private Sensor hygrometer;
53     private boolean online;
54     private Sender sender;
55
56     /**
57      * Returns the operational function of this chronothermostat module.
58      *
59      * @return a string containing the module operational function
60      */
61     public String getFunction() {
62         return function;
63     }
64
65     /**
66      * Returns the operational mode of this chronothermostat module.
67      *
68      * @return a string containing the module operational mode
69      */
70     public String getMode() {
71         return mode;
72     }
73
74     /**
75      * Returns the operational setpoint temperature of this chronothermostat module.
76      *
77      * @return a {@link Measure} object representing the module operational setpoint temperature
78      */
79     public Measure getSetPointTemperature() {
80         return setPointTemperature;
81     }
82
83     /**
84      * Returns the list of programs registered on this chronothermostat module.
85      *
86      * @return the list of registered programs, or an empty list in case of no programs available
87      */
88     public List<Program> getPrograms() {
89         return (programs != null) ? programs : Collections.emptyList();
90     }
91
92     /**
93      * Returns the operational temperature format of this chronothermostat module.
94      *
95      * @return a string containing the module operational temperature format
96      */
97     public String getTemperatureFormat() {
98         return temperatureFormat;
99     }
100
101     /**
102      * Returns the operational temperature format of this chronothermostat module.
103      *
104      * @return a {@link MeasureUnit} object representing the module operational temperature format
105      *
106      * @throws {@link SmartherIllegalPropertyValueException}
107      *             if the measure internal raw unit cannot be mapped to any valid measure unit
108      */
109     public MeasureUnit getTemperatureFormatUnit() throws SmartherIllegalPropertyValueException {
110         return MeasureUnit.fromValue(temperatureFormat);
111     }
112
113     /**
114      * Returns the operational load state of this chronothermostat module.
115      *
116      * @return a string containing the module operational load state
117      */
118     public String getLoadState() {
119         return loadState;
120     }
121
122     /**
123      * Tells whether the load state of this chronothermostat module is "active" (i.e. module is turned on).
124      *
125      * @return {@code true} if the load state is active, {@code false} otherwise
126      *
127      * @throws {@link SmartherIllegalPropertyValueException}
128      *             if the load state internal raw value cannot be mapped to any valid load state enum value
129      */
130     public boolean isActive() throws SmartherIllegalPropertyValueException {
131         return LoadState.fromValue(loadState).isActive();
132     }
133
134     /**
135      * Returns the operational activation time of this chronothermostat module.
136      *
137      * @return a string containing the module operational activation time
138      */
139     public String getActivationTime() {
140         return activationTime;
141     }
142
143     /**
144      * Returns a label for the operational activation time of this chronothermostat module.
145      *
146      * @return a string containing the module operational activation time label, or {@code null} if the activation time
147      *         cannot be parsed to a valid date/time
148      */
149     public @Nullable String getActivationTimeLabel() {
150         String timeLabel = TIME_FOREVER;
151         if (activationTime != null) {
152             try {
153                 final ZonedDateTime dateActivationTime = DateUtil.parseZonedTime(activationTime, DTF_DATETIME_EXT);
154                 final ZonedDateTime dateTomorrow = DateUtil.getZonedStartOfDay(1, dateActivationTime.getZone());
155
156                 if (dateActivationTime.isBefore(dateTomorrow)) {
157                     timeLabel = DateUtil.format(dateActivationTime, DTF_TODAY);
158                 } else if (dateActivationTime.isBefore(dateTomorrow.plusDays(1))) {
159                     timeLabel = DateUtil.format(dateActivationTime, DTF_TOMORROW);
160                 } else {
161                     timeLabel = DateUtil.format(dateActivationTime, DTF_DAY_HHMM);
162                 }
163             } catch (DateTimeParseException e) {
164                 timeLabel = null;
165             }
166         }
167         return timeLabel;
168     }
169
170     /**
171      * Returns the current time (clock) of this chronothermostat module.
172      *
173      * @return a string containing the module current time
174      */
175     public String getTime() {
176         return time;
177     }
178
179     /**
180      * Returns the thermometer sensor of this chronothermostat module.
181      *
182      * @return the thermometer sensor of this module
183      */
184     public Sensor getThermometer() {
185         return thermometer;
186     }
187
188     /**
189      * Returns the hygrometer sensor of this chronothermostat module.
190      *
191      * @return the hygrometer sensor of this module
192      */
193     public Sensor getHygrometer() {
194         return hygrometer;
195     }
196
197     /**
198      * Tells whether this module is online.
199      *
200      * @return {@code true} if the module is online, {@code false} otherwise
201      */
202     public boolean isOnline() {
203         return online;
204     }
205
206     /**
207      * Returns the sender associated with this chronothermostat module.
208      *
209      * @return a {@link Sender} object representing the sender associated with this module, or {@code null} in case of
210      *         no sender information available
211      */
212     public @Nullable Sender getSender() {
213         return sender;
214     }
215
216     /**
217      * Returns the operational program of this chronothermostat module.
218      *
219      * @return a {@link Program} object representing the module operational program, or {@code null} in case of no
220      *         program currently set for this module
221      */
222     public @Nullable Program getProgram() {
223         return (programs != null && !programs.isEmpty()) ? programs.get(0) : null;
224     }
225
226     @Override
227     public String toString() {
228         return String.format(
229                 "function=%s, mode=%s, setPointTemperature=[%s], programs=%s, temperatureFormat=%s, loadState=%s, time=%s, activationTime=%s, thermometer=[%s], hygrometer=[%s], online=%s, sender=[%s]",
230                 function, mode, setPointTemperature, programs, temperatureFormat, loadState, time, activationTime,
231                 thermometer, hygrometer, online, sender);
232     }
233 }