2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.bticinosmarther.internal.model;
15 import static org.openhab.binding.bticinosmarther.internal.SmartherBindingConstants.*;
17 import java.time.LocalDate;
18 import java.time.LocalDateTime;
19 import java.time.temporal.ChronoUnit;
21 import javax.measure.Unit;
22 import javax.measure.quantity.Temperature;
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;
37 * The {@code ModuleSettings} class defines the operational settings of a Smarther Chronothermostat.
39 * @author Fabio Possieri - Initial contribution
42 public class ModuleSettings {
44 private transient String plantId;
45 private transient String moduleId;
46 private Function function;
48 private QuantityType<Temperature> setPointTemperature;
50 private BoostTime boostTime;
51 private @Nullable String endDate;
53 private int endMinute;
56 * Constructs a {@code ModuleSettings} with the specified plant and module identifiers.
59 * the identifier of the plant
61 * the identifier of the chronothermostat module inside the plant
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);
70 this.boostTime = BoostTime.MINUTES_30;
77 * Updates this module settings from a {@link Chronothermostat} dto object.
79 * @param chronothermostat
80 * the chronothermostat dto to get data from
82 * @throws {@link SmartherIllegalPropertyValueException}
83 * if at least one of the module properties cannot be mapped to any valid enum value
85 public void updateFromChronothermostat(Chronothermostat chronothermostat)
86 throws SmartherIllegalPropertyValueException {
87 this.function = Function.fromValue(chronothermostat.getFunction());
91 * Returns the plant identifier.
93 * @return a string containing the plant identifier.
95 public String getPlantId() {
100 * Returns the module identifier.
102 * @return a string containing the module identifier.
104 public String getModuleId() {
109 * Returns the module operational function.
111 * @return a {@link Function} enum representing the module operational function
113 public Function getFunction() {
118 * Returns the module operational mode.
120 * @return a {@link Mode} enum representing the module operational mode
122 public Mode getMode() {
127 * Sets the module operational mode.
130 * a {@link Mode} enum representing the module operational mode to set
132 public void setMode(Mode mode) {
137 * Returns the module operational setpoint temperature for "manual" mode.
139 * @return a {@link QuantityType<Temperature>} object representing the module operational setpoint temperature
141 public QuantityType<Temperature> getSetPointTemperature() {
142 return setPointTemperature;
146 * Returns the module operational setpoint temperature for "manual" mode, using a target unit.
149 * the {@link Unit} unit to convert the setpoint temperature to
151 * @return a {@link QuantityType<Temperature>} object representing the module operational setpoint temperature
153 public @Nullable QuantityType<Temperature> getSetPointTemperature(Unit<?> targetUnit) {
154 return setPointTemperature.toUnit(targetUnit);
158 * Sets the module operational setpoint temperature for "manual" mode.
160 * @param setPointTemperature
161 * a {@link QuantityType<Temperature>} object representing the setpoint temperature to set
163 public void setSetPointTemperature(QuantityType<Temperature> setPointTemperature) {
164 this.setPointTemperature = setPointTemperature;
168 * Returns the module operational program for "automatic" mode.
170 * @return the module operational program for automatic mode
172 public int getProgram() {
177 * Sets the module operational program for "automatic" mode.
180 * the module operational program to set
182 public void setProgram(int program) {
183 this.program = program;
187 * Returns the module operational boost time for "boost" mode.
189 * @return a {@link BoostTime} enum representing the module operational boost time
191 public BoostTime getBoostTime() {
196 * Sets the module operational boost time for "boost" mode.
199 * a {@link BoostTime} enum representing the module operational boost time to set
201 public void setBoostTime(BoostTime boostTime) {
202 this.boostTime = boostTime;
206 * Returns the module operational end date for "manual" mode.
208 * @return a string containing the module operational end date, may be {@code null}
210 public @Nullable String getEndDate() {
215 * Tells whether the module operational end date for "manual" mode has expired.
217 * @return {@code true} if the end date has expired, {@code false} otherwise
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();
224 return (dtEndDate.isBefore(dtToday));
231 * Refreshes the module operational end date for "manual" mode, setting it to current local date.
233 public void refreshEndDate() {
234 if (endDate != null) {
235 this.endDate = DateUtil.format(LocalDateTime.now(), DTF_DATE);
240 * Sets the module operational end date for "manual" mode.
243 * the module operational end date to set
245 public void setEndDate(String endDate) {
246 this.endDate = StringUtil.stripToNull(endDate);
250 * Returns the module operational end hour for "manual" mode.
252 * @return the module operational end hour
254 public int getEndHour() {
259 * Sets the module operational end hour for "manual" mode.
262 * the module operational end hour to set
264 public void setEndHour(int endHour) {
265 this.endHour = endHour;
269 * Returns the module operational end minute for "manual" mode.
271 * @return the module operational end minute
273 public int getEndMinute() {
278 * Sets the module operational end minute for "manual" mode.
281 * the module operational end minute to set
283 public void setEndMinute(int endMinute) {
284 this.endMinute = endMinute;
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.
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
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);
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);