]> git.basschouten.com Git - openhab-addons.git/blob
14310ddeb2a1c0f749ce2c2c5961fbc98df38f80
[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.lutron.action;
14
15 import java.lang.reflect.Method;
16 import java.lang.reflect.Proxy;
17 import java.math.BigDecimal;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.lutron.internal.handler.DimmerHandler;
22 import org.openhab.binding.lutron.internal.protocol.LutronDuration;
23 import org.openhab.core.automation.annotation.ActionInput;
24 import org.openhab.core.automation.annotation.RuleAction;
25 import org.openhab.core.thing.binding.ThingActions;
26 import org.openhab.core.thing.binding.ThingActionsScope;
27 import org.openhab.core.thing.binding.ThingHandler;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 /**
32  * The {@link DimmerActions} defines thing actions for DimmerHandler.
33  *
34  * @author Bob Adair - Initial contribution
35  */
36 @ThingActionsScope(name = "lutron")
37 @NonNullByDefault
38 public class DimmerActions implements ThingActions, IDimmerActions {
39     private final Logger logger = LoggerFactory.getLogger(DimmerActions.class);
40
41     private @Nullable DimmerHandler handler;
42
43     public DimmerActions() {
44         logger.trace("Lutron Dimmer actions service created");
45     }
46
47     @Override
48     public void setThingHandler(@Nullable ThingHandler handler) {
49         if (handler instanceof DimmerHandler) {
50             this.handler = (DimmerHandler) handler;
51         }
52     }
53
54     @Override
55     public @Nullable ThingHandler getThingHandler() {
56         return handler;
57     }
58
59     /**
60      * The setLevel dimmer thing action
61      */
62     @Override
63     @RuleAction(label = "setLevel", description = "Send set level command with fade and delay times")
64     public void setLevel(
65             @ActionInput(name = "level", label = "Dimmer Level", description = "New dimmer level (0-100)") @Nullable Double level,
66             @ActionInput(name = "fadeTime", label = "Fade Time", description = "Time to fade to new level (seconds)") @Nullable Double fadeTime,
67             @ActionInput(name = "delayTime", label = "Delay Time", description = "Delay before starting fade (seconds)") @Nullable Double delayTime) {
68         DimmerHandler dimmerHandler = handler;
69         if (dimmerHandler == null) {
70             logger.debug("Handler not set for Dimmer thing actions.");
71             return;
72         }
73         if (level == null) {
74             logger.debug("Ignoring setLevel command due to null level value.");
75             return;
76         }
77         if (fadeTime == null) {
78             logger.debug("Ignoring setLevel command due to null value for fadeTime.");
79             return;
80         }
81         if (delayTime == null) {
82             logger.debug("Ignoring setLevel command due to null value for delayTime.");
83             return;
84         }
85
86         Double lightLevel = level;
87         if (lightLevel > 100.0) {
88             lightLevel = 100.0;
89         } else if (lightLevel < 0.0) {
90             lightLevel = 0.0;
91         }
92         try {
93             dimmerHandler.setLightLevel(new BigDecimal(lightLevel).setScale(2, BigDecimal.ROUND_HALF_UP),
94                     new LutronDuration(fadeTime), new LutronDuration(delayTime));
95         } catch (IllegalArgumentException e) {
96             logger.debug("Ignoring setLevel command due to illegal argument exception: {}", e.getMessage());
97         }
98     }
99
100     /**
101      * Static setLevel method for Rules DSL backward compatibility
102      */
103     public static void setLevel(@Nullable ThingActions actions, @Nullable Double level, @Nullable Double fadeTime,
104             @Nullable Double delayTime) {
105         invokeMethodOf(actions).setLevel(level, fadeTime, delayTime); // Replace when core issue #1536 is fixed
106     }
107
108     /**
109      * This is only necessary to work around a bug in openhab-core (issue #1536). It should be removed once that is
110      * resolved.
111      */
112     private static IDimmerActions invokeMethodOf(@Nullable ThingActions actions) {
113         if (actions == null) {
114             throw new IllegalArgumentException("actions cannot be null");
115         }
116         if (actions.getClass().getName().equals(DimmerActions.class.getName())) {
117             if (actions instanceof IDimmerActions) {
118                 return (IDimmerActions) actions;
119             } else {
120                 return (IDimmerActions) Proxy.newProxyInstance(IDimmerActions.class.getClassLoader(),
121                         new Class[] { IDimmerActions.class }, (Object proxy, Method method, Object[] args) -> {
122                             Method m = actions.getClass().getDeclaredMethod(method.getName(),
123                                     method.getParameterTypes());
124                             return m.invoke(actions, args);
125                         });
126             }
127         }
128         throw new IllegalArgumentException("Actions is not an instance of DimmerActions");
129     }
130 }