]> git.basschouten.com Git - openhab-addons.git/blob
2899e11b2c7aa8a55812486a2fb42394cb79cbef
[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 MPEG noise reduction modes available for the projector
27  *
28  * @author Laurent Garnier - Initial contribution
29  */
30 @NonNullByDefault
31 public enum SonyProjectorMpegNr {
32
33     // Category 1: VW260, VW270, VW285, VW295, VW315, VW320, VW328, VW365, VW>385, VW500, VW515, VW520, VW528, VW550,
34     // VW570, VW600, VW665, VW675, VW695, VW760, VW870, VW885, VW995, HW60, HW65, HW68
35     CAT1_AUTO(1, "Auto", null, new byte[] { 0x00, 0x04 }),
36     CAT1_HIGH(1, "High", null, new byte[] { 0x00, 0x03 }),
37     CAT1_MIDDLE(1, "Middle", null, new byte[] { 0x00, 0x02 }),
38     CAT1_LOW(1, "Low", null, new byte[] { 0x00, 0x01 }),
39     CAT1_OFF(1, "Off", null, new byte[] { 0x00, 0x00 }),
40
41     // Category 2: HW35ES, HW40ES, HW45ES, HW50ES, HW55ES, HW58ES
42     CAT2_HIGH(2, "High", null, new byte[] { 0x00, 0x03 }),
43     CAT2_MIDDLE(2, "Middle", null, new byte[] { 0x00, 0x02 }),
44     CAT2_LOW(2, "Low", null, new byte[] { 0x00, 0x01 }),
45     CAT2_OFF(2, "Off", null, new byte[] { 0x00, 0x00 });
46
47     private int category;
48     private String name;
49     private @Nullable String label;
50     private byte[] dataCode;
51
52     /**
53      * Constructor
54      *
55      * @param category a category of projector models for which the MPEG noise reduction mode is available
56      * @param name the name of the MPEG noise reduction mode
57      * @param label the label of the MPEG noise reduction mode; can be null when the label is identical to the name
58      * @param dataCode the data code identifying the MPEG noise reduction mode
59      */
60     private SonyProjectorMpegNr(int category, String name, @Nullable String label, byte[] dataCode) {
61         this.category = category;
62         this.name = name;
63         this.label = label;
64         this.dataCode = dataCode;
65     }
66
67     /**
68      * Get the category of projector models for the current MPEG noise reduction mode
69      *
70      * @return the category of projector models
71      */
72     public int getCategory() {
73         return category;
74     }
75
76     /**
77      * Get the data code identifying the current MPEG noise reduction mode
78      *
79      * @return the data code
80      */
81     public byte[] getDataCode() {
82         return dataCode;
83     }
84
85     /**
86      * Get the label of the current MPEG noise reduction mode
87      *
88      * @return the label
89      */
90     public @Nullable String getLabel() {
91         return label;
92     }
93
94     /**
95      * Get the name of the current MPEG noise reduction mode
96      *
97      * @return the name
98      */
99     public String getName() {
100         return name;
101     }
102
103     /**
104      * Get the list of {@link StateOption} associated to the available MPEG noise reduction modes for a particular
105      * category of projector models
106      *
107      * @param category a category of projector models
108      *
109      * @return the list of {@link StateOption} associated to the available MPEG noise reduction modes for a provided
110      *         category of projector models
111      */
112     public static List<StateOption> getStateOptions(int category) {
113         List<StateOption> options = new ArrayList<>();
114         for (SonyProjectorMpegNr value : SonyProjectorMpegNr.values()) {
115             if (value.getCategory() == category) {
116                 options.add(new StateOption(value.getName(),
117                         value.getLabel() != null ? value.getLabel() : value.getName()));
118             }
119         }
120         return options;
121     }
122
123     /**
124      * Get the MPEG noise reduction mode associated to a name for a particular category of projector models
125      *
126      * @param category a category of projector models
127      * @param name the name used to identify the MPEG noise reduction mode
128      *
129      * @return the MPEG noise reduction mode associated to the searched name for the provided category of projector
130      *         models
131      *
132      * @throws SonyProjectorException - If no MPEG noise reduction mode is associated to the searched name for the
133      *             provided category
134      */
135     public static SonyProjectorMpegNr getFromName(int category, String name) throws SonyProjectorException {
136         for (SonyProjectorMpegNr value : SonyProjectorMpegNr.values()) {
137             if (value.getCategory() == category && value.getName().equals(name)) {
138                 return value;
139             }
140         }
141         throw new SonyProjectorException("Invalid name for a MPEG noise reduction mode: " + name);
142     }
143
144     /**
145      * Get the MPEG noise reduction mode associated to a data code for a particular category of projector models
146      *
147      * @param category a category of projector models
148      * @param dataCode the data code used to identify the MPEG noise reduction mode
149      *
150      * @return the MPEG noise reduction mode associated to the searched data code for the provided category of projector
151      *         models
152      *
153      * @throws SonyProjectorException - If no MPEG noise reduction mode is associated to the searched data code for the
154      *             provided category
155      */
156     public static SonyProjectorMpegNr getFromDataCode(int category, byte[] dataCode) throws SonyProjectorException {
157         for (SonyProjectorMpegNr value : SonyProjectorMpegNr.values()) {
158             if (value.getCategory() == category && Arrays.equals(dataCode, value.getDataCode())) {
159                 return value;
160             }
161         }
162         throw new SonyProjectorException(
163                 "Invalid data code for a MPEG noise reduction mode: " + HexUtils.bytesToHex(dataCode));
164     }
165 }