]> git.basschouten.com Git - openhab-addons.git/blob
bbcd10401726cf13e96eb61eccc7008d29e46e17
[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.alarmdecoder.internal.actions;
14
15 import java.lang.reflect.Method;
16 import java.lang.reflect.Proxy;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.openhab.binding.alarmdecoder.internal.handler.ADBridgeHandler;
21 import org.openhab.binding.alarmdecoder.internal.protocol.ADCommand;
22 import org.openhab.core.automation.annotation.RuleAction;
23 import org.openhab.core.thing.binding.ThingActions;
24 import org.openhab.core.thing.binding.ThingActionsScope;
25 import org.openhab.core.thing.binding.ThingHandler;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 /**
30  * The {@link BridgeActions} class defines thing actions for alarmdecoder bridges.
31  *
32  * @author Bob Adair - Initial contribution
33  */
34 @ThingActionsScope(name = "alarmdecoder")
35 @NonNullByDefault
36 public class BridgeActions implements ThingActions, IBridgeActions {
37
38     private final Logger logger = LoggerFactory.getLogger(BridgeActions.class);
39
40     private @Nullable ADBridgeHandler bridge;
41
42     public BridgeActions() {
43         logger.trace("Alarm Decoder bridge actions service created");
44     }
45
46     @Override
47     public void setThingHandler(@Nullable ThingHandler handler) {
48         if (handler instanceof ADBridgeHandler) {
49             this.bridge = (ADBridgeHandler) handler;
50         }
51     }
52
53     @Override
54     public @Nullable ThingHandler getThingHandler() {
55         return bridge;
56     }
57
58     /**
59      * Reboot thing action
60      */
61     @Override
62     @RuleAction(label = "Reboot", description = "Reboot the Alarm Decoder device")
63     public void reboot() {
64         ADBridgeHandler bridge = this.bridge;
65         if (bridge != null) {
66             bridge.sendADCommand(ADCommand.reboot());
67             logger.debug("Sending reboot command.");
68         } else {
69             logger.debug("Request for reboot action, but bridge is undefined.");
70         }
71     }
72
73     // Static method for Rules DSL backward compatibility
74     public static void reboot(@Nullable ThingActions actions) {
75         // if (actions instanceof BridgeActions) {
76         // ((BridgeActions) actions).reboot();
77         // } else {
78         // throw new IllegalArgumentException("Instance is not a BridgeActions class.");
79         // }
80         invokeMethodOf(actions).reboot(); // Remove and uncomment above when core issue #1536 is fixed
81     }
82
83     /**
84      * This is only necessary to work around a bug in openhab-core (issue #1536). It should be removed once that is
85      * resolved.
86      */
87     private static IBridgeActions invokeMethodOf(@Nullable ThingActions actions) {
88         if (actions == null) {
89             throw new IllegalArgumentException("actions cannot be null");
90         }
91         if (actions.getClass().getName().equals(BridgeActions.class.getName())) {
92             if (actions instanceof IBridgeActions) {
93                 return (IBridgeActions) actions;
94             } else {
95                 return (IBridgeActions) Proxy.newProxyInstance(IBridgeActions.class.getClassLoader(),
96                         new Class[] { IBridgeActions.class }, (Object proxy, Method method, Object[] args) -> {
97                             Method m = actions.getClass().getDeclaredMethod(method.getName(),
98                                     method.getParameterTypes());
99                             return m.invoke(actions, args);
100                         });
101             }
102         }
103         throw new IllegalArgumentException("Actions is not an instance of BridgeActions");
104     }
105 }