2 * Copyright (c) 2010-2020 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.alarmdecoder.internal.actions;
15 import java.lang.reflect.Method;
16 import java.lang.reflect.Proxy;
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;
30 * The {@link BridgeActions} class defines thing actions for alarmdecoder bridges.
32 * @author Bob Adair - Initial contribution
34 @ThingActionsScope(name = "alarmdecoder")
36 public class BridgeActions implements ThingActions, IBridgeActions {
38 private final Logger logger = LoggerFactory.getLogger(BridgeActions.class);
40 private @Nullable ADBridgeHandler bridge;
42 public BridgeActions() {
43 logger.trace("Alarm Decoder bridge actions service created");
47 public void setThingHandler(@Nullable ThingHandler handler) {
48 if (handler instanceof ADBridgeHandler) {
49 this.bridge = (ADBridgeHandler) handler;
54 public @Nullable ThingHandler getThingHandler() {
62 @RuleAction(label = "Reboot", description = "Reboot the Alarm Decoder device")
63 public void reboot() {
64 ADBridgeHandler bridge = this.bridge;
66 bridge.sendADCommand(ADCommand.reboot());
67 logger.debug("Sending reboot command.");
69 logger.debug("Request for reboot action, but bridge is undefined.");
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();
78 // throw new IllegalArgumentException("Instance is not a BridgeActions class.");
80 invokeMethodOf(actions).reboot(); // Remove and uncomment above when core issue #1536 is fixed
84 * This is only necessary to work around a bug in openhab-core (issue #1536). It should be removed once that is
87 private static IBridgeActions invokeMethodOf(@Nullable ThingActions actions) {
88 if (actions == null) {
89 throw new IllegalArgumentException("actions cannot be null");
91 if (actions.getClass().getName().equals(BridgeActions.class.getName())) {
92 if (actions instanceof IBridgeActions) {
93 return (IBridgeActions) actions;
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);
103 throw new IllegalArgumentException("Actions is not an instance of BridgeActions");