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