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