]> git.basschouten.com Git - openhab-addons.git/blob
33fff3972f16a7b6eb836812cd6890dd703eec97
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.lifx.internal.protocol;
14
15 import static org.openhab.binding.lifx.internal.protocol.Product.Feature.*;
16 import static org.openhab.binding.lifx.internal.protocol.Product.TemperatureRange.*;
17 import static org.openhab.binding.lifx.internal.protocol.Product.Vendor.LIFX;
18
19 import java.util.Arrays;
20 import java.util.EnumSet;
21
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.openhab.binding.lifx.internal.LifxBindingConstants;
24 import org.openhab.core.thing.ThingTypeUID;
25
26 /**
27  * Enumerates the LIFX products, their IDs and feature set.
28  *
29  * @see https://lan.developer.lifx.com/docs/lifx-products
30  *
31  * @author Wouter Born - Support LIFX 2016 product line-up and infrared functionality
32  * @author Wouter Born - Add temperature ranges and simplify feature definitions
33  */
34 @NonNullByDefault
35 public enum Product {
36
37     PRODUCT_1(1, "Original 1000", TR_2500_9000, COLOR),
38     PRODUCT_3(3, "Color 650", TR_2500_9000, COLOR),
39     PRODUCT_10(10, "White 800 (Low Voltage)", TR_2700_6500),
40     PRODUCT_11(11, "White 800 (High Voltage)", TR_2700_6500),
41     PRODUCT_18(18, "White 900 BR30 (Low Voltage)", TR_2700_6500),
42     PRODUCT_20(20, "Color 1000 BR30", TR_2500_9000, COLOR),
43     PRODUCT_22(22, "Color 1000", TR_2500_9000, COLOR),
44     PRODUCT_27(27, "LIFX A19", TR_2500_9000, COLOR),
45     PRODUCT_28(28, "LIFX BR30", TR_2500_9000, COLOR),
46     PRODUCT_29(29, "LIFX+ A19", TR_2500_9000, COLOR, INFRARED),
47     PRODUCT_30(30, "LIFX+ BR30", TR_2500_9000, COLOR, INFRARED),
48     PRODUCT_31(31, "LIFX Z", TR_2500_9000, COLOR, MULTIZONE),
49     PRODUCT_32(32, "LIFX Z 2", TR_2500_9000, COLOR, MULTIZONE),
50     PRODUCT_36(36, "LIFX Downlight", TR_2500_9000, COLOR),
51     PRODUCT_37(37, "LIFX Downlight", TR_2500_9000, COLOR),
52     PRODUCT_38(38, "LIFX Beam", TR_2500_9000, COLOR, MULTIZONE),
53     PRODUCT_43(43, "LIFX A19", TR_2500_9000, COLOR),
54     PRODUCT_44(44, "LIFX BR30", TR_2500_9000, COLOR),
55     PRODUCT_45(45, "LIFX+ A19", TR_2500_9000, COLOR, INFRARED),
56     PRODUCT_46(46, "LIFX+ BR30", TR_2500_9000, COLOR, INFRARED),
57     PRODUCT_49(49, "LIFX Mini", TR_2500_9000, COLOR),
58     PRODUCT_50(50, "LIFX Mini Day and Dusk", TR_1500_4000),
59     PRODUCT_51(51, "LIFX Mini White", TR_2700_2700),
60     PRODUCT_52(52, "LIFX GU10", TR_2500_9000, COLOR),
61     PRODUCT_55(55, "LIFX Tile", TR_2500_9000, CHAIN, COLOR, MATRIX, TILE_EFFECT),
62     PRODUCT_57(57, "LIFX Candle", TR_1500_9000, COLOR, MATRIX),
63     PRODUCT_59(59, "LIFX Mini Color", TR_2500_9000, COLOR),
64     PRODUCT_60(60, "LIFX Mini Day and Dusk", TR_1500_4000),
65     PRODUCT_61(61, "LIFX Mini White", TR_2700_2700),
66     PRODUCT_62(62, "LIFX A19", TR_2500_9000, COLOR),
67     PRODUCT_63(63, "LIFX BR30", TR_2500_9000, COLOR),
68     PRODUCT_64(64, "LIFX+ A19", TR_2500_9000, COLOR, INFRARED),
69     PRODUCT_65(65, "LIFX+ BR30", TR_2500_9000, COLOR, INFRARED),
70     PRODUCT_68(68, "LIFX Candle", TR_1500_9000, COLOR, MATRIX),
71     PRODUCT_81(81, "LIFX Candle Warm to White", TR_2200_6500, MATRIX),
72     PRODUCT_82(82, "LIFX Filament", TR_2000_2000);
73
74     /**
75      * Enumerates the product features.
76      */
77     public enum Feature {
78         CHAIN,
79         COLOR,
80         INFRARED,
81         MATRIX,
82         MULTIZONE,
83         TILE_EFFECT
84     }
85
86     /**
87      * Enumerates the product vendors.
88      */
89     public enum Vendor {
90         LIFX(1, "LIFX");
91
92         private final int id;
93         private final String name;
94
95         Vendor(int id, String name) {
96             this.id = id;
97             this.name = name;
98         }
99
100         public int getID() {
101             return id;
102         }
103
104         public String getName() {
105             return name;
106         }
107     }
108
109     /**
110      * Enumerates the color temperature ranges of lights.
111      */
112     public enum TemperatureRange {
113         /**
114          * 1500-4000K
115          */
116         TR_1500_4000(1500, 4000),
117
118         /**
119          * 1500-9000K
120          */
121         TR_1500_9000(1500, 9000),
122
123         /**
124          * 2000-2000K
125          */
126         TR_2000_2000(2000, 2000),
127
128         /**
129          * 2200-6500K
130          */
131         TR_2200_6500(2200, 6500),
132
133         /**
134          * 2500-9000K
135          */
136         TR_2500_9000(2500, 9000),
137
138         /**
139          * 2700-2700K
140          */
141         TR_2700_2700(2700, 2700),
142
143         /**
144          * 2700-6500K
145          */
146         TR_2700_6500(2700, 6500);
147
148         private final int minimum;
149         private final int maximum;
150
151         TemperatureRange(int minimum, int maximum) {
152             this.minimum = minimum;
153             this.maximum = maximum;
154         }
155
156         /**
157          * The minimum color temperature in degrees Kelvin.
158          *
159          * @return minimum color temperature (K)
160          */
161         public int getMinimum() {
162             return minimum;
163         }
164
165         /**
166          * The maxiumum color temperature in degrees Kelvin.
167          *
168          * @return maximum color temperature (K)
169          */
170         public int getMaximum() {
171             return maximum;
172         }
173
174         /**
175          * The color temperature range in degrees Kelvin.
176          *
177          * @return difference between maximum and minimum color temperature values
178          */
179         public int getRange() {
180             return maximum - minimum;
181         }
182     }
183
184     private final Vendor vendor;
185     private final long id;
186     private final String name;
187     private final TemperatureRange temperatureRange;
188     private final EnumSet<Feature> features = EnumSet.noneOf(Feature.class);
189
190     private Product(long id, String name, TemperatureRange temperatureRange) {
191         this(LIFX, id, name, temperatureRange);
192     }
193
194     private Product(long id, String name, TemperatureRange temperatureRange, Feature... features) {
195         this(LIFX, id, name, temperatureRange, features);
196     }
197
198     private Product(Vendor vendor, long id, String name, TemperatureRange temperatureRange) {
199         this(vendor, id, name, temperatureRange, new Feature[0]);
200     }
201
202     private Product(Vendor vendor, long id, String name, TemperatureRange temperatureRange, Feature... features) {
203         this.vendor = vendor;
204         this.id = id;
205         this.name = name;
206         this.temperatureRange = temperatureRange;
207         this.features.addAll(Arrays.asList(features));
208     }
209
210     @Override
211     public String toString() {
212         return name;
213     }
214
215     public Vendor getVendor() {
216         return vendor;
217     }
218
219     public long getID() {
220         return id;
221     }
222
223     public String getName() {
224         return name;
225     }
226
227     public TemperatureRange getTemperatureRange() {
228         return temperatureRange;
229     }
230
231     public ThingTypeUID getThingTypeUID() {
232         if (hasFeature(COLOR)) {
233             if (hasFeature(TILE_EFFECT)) {
234                 return LifxBindingConstants.THING_TYPE_TILELIGHT;
235             } else if (hasFeature(INFRARED)) {
236                 return LifxBindingConstants.THING_TYPE_COLORIRLIGHT;
237             } else if (hasFeature(MULTIZONE)) {
238                 return LifxBindingConstants.THING_TYPE_COLORMZLIGHT;
239             } else {
240                 return LifxBindingConstants.THING_TYPE_COLORLIGHT;
241             }
242         } else {
243             return LifxBindingConstants.THING_TYPE_WHITELIGHT;
244         }
245     }
246
247     public boolean hasFeature(Feature feature) {
248         return features.contains(feature);
249     }
250
251     /**
252      * Returns a product that has the given thing type UID.
253      *
254      * @param uid a thing type UID
255      * @return a product that has the given thing type UID
256      * @throws IllegalArgumentException when <code>uid</code> is not a valid LIFX thing type UID
257      */
258     public static Product getLikelyProduct(ThingTypeUID uid) throws IllegalArgumentException {
259         for (Product product : Product.values()) {
260             if (product.getThingTypeUID().equals(uid)) {
261                 return product;
262             }
263         }
264
265         throw new IllegalArgumentException(uid + " is not a valid product thing type UID");
266     }
267
268     /**
269      * Returns the product that has the given product ID.
270      *
271      * @param id the product ID
272      * @return the product that has the given product ID
273      * @throws IllegalArgumentException when <code>id</code> is not a valid LIFX product ID
274      */
275     public static Product getProductFromProductID(long id) throws IllegalArgumentException {
276         for (Product product : Product.values()) {
277             if (product.id == id) {
278                 return product;
279             }
280         }
281
282         throw new IllegalArgumentException(id + " is not a valid product ID");
283     }
284 }