]> git.basschouten.com Git - openhab-addons.git/blob
9245ee35a71e14d7dabeae96f243d2cf6222967a
[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.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     @Override
77     public boolean equals(@Nullable Object obj) {
78         if (this == obj) {
79             return true;
80         }
81         if (obj == null) {
82             return false;
83         }
84         if (getClass() != obj.getClass()) {
85             return false;
86         }
87         Input other = (Input) obj;
88         if (!value.equals(other.value)) {
89             return false;
90         }
91         return true;
92     }
93
94     public InputType getInputType() throws ResponseException {
95         return InputType.parseString(this.value);
96     }
97
98     public String getInputNumber() throws ResponseException {
99         String inputNumber = this.value.substring(1, 2);
100         if (!INPUT_NUMBER_PATTERN.matcher(inputNumber).matches()) {
101             throw new ResponseException("Illegal channel number: " + inputNumber);
102         }
103
104         return inputNumber;
105     }
106
107     public void validate() throws ResponseException {
108         if (this.value.length() != 2) {
109             throw new ResponseException("Illegal input description: " + value);
110         }
111         // these method also validate
112         getInputType();
113         getInputNumber();
114     }
115
116     public String getValue() {
117         return this.value;
118     }
119
120     public String getPJLinkRepresentation() {
121         return this.value;
122     }
123
124     public String getText() throws ResponseException {
125         return getInputType().getText() + " " + getInputNumber();
126     }
127 }