]> git.basschouten.com Git - openhab-addons.git/blob
f4a90a08b044925ef9996a45738dd73c78d799f2
[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.serial;
14
15 import java.util.Arrays;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.openhab.core.i18n.CommunicationException;
19 import org.openhab.core.util.HexUtils;
20
21 /**
22  * Represents the different error codes returned by the projector in serial mode
23  *
24  * @author Laurent Garnier - Initial contribution
25  */
26 @NonNullByDefault
27 public enum SonyProjectorSerialError {
28
29     COMPLETE("Complete", new byte[] { 0x00, 0x00 }),
30     UNDEFINED_COMMAND("Undefined Command", new byte[] { 0x01, 0x01 }),
31     SIZE_ERROR("Size Error", new byte[] { 0x01, 0x04 }),
32     SELECT_ERROR("Select Error", new byte[] { 0x01, 0x05 }),
33     RANGE_OVER("Range Over", new byte[] { 0x01, 0x06 }),
34     NOT_APPLICABLE("Not Applicable", new byte[] { 0x01, 0x0A }),
35     CHECK_SUM_ERROR("Check Sum Error", new byte[] { (byte) 0xF0, 0x10 }),
36     FRAMING_ERROR("Framing Error", new byte[] { (byte) 0xF0, 0x20 }),
37     PARITY_ERROR("Parity Error", new byte[] { (byte) 0xF0, 0x30 }),
38     OVER_RUN_ERROR("Over Run Error", new byte[] { (byte) 0xF0, 0x40 }),
39     OTHER_COMM_ERROR("Other Comm Error", new byte[] { (byte) 0xF0, 0x50 });
40
41     private String message;
42     private byte[] dataCode;
43
44     /**
45      * Constructor
46      *
47      * @param message the error message
48      * @param dataCode the data code identifying the error
49      */
50     private SonyProjectorSerialError(String message, byte[] dataCode) {
51         this.message = message;
52         this.dataCode = dataCode;
53     }
54
55     /**
56      * Get the error message
57      *
58      * @return the message
59      */
60     public String getMessage() {
61         return message;
62     }
63
64     /**
65      * Get the data code identifying the error
66      *
67      * @return the data code
68      */
69     public byte[] getDataCode() {
70         return dataCode;
71     }
72
73     /**
74      * Get the error associated to a data code
75      *
76      * @param dataCode the data code used to identify the error
77      *
78      * @return the error associated to the searched data code
79      *
80      * @throws CommunicationException if no error is associated to the searched data code
81      */
82     public static SonyProjectorSerialError getFromDataCode(byte[] dataCode) throws CommunicationException {
83         for (SonyProjectorSerialError value : SonyProjectorSerialError.values()) {
84             if (Arrays.equals(dataCode, value.getDataCode())) {
85                 return value;
86             }
87         }
88         throw new CommunicationException("Unknwon error code: " + HexUtils.bytesToHex(dataCode));
89     }
90 }