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.api.dto;
15 import static org.openhab.binding.bticinosmarther.internal.SmartherBindingConstants.*;
17 import java.time.ZonedDateTime;
18 import java.time.format.DateTimeParseException;
19 import java.util.Collections;
20 import java.util.List;
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;
29 import com.google.gson.annotations.SerializedName;
32 * The {@code Chronothermostat} class defines the dto for Smarther API chronothermostat object.
34 * @author Fabio Possieri - Initial contribution
36 public class Chronothermostat {
38 private static final String TIME_FOREVER = "Forever";
40 private String function;
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;
52 private Sensor thermometer;
53 private Sensor hygrometer;
54 private boolean online;
55 private Sender sender;
58 * Returns the operational function of this chronothermostat module.
60 * @return a string containing the module operational function
62 public String getFunction() {
67 * Returns the operational mode of this chronothermostat module.
69 * @return a string containing the module operational mode
71 public String getMode() {
76 * Returns the operational setpoint temperature of this chronothermostat module.
78 * @return a {@link Measure} object representing the module operational setpoint temperature
80 public Measure getSetPointTemperature() {
81 return setPointTemperature;
85 * Returns the list of programs registered on this chronothermostat module.
87 * @return the list of registered programs, or an empty list in case of no programs available
89 public List<Program> getPrograms() {
90 return (programs != null) ? programs : Collections.emptyList();
94 * Returns the operational temperature format of this chronothermostat module.
96 * @return a string containing the module operational temperature format
98 public String getTemperatureFormat() {
99 return temperatureFormat;
103 * Returns the operational temperature format of this chronothermostat module.
105 * @return a {@link MeasureUnit} object representing the module operational temperature format
107 * @throws SmartherIllegalPropertyValueException if the measure internal raw unit cannot be mapped to any valid
110 public MeasureUnit getTemperatureFormatUnit() throws SmartherIllegalPropertyValueException {
111 return MeasureUnit.fromValue(temperatureFormat);
115 * Returns the operational load state of this chronothermostat module.
117 * @return a string containing the module operational load state
119 public String getLoadState() {
124 * Tells whether the load state of this chronothermostat module is "active" (i.e. module is turned on).
126 * @return {@code true} if the load state is active, {@code false} otherwise
128 * @throws SmartherIllegalPropertyValueException if the load state internal raw value cannot be mapped to any valid
129 * load state enum value
131 public boolean isActive() throws SmartherIllegalPropertyValueException {
132 return LoadState.fromValue(loadState).isActive();
136 * Returns the operational activation time of this chronothermostat module.
138 * @return a string containing the module operational activation time
140 public String getActivationTime() {
141 return activationTime;
145 * Returns a label for the operational activation time of this chronothermostat module.
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
150 public @Nullable String getActivationTimeLabel(TimeZoneProvider timeZoneProvider) {
151 String timeLabel = TIME_FOREVER;
152 if (activationTime != null) {
154 boolean dateActivationTimeIsZoned = activationTime.length() > 19;
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());
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);
166 timeLabel = DateUtil.format(dateActivationTime, DTF_DAY_HHMM);
168 } catch (DateTimeParseException e) {
176 * Returns the current time (clock) of this chronothermostat module.
178 * @return a string containing the module current time
180 public String getTime() {
185 * Returns the thermometer sensor of this chronothermostat module.
187 * @return the thermometer sensor of this module
189 public Sensor getThermometer() {
194 * Returns the hygrometer sensor of this chronothermostat module.
196 * @return the hygrometer sensor of this module
198 public Sensor getHygrometer() {
203 * Tells whether this module is online.
205 * @return {@code true} if the module is online, {@code false} otherwise
207 public boolean isOnline() {
212 * Returns the sender associated with this chronothermostat module.
214 * @return a {@link Sender} object representing the sender associated with this module, or {@code null} in case of
215 * no sender information available
217 public @Nullable Sender getSender() {
222 * Returns the operational program of this chronothermostat module.
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
227 public @Nullable Program getProgram() {
228 return (programs != null && !programs.isEmpty()) ? programs.get(0) : null;
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);