]> git.basschouten.com Git - openhab-addons.git/blob
5aa5c3b2366d670b4f5de6f60bc72d93bea1b95d
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.pioneeravr.internal.protocol;
14
15 import org.openhab.binding.pioneeravr.internal.protocol.avr.AvrCommand;
16 import org.openhab.binding.pioneeravr.internal.protocol.avr.AvrConnectionException;
17
18 /**
19  * A command which accept a parameter.
20  *
21  * @author Antoine Besnard - Initial contribution
22  * @author Leroy Foerster - Listening Mode, Playing Listening Mode
23  */
24 public class ParameterizedCommand extends SimpleCommand {
25
26     /**
27      * List of the commands with a parameter.
28      */
29     public enum ParameterizedCommandType implements AvrCommand.CommandType {
30
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");
35
36         private String[] zoneCommands;
37         private String parameterPattern;
38
39         private ParameterizedCommandType(String parameterPattern, String... zoneCommands) {
40             this.zoneCommands = zoneCommands;
41             this.parameterPattern = parameterPattern;
42         }
43
44         @Override
45         public String getCommand() {
46             return zoneCommands[0];
47         }
48
49         @Override
50         public String getCommand(int zone) {
51             return zoneCommands[zone - 1];
52         }
53
54         public String getParameterPattern() {
55             return parameterPattern;
56         }
57     }
58
59     private String parameter;
60
61     private String parameterPattern;
62
63     protected ParameterizedCommand(ParameterizedCommandType command) {
64         this(command, 0);
65     }
66
67     protected ParameterizedCommand(ParameterizedCommandType command, int zone) {
68         super(command, zone);
69         this.parameterPattern = command.getParameterPattern();
70     }
71
72     /**
73      * Return the command to send to the AVR with the parameter value configured.
74      *
75      * throws {@link AvrConnectionException} if the parameter is null, empty or has a bad format.
76      */
77     @Override
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.");
82         }
83
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);
87         }
88
89         return parameter + super.getCommand();
90     }
91
92     public ParameterizedCommand setParameter(String parameter) {
93         this.parameter = parameter;
94         return this;
95     }
96
97     public String getParameter() {
98         return this.parameter;
99     }
100
101     public String getParameterPattern() {
102         return parameterPattern;
103     }
104 }