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.dmx.action;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
/**
* The {@link IDmxActions} defines the actions for DMX Bridges
*
* @author Laurent Garnier - Initial contribution
*/
@NonNullByDefault
public interface IDmxActions {
public void sendFade(@Nullable String channels, @Nullable String fade, @Nullable Boolean resumeAfter);
}

View File

@@ -21,7 +21,7 @@ import java.util.List;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import org.openhab.binding.dmx.action.DmxActions;
import org.openhab.binding.dmx.internal.action.DmxActions;
import org.openhab.binding.dmx.internal.action.FadeAction;
import org.openhab.binding.dmx.internal.action.ResumeAction;
import org.openhab.binding.dmx.internal.config.DmxBridgeHandlerConfiguration;

View File

@@ -10,10 +10,7 @@
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.dmx.action;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
package org.openhab.binding.dmx.internal.action;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
@@ -28,29 +25,23 @@ import org.slf4j.LoggerFactory;
/**
* The {@link DmxActions} provides actions for DMX Bridges
* <p>
* <b>Note:</b>The static method <b>invokeMethodOf</b> handles the case where
* the test <i>actions instanceof DmxActions</i> fails. This test can fail
* due to an issue in openHAB core v2.5.0 where the {@link DmxActions} class
* can be loaded by a different classloader than the <i>actions</i> instance.
*
* @author Jan N. Klug - Initial contribution
*/
@ThingActionsScope(name = "dmx")
@NonNullByDefault
public class DmxActions implements ThingActions, IDmxActions {
public class DmxActions implements ThingActions {
private final Logger logger = LoggerFactory.getLogger(DmxActions.class);
private @Nullable DmxBridgeHandler handler;
@Override
@RuleAction(label = "DMX Output", description = "immediately performs fade on selected DMX channels")
@RuleAction(label = "immediately fade channels", description = "Immediately performs fade on selected DMX channels.")
public void sendFade(@ActionInput(name = "channels") @Nullable String channels,
@ActionInput(name = "fade") @Nullable String fade,
@ActionInput(name = "resumeAfter") @Nullable Boolean resumeAfter) {
logger.debug("thingHandlerAction called with inputs: {} {} {}", channels, fade, resumeAfter);
DmxBridgeHandler handler = this.handler;
if (handler == null) {
logger.warn("DMX Action service ThingHandler is null!");
@@ -77,26 +68,11 @@ public class DmxActions implements ThingActions, IDmxActions {
public static void sendFade(@Nullable ThingActions actions, @Nullable String channels, @Nullable String fade,
@Nullable Boolean resumeAfter) {
invokeMethodOf(actions).sendFade(channels, fade, resumeAfter);
}
private static IDmxActions invokeMethodOf(@Nullable ThingActions actions) {
if (actions == null) {
throw new IllegalArgumentException("actions cannot be null");
if (actions instanceof DmxActions) {
((DmxActions) actions).sendFade(channels, fade, resumeAfter);
} else {
throw new IllegalArgumentException("Actions is not an instance of DmxActions");
}
if (actions.getClass().getName().equals(DmxActions.class.getName())) {
if (actions instanceof IDmxActions) {
return (IDmxActions) actions;
} else {
return (IDmxActions) Proxy.newProxyInstance(IDmxActions.class.getClassLoader(),
new Class[] { IDmxActions.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 DmxActions");
}
@Override
@@ -108,6 +84,6 @@ public class DmxActions implements ThingActions, IDmxActions {
@Override
public @Nullable ThingHandler getThingHandler() {
return this.handler;
return handler;
}
}