]> git.basschouten.com Git - openhab-addons.git/blob
5cae8b5d4eb3dad2bf49dc7ea439a09314fdf2a9
[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.input;
14
15 import java.util.regex.Pattern;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.binding.pjlinkdevice.internal.device.command.ResponseException;
20
21 /**
22  * Describes an A/V source that can be selected on the PJLink device.
23  *
24  * @author Nils Schnabel - Initial contribution
25  */
26 @NonNullByDefault
27 public class Input {
28
29     private static final Pattern INPUT_NUMBER_PATTERN = Pattern.compile("[0-9A-Z]");
30
31     enum InputType {
32         RGB("RGB", '1'),
33         VIDEO("Video", '2'),
34         DIGITAL("Digital", '3'),
35         STORAGE("Storage", '4'),
36         NETWORK("Network", '5');
37
38         private String text;
39         private char code;
40
41         private InputType(String text, char code) {
42             this.text = text;
43             this.code = code;
44         }
45
46         public String getText() {
47             return this.text;
48         }
49
50         public static InputType parseString(String value) throws ResponseException {
51             for (InputType result : InputType.values()) {
52                 if (result.code == value.charAt(0)) {
53                     return result;
54                 }
55             }
56
57             throw new ResponseException("Unknown input channel type: " + value);
58         }
59     }
60
61     private String value;
62
63     public Input(String value) throws ResponseException {
64         this.value = value;
65         validate();
66     }
67
68     @Override
69     public int hashCode() {
70         final int prime = 31;
71         int result = 1;
72         result = prime * result + value.hashCode();
73         return result;
74     }
75
76     @SuppressWarnings("PMD.SimplifyBooleanReturns")
77     @Override
78     public boolean equals(@Nullable Object obj) {
79         if (this == obj) {
80             return true;
81         }
82         if (obj == null) {
83             return false;
84         }
85         if (getClass() != obj.getClass()) {
86             return false;
87         }
88         Input other = (Input) obj;
89         if (!value.equals(other.value)) {
90             return false;
91         }
92         return true;
93     }
94
95     public InputType getInputType() throws ResponseException {
96         return InputType.parseString(this.value);
97     }
98
99     public String getInputNumber() throws ResponseException {
100         String inputNumber = this.value.substring(1, 2);
101         if (!INPUT_NUMBER_PATTERN.matcher(inputNumber).matches()) {
102             throw new ResponseException("Illegal channel number: " + inputNumber);
103         }
104
105         return inputNumber;
106     }
107
108     public void validate() throws ResponseException {
109         if (this.value.length() != 2) {
110             throw new ResponseException("Illegal input description: " + value);
111         }
112         // these method also validate
113         getInputType();
114         getInputNumber();
115     }
116
117     public String getValue() {
118         return this.value;
119     }
120
121     public String getPJLinkRepresentation() {
122         return this.value;
123     }
124
125     public String getText() throws ResponseException {
126         return getInputType().getText() + " " + getInputNumber();
127     }
128 }