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:
Wouter Born
2020-10-14 19:01:12 +02:00
committed by GitHub
parent 4adc214c69
commit bef1046258
86 changed files with 708 additions and 2096 deletions

View File

@@ -1,27 +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.kaleidescape.internal;
import org.eclipse.jdt.annotation.NonNullByDefault;
/**
* The {@link IKaleidescapeThingActions} defines the interface for all thing actions supported by the binding.
* These methods, parameters, and return types are explained in {@link KaleidescapeThingActions}.
*
* @author Michael Lobstein - Initial contribution
*/
@NonNullByDefault
public interface IKaleidescapeThingActions {
void sendKCommand(String kCommand);
}

View File

@@ -12,9 +12,6 @@
*/
package org.openhab.binding.kaleidescape.internal;
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.kaleidescape.internal.handler.KaleidescapeHandler;
@@ -30,16 +27,15 @@ import org.slf4j.LoggerFactory;
* Some automation actions to be used with a {@link KaleidescapeThingActions}
*
* @author Michael Lobstein - initial contribution
*
*/
@ThingActionsScope(name = "kaleidescape")
@NonNullByDefault
public class KaleidescapeThingActions implements ThingActions, IKaleidescapeThingActions {
public class KaleidescapeThingActions implements ThingActions {
private final Logger logger = LoggerFactory.getLogger(KaleidescapeThingActions.class);
private @Nullable KaleidescapeHandler handler;
@RuleAction(label = "sendKCommand", description = "Action that sends raw command to the kaleidescape zone")
@RuleAction(label = "send a raw command", description = "Action that sends raw command to the kaleidescape zone.")
public void sendKCommand(@ActionInput(name = "sendKCommand") String kCommand) {
KaleidescapeHandler localHandler = handler;
if (localHandler != null) {
@@ -52,7 +48,11 @@ public class KaleidescapeThingActions implements ThingActions, IKaleidescapeThin
/** Static alias to support the old DSL rules engine and make the action available there. */
public static void sendKCommand(@Nullable ThingActions actions, String kCommand) throws IllegalArgumentException {
invokeMethodOf(actions).sendKCommand(kCommand);
if (actions instanceof KaleidescapeThingActions) {
((KaleidescapeThingActions) actions).sendKCommand(kCommand);
} else {
throw new IllegalArgumentException("Actions is not an instance of KaleidescapeThingActions");
}
}
@Override
@@ -62,27 +62,6 @@ public class KaleidescapeThingActions implements ThingActions, IKaleidescapeThin
@Override
public @Nullable ThingHandler getThingHandler() {
return this.handler;
}
private static IKaleidescapeThingActions invokeMethodOf(@Nullable ThingActions actions) {
if (actions == null) {
throw new IllegalArgumentException("actions cannot be null");
}
if (actions.getClass().getName().equals(KaleidescapeThingActions.class.getName())) {
if (actions instanceof KaleidescapeThingActions) {
return (IKaleidescapeThingActions) actions;
} else {
return (IKaleidescapeThingActions) Proxy.newProxyInstance(
IKaleidescapeThingActions.class.getClassLoader(),
new Class[] { IKaleidescapeThingActions.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 KaleidescapeThingActions");
return handler;
}
}