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