]> git.basschouten.com Git - openhab-addons.git/blob
9997ca97ef264df81fb87060ca66563973885fb3
[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.lutron.internal.protocol;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.openhab.binding.lutron.internal.handler.LeapBridgeHandler;
18 import org.openhab.binding.lutron.internal.protocol.leap.LeapCommand;
19 import org.openhab.binding.lutron.internal.protocol.lip.LutronCommandType;
20 import org.openhab.binding.lutron.internal.protocol.lip.LutronOperation;
21 import org.openhab.binding.lutron.internal.protocol.lip.TargetType;
22
23 /**
24  * Generic LIP command for use inside bridge handler
25  *
26  * @author Bob Adair - Initial contribution
27  */
28 @NonNullByDefault
29 public class LIPCommand extends LutronCommandNew {
30     private final Object[] parameters;
31
32     public LIPCommand(TargetType targetType, LutronOperation operation, LutronCommandType CommandType,
33             @Nullable Integer integrationId, Object... parameters) {
34         super(targetType, operation, CommandType, integrationId);
35         this.parameters = parameters;
36     }
37
38     @Override
39     public String lipCommand() {
40         StringBuilder builder = new StringBuilder().append(operation).append(commandType);
41         if (integrationId != null) {
42             builder.append(',').append(integrationId);
43         }
44         if (parameters != null) { // This CAN be null
45             for (Object parameter : parameters) {
46                 builder.append(',').append(parameter);
47             }
48         }
49
50         return builder.toString();
51     }
52
53     @Override
54     public @Nullable LeapCommand leapCommand(LeapBridgeHandler bridgeHandler, @Nullable Integer leapZone) {
55         return null;
56     }
57
58     @Override
59     public String toString() {
60         return lipCommand();
61     }
62
63     public int getNumberParameter(int position) {
64         if (parameters.length > position && parameters[position] instanceof Number num) {
65             return num.intValue();
66         } else {
67             throw new IllegalArgumentException("Invalid command parameter");
68         }
69     }
70 }