]> git.basschouten.com Git - openhab-addons.git/blob
6ed96f2f37d8decf4a361b177ce6eae1dcf79b9c
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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 java.util.Locale;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.binding.lutron.internal.handler.LeapBridgeHandler;
20 import org.openhab.binding.lutron.internal.protocol.leap.CommandType;
21 import org.openhab.binding.lutron.internal.protocol.leap.LeapCommand;
22 import org.openhab.binding.lutron.internal.protocol.leap.Request;
23 import org.openhab.binding.lutron.internal.protocol.lip.LutronCommandType;
24 import org.openhab.binding.lutron.internal.protocol.lip.LutronOperation;
25 import org.openhab.binding.lutron.internal.protocol.lip.TargetType;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 /**
30  * Lutron OUTPUT command object
31  *
32  * @author Bob Adair - Initial contribution
33  */
34 @NonNullByDefault
35 public class OutputCommand extends LutronCommandNew {
36     // shade, blind, dimmer defs
37     public static final Integer ACTION_ZONELEVEL = 1;
38     public static final Integer ACTION_STARTRAISING = 2;
39     public static final Integer ACTION_STARTLOWERING = 3;
40     public static final Integer ACTION_STOP = 4;
41     public static final Integer ACTION_POSITION_UPDATE = 32; // For shades/blinds. Undocumented in protocol guide.
42
43     // blind defs
44     public static final Integer ACTION_LIFTLEVEL = 1;
45     public static final Integer ACTION_TILTLEVEL = 9;
46     public static final Integer ACTION_LIFTTILTLEVEL = 10;
47     public static final Integer ACTION_STARTRAISINGTILT = 11;
48     public static final Integer ACTION_STARTLOWERINGTILT = 12;
49     public static final Integer ACTION_STOPTILT = 13;
50     public static final Integer ACTION_STARTRAISINGLIFT = 14;
51     public static final Integer ACTION_STARTLOWERINGLIFT = 15;
52     public static final Integer ACTION_STOPLIFT = 16;
53
54     // cco defs
55     public static final Integer ACTION_STATE = 1;
56     public static final Integer ACTION_PULSE = 6;
57
58     private final Logger logger = LoggerFactory.getLogger(OutputCommand.class);
59
60     private final Integer action;
61     private final @Nullable Number parameter;
62     private final @Nullable LutronDuration fadeTime;
63     private final @Nullable LutronDuration delayTime;
64     private final FanSpeedType fanSpeed;
65
66     /**
67      * OutputCommand constructor
68      *
69      * @param targetType
70      * @param operation
71      * @param integrationId
72      * @param action
73      * @param parameter
74      * @param fadeTime
75      * @param delayTime
76      */
77     public OutputCommand(TargetType targetType, LutronOperation operation, Integer integrationId, Integer action,
78             @Nullable Number parameter, @Nullable LutronDuration fadeTime, @Nullable LutronDuration delayTime) {
79         super(targetType, operation, LutronCommandType.OUTPUT, integrationId);
80         this.action = action;
81         this.parameter = parameter;
82         if (parameter != null) {
83             this.fanSpeed = FanSpeedType.toFanSpeedType(parameter.intValue());
84         } else {
85             this.fanSpeed = FanSpeedType.OFF;
86         }
87         this.fadeTime = fadeTime;
88         this.delayTime = delayTime;
89     }
90
91     /**
92      * OutputCommand constructor for fan commands
93      *
94      * @param targetType
95      * @param operation
96      * @param integrationId
97      * @param action
98      * @param fanSpeed
99      * @param fadeTime
100      * @param delayTime
101      */
102     public OutputCommand(TargetType targetType, LutronOperation operation, Integer integrationId, Integer action,
103             FanSpeedType fanSpeed, @Nullable LutronDuration fadeTime, @Nullable LutronDuration delayTime) {
104         super(targetType, operation, LutronCommandType.OUTPUT, integrationId);
105         this.action = action;
106         this.fanSpeed = fanSpeed;
107         this.parameter = fanSpeed.speed();
108         this.fadeTime = fadeTime;
109         this.delayTime = delayTime;
110     }
111
112     @Override
113     public String lipCommand() {
114         StringBuilder builder = new StringBuilder().append(operation).append(commandType);
115         builder.append(',').append(integrationId);
116         builder.append(',').append(action);
117
118         if (parameter != null && targetType == TargetType.CCO && action.equals(OutputCommand.ACTION_PULSE)) {
119             builder.append(',').append(String.format(Locale.ROOT, "%.2f", parameter));
120         } else if (parameter != null) {
121             builder.append(',').append(parameter);
122         }
123
124         if (fadeTime != null) {
125             builder.append(',').append(fadeTime);
126         } else if (fadeTime == null && delayTime != null) {
127             // must add 0 placeholder here in order to set delay time
128             builder.append(',').append("0");
129         }
130         if (delayTime != null) {
131             builder.append(',').append(delayTime);
132         }
133
134         return builder.toString();
135     }
136
137     @Override
138     public @Nullable LeapCommand leapCommand(LeapBridgeHandler bridgeHandler, @Nullable Integer leapZone) {
139         int zone;
140         Number parameter = this.parameter;
141
142         if (leapZone == null) {
143             return null;
144         } else {
145             zone = leapZone;
146         }
147
148         if (operation == LutronOperation.QUERY) {
149             if (action.equals(OutputCommand.ACTION_ZONELEVEL)) {
150                 return new LeapCommand(Request.getZoneStatus(zone));
151             } else {
152                 logger.debug("Ignoring unsupported query action");
153                 return null;
154             }
155         } else if (operation == LutronOperation.EXECUTE) {
156             if (targetType == TargetType.SWITCH) {
157                 if (action.equals(OutputCommand.ACTION_ZONELEVEL) && parameter != null) {
158                     return new LeapCommand(Request.goToLevel(zone, parameter.intValue()));
159                 } else {
160                     logger.debug("Ignoring unsupported switch action");
161                     return null;
162                 }
163             } else if (targetType == TargetType.DIMMER) {
164                 if (action.equals(OutputCommand.ACTION_ZONELEVEL) && parameter != null) {
165                     if (fadeTime == null && delayTime == null) {
166                         return new LeapCommand(Request.goToLevel(zone, parameter.intValue()));
167                     } else {
168                         LutronDuration fade = (fadeTime == null) ? new LutronDuration(0) : fadeTime;
169                         LutronDuration delay = (delayTime == null) ? new LutronDuration(0) : delayTime;
170                         return new LeapCommand(Request.goToDimmedLevel(zone, parameter.intValue(), fade.asLeapString(),
171                                 delay.asLeapString()));
172                     }
173                 } else {
174                     logger.debug("Ignoring unsupported dimmer action");
175                     return null;
176                 }
177             } else if (targetType == TargetType.FAN) {
178                 if (action.equals(OutputCommand.ACTION_ZONELEVEL)) {
179                     return new LeapCommand(Request.goToFanSpeed(zone, fanSpeed));
180                 } else {
181                     logger.debug("Ignoring unsupported fan action");
182                     return null;
183                 }
184             } else if (targetType == TargetType.SHADE) {
185                 if (action.equals(OutputCommand.ACTION_ZONELEVEL) && parameter != null) {
186                     return new LeapCommand(Request.goToLevel(zone, parameter.intValue()));
187                 } else if (action.equals(OutputCommand.ACTION_STARTRAISING)) {
188                     return new LeapCommand(Request.zoneCommand(zone, CommandType.RAISE));
189                 } else if (action.equals(OutputCommand.ACTION_STARTLOWERING)) {
190                     return new LeapCommand(Request.zoneCommand(zone, CommandType.LOWER));
191                 } else if (action.equals(OutputCommand.ACTION_STOP)) {
192                     return new LeapCommand(Request.zoneCommand(zone, CommandType.STOP));
193                 } else {
194                     logger.debug("Ignoring unsupported shade action");
195                     return null;
196                 }
197             } else {
198                 logger.debug("Ignoring unsupported target type: {}", targetType);
199                 return null;
200             }
201         } else {
202             logger.debug("Ignoring unsupported operation: {}", operation);
203             return null;
204         }
205     }
206
207     @Override
208     public String toString() {
209         return lipCommand();
210     }
211 }