]> git.basschouten.com Git - openhab-addons.git/blob
b74e6f85f6a2997d35f4812bde0544a3f8b52b8b
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.apache.commons.lang.StringUtils;
16 import org.openhab.binding.pioneeravr.internal.protocol.avr.AvrCommand;
17 import org.openhab.binding.pioneeravr.internal.protocol.avr.AvrConnectionException;
18
19 /**
20  * A command which accept a parameter.
21  *
22  * @author Antoine Besnard - Initial contribution
23  * @author Leroy Foerster - Listening Mode, Playing Listening Mode
24  */
25 public class ParameterizedCommand extends SimpleCommand {
26
27     /**
28      * List of the commands with a parameter.
29      */
30     public enum ParameterizedCommandType implements AvrCommand.CommandType {
31
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");
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(int zone) {
46             return zoneCommands[zone - 1];
47         }
48
49         public String getParameterPattern() {
50             return parameterPattern;
51         }
52     }
53
54     private String parameter;
55
56     private String parameterPattern;
57
58     protected ParameterizedCommand(ParameterizedCommandType command, int zone) {
59         super(command, zone);
60         this.parameterPattern = command.getParameterPattern();
61     }
62
63     /**
64      * Return the command to send to the AVR with the parameter value configured.
65      *
66      * throws {@link AvrConnectionException} if the parameter is null, empty or has a bad format.
67      */
68     @Override
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.");
73         }
74
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);
78         }
79
80         return parameter + super.getCommand();
81     }
82
83     public ParameterizedCommand setParameter(String parameter) {
84         this.parameter = parameter;
85         return this;
86     }
87
88     public String getParameter() {
89         return this.parameter;
90     }
91
92     public String getParameterPattern() {
93         return parameterPattern;
94     }
95 }