]> git.basschouten.com Git - openhab-addons.git/blob
fa588b04e29c8cb97755cba010a1e9684ce95d0b
[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.somneo.internal.model;
14
15 import java.time.LocalTime;
16 import java.time.ZonedDateTime;
17
18 import javax.measure.quantity.Time;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.core.library.types.DateTimeType;
23 import org.openhab.core.library.types.DecimalType;
24 import org.openhab.core.library.types.OnOffType;
25 import org.openhab.core.library.types.PercentType;
26 import org.openhab.core.library.types.QuantityType;
27 import org.openhab.core.library.types.StringType;
28 import org.openhab.core.library.unit.Units;
29 import org.openhab.core.types.State;
30 import org.openhab.core.types.UnDefType;
31
32 import com.google.gson.annotations.SerializedName;
33
34 /**
35  * This class represents the audio state from the API.
36  *
37  * @author Michael Myrcik - Initial contribution
38  */
39 @NonNullByDefault
40 public class AlarmSettingsData {
41
42     private static final int POWER_WAKE_ENABLED = 255;
43     private static final int POWER_WAKE_DISABLED = 0;
44
45     @SerializedName("prfnr")
46     private @Nullable Integer position;
47
48     @SerializedName("prfvs")
49     private @Nullable Boolean configured;
50
51     @SerializedName("prfen")
52     private @Nullable Boolean enabled;
53
54     /**
55      * None = 0,
56      * Monday = 2,
57      * Tuesday = 4,
58      * Wednesday = 8,
59      * Thursday = 16,
60      * Friday = 32,
61      * Saturday = 64,
62      * Sunday = 128
63      */
64     @SerializedName("daynm")
65     private @Nullable Integer repeatDay;
66
67     @SerializedName("almhr")
68     private @Nullable Integer hour;
69
70     @SerializedName("almmn")
71     private @Nullable Integer minute;
72
73     /**
74      * Brightness range from 0 to 25.
75      */
76     @SerializedName("curve")
77     private @Nullable Integer sunriseBrightness;
78
79     @SerializedName("durat")
80     private @Nullable Integer sunriseDurationInMin;
81
82     @SerializedName("ctype")
83     private @Nullable Integer sunriseSchema;
84
85     @SerializedName("snddv")
86     private @Nullable String soundSource;
87
88     @SerializedName("sndch")
89     private @Nullable String soundChannel;
90
91     @SerializedName("sndlv")
92     private @Nullable Integer soundVolume;
93
94     @SerializedName("pwrsz")
95     private @Nullable Integer powerWake;
96
97     @SerializedName("pszhr")
98     private @Nullable Integer powerWakeHour;
99
100     @SerializedName("pszmn")
101     private @Nullable Integer powerWakeMinute;
102
103     public void setPosition(Integer position) {
104         this.position = position;
105     }
106
107     public State getConfiguredState() {
108         final Boolean configured = this.configured;
109         if (configured == null) {
110             return UnDefType.NULL;
111         }
112         return OnOffType.from(configured);
113     }
114
115     public void setConfigured(Boolean configured) {
116         this.configured = configured;
117     }
118
119     public State getEnabledState() {
120         final Boolean enabled = this.enabled;
121         if (enabled == null) {
122             return UnDefType.NULL;
123         }
124         return OnOffType.from(enabled);
125     }
126
127     public void setEnabled(boolean enabled) {
128         this.enabled = enabled;
129     }
130
131     public void setEnabledState(OnOffType enabled) {
132         setEnabled(OnOffType.ON.equals(enabled));
133     }
134
135     public void setAlarmTime(LocalTime time) {
136         Integer powerWakeDelay = getPowerWakeDelay();
137         if (powerWakeDelay == null) {
138             powerWakeDelay = 0;
139         }
140         this.hour = time.getHour();
141         this.minute = time.getMinute();
142
143         final Integer powerWake = this.powerWake;
144         if (powerWake != null && powerWake.intValue() == POWER_WAKE_ENABLED) {
145             LocalTime powerWakeTime = time.plusMinutes(powerWakeDelay);
146             this.powerWakeHour = powerWakeTime.getHour();
147             this.powerWakeMinute = powerWakeTime.getMinute();
148         }
149     }
150
151     public void setAlarmTime(DateTimeType timeState) {
152         final ZonedDateTime zonedTime = timeState.getZonedDateTime();
153         final LocalTime time = LocalTime.of(zonedTime.getHour(), zonedTime.getMinute());
154         if (time == null) {
155             return;
156         }
157         setAlarmTime(time);
158     }
159
160     public State getRepeatDayState() {
161         final Integer repeatDay = this.repeatDay;
162         if (repeatDay == null) {
163             return UnDefType.NULL;
164         }
165         return new DecimalType(repeatDay);
166     }
167
168     public void setRepeatDay(int days) {
169         this.repeatDay = days;
170     }
171
172     public void setRepeatDayState(DecimalType days) {
173         setRepeatDay(days.intValue());
174     }
175
176     public State getAlarmTimeState() {
177         final Integer hour = this.hour;
178         if (hour == null) {
179             return UnDefType.NULL;
180         }
181         final Integer minute = this.minute;
182         if (minute == null) {
183             return UnDefType.NULL;
184         }
185         final String alarmTimeString = String.format("%02d:%02d:00", hour, minute);
186         if (alarmTimeString == null) {
187             return UnDefType.NULL;
188         }
189         return DateTimeType.valueOf(alarmTimeString);
190     }
191
192     public State getPowerWakeState() {
193         final Integer powerWake = this.powerWake;
194         if (powerWake == null) {
195             return UnDefType.NULL;
196         }
197         return OnOffType.from((int) powerWake == POWER_WAKE_ENABLED);
198     }
199
200     public void setPowerWakeState(OnOffType state) {
201         if (OnOffType.ON.equals(state)) {
202             powerWake = POWER_WAKE_ENABLED;
203             powerWakeHour = hour;
204             powerWakeMinute = minute;
205         } else {
206             powerWake = POWER_WAKE_DISABLED;
207             powerWakeHour = 0;
208             powerWakeMinute = 0;
209         }
210     }
211
212     public @Nullable Integer getPowerWakeDelay() {
213         if (OnOffType.OFF.equals(getPowerWakeState())) {
214             return Integer.valueOf(0);
215         }
216         final Integer hour = this.hour;
217         if (hour == null) {
218             return null;
219         }
220         final Integer minute = this.minute;
221         if (minute == null) {
222             return null;
223         }
224         final Integer powerWakeHour = this.powerWakeHour;
225         if (powerWakeHour == null) {
226             return null;
227         }
228         final Integer powerWakeMinute = this.powerWakeMinute;
229         if (powerWakeMinute == null) {
230             return null;
231         }
232         int delay = powerWakeMinute - minute;
233         if (!powerWakeHour.equals(hour)) {
234             // Simplify the algorithm, as the delta cannot be greater than 59 minutes.
235             delay += 60;
236         }
237         return delay;
238     }
239
240     public void setPowerWakeDelay(int minutes) {
241         final Integer hour = this.hour;
242         if (hour == null) {
243             return;
244         }
245         final Integer minute = this.minute;
246         if (minute == null) {
247             return;
248         }
249
250         LocalTime localTime = LocalTime.of(hour, minute);
251         localTime = localTime.plusMinutes(minutes);
252
253         this.powerWake = POWER_WAKE_ENABLED;
254         this.powerWakeHour = localTime.getHour();
255         this.powerWakeMinute = localTime.getMinute();
256     }
257
258     public void setPowerWakeDelayState(QuantityType<Time> time) {
259         setPowerWakeDelay(time.intValue());
260     }
261
262     public State getPowerWakeDelayState() {
263         final Integer delay = getPowerWakeDelay();
264         if (delay == null) {
265             return UnDefType.NULL;
266         }
267         return new QuantityType<>(delay, Units.MINUTE);
268     }
269
270     public State getSunriseBrightness() {
271         final Integer sunriseBrightness = this.sunriseBrightness;
272         if (sunriseBrightness == null) {
273             return UnDefType.NULL;
274         }
275         return new PercentType(sunriseBrightness * 4);
276     }
277
278     public void setSunriseBrightnessState(PercentType percent) {
279         sunriseBrightness = percent.intValue() / 4;
280     }
281
282     public State getSunriseDurationInMin() {
283         final Integer sunriseDurationInMin = this.sunriseDurationInMin;
284         if (sunriseDurationInMin == null) {
285             return UnDefType.NULL;
286         }
287         return new QuantityType<>(sunriseDurationInMin, Units.MINUTE);
288     }
289
290     public void setSunriseDurationState(QuantityType<Time> duration) {
291         sunriseDurationInMin = duration.intValue();
292     }
293
294     public State getSunriseSchema() {
295         final Integer sunriseSchema = this.sunriseSchema;
296         if (sunriseSchema == null) {
297             return UnDefType.NULL;
298         }
299         return new DecimalType(sunriseSchema);
300     }
301
302     public void setSunriseSchemaState(DecimalType schema) {
303         this.sunriseSchema = schema.intValue();
304     }
305
306     public State getSound() {
307         final String soundSource = this.soundSource;
308         if (soundSource == null) {
309             return UnDefType.NULL;
310         }
311         final String suffix = "off".equals(soundSource) ? "" : "-" + soundChannel;
312         return new StringType(soundSource + suffix);
313     }
314
315     public void setAlarmSoundState(StringType sound) {
316         final String[] values = sound.toFullString().split("-");
317         soundSource = values[0];
318         soundChannel = values.length == 1 ? "" : values[1];
319     }
320
321     public State getSoundVolume() {
322         final Integer soundVolume = this.soundVolume;
323         if (soundVolume == null) {
324             return UnDefType.NULL;
325         }
326         return new PercentType(soundVolume * 4);
327     }
328
329     public void setAlarmVolumeState(PercentType volume) {
330         soundVolume = volume.intValue() / 4;
331     }
332
333     public static AlarmSettingsData withDefaultValues(int position) {
334         final AlarmSettingsData data = new AlarmSettingsData();
335         data.position = position;
336         data.configured = false;
337         data.enabled = false;
338         data.hour = 7;
339         data.minute = 30;
340         data.powerWake = POWER_WAKE_DISABLED;
341         data.powerWakeHour = 0;
342         data.powerWakeMinute = 0;
343         data.sunriseSchema = 0;
344         data.sunriseBrightness = 20;
345         data.sunriseDurationInMin = 30;
346         data.repeatDay = 254;
347         data.soundSource = "wus";
348         data.soundChannel = "1";
349         data.soundVolume = 12;
350         return data;
351     }
352 }