2 * Copyright (c) 2010-2022 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.eclipse.jdt.annotation.Nullable;
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"),
41 MCACC_MEMORY("[1-6]{1}", "MC");
43 private String[] responsePrefixZone;
45 private @Nullable String parameterPattern;
47 private Pattern[] matchPatternZone;
49 private ResponseType(@Nullable String parameterPattern, String... responsePrefixZone) {
50 this.responsePrefixZone = responsePrefixZone;
51 this.parameterPattern = parameterPattern;
53 matchPatternZone = new Pattern[responsePrefixZone.length];
55 for (int zoneIndex = 0; zoneIndex < responsePrefixZone.length; zoneIndex++) {
56 String responsePrefix = responsePrefixZone[zoneIndex];
57 matchPatternZone[zoneIndex] = Pattern
58 .compile(responsePrefix + "(" + (parameterPattern == null ? "" : parameterPattern) + ")");
63 public String getResponsePrefix(int zone) {
64 return responsePrefixZone[zone - 1];
68 public boolean hasParameter() {
69 return parameterPattern != null && !parameterPattern.isEmpty();
73 public @Nullable String getParameterPattern() {
74 return parameterPattern;
78 public Integer match(String responseData) {
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()) {
92 * Return the parameter value of the given responseData.
98 public String parseParameter(String responseData) {
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);
112 private ResponseType responseType;
114 private Integer zone;
116 private String parameter;
118 public Response(String responseData) throws AvrConnectionException {
119 if (responseData == null || responseData.isEmpty()) {
120 throw new AvrConnectionException("responseData is empty. Cannot parse the response.");
123 parseResponseType(responseData);
125 if (this.responseType == null) {
126 throw new AvrConnectionException("Cannot find the responseType of the responseData " + responseData);
129 if (this.responseType.hasParameter()) {
130 this.parameter = this.responseType.parseParameter(responseData);
135 * Parse the given response data and fill the
137 * @param responseData
140 private void parseResponseType(String responseData) {
141 for (ResponseType responseType : ResponseType.values()) {
142 zone = responseType.match(responseData);
144 this.responseType = responseType;
151 public ResponseType getResponseType() {
152 return this.responseType;
156 public String getParameterValue() {
161 public boolean hasParameter() {
162 return responseType.hasParameter();
166 public Integer getZone() {