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:
@@ -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.mpd.internal.action;
|
||||
|
||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||
import org.eclipse.jdt.annotation.Nullable;
|
||||
|
||||
/**
|
||||
* The {@link IMPDActions} interface defines rule actions for sending commands to a Music Player Daemon
|
||||
*
|
||||
* @author Stefan Röllin - Initial contribution
|
||||
*/
|
||||
@NonNullByDefault
|
||||
public interface IMPDActions {
|
||||
|
||||
public void sendCommand(@Nullable String command, @Nullable String parameter);
|
||||
|
||||
public void sendCommand(@Nullable String command);
|
||||
}
|
||||
@@ -12,9 +12,6 @@
|
||||
*/
|
||||
package org.openhab.binding.mpd.internal.action;
|
||||
|
||||
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.mpd.internal.handler.MPDHandler;
|
||||
@@ -33,7 +30,7 @@ import org.slf4j.LoggerFactory;
|
||||
*/
|
||||
@ThingActionsScope(name = "mpd")
|
||||
@NonNullByDefault
|
||||
public class MPDActions implements ThingActions, IMPDActions {
|
||||
public class MPDActions implements ThingActions {
|
||||
|
||||
private final Logger logger = LoggerFactory.getLogger(MPDActions.class);
|
||||
|
||||
@@ -51,8 +48,7 @@ public class MPDActions implements ThingActions, IMPDActions {
|
||||
return handler;
|
||||
}
|
||||
|
||||
@Override
|
||||
@RuleAction(label = "MPD : Send command", description = "Send a command to the Music Player Daemon.")
|
||||
@RuleAction(label = "send a command with a parameter", description = "Send a command to the Music Player Daemon.")
|
||||
public void sendCommand(@ActionInput(name = "command") @Nullable String command,
|
||||
@ActionInput(name = "parameter") @Nullable String parameter) {
|
||||
logger.debug("sendCommand called with {}", command);
|
||||
@@ -65,8 +61,7 @@ public class MPDActions implements ThingActions, IMPDActions {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@RuleAction(label = "MPD : Send command", description = "Send a command to the Music Player Daemon.")
|
||||
@RuleAction(label = "send a command", description = "Send a command to the Music Player Daemon.")
|
||||
public void sendCommand(@ActionInput(name = "command") @Nullable String command) {
|
||||
logger.debug("sendCommand called with {}", command);
|
||||
|
||||
@@ -78,31 +73,20 @@ public class MPDActions implements ThingActions, IMPDActions {
|
||||
}
|
||||
}
|
||||
|
||||
private static IMPDActions invokeMethodOf(@Nullable ThingActions actions) {
|
||||
if (actions == null) {
|
||||
throw new IllegalArgumentException("actions cannot be null");
|
||||
}
|
||||
if (actions.getClass().getName().equals(MPDActions.class.getName())) {
|
||||
if (actions instanceof IMPDActions) {
|
||||
return (IMPDActions) actions;
|
||||
} else {
|
||||
return (IMPDActions) Proxy.newProxyInstance(IMPDActions.class.getClassLoader(),
|
||||
new Class[] { IMPDActions.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 MPDActions");
|
||||
}
|
||||
|
||||
public static void sendCommand(@Nullable ThingActions actions, @Nullable String command,
|
||||
@Nullable String parameter) {
|
||||
invokeMethodOf(actions).sendCommand(command, parameter);
|
||||
if (actions instanceof MPDActions) {
|
||||
((MPDActions) actions).sendCommand(command, parameter);
|
||||
} else {
|
||||
throw new IllegalArgumentException("Actions is not an instance of MPDActions");
|
||||
}
|
||||
}
|
||||
|
||||
public static void sendCommand(@Nullable ThingActions actions, @Nullable String command) {
|
||||
invokeMethodOf(actions).sendCommand(command);
|
||||
if (actions instanceof MPDActions) {
|
||||
((MPDActions) actions).sendCommand(command);
|
||||
} else {
|
||||
throw new IllegalArgumentException("Actions is not an instance of MPDActions");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user