]> git.basschouten.com Git - openhab-addons.git/blob
f8435dc1ef084ed3f4e26c0cc9b13de65fd1cd10
[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.CommandType;
19 import org.openhab.binding.lutron.internal.protocol.leap.LeapCommand;
20 import org.openhab.binding.lutron.internal.protocol.leap.Request;
21 import org.openhab.binding.lutron.internal.protocol.lip.LutronCommandType;
22 import org.openhab.binding.lutron.internal.protocol.lip.LutronOperation;
23 import org.openhab.binding.lutron.internal.protocol.lip.TargetType;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 /**
28  * Lutron DEVICE command object
29  *
30  * @author Bob Adair - Initial contribution
31  */
32 @NonNullByDefault
33 public class DeviceCommand extends LutronCommandNew {
34     // keypad defs
35     public static final Integer ACTION_PRESS = 3;
36     public static final Integer ACTION_RELEASE = 4;
37     public static final Integer ACTION_HOLD = 5;
38     public static final Integer ACTION_LED_STATE = 9;
39     public static final Integer LED_OFF = 0;
40     public static final Integer LED_ON = 1;
41     public static final Integer LED_FLASH = 2; // Same as 1 on RA2 keypads
42     public static final Integer LED_RAPIDFLASH = 3; // Same as 1 on RA2 keypads
43
44     // occupancy sensor defs
45     public static final String OCCUPIED_STATE_COMPONENT = "2";
46     public static final String STATE_OCCUPIED = "3";
47     public static final String STATE_UNOCCUPIED = "4";
48
49     private final Logger logger = LoggerFactory.getLogger(DeviceCommand.class);
50
51     private final Integer component;
52     private final @Nullable Integer leapComponent;
53     private final Integer action;
54     private final @Nullable Object parameter;
55
56     public DeviceCommand(TargetType targetType, LutronOperation operation, Integer integrationId, Integer component,
57             @Nullable Integer leapComponent, Integer action, @Nullable Object parameter) {
58         super(targetType, operation, LutronCommandType.DEVICE, integrationId);
59         this.action = action;
60         this.component = component;
61         this.leapComponent = leapComponent;
62         this.parameter = parameter;
63     }
64
65     @Override
66     public String lipCommand() {
67         StringBuilder builder = new StringBuilder().append(operation).append(commandType);
68         builder.append(',').append(integrationId);
69         builder.append(',').append(component);
70         builder.append(',').append(action);
71         if (parameter != null) {
72             builder.append(',').append(parameter);
73         }
74
75         return builder.toString();
76     }
77
78     @Override
79     public @Nullable LeapCommand leapCommand(LeapBridgeHandler bridgeHandler, @Nullable Integer leapZone) {
80         if (targetType == TargetType.KEYPAD) {
81             if (leapComponent == null) {
82                 logger.debug("Ignoring device command. No leap component in command.");
83                 return null;
84             }
85
86             Integer integrationId = this.integrationId;
87             Integer leapComponent = this.leapComponent; // make the broken null checker happy
88             if (action.equals(DeviceCommand.ACTION_PRESS) && integrationId != null && leapComponent != null) {
89                 int button = bridgeHandler.getButton(integrationId, leapComponent);
90                 if (button > 0) {
91                     return new LeapCommand(Request.buttonCommand(button, CommandType.PRESSANDHOLD));
92                 }
93             } else if (action.equals(DeviceCommand.ACTION_RELEASE) && integrationId != null && leapComponent != null) {
94                 int button = bridgeHandler.getButton(integrationId, leapComponent);
95                 if (button > 0) {
96                     return new LeapCommand(Request.buttonCommand(button, CommandType.RELEASE));
97                 }
98             } else {
99                 logger.debug("Ignoring device command with unsupported action.");
100                 return null;
101             }
102         } else if (targetType == TargetType.VIRTUALKEYPAD) {
103             if (action.equals(DeviceCommand.ACTION_PRESS)) {
104                 return new LeapCommand(Request.virtualButtonCommand(component, CommandType.PRESSANDRELEASE));
105             } else if (action.equals(DeviceCommand.ACTION_RELEASE)) {
106                 logger.trace("Ignoring release command for virtual keypad button.");
107                 return null;
108             } else {
109                 logger.debug("Ignoring device command with unsupported action.");
110                 return null;
111             }
112         } else {
113             logger.debug("Ignoring device command with unsupported target type.");
114             return null;
115         }
116         logger.debug("Ignoring unsupported device command.");
117         return null;
118     }
119
120     @Override
121     public String toString() {
122         return lipCommand();
123     }
124 }