Rework ThingActions (#8732)
* Remove proxy workarounds * Move ThingActions and a few other classes into the internal package * Use more consistent action labels/descriptions Related to: * openhab/openhab-core#1714 * openhab/openhab-core#1639 Signed-off-by: Wouter Born <github@maindrain.net>
This commit is contained in:
@@ -12,9 +12,6 @@
|
||||
*/
|
||||
package org.openhab.binding.alarmdecoder.internal.actions;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Proxy;
|
||||
|
||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||
import org.eclipse.jdt.annotation.Nullable;
|
||||
import org.openhab.binding.alarmdecoder.internal.handler.ADBridgeHandler;
|
||||
@@ -33,7 +30,7 @@ import org.slf4j.LoggerFactory;
|
||||
*/
|
||||
@ThingActionsScope(name = "alarmdecoder")
|
||||
@NonNullByDefault
|
||||
public class BridgeActions implements ThingActions, IBridgeActions {
|
||||
public class BridgeActions implements ThingActions {
|
||||
|
||||
private final Logger logger = LoggerFactory.getLogger(BridgeActions.class);
|
||||
|
||||
@@ -58,8 +55,7 @@ public class BridgeActions implements ThingActions, IBridgeActions {
|
||||
/**
|
||||
* Reboot thing action
|
||||
*/
|
||||
@Override
|
||||
@RuleAction(label = "Reboot", description = "Reboot the Alarm Decoder device")
|
||||
@RuleAction(label = "reboot the device", description = "Reboot the Alarm Decoder device.")
|
||||
public void reboot() {
|
||||
ADBridgeHandler bridge = this.bridge;
|
||||
if (bridge != null) {
|
||||
@@ -72,34 +68,10 @@ public class BridgeActions implements ThingActions, IBridgeActions {
|
||||
|
||||
// Static method for Rules DSL backward compatibility
|
||||
public static void reboot(@Nullable ThingActions actions) {
|
||||
// if (actions instanceof BridgeActions) {
|
||||
// ((BridgeActions) actions).reboot();
|
||||
// } else {
|
||||
// throw new IllegalArgumentException("Instance is not a BridgeActions class.");
|
||||
// }
|
||||
invokeMethodOf(actions).reboot(); // Remove and uncomment above when core issue #1536 is fixed
|
||||
}
|
||||
|
||||
/**
|
||||
* This is only necessary to work around a bug in openhab-core (issue #1536). It should be removed once that is
|
||||
* resolved.
|
||||
*/
|
||||
private static IBridgeActions invokeMethodOf(@Nullable ThingActions actions) {
|
||||
if (actions == null) {
|
||||
throw new IllegalArgumentException("actions cannot be null");
|
||||
if (actions instanceof BridgeActions) {
|
||||
((BridgeActions) actions).reboot();
|
||||
} else {
|
||||
throw new IllegalArgumentException("Actions is not an instance of BridgeActions");
|
||||
}
|
||||
if (actions.getClass().getName().equals(BridgeActions.class.getName())) {
|
||||
if (actions instanceof IBridgeActions) {
|
||||
return (IBridgeActions) actions;
|
||||
} else {
|
||||
return (IBridgeActions) Proxy.newProxyInstance(IBridgeActions.class.getClassLoader(),
|
||||
new Class[] { IBridgeActions.class }, (Object proxy, Method method, Object[] args) -> {
|
||||
Method m = actions.getClass().getDeclaredMethod(method.getName(),
|
||||
method.getParameterTypes());
|
||||
return m.invoke(actions, args);
|
||||
});
|
||||
}
|
||||
}
|
||||
throw new IllegalArgumentException("Actions is not an instance of BridgeActions");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,29 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2010-2020 Contributors to the openHAB project
|
||||
*
|
||||
* See the NOTICE file(s) distributed with this work for additional
|
||||
* information.
|
||||
*
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*/
|
||||
package org.openhab.binding.alarmdecoder.internal.actions;
|
||||
|
||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||
|
||||
/**
|
||||
* The {@link IBridgeActions} defines the interface for all thing actions supported by the bridges.
|
||||
* This is only necessary to work around a bug in openhab-core (issue #1536). It should be removed once that is
|
||||
* resolved.
|
||||
*
|
||||
* @author Bob Adair - Initial contribution
|
||||
*
|
||||
*/
|
||||
@NonNullByDefault
|
||||
public interface IBridgeActions {
|
||||
|
||||
public void reboot();
|
||||
}
|
||||
Reference in New Issue
Block a user