]> git.basschouten.com Git - openhab-addons.git/blob
bc392432cb7438e7b56562ac0a690728b1f3631e
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.sonyprojector.internal.communication;
14
15 import java.util.ArrayList;
16 import java.util.Arrays;
17 import java.util.List;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.sonyprojector.internal.SonyProjectorException;
22 import org.openhab.core.types.StateOption;
23 import org.openhab.core.util.HexUtils;
24
25 /**
26  * Represents the different iris modes available for the projector
27  *
28  * @author Laurent Garnier - Initial contribution
29  */
30 @NonNullByDefault
31 public enum SonyProjectorIrisMode {
32
33     // Category 1: VW385, VW500, VW515, VW520, VW528, VW550, VW570, VW600, VW665, VW675, VW695, VW760, VW870, VW885,
34     // VW995, HW60, HW65, HW68
35     CAT1_FULL(1, "Full", null, new byte[] { 0x00, 0x02 }),
36     CAT1_LIMITED(1, "Limited", null, new byte[] { 0x00, 0x03 }),
37     CAT1_OFF(1, "Off", null, new byte[] { 0x00, 0x00 }),
38
39     // Category 2: VW40, VW50, VW60
40     CAT2_ON(2, "On", null, new byte[] { 0x00, 0x01 }),
41     CAT2_AUTO1(2, "Auto1", "Auto 1", new byte[] { 0x00, 0x02 }),
42     CAT2_AUTO2(2, "Auto2", "Auto 2", new byte[] { 0x00, 0x03 }),
43     CAT2_OFF(2, "Off", null, new byte[] { 0x00, 0x00 }),
44
45     // Category 3: VW70, VW80, VW85, VW90, VW95, VW200, HW10, HW15, HW20, HW30ES
46     CAT3_AUTO1(3, "Auto1", "Auto 1", new byte[] { 0x00, 0x02 }),
47     CAT3_AUTO2(3, "Auto2", "Auto 2", new byte[] { 0x00, 0x03 }),
48     CAT3_MANUAL(3, "Manual", null, new byte[] { 0x00, 0x01 }),
49     CAT3_OFF(3, "Off", null, new byte[] { 0x00, 0x00 }),
50
51     // Category 4: VW100
52     CAT4_ON(4, "On", null, new byte[] { 0x00, 0x01 }),
53     CAT4_AUTO(4, "Auto", null, new byte[] { 0x00, 0x02 }),
54     CAT4_OFF(4, "Off", null, new byte[] { 0x00, 0x00 }),
55
56     // Category 5: VW1000ES, VW1100ES, HW50ES, HW55ES
57     CAT5_AUTO_FULL(5, "AutoFull", "Auto Full", new byte[] { 0x00, 0x02 }),
58     CAT5_AUTO_LIMITED(5, "AutoLimited", "Auto Limited", new byte[] { 0x00, 0x03 }),
59     CAT5_MANUAL(5, "Manual", null, new byte[] { 0x00, 0x01 }),
60     CAT5_OFF(5, "Off", null, new byte[] { 0x00, 0x00 });
61
62     private int category;
63     private String name;
64     private @Nullable String label;
65     private byte[] dataCode;
66
67     /**
68      * Constructor
69      *
70      * @param category a category of projector models for which the iris mode is available
71      * @param name the name of the iris mode
72      * @param label the label of the iris mode; can be null when the label is identical to the name
73      * @param dataCode the data code identifying the iris mode
74      */
75     private SonyProjectorIrisMode(int category, String name, @Nullable String label, byte[] dataCode) {
76         this.category = category;
77         this.name = name;
78         this.label = label;
79         this.dataCode = dataCode;
80     }
81
82     /**
83      * Get the category of projector models for the current iris mode
84      *
85      * @return the category of projector models
86      */
87     public int getCategory() {
88         return category;
89     }
90
91     /**
92      * Get the data code identifying the current iris mode
93      *
94      * @return the data code
95      */
96     public byte[] getDataCode() {
97         return dataCode;
98     }
99
100     /**
101      * Get the label of the current iris mode
102      *
103      * @return the label
104      */
105     public @Nullable String getLabel() {
106         return label;
107     }
108
109     /**
110      * Get the name of the current iris mode
111      *
112      * @return the name
113      */
114     public String getName() {
115         return name;
116     }
117
118     /**
119      * Get the list of {@link StateOption} associated to the available iris modes for a particular category of projector
120      * models
121      *
122      * @param category a category of projector models
123      *
124      * @return the list of {@link StateOption} associated to the available iris modes for a provided category of
125      *         projector models
126      */
127     public static List<StateOption> getStateOptions(int category) {
128         List<StateOption> options = new ArrayList<>();
129         for (SonyProjectorIrisMode value : SonyProjectorIrisMode.values()) {
130             if (value.getCategory() == category) {
131                 options.add(new StateOption(value.getName(),
132                         value.getLabel() != null ? value.getLabel() : value.getName()));
133             }
134         }
135         return options;
136     }
137
138     /**
139      * Get the iris mode associated to a name for a particular category of projector models
140      *
141      * @param category a category of projector models
142      * @param name the name used to identify the iris mode
143      *
144      * @return the iris mode associated to the searched name for the provided category of projector models
145      *
146      * @throws SonyProjectorException - If no iris mode is associated to the searched name for the provided category
147      */
148     public static SonyProjectorIrisMode getFromName(int category, String name) throws SonyProjectorException {
149         for (SonyProjectorIrisMode value : SonyProjectorIrisMode.values()) {
150             if (value.getCategory() == category && value.getName().equals(name)) {
151                 return value;
152             }
153         }
154         throw new SonyProjectorException("Invalid name for an iris mode: " + name);
155     }
156
157     /**
158      * Get the iris mode associated to a data code for a particular category of projector models
159      *
160      * @param category a category of projector models
161      * @param dataCode the data code used to identify the iris mode
162      *
163      * @return the iris mode associated to the searched data code for the provided category of projector models
164      *
165      * @throws SonyProjectorException - If no iris mode is associated to the searched data code for the provided
166      *             category
167      */
168     public static SonyProjectorIrisMode getFromDataCode(int category, byte[] dataCode) throws SonyProjectorException {
169         for (SonyProjectorIrisMode value : SonyProjectorIrisMode.values()) {
170             if (value.getCategory() == category && Arrays.equals(dataCode, value.getDataCode())) {
171                 return value;
172             }
173         }
174         throw new SonyProjectorException("Invalid data code for an iris mode: " + HexUtils.bytesToHex(dataCode));
175     }
176 }