2 * Copyright (c) 2010-2020 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.apache.commons.lang.StringUtils;
16 import org.openhab.binding.pioneeravr.internal.protocol.avr.AvrCommand;
17 import org.openhab.binding.pioneeravr.internal.protocol.avr.AvrConnectionException;
20 * A command which accept a parameter.
22 * @author Antoine Besnard - Initial contribution
23 * @author Leroy Foerster - Listening Mode, Playing Listening Mode
25 public class ParameterizedCommand extends SimpleCommand {
28 * List of the commands with a parameter.
30 public enum ParameterizedCommandType implements AvrCommand.CommandType {
32 VOLUME_SET("[0-9]{2,3}", "VL", "ZV", "YV", "HZV"),
33 INPUT_CHANNEL_SET("[0-9]{2}", "FN", "ZS", "ZT", "ZEA"),
34 LISTENING_MODE_SET("[0-9]{4}", "SR");
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(int zone) {
46 return zoneCommands[zone - 1];
49 public String getParameterPattern() {
50 return parameterPattern;
54 private String parameter;
56 private String parameterPattern;
58 protected ParameterizedCommand(ParameterizedCommandType command, int zone) {
60 this.parameterPattern = command.getParameterPattern();
64 * Return the command to send to the AVR with the parameter value configured.
66 * throws {@link AvrConnectionException} if the parameter is null, empty or has a bad format.
69 public String getCommand() throws AvrConnectionException {
70 if (parameter == null) {
71 throw new AvrConnectionException(
72 "The parameter of the command " + super.getCommand() + " must not be null.");
75 if (StringUtils.isNotEmpty(parameterPattern) && !parameter.matches(parameterPattern)) {
76 throw new AvrConnectionException("The parameter value " + parameter + " of the command "
77 + super.getCommand() + " does not match the pattern " + parameterPattern);
80 return parameter + super.getCommand();
83 public ParameterizedCommand setParameter(String parameter) {
84 this.parameter = parameter;
88 public String getParameter() {
89 return this.parameter;
92 public String getParameterPattern() {
93 return parameterPattern;