]> git.basschouten.com Git - openhab-addons.git/blob
238ace9368d8a5d1b423eaa8f2df1cdd08382d88
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.apache.commons.lang.StringUtils;
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
42         private String[] responsePrefixZone;
43
44         private String parameterPattern;
45
46         private Pattern[] matchPatternZone;
47
48         private ResponseType(String parameterPattern, String... responsePrefixZone) {
49             this.responsePrefixZone = responsePrefixZone;
50             this.parameterPattern = parameterPattern;
51
52             matchPatternZone = new Pattern[responsePrefixZone.length];
53
54             for (int zoneIndex = 0; zoneIndex < responsePrefixZone.length; zoneIndex++) {
55                 String responsePrefix = responsePrefixZone[zoneIndex];
56                 matchPatternZone[zoneIndex] = Pattern.compile(responsePrefix + "("
57                         + (StringUtils.isNotEmpty(parameterPattern) ? parameterPattern : "") + ")");
58             }
59         }
60
61         @Override
62         public String getResponsePrefix(int zone) {
63             return responsePrefixZone[zone - 1];
64         }
65
66         @Override
67         public boolean hasParameter() {
68             return StringUtils.isNotEmpty(parameterPattern);
69         }
70
71         @Override
72         public String getParameterPattern() {
73             return parameterPattern;
74         }
75
76         @Override
77         public Integer match(String responseData) {
78             Integer zone = null;
79             // Check the response data against all zone prefixes.
80             for (int zoneIndex = 0; zoneIndex < matchPatternZone.length; zoneIndex++) {
81                 if (matchPatternZone[zoneIndex].matcher(responseData).matches()) {
82                     zone = zoneIndex + 1;
83                     break;
84                 }
85             }
86
87             return zone;
88         }
89
90         /**
91          * Return the parameter value of the given responseData.
92          *
93          * @param responseData
94          * @return
95          */
96         @Override
97         public String parseParameter(String responseData) {
98             String result = null;
99             // Check the response data against all zone prefixes.
100             for (int zoneIndex = 0; zoneIndex < matchPatternZone.length; zoneIndex++) {
101                 Matcher matcher = matchPatternZone[zoneIndex].matcher(responseData);
102                 if (matcher.find()) {
103                     result = matcher.group(1);
104                     break;
105                 }
106             }
107             return result;
108         }
109     }
110
111     private ResponseType responseType;
112
113     private Integer zone;
114
115     private String parameter;
116
117     public Response(String responseData) throws AvrConnectionException {
118         if (StringUtils.isEmpty(responseData)) {
119             throw new AvrConnectionException("responseData is empty. Cannot parse the response.");
120         }
121
122         parseResponseType(responseData);
123
124         if (this.responseType == null) {
125             throw new AvrConnectionException("Cannot find the responseType of the responseData " + responseData);
126         }
127
128         if (this.responseType.hasParameter()) {
129             this.parameter = this.responseType.parseParameter(responseData);
130         }
131     }
132
133     /**
134      * Parse the given response data and fill the
135      *
136      * @param responseData
137      * @return
138      */
139     private void parseResponseType(String responseData) {
140         for (ResponseType responseType : ResponseType.values()) {
141             zone = responseType.match(responseData);
142             if (zone != null) {
143                 this.responseType = responseType;
144                 break;
145             }
146         }
147     }
148
149     @Override
150     public ResponseType getResponseType() {
151         return this.responseType;
152     }
153
154     @Override
155     public String getParameterValue() {
156         return parameter;
157     }
158
159     @Override
160     public boolean hasParameter() {
161         return responseType.hasParameter();
162     }
163
164     @Override
165     public Integer getZone() {
166         return this.zone;
167     }
168 }