]> git.basschouten.com Git - openhab-addons.git/blob
9f8f66a0457e109581d9a4a7e19d7f3338b80a7e
[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.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 video inputs available for the projector
27  *
28  * @author Laurent Garnier - Initial contribution
29  */
30 @NonNullByDefault
31 public enum SonyProjectorInput {
32
33     // Category 1: VW260, VW270, VW285, VW295, VW300, VW315, VW320, VW328, VW350, VW365, VW385, VW500, VW515, VW520,
34     // VW528, VW550, VW570, VW600, VW665, VW675, VW695, VW760, VW870, VW885, VW995, HW45ES, HW60, HW65, HW68
35     CAT1_HDMI1(1, "HDMI1", "HDMI 1", new byte[] { 0x00, 0x02 }),
36     CAT1_HDMI2(1, "HDMI2", "HDMI 2", new byte[] { 0x00, 0x03 }),
37
38     // Category 2: VW40, VW50, VW60, VW70, VW80, VW85, VW200, HW10, HW15, HW20
39     CAT2_VIDEO(2, "Video", null, new byte[] { 0x00, 0x00 }),
40     CAT2_SVIDEO(2, "SVideo", "S-Video", new byte[] { 0x00, 0x01 }),
41     CAT2_INPUT_A(2, "InputA", "Input A", new byte[] { 0x00, 0x02 }),
42     CAT2_COMPONENT(2, "Component", null, new byte[] { 0x00, 0x03 }),
43     CAT2_HDMI1(2, "HDMI1", "HDMI 1", new byte[] { 0x00, 0x04 }),
44     CAT2_HDMI2(2, "HDMI2", "HDMI 2", new byte[] { 0x00, 0x05 }),
45
46     // Category 3: VW95, VW1000ES, VW1100ES, HW30ES, HW40ES, HW50ES, HW55ES, HW58ES
47     CAT3_INPUT_A(3, "InputA", "Input A", new byte[] { 0x00, 0x02 }),
48     CAT3_COMPONENT(3, "Component", null, new byte[] { 0x00, 0x03 }),
49     CAT3_HDMI1(3, "HDMI1", "HDMI 1", new byte[] { 0x00, 0x04 }),
50     CAT3_HDMI2(3, "HDMI2", "HDMI 2", new byte[] { 0x00, 0x05 }),
51
52     // Category 4: VW100
53     CAT4_VIDEO(4, "Video", null, new byte[] { 0x00, 0x00 }),
54     CAT4_SVIDEO(4, "SVideo", "S-Video", new byte[] { 0x00, 0x01 }),
55     CAT4_INPUT_A(4, "InputA", "Input A", new byte[] { 0x00, 0x02 }),
56     CAT4_COMPONENT(4, "Component", null, new byte[] { 0x00, 0x03 }),
57     CAT4_HDMI(4, "HDMI", null, new byte[] { 0x00, 0x04 }),
58     CAT4_DVI(4, "DVI", null, new byte[] { 0x00, 0x05 }),
59
60     // Category 5: VW90
61     CAT5_VIDEO(5, "Video", null, new byte[] { 0x00, 0x00 }),
62     CAT5_INPUT_A(5, "InputA", "Input A", new byte[] { 0x00, 0x02 }),
63     CAT5_COMPONENT(5, "Component", null, new byte[] { 0x00, 0x03 }),
64     CAT5_HDMI1(5, "HDMI1", "HDMI 1", new byte[] { 0x00, 0x04 }),
65     CAT5_HDMI2(5, "HDMI2", "HDMI 2", new byte[] { 0x00, 0x05 });
66
67     private int category;
68     private String name;
69     private @Nullable String label;
70     private byte[] dataCode;
71
72     /**
73      * Constructor
74      *
75      * @param category a category of projector models for which the video input is available
76      * @param name the name of the video input
77      * @param label the label of the video input; can be null when the label is identical to the name
78      * @param dataCode the data code identifying the video input
79      */
80     private SonyProjectorInput(int category, String name, @Nullable String label, byte[] dataCode) {
81         this.category = category;
82         this.name = name;
83         this.label = label;
84         this.dataCode = dataCode;
85     }
86
87     /**
88      * Get the category of projector models for the current video input
89      *
90      * @return the category of projector models
91      */
92     public int getCategory() {
93         return category;
94     }
95
96     /**
97      * Get the data code identifying the current video input
98      *
99      * @return the data code
100      */
101     public byte[] getDataCode() {
102         return dataCode;
103     }
104
105     /**
106      * Get the label of the current video input
107      *
108      * @return the label
109      */
110     public @Nullable String getLabel() {
111         return label;
112     }
113
114     /**
115      * Get the name of the current video input
116      *
117      * @return the name
118      */
119     public String getName() {
120         return name;
121     }
122
123     /**
124      * Get the list of {@link StateOption} associated to the available video inputs for a particular category of
125      * projector models
126      *
127      * @param category a category of projector models
128      *
129      * @return the list of {@link StateOption} associated to the available video inputs for a provided category of
130      *         projector models
131      */
132     public static List<StateOption> getStateOptions(int category) {
133         List<StateOption> options = new ArrayList<>();
134         for (SonyProjectorInput value : SonyProjectorInput.values()) {
135             if (value.getCategory() == category) {
136                 options.add(new StateOption(value.getName(),
137                         value.getLabel() != null ? value.getLabel() : value.getName()));
138             }
139         }
140         return options;
141     }
142
143     /**
144      * Get the video input associated to a name for a particular category of projector models
145      *
146      * @param category a category of projector models
147      * @param name the name used to identify the video input
148      *
149      * @return the video input associated to the searched name for the provided category of projector models
150      *
151      * @throws SonyProjectorException - If no video input is associated to the searched name for the provided category
152      */
153     public static SonyProjectorInput getFromName(int category, String name) throws SonyProjectorException {
154         for (SonyProjectorInput value : SonyProjectorInput.values()) {
155             if (value.getCategory() == category && value.getName().equals(name)) {
156                 return value;
157             }
158         }
159         throw new SonyProjectorException("Invalid name for a video input: " + name);
160     }
161
162     /**
163      * Get the video input associated to a data code for a particular category of projector models
164      *
165      * @param category a category of projector models
166      * @param dataCode the data code used to identify the video input
167      *
168      * @return the video input associated to the searched data code for the provided category of projector models
169      *
170      * @throws SonyProjectorException - If no video input is associated to the searched data code for the provided
171      *             category
172      */
173     public static SonyProjectorInput getFromDataCode(int category, byte[] dataCode) throws SonyProjectorException {
174         for (SonyProjectorInput value : SonyProjectorInput.values()) {
175             if (value.getCategory() == category && Arrays.equals(dataCode, value.getDataCode())) {
176                 return value;
177             }
178         }
179         throw new SonyProjectorException("Invalid data code for a video input: " + HexUtils.bytesToHex(dataCode));
180     }
181 }