]> git.basschouten.com Git - openhab-addons.git/blob
99e60edfb72c773822fcc3dc27f87f83d35d8760
[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.pjlinkdevice.internal.device.command;
14
15 import java.text.MessageFormat;
16 import java.util.Set;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20
21 /**
22  * Error codes as specified in <a href="https://pjlink.jbmia.or.jp/english/data_cl2/PJLink_5-1.pdf">[PJLinkSpec]</a>
23  * chapters
24  * 2.3. Set commands
25  * 2.4. Get commands
26  *
27  * @author Nils Schnabel - Initial contribution
28  */
29 @NonNullByDefault
30 public enum ErrorCode {
31     UNDEFINED_COMMAND("Undefined command", "ERR1"),
32     OUT_OF_PARAMETER("Out of parameter", "ERR2"),
33     UNAVAILABLE_TIME("Unavailable time", "ERR3"),
34     DEVICE_FAILURE("Projector/Display failure", "ERR4");
35
36     private String text;
37     private String code;
38
39     private ErrorCode(String text, String code) {
40         this.text = text;
41         this.code = code;
42     }
43
44     public static @Nullable ErrorCode getValueForCode(String code) {
45         for (ErrorCode result : ErrorCode.values()) {
46             if (result.code.equalsIgnoreCase(code)) {
47                 return result;
48             }
49         }
50
51         return null;
52     }
53
54     public String getText() {
55         return this.text;
56     }
57
58     /**
59      * Checks if a the given code is an error code. Optionally, restrictCodesTo can restrict the allowed error codes
60      * (based on PJLink specification).
61      *
62      * @param code string to be checked for error codes
63      * @param restrictCodesTo list of expected error codes according to PJLink specification, can be null if all error
64      *            codes (ERR1-ERR4) can be expected
65      * @throws ResponseException
66      */
67     public static void checkForErrorStatus(String code, @Nullable Set<ErrorCode> restrictCodesTo)
68             throws ResponseException {
69         ErrorCode parsed = getValueForCode(code);
70         if (parsed != null && (restrictCodesTo == null || restrictCodesTo.contains(parsed))) {
71             throw new ResponseException(MessageFormat.format("Got error status {0} ({1})", parsed.getText(), code));
72         }
73     }
74 }