2 * Copyright (c) 2010-2023 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 org.openhab.binding.pioneeravr.internal.protocol.avr.AvrCommand;
16 import org.openhab.binding.pioneeravr.internal.protocol.avr.AvrConnectionException;
19 * A command which accept a parameter.
21 * @author Antoine Besnard - Initial contribution
22 * @author Leroy Foerster - Listening Mode, Playing Listening Mode
24 public class ParameterizedCommand extends SimpleCommand {
27 * List of the commands with a parameter.
29 public enum ParameterizedCommandType implements AvrCommand.CommandType {
31 VOLUME_SET("[0-9]{2,3}", "VL", "ZV", "YV", "HZV"),
32 INPUT_CHANNEL_SET("[0-9]{2}", "FN", "ZS", "ZT", "ZEA"),
33 LISTENING_MODE_SET("[0-9]{4}", "SR"),
34 MCACC_MEMORY_SET("[1-6]{1}", "MC");
36 private String[] zoneCommands;
37 private String parameterPattern;
39 private ParameterizedCommandType(String parameterPattern, String... zoneCommands) {
40 this.zoneCommands = zoneCommands;
41 this.parameterPattern = parameterPattern;
45 public String getCommand() {
46 return zoneCommands[0];
50 public String getCommand(int zone) {
51 return zoneCommands[zone - 1];
54 public String getParameterPattern() {
55 return parameterPattern;
59 private String parameter;
61 private String parameterPattern;
63 protected ParameterizedCommand(ParameterizedCommandType command) {
67 protected ParameterizedCommand(ParameterizedCommandType command, int zone) {
69 this.parameterPattern = command.getParameterPattern();
73 * Return the command to send to the AVR with the parameter value configured.
75 * throws {@link AvrConnectionException} if the parameter is null, empty or has a bad format.
78 public String getCommand() throws AvrConnectionException {
79 if (parameter == null) {
80 throw new AvrConnectionException(
81 "The parameter of the command " + super.getCommand() + " must not be null.");
84 if (parameterPattern != null && !parameterPattern.isEmpty() && !parameter.matches(parameterPattern)) {
85 throw new AvrConnectionException("The parameter value " + parameter + " of the command "
86 + super.getCommand() + " does not match the pattern " + parameterPattern);
89 return parameter + super.getCommand();
92 public ParameterizedCommand setParameter(String parameter) {
93 this.parameter = parameter;
97 public String getParameter() {
98 return this.parameter;
101 public String getParameterPattern() {
102 return parameterPattern;