]> git.basschouten.com Git - openhab-addons.git/blob
450bbdc051ffec1b6f24aec4cc663e15bbb3119b
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.openhab.binding.sonyprojector.internal.SonyProjectorException;
21 import org.openhab.core.types.StateOption;
22 import org.openhab.core.util.HexUtils;
23
24 /**
25  * Represents the different lamp control modes available for the projector
26  *
27  * @author Laurent Garnier - Initial contribution
28  */
29 @NonNullByDefault
30 public enum SonyProjectorLampControl {
31
32     HIGH("High", new byte[] { 0x00, 0x01 }),
33     LOW("Low", new byte[] { 0x00, 0x00 });
34
35     private String name;
36     private byte[] dataCode;
37
38     /**
39      * Constructor
40      *
41      * @param name the name of the lamp control mode
42      * @param dataCode the data code identifying the lamp control mode
43      */
44     private SonyProjectorLampControl(String name, byte[] dataCode) {
45         this.name = name;
46         this.dataCode = dataCode;
47     }
48
49     /**
50      * Get the data code identifying the current lamp control mode
51      *
52      * @return the data code
53      */
54     public byte[] getDataCode() {
55         return dataCode;
56     }
57
58     /**
59      * Get the name of the current lamp control mode
60      *
61      * @return the name
62      */
63     public String getName() {
64         return name;
65     }
66
67     /**
68      * Get the list of {@link StateOption} associated to the available lamp control modes
69      *
70      * @return the list of {@link StateOption} associated to the available lamp control modes
71      */
72     public static List<StateOption> getStateOptions() {
73         List<StateOption> options = new ArrayList<>();
74         for (SonyProjectorLampControl value : SonyProjectorLampControl.values()) {
75             options.add(new StateOption(value.getName(), value.getName()));
76         }
77         return options;
78     }
79
80     /**
81      * Get the lamp control mode associated to a name
82      *
83      * @param name the name used to identify the lamp control mode
84      *
85      * @return the lamp control mode associated to the searched name
86      *
87      * @throws SonyProjectorException - If no lamp control mode is associated to the searched name
88      */
89     public static SonyProjectorLampControl getFromName(String name) throws SonyProjectorException {
90         for (SonyProjectorLampControl value : SonyProjectorLampControl.values()) {
91             if (value.getName().equals(name)) {
92                 return value;
93             }
94         }
95         throw new SonyProjectorException("Invalid name for a lamp control mode: " + name);
96     }
97
98     /**
99      * Get the lamp control mode associated to a data code
100      *
101      * @param dataCode the data code used to identify the lamp control mode
102      *
103      * @return the lamp control mode associated to the searched data code
104      *
105      * @throws SonyProjectorException - If no lamp control mode is associated to the searched data code
106      */
107     public static SonyProjectorLampControl getFromDataCode(byte[] dataCode) throws SonyProjectorException {
108         for (SonyProjectorLampControl value : SonyProjectorLampControl.values()) {
109             if (Arrays.equals(dataCode, value.getDataCode())) {
110                 return value;
111             }
112         }
113         throw new SonyProjectorException("Invalid data code for a lamp control mode: " + HexUtils.bytesToHex(dataCode));
114     }
115 }