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,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.hue.internal.action;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.library.types.DecimalType;
import org.openhab.core.types.Command;
/**
* The {@link ILightActions} defines the interface for all thing actions supported by the binding.
*
* @author Laurent Garnier - Initial contribution
*/
@NonNullByDefault
public interface ILightActions {
public void fadingLightCommand(@Nullable String channel, @Nullable Command command, @Nullable DecimalType fadeTime);
}

View File

@@ -12,9 +12,6 @@
*/
package org.openhab.binding.hue.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.hue.internal.handler.HueLightHandler;
@@ -30,18 +27,13 @@ import org.slf4j.LoggerFactory;
/**
* The {@link LightActions} defines the thing actions for the hue binding.
* <p>
* <b>Note:</b>The static method <b>invokeMethodOf</b> handles the case where
* the test <i>actions instanceof LightActions</i> fails. This test can fail
* due to an issue in openHAB core v2.5.0 where the {@link LightActions} class
* can be loaded by a different classloader than the <i>actions</i> instance.
*
* @author Jochen Leopold - Initial contribution
* @author Laurent Garnier - new method invokeMethodOf + interface ILightActions
*/
@ThingActionsScope(name = "hue")
@NonNullByDefault
public class LightActions implements ThingActions, ILightActions {
public class LightActions implements ThingActions {
private final Logger logger = LoggerFactory.getLogger(LightActions.class);
private @Nullable HueLightHandler handler;
@@ -52,10 +44,9 @@ public class LightActions implements ThingActions, ILightActions {
@Override
public @Nullable ThingHandler getThingHandler() {
return this.handler;
return handler;
}
@Override
@RuleAction(label = "@text/actionLabel", description = "@text/actionDesc")
public void fadingLightCommand(
@ActionInput(name = "channel", label = "@text/actionInputChannelLabel", description = "@text/actionInputChannelDesc") @Nullable String channel,
@@ -85,27 +76,12 @@ public class LightActions implements ThingActions, ILightActions {
logger.debug("send LightAction to {} with {}ms of fadeTime", channel, fadeTime);
}
private static ILightActions invokeMethodOf(@Nullable ThingActions actions) {
if (actions == null) {
throw new IllegalArgumentException("actions cannot be null");
}
if (actions.getClass().getName().equals(LightActions.class.getName())) {
if (actions instanceof ILightActions) {
return (ILightActions) actions;
} else {
return (ILightActions) Proxy.newProxyInstance(ILightActions.class.getClassLoader(),
new Class[] { ILightActions.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 LightActions");
}
public static void fadingLightCommand(@Nullable ThingActions actions, @Nullable String channel,
@Nullable Command command, @Nullable DecimalType fadeTime) {
invokeMethodOf(actions).fadingLightCommand(channel, command, fadeTime);
if (actions instanceof LightActions) {
((LightActions) actions).fadingLightCommand(channel, command, fadeTime);
} else {
throw new IllegalArgumentException("Actions is not an instance of LightActions");
}
}
}