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 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");
35 private String[] zoneCommands;
36 private String parameterPattern;
38 private ParameterizedCommandType(String parameterPattern, String... zoneCommands) {
39 this.zoneCommands = zoneCommands;
40 this.parameterPattern = parameterPattern;
44 public String getCommand(int zone) {
45 return zoneCommands[zone - 1];
48 public String getParameterPattern() {
49 return parameterPattern;
53 private String parameter;
55 private String parameterPattern;
57 protected ParameterizedCommand(ParameterizedCommandType command, int zone) {
59 this.parameterPattern = command.getParameterPattern();
63 * Return the command to send to the AVR with the parameter value configured.
65 * throws {@link AvrConnectionException} if the parameter is null, empty or has a bad format.
68 public String getCommand() throws AvrConnectionException {
69 if (parameter == null) {
70 throw new AvrConnectionException(
71 "The parameter of the command " + super.getCommand() + " must not be null.");
74 if (parameterPattern != null && !parameterPattern.isEmpty() && !parameter.matches(parameterPattern)) {
75 throw new AvrConnectionException("The parameter value " + parameter + " of the command "
76 + super.getCommand() + " does not match the pattern " + parameterPattern);
79 return parameter + super.getCommand();
82 public ParameterizedCommand setParameter(String parameter) {
83 this.parameter = parameter;
87 public String getParameter() {
88 return this.parameter;
91 public String getParameterPattern() {
92 return parameterPattern;