]> git.basschouten.com Git - openhab-addons.git/blob
a266dc15acb73017ccea35e113d0b7acf372f14d
[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 picture positions available for the projector
27  *
28  * @author Laurent Garnier - Initial contribution
29  */
30 @NonNullByDefault
31 public enum SonyProjectorPicturePosition {
32
33     // Category 1: VW385, VW500, VW515, VW520, VW528, VW550, VW570, VW600, VW665, VW675, VW695, VW760, VW870, VW885,
34     // VW995, VW1000ES, VW1100ES
35     CAT1_185(1, "185", "1.85:1", new byte[] { 0x00, 0x00 }),
36     CAT1_235(1, "235", "2.35:1", new byte[] { 0x00, 0x01 }),
37     CAT1_CUSTOM1(1, "Custom1", "Custom 1", new byte[] { 0x00, 0x02 }),
38     CAT1_CUSTOM2(1, "Custom2", "Custom 2", new byte[] { 0x00, 0x03 }),
39     CAT1_CUSTOM3(1, "Custom3", "Custom 3", new byte[] { 0x00, 0x04 }),
40
41     // Category 2: VW95
42     CAT2_POSITION1(2, "Position1", "Position 1", new byte[] { 0x00, 0x00 }),
43     CAT2_POSITION2(2, "Position2", "Position 2", new byte[] { 0x00, 0x01 }),
44     CAT2_POSITION3(2, "Position3", "Position 3", new byte[] { 0x00, 0x02 }),
45     CAT2_POSITION4(2, "Position4", "Position 4", new byte[] { 0x00, 0x03 }),
46     CAT2_POSITION5(2, "Position5", "Position 5", new byte[] { 0x00, 0x04 });
47
48     private int category;
49     private String name;
50     private @Nullable String label;
51     private byte[] dataCode;
52
53     /**
54      * Constructor
55      *
56      * @param category a category of projector models for which the picture position is available
57      * @param name the name of the picture position
58      * @param label the label of the picture position; can be null when the label is identical to the name
59      * @param dataCode the data code identifying the picture position
60      */
61     private SonyProjectorPicturePosition(int category, String name, @Nullable String label, byte[] dataCode) {
62         this.category = category;
63         this.name = name;
64         this.label = label;
65         this.dataCode = dataCode;
66     }
67
68     /**
69      * Get the category of projector models for the current picture position
70      *
71      * @return the category of projector models
72      */
73     public int getCategory() {
74         return category;
75     }
76
77     /**
78      * Get the data code identifying the current picture position
79      *
80      * @return the data code
81      */
82     public byte[] getDataCode() {
83         return dataCode;
84     }
85
86     /**
87      * Get the label of the current picture position
88      *
89      * @return the label
90      */
91     public @Nullable String getLabel() {
92         return label;
93     }
94
95     /**
96      * Get the name of the current picture position
97      *
98      * @return the name
99      */
100     public String getName() {
101         return name;
102     }
103
104     /**
105      * Get the list of {@link StateOption} associated to the available picture positions for a particular category of
106      * projector models
107      *
108      * @param category a category of projector models
109      *
110      * @return the list of {@link StateOption} associated to the available picture positions for a provided category of
111      *         projector models
112      */
113     public static List<StateOption> getStateOptions(int category) {
114         List<StateOption> options = new ArrayList<>();
115         for (SonyProjectorPicturePosition value : SonyProjectorPicturePosition.values()) {
116             if (value.getCategory() == category) {
117                 options.add(new StateOption(value.getName(),
118                         value.getLabel() != null ? value.getLabel() : value.getName()));
119             }
120         }
121         return options;
122     }
123
124     /**
125      * Get the picture position associated to a name for a particular category of projector models
126      *
127      * @param category a category of projector models
128      * @param name the name used to identify the picture position
129      *
130      * @return the picture position associated to the searched name for the provided category of projector models
131      *
132      * @throws SonyProjectorException - If no picture position is associated to the searched name for the provided
133      *             category
134      */
135     public static SonyProjectorPicturePosition getFromName(int category, String name) throws SonyProjectorException {
136         for (SonyProjectorPicturePosition value : SonyProjectorPicturePosition.values()) {
137             if (value.getCategory() == category && value.getName().equals(name)) {
138                 return value;
139             }
140         }
141         throw new SonyProjectorException("Invalid name for a picture position: " + name);
142     }
143
144     /**
145      * Get the picture position 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 picture position
149      *
150      * @return the picture position associated to the searched data code for the provided category of projector models
151      *
152      * @throws SonyProjectorException - If no picture position is associated to the searched data code for the provided
153      *             category
154      */
155     public static SonyProjectorPicturePosition getFromDataCode(int category, byte[] dataCode)
156             throws SonyProjectorException {
157         for (SonyProjectorPicturePosition value : SonyProjectorPicturePosition.values()) {
158             if (value.getCategory() == category && Arrays.equals(dataCode, value.getDataCode())) {
159                 return value;
160             }
161         }
162         throw new SonyProjectorException("Invalid data code for a picture position: " + HexUtils.bytesToHex(dataCode));
163     }
164 }