]> git.basschouten.com Git - openhab-addons.git/blob
190f92080c06c0a99b2bfaa6276bd97aa0cae129
[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.pioneeravr.internal.protocol;
14
15 import java.util.regex.Matcher;
16 import java.util.regex.Pattern;
17
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.binding.pioneeravr.internal.protocol.avr.AvrConnectionException;
20 import org.openhab.binding.pioneeravr.internal.protocol.avr.AvrResponse;
21
22 /**
23  * Represent an AVR response.
24  *
25  * @author Antoine Besnard - Initial contribution
26  * @author Leroy Foerster - Listening Mode, Playing Listening Mode
27  */
28 public class Response implements AvrResponse {
29
30     /**
31      * List of all supported responses coming from AVR.
32      */
33     public enum ResponseType implements AvrResponse.AvrResponseType {
34         POWER_STATE("[0-2]", "PWR", "APR", "BPR", "ZEP"),
35         VOLUME_LEVEL("[0-9]{2,3}", "VOL", "ZV", "YV", "HZV"),
36         MUTE_STATE("[0-1]", "MUT", "Z2MUT", "Z3MUT", "HZM"),
37         INPUT_SOURCE_CHANNEL("[0-9]{2}", "FN", "Z2F", "Z3F", "ZEA"),
38         LISTENING_MODE("[0-9]{4}", "SR"),
39         PLAYING_LISTENING_MODE("[0-9a-f]{4}", "LM"),
40         DISPLAY_INFORMATION("[0-9a-fA-F]{30}", "FL"),
41         MCACC_MEMORY("[1-6]{1}", "MC");
42
43         private String[] responsePrefixZone;
44
45         private @Nullable String parameterPattern;
46
47         private Pattern[] matchPatternZone;
48
49         private ResponseType(@Nullable String parameterPattern, String... responsePrefixZone) {
50             this.responsePrefixZone = responsePrefixZone;
51             this.parameterPattern = parameterPattern;
52
53             matchPatternZone = new Pattern[responsePrefixZone.length];
54
55             for (int zoneIndex = 0; zoneIndex < responsePrefixZone.length; zoneIndex++) {
56                 String responsePrefix = responsePrefixZone[zoneIndex];
57                 matchPatternZone[zoneIndex] = Pattern
58                         .compile(responsePrefix + "(" + (parameterPattern == null ? "" : parameterPattern) + ")");
59             }
60         }
61
62         @Override
63         public String getResponsePrefix(int zone) {
64             return responsePrefixZone[zone - 1];
65         }
66
67         @Override
68         public boolean hasParameter() {
69             return parameterPattern != null && !parameterPattern.isEmpty();
70         }
71
72         @Override
73         public @Nullable String getParameterPattern() {
74             return parameterPattern;
75         }
76
77         @Override
78         public Integer match(String responseData) {
79             Integer zone = null;
80             // Check the response data against all zone prefixes.
81             for (int zoneIndex = 0; zoneIndex < matchPatternZone.length; zoneIndex++) {
82                 if (matchPatternZone[zoneIndex].matcher(responseData).matches()) {
83                     zone = zoneIndex + 1;
84                     break;
85                 }
86             }
87
88             return zone;
89         }
90
91         /**
92          * Return the parameter value of the given responseData.
93          *
94          * @param responseData
95          * @return
96          */
97         @Override
98         public String parseParameter(String responseData) {
99             String result = null;
100             // Check the response data against all zone prefixes.
101             for (int zoneIndex = 0; zoneIndex < matchPatternZone.length; zoneIndex++) {
102                 Matcher matcher = matchPatternZone[zoneIndex].matcher(responseData);
103                 if (matcher.find()) {
104                     result = matcher.group(1);
105                     break;
106                 }
107             }
108             return result;
109         }
110     }
111
112     private ResponseType responseType;
113
114     private Integer zone;
115
116     private String parameter;
117
118     public Response(String responseData) throws AvrConnectionException {
119         if (responseData == null || responseData.isEmpty()) {
120             throw new AvrConnectionException("responseData is empty. Cannot parse the response.");
121         }
122
123         parseResponseType(responseData);
124
125         if (this.responseType == null) {
126             throw new AvrConnectionException("Cannot find the responseType of the responseData " + responseData);
127         }
128
129         if (this.responseType.hasParameter()) {
130             this.parameter = this.responseType.parseParameter(responseData);
131         }
132     }
133
134     /**
135      * Parse the given response data and fill the
136      *
137      * @param responseData
138      * @return
139      */
140     private void parseResponseType(String responseData) {
141         for (ResponseType responseType : ResponseType.values()) {
142             zone = responseType.match(responseData);
143             if (zone != null) {
144                 this.responseType = responseType;
145                 break;
146             }
147         }
148     }
149
150     @Override
151     public ResponseType getResponseType() {
152         return this.responseType;
153     }
154
155     @Override
156     public String getParameterValue() {
157         return parameter;
158     }
159
160     @Override
161     public boolean hasParameter() {
162         return responseType.hasParameter();
163     }
164
165     @Override
166     public Integer getZone() {
167         return this.zone;
168     }
169 }