]> git.basschouten.com Git - openhab-addons.git/blob
9fe98faaa27487f03f6a03c2b84f2254debb0cdc
[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.model;
14
15 import static org.openhab.binding.bticinosmarther.internal.SmartherBindingConstants.*;
16
17 import java.time.LocalDate;
18 import java.time.LocalDateTime;
19 import java.time.temporal.ChronoUnit;
20
21 import javax.measure.Unit;
22 import javax.measure.quantity.Temperature;
23
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.eclipse.jdt.annotation.Nullable;
26 import org.openhab.binding.bticinosmarther.internal.api.dto.Chronothermostat;
27 import org.openhab.binding.bticinosmarther.internal.api.dto.Enums.BoostTime;
28 import org.openhab.binding.bticinosmarther.internal.api.dto.Enums.Function;
29 import org.openhab.binding.bticinosmarther.internal.api.dto.Enums.Mode;
30 import org.openhab.binding.bticinosmarther.internal.api.exception.SmartherIllegalPropertyValueException;
31 import org.openhab.binding.bticinosmarther.internal.util.DateUtil;
32 import org.openhab.binding.bticinosmarther.internal.util.StringUtil;
33 import org.openhab.core.library.types.QuantityType;
34 import org.openhab.core.library.unit.SIUnits;
35
36 /**
37  * The {@code ModuleSettings} class defines the operational settings of a Smarther Chronothermostat.
38  *
39  * @author Fabio Possieri - Initial contribution
40  */
41 @NonNullByDefault
42 public class ModuleSettings {
43
44     private transient String plantId;
45     private transient String moduleId;
46     private Function function;
47     private Mode mode;
48     private QuantityType<Temperature> setPointTemperature;
49     private int program;
50     private BoostTime boostTime;
51     private @Nullable String endDate;
52     private int endHour;
53     private int endMinute;
54
55     /**
56      * Constructs a {@code ModuleSettings} with the specified plant and module identifiers.
57      *
58      * @param plantId
59      *            the identifier of the plant
60      * @param moduleId
61      *            the identifier of the chronothermostat module inside the plant
62      */
63     public ModuleSettings(String plantId, String moduleId) {
64         this.plantId = plantId;
65         this.moduleId = moduleId;
66         this.function = Function.HEATING;
67         this.mode = Mode.AUTOMATIC;
68         this.setPointTemperature = QuantityType.valueOf(7.0, SIUnits.CELSIUS);
69         this.program = 0;
70         this.boostTime = BoostTime.MINUTES_30;
71         this.endDate = null;
72         this.endHour = 0;
73         this.endMinute = 0;
74     }
75
76     /**
77      * Updates this module settings from a {@link Chronothermostat} dto object.
78      *
79      * @param chronothermostat
80      *            the chronothermostat dto to get data from
81      * 
82      * @throws {@link SmartherIllegalPropertyValueException}
83      *             if at least one of the module properties cannot be mapped to any valid enum value
84      */
85     public void updateFromChronothermostat(Chronothermostat chronothermostat)
86             throws SmartherIllegalPropertyValueException {
87         this.function = Function.fromValue(chronothermostat.getFunction());
88     }
89
90     /**
91      * Returns the plant identifier.
92      *
93      * @return a string containing the plant identifier.
94      */
95     public String getPlantId() {
96         return plantId;
97     }
98
99     /**
100      * Returns the module identifier.
101      *
102      * @return a string containing the module identifier.
103      */
104     public String getModuleId() {
105         return moduleId;
106     }
107
108     /**
109      * Returns the module operational function.
110      *
111      * @return a {@link Function} enum representing the module operational function
112      */
113     public Function getFunction() {
114         return function;
115     }
116
117     /**
118      * Returns the module operational mode.
119      *
120      * @return a {@link Mode} enum representing the module operational mode
121      */
122     public Mode getMode() {
123         return mode;
124     }
125
126     /**
127      * Sets the module operational mode.
128      *
129      * @param mode
130      *            a {@link Mode} enum representing the module operational mode to set
131      */
132     public void setMode(Mode mode) {
133         this.mode = mode;
134     }
135
136     /**
137      * Returns the module operational setpoint temperature for "manual" mode.
138      *
139      * @return a {@link QuantityType<Temperature>} object representing the module operational setpoint temperature
140      */
141     public QuantityType<Temperature> getSetPointTemperature() {
142         return setPointTemperature;
143     }
144
145     /**
146      * Returns the module operational setpoint temperature for "manual" mode, using a target unit.
147      *
148      * @param targetUnit
149      *            the {@link Unit} unit to convert the setpoint temperature to
150      *
151      * @return a {@link QuantityType<Temperature>} object representing the module operational setpoint temperature
152      */
153     public @Nullable QuantityType<Temperature> getSetPointTemperature(Unit<?> targetUnit) {
154         return setPointTemperature.toUnit(targetUnit);
155     }
156
157     /**
158      * Sets the module operational setpoint temperature for "manual" mode.
159      *
160      * @param setPointTemperature
161      *            a {@link QuantityType<Temperature>} object representing the setpoint temperature to set
162      */
163     public void setSetPointTemperature(QuantityType<Temperature> setPointTemperature) {
164         this.setPointTemperature = setPointTemperature;
165     }
166
167     /**
168      * Returns the module operational program for "automatic" mode.
169      *
170      * @return the module operational program for automatic mode
171      */
172     public int getProgram() {
173         return program;
174     }
175
176     /**
177      * Sets the module operational program for "automatic" mode.
178      *
179      * @param program
180      *            the module operational program to set
181      */
182     public void setProgram(int program) {
183         this.program = program;
184     }
185
186     /**
187      * Returns the module operational boost time for "boost" mode.
188      *
189      * @return a {@link BoostTime} enum representing the module operational boost time
190      */
191     public BoostTime getBoostTime() {
192         return boostTime;
193     }
194
195     /**
196      * Sets the module operational boost time for "boost" mode.
197      *
198      * @param boostTime
199      *            a {@link BoostTime} enum representing the module operational boost time to set
200      */
201     public void setBoostTime(BoostTime boostTime) {
202         this.boostTime = boostTime;
203     }
204
205     /**
206      * Returns the module operational end date for "manual" mode.
207      *
208      * @return a string containing the module operational end date, may be {@code null}
209      */
210     public @Nullable String getEndDate() {
211         return endDate;
212     }
213
214     /**
215      * Tells whether the module operational end date for "manual" mode has expired.
216      *
217      * @return {@code true} if the end date has expired, {@code false} otherwise
218      */
219     public boolean isEndDateExpired() {
220         if (endDate != null) {
221             final LocalDateTime dtEndDate = DateUtil.parseDate(endDate, DTF_DATE).atStartOfDay();
222             final LocalDateTime dtToday = LocalDate.now().atStartOfDay();
223
224             return (dtEndDate.isBefore(dtToday));
225         } else {
226             return false;
227         }
228     }
229
230     /**
231      * Refreshes the module operational end date for "manual" mode, setting it to current local date.
232      */
233     public void refreshEndDate() {
234         if (endDate != null) {
235             this.endDate = DateUtil.format(LocalDateTime.now(), DTF_DATE);
236         }
237     }
238
239     /**
240      * Sets the module operational end date for "manual" mode.
241      *
242      * @param endDate
243      *            the module operational end date to set
244      */
245     public void setEndDate(String endDate) {
246         this.endDate = StringUtil.stripToNull(endDate);
247     }
248
249     /**
250      * Returns the module operational end hour for "manual" mode.
251      *
252      * @return the module operational end hour
253      */
254     public int getEndHour() {
255         return endHour;
256     }
257
258     /**
259      * Sets the module operational end hour for "manual" mode.
260      *
261      * @param endHour
262      *            the module operational end hour to set
263      */
264     public void setEndHour(int endHour) {
265         this.endHour = endHour;
266     }
267
268     /**
269      * Returns the module operational end minute for "manual" mode.
270      *
271      * @return the module operational end minute
272      */
273     public int getEndMinute() {
274         return endMinute;
275     }
276
277     /**
278      * Sets the module operational end minute for "manual" mode.
279      *
280      * @param endMinute
281      *            the module operational end minute to set
282      */
283     public void setEndMinute(int endMinute) {
284         this.endMinute = endMinute;
285     }
286
287     /**
288      * Returns the date and time (format YYYY-MM-DDThh:mm:ss) to which this module settings will be maintained.
289      * For boost mode a range is returned, as duration is limited to 30, 60 or 90 minutes, indicating starting (current)
290      * and final date and time.
291      *
292      * @return a string containing the module settings activation time, or and empty ("") string if the module operation
293      *         mode doesn't allow for an activation time
294      */
295     public String getActivationTime() {
296         if (mode.equals(Mode.MANUAL) && (endDate != null)) {
297             LocalDateTime d = DateUtil.parseDate(endDate, DTF_DATE).atTime(endHour, endMinute);
298             return DateUtil.format(d, DTF_DATETIME);
299         } else if (mode.equals(Mode.BOOST)) {
300             LocalDateTime d1 = LocalDateTime.now().truncatedTo(ChronoUnit.MINUTES);
301             LocalDateTime d2 = d1.plusMinutes(boostTime.getValue());
302             return DateUtil.formatRange(d1, d2, DTF_DATETIME);
303         } else {
304             return "";
305         }
306     }
307
308     @Override
309     public String toString() {
310         return String.format(
311                 "plantId=%s, moduleId=%s, mode=%s, setPointTemperature=%s, program=%s, boostTime=%s, endDate=%s, endHour=%s, endMinute=%s",
312                 plantId, moduleId, mode, setPointTemperature, program, boostTime, endDate, endHour, endMinute);
313     }
314 }