]> git.basschouten.com Git - openhab-addons.git/blob
e8fd695bc24cb292e50563a18a0d5fe9b1980ee1
[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.Duration;
16 import java.time.LocalTime;
17 import java.util.List;
18
19 import org.eclipse.jdt.annotation.NonNull;
20 import org.openhab.core.library.types.OnOffType;
21 import org.openhab.core.library.types.QuantityType;
22 import org.openhab.core.library.unit.Units;
23 import org.openhab.core.types.State;
24 import org.openhab.core.types.UnDefType;
25
26 import com.google.gson.annotations.SerializedName;
27
28 /**
29  * This class represents the alarm state from the API.
30  *
31  * @author Michael Myrcik - Initial contribution
32  */
33 public class AlarmStateData {
34
35     private static final int POWER_WAKE_ENABLED = 255;
36
37     @SerializedName("prfen")
38     private List<Boolean> enabled;
39
40     @SerializedName("prfvs")
41     private List<Boolean> configured;
42
43     @SerializedName("pwrsv")
44     private List<Integer> powerWake;
45
46     public @NonNull State getEnabledState(int position) {
47         final List<Boolean> enabled = this.enabled;
48         if (enabled == null) {
49             return UnDefType.NULL;
50         }
51         return OnOffType.from(enabled.get(position - 1));
52     }
53
54     public @NonNull State getConfiguredState(int position) {
55         final List<Boolean> configured = this.configured;
56         if (configured == null) {
57             return UnDefType.NULL;
58         }
59         return OnOffType.from(configured.get(position - 1));
60     }
61
62     public @NonNull State getPowerWakeState(int position) {
63         final List<Integer> powerWake = this.powerWake;
64         if (powerWake == null) {
65             return UnDefType.NULL;
66         }
67         position -= 1;
68         position *= 3;
69         return OnOffType.from((int) powerWake.get(position) == POWER_WAKE_ENABLED);
70     }
71
72     public @NonNull State getPowerWakeDelayState(int position, LocalTime alarmTime) {
73         final State powerWakeState = getPowerWakeState(position);
74         if (UnDefType.NULL.equals(powerWakeState)) {
75             return UnDefType.NULL;
76         }
77         if (OnOffType.OFF.equals(powerWakeState)) {
78             return QuantityType.valueOf(0, Units.MINUTE);
79         }
80         if (alarmTime == null) {
81             return UnDefType.NULL;
82         }
83
84         final LocalTime powerWakeTime = getPowerWakeTime(position);
85         if (powerWakeTime == null) {
86             return UnDefType.NULL;
87         }
88
89         Duration delay = Duration.between(alarmTime, powerWakeTime);
90         if (delay == null) {
91             return UnDefType.NULL;
92         }
93
94         return QuantityType.valueOf(delay.toMinutes(), Units.MINUTE);
95     }
96
97     private LocalTime getPowerWakeTime(int position) {
98         final List<Integer> powerWake = this.powerWake;
99         if (powerWake == null) {
100             return null;
101         }
102         position--;
103         position *= 3;
104         final Integer hour = powerWake.get(position + 1);
105         final Integer minute = powerWake.get(position + 2);
106         return LocalTime.of(hour, minute);
107     }
108
109     public int getAlarmCount() {
110         final List<Boolean> configured = this.configured;
111         if (configured == null) {
112             return 0;
113         }
114         return configured.size();
115     }
116 }