]> git.basschouten.com Git - openhab-addons.git/blob
6939da6f2461e0439fc764371956ee5a9a8f4d85
[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.api.dto;
14
15 import javax.measure.Unit;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.openhab.binding.bticinosmarther.internal.api.exception.SmartherIllegalPropertyValueException;
19 import org.openhab.core.library.unit.ImperialUnits;
20 import org.openhab.core.library.unit.SIUnits;
21 import org.openhab.core.library.unit.Units;
22
23 /**
24  * The {@code Enums} class represents a container for enums related to Smarther API.
25  *
26  * @author Fabio Possieri - Initial contribution
27  */
28 @NonNullByDefault
29 public class Enums {
30
31     /**
32      * The {@code Function} enum maps the values of chronothermostat operation function.
33      */
34     public enum Function implements TypeWithStringProperty {
35         HEATING("HEATING"),
36         COOLING("COOLING");
37
38         private final String value;
39
40         Function(String value) {
41             this.value = value;
42         }
43
44         @Override
45         public String getValue() {
46             return value;
47         }
48
49         /**
50          * Returns a {@code Function} enum value from the given raw value.
51          *
52          * @param value
53          *            the raw value to get an enum value from
54          *
55          * @return the enum value representing the given raw value
56          *
57          * @throws {@link SmartherIllegalPropertyValueException}
58          *             if the raw value cannot be mapped to any valid enum value
59          */
60         public static Function fromValue(String value) throws SmartherIllegalPropertyValueException {
61             return lookup(Function.class, value);
62         }
63     }
64
65     /**
66      * The {@code Mode} enum maps the values of chronothermostat operation mode.
67      */
68     public enum Mode implements TypeWithStringProperty {
69         AUTOMATIC("AUTOMATIC"),
70         MANUAL("MANUAL"),
71         BOOST("BOOST"),
72         OFF("OFF"),
73         PROTECTION("PROTECTION");
74
75         private final String value;
76
77         Mode(String value) {
78             this.value = value;
79         }
80
81         @Override
82         public String getValue() {
83             return value;
84         }
85
86         /**
87          * Returns a {@code Mode} enum value from the given raw value.
88          *
89          * @param value
90          *            the raw value to get an enum value from
91          *
92          * @return the enum value representing the given raw value
93          *
94          * @throws {@link SmartherIllegalPropertyValueException}
95          *             if the raw value cannot be mapped to any valid enum value
96          */
97         public static Mode fromValue(String value) throws SmartherIllegalPropertyValueException {
98             return lookup(Mode.class, value);
99         }
100     }
101
102     /**
103      * The {@code LoadState} enum maps the values of chronothermostat operation load state.
104      */
105     public enum LoadState implements TypeWithStringProperty {
106         ACTIVE("ACTIVE"),
107         INACTIVE("INACTIVE");
108
109         private final String value;
110
111         LoadState(String value) {
112             this.value = value;
113         }
114
115         @Override
116         public String getValue() {
117             return value;
118         }
119
120         /**
121          * Tells whether the load state value is "active".
122          *
123          * @return {@code true} if the load state value is "active", {@code false} otherwise
124          */
125         public boolean isActive() {
126             return ACTIVE.getValue().equals(value);
127         }
128
129         /**
130          * Returns a {@code LoadState} enum value from the given raw value.
131          *
132          * @param value
133          *            the raw value to get an enum value from
134          *
135          * @return the enum value representing the given raw value
136          *
137          * @throws {@link SmartherIllegalPropertyValueException}
138          *             if the raw value cannot be mapped to any valid enum value
139          */
140         public static LoadState fromValue(String value) throws SmartherIllegalPropertyValueException {
141             return lookup(LoadState.class, value);
142         }
143     }
144
145     /**
146      * The {@code MeasureUnit} enum maps the values of managed measure unit.
147      */
148     public enum MeasureUnit implements TypeWithStringProperty {
149         CELSIUS("C"),
150         FAHRENHEIT("F"),
151         PERCENTAGE("%"),
152         DIMENSIONLESS("");
153
154         private final String value;
155
156         MeasureUnit(String value) {
157             this.value = value;
158         }
159
160         @Override
161         public String getValue() {
162             return value;
163         }
164
165         /**
166          * Returns a {@code MeasureUnit} enum value for the given measure {@link Unit}.
167          *
168          * @param unit
169          *            the measure unit to get an enum value for
170          *
171          * @return the enum value representing the given measure unit
172          */
173         public static MeasureUnit fromUnit(Unit<?> unit) {
174             if (SIUnits.CELSIUS.equals(unit)) {
175                 return CELSIUS;
176             } else if (ImperialUnits.FAHRENHEIT.equals(unit)) {
177                 return FAHRENHEIT;
178             } else if (Units.PERCENT.equals(unit)) {
179                 return PERCENTAGE;
180             } else {
181                 return DIMENSIONLESS;
182             }
183         }
184
185         /**
186          * Returns a {@code MeasureUnit} enum value from the given raw value.
187          *
188          * @param value
189          *            the raw value to get an enum value from
190          *
191          * @return the enum value representing the given raw value
192          *
193          * @throws {@link SmartherIllegalPropertyValueException}
194          *             if the raw value cannot be mapped to any valid enum value
195          */
196         public static MeasureUnit fromValue(String value) throws SmartherIllegalPropertyValueException {
197             return lookup(MeasureUnit.class, value);
198         }
199     }
200
201     /**
202      * The {@code BoostTime} enum maps the time values of chronothermostat boost mode.
203      */
204     public enum BoostTime implements TypeWithIntProperty {
205         MINUTES_30(30),
206         MINUTES_60(60),
207         MINUTES_90(90);
208
209         private final int value;
210
211         BoostTime(int value) {
212             this.value = value;
213         }
214
215         @Override
216         public int getValue() {
217             return value;
218         }
219
220         /**
221          * Returns a {@code BoostTime} enum value from the given raw value.
222          *
223          * @param value
224          *            the raw value to get an enum value from
225          *
226          * @return the enum value representing the given raw value
227          *
228          * @throws {@link SmartherIllegalPropertyValueException}
229          *             if the raw value cannot be mapped to any valid enum value
230          */
231         public static BoostTime fromValue(int value) throws SmartherIllegalPropertyValueException {
232             return lookup(BoostTime.class, value);
233         }
234     }
235
236     // ------------------------------
237     // UTILITY INTERFACES AND METHODS
238     // ------------------------------
239
240     interface TypeWithIntProperty {
241         int getValue();
242     }
243
244     public static <E extends Enum<E> & TypeWithIntProperty> E lookup(Class<E> en, int value)
245             throws SmartherIllegalPropertyValueException {
246         E[] constants = en.getEnumConstants();
247         if (constants != null) {
248             for (E constant : constants) {
249                 if (constant.getValue() == value) {
250                     return constant;
251                 }
252             }
253         }
254         throw new SmartherIllegalPropertyValueException(en.getSimpleName(), String.valueOf(value));
255     }
256
257     interface TypeWithStringProperty {
258         String getValue();
259     }
260
261     public static <E extends Enum<E> & TypeWithStringProperty> E lookup(Class<E> en, String value)
262             throws SmartherIllegalPropertyValueException {
263         E[] constants = en.getEnumConstants();
264         if (constants != null) {
265             for (E constant : constants) {
266                 if (constant.getValue().equals(value)) {
267                     return constant;
268                 }
269             }
270         }
271         throw new SmartherIllegalPropertyValueException(en.getSimpleName(), value);
272     }
273 }