2 * Copyright (c) 2010-2021 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.pioneeravr.internal.protocol;
15 import java.util.regex.Matcher;
16 import java.util.regex.Pattern;
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;
23 * Represent an AVR response.
25 * @author Antoine Besnard - Initial contribution
26 * @author Leroy Foerster - Listening Mode, Playing Listening Mode
28 public class Response implements AvrResponse {
31 * List of all supported responses coming from AVR.
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");
42 private String[] responsePrefixZone;
44 private String parameterPattern;
46 private Pattern[] matchPatternZone;
48 private ResponseType(String parameterPattern, String... responsePrefixZone) {
49 this.responsePrefixZone = responsePrefixZone;
50 this.parameterPattern = parameterPattern;
52 matchPatternZone = new Pattern[responsePrefixZone.length];
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 : "") + ")");
62 public String getResponsePrefix(int zone) {
63 return responsePrefixZone[zone - 1];
67 public boolean hasParameter() {
68 return StringUtils.isNotEmpty(parameterPattern);
72 public String getParameterPattern() {
73 return parameterPattern;
77 public Integer match(String responseData) {
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()) {
91 * Return the parameter value of the given responseData.
97 public String parseParameter(String responseData) {
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);
111 private ResponseType responseType;
113 private Integer zone;
115 private String parameter;
117 public Response(String responseData) throws AvrConnectionException {
118 if (StringUtils.isEmpty(responseData)) {
119 throw new AvrConnectionException("responseData is empty. Cannot parse the response.");
122 parseResponseType(responseData);
124 if (this.responseType == null) {
125 throw new AvrConnectionException("Cannot find the responseType of the responseData " + responseData);
128 if (this.responseType.hasParameter()) {
129 this.parameter = this.responseType.parseParameter(responseData);
134 * Parse the given response data and fill the
136 * @param responseData
139 private void parseResponseType(String responseData) {
140 for (ResponseType responseType : ResponseType.values()) {
141 zone = responseType.match(responseData);
143 this.responseType = responseType;
150 public ResponseType getResponseType() {
151 return this.responseType;
155 public String getParameterValue() {
160 public boolean hasParameter() {
161 return responseType.hasParameter();
165 public Integer getZone() {