]> git.basschouten.com Git - openhab-addons.git/blob
8502eef1cd286ccb61f0eda6e44f102b48e21962
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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
35         private String[] zoneCommands;
36         private String parameterPattern;
37
38         private ParameterizedCommandType(String parameterPattern, String... zoneCommands) {
39             this.zoneCommands = zoneCommands;
40             this.parameterPattern = parameterPattern;
41         }
42
43         @Override
44         public String getCommand(int zone) {
45             return zoneCommands[zone - 1];
46         }
47
48         public String getParameterPattern() {
49             return parameterPattern;
50         }
51     }
52
53     private String parameter;
54
55     private String parameterPattern;
56
57     protected ParameterizedCommand(ParameterizedCommandType command, int zone) {
58         super(command, zone);
59         this.parameterPattern = command.getParameterPattern();
60     }
61
62     /**
63      * Return the command to send to the AVR with the parameter value configured.
64      *
65      * throws {@link AvrConnectionException} if the parameter is null, empty or has a bad format.
66      */
67     @Override
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.");
72         }
73
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);
77         }
78
79         return parameter + super.getCommand();
80     }
81
82     public ParameterizedCommand setParameter(String parameter) {
83         this.parameter = parameter;
84         return this;
85     }
86
87     public String getParameter() {
88         return this.parameter;
89     }
90
91     public String getParameterPattern() {
92         return parameterPattern;
93     }
94 }