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