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,30 +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.lutron.action;
|
||||
|
||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||
import org.eclipse.jdt.annotation.Nullable;
|
||||
|
||||
/**
|
||||
* The {@link IDimmerActions} interface defines the interface for all thing actions supported by the dimmer thing.
|
||||
* 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 IDimmerActions {
|
||||
|
||||
public void setLevel(@Nullable Double level, @Nullable Double fadeTime, @Nullable Double delayTime);
|
||||
}
|
||||
@@ -10,10 +10,8 @@
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*/
|
||||
package org.openhab.binding.lutron.action;
|
||||
package org.openhab.binding.lutron.internal.action;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Proxy;
|
||||
import java.math.BigDecimal;
|
||||
import java.math.RoundingMode;
|
||||
|
||||
@@ -36,7 +34,7 @@ import org.slf4j.LoggerFactory;
|
||||
*/
|
||||
@ThingActionsScope(name = "lutron")
|
||||
@NonNullByDefault
|
||||
public class DimmerActions implements ThingActions, IDimmerActions {
|
||||
public class DimmerActions implements ThingActions {
|
||||
private final Logger logger = LoggerFactory.getLogger(DimmerActions.class);
|
||||
|
||||
private @Nullable DimmerHandler handler;
|
||||
@@ -60,8 +58,7 @@ public class DimmerActions implements ThingActions, IDimmerActions {
|
||||
/**
|
||||
* The setLevel dimmer thing action
|
||||
*/
|
||||
@Override
|
||||
@RuleAction(label = "setLevel", description = "Send set level command with fade and delay times")
|
||||
@RuleAction(label = "send a set level command", description = "Send set level command with fade and delay times.")
|
||||
public void setLevel(
|
||||
@ActionInput(name = "level", label = "Dimmer Level", description = "New dimmer level (0-100)") @Nullable Double level,
|
||||
@ActionInput(name = "fadeTime", label = "Fade Time", description = "Time to fade to new level (seconds)") @Nullable Double fadeTime,
|
||||
@@ -103,29 +100,10 @@ public class DimmerActions implements ThingActions, IDimmerActions {
|
||||
*/
|
||||
public static void setLevel(@Nullable ThingActions actions, @Nullable Double level, @Nullable Double fadeTime,
|
||||
@Nullable Double delayTime) {
|
||||
invokeMethodOf(actions).setLevel(level, fadeTime, delayTime); // Replace 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 IDimmerActions invokeMethodOf(@Nullable ThingActions actions) {
|
||||
if (actions == null) {
|
||||
throw new IllegalArgumentException("actions cannot be null");
|
||||
if (actions instanceof DimmerActions) {
|
||||
((DimmerActions) actions).setLevel(level, fadeTime, delayTime);
|
||||
} else {
|
||||
throw new IllegalArgumentException("Actions is not an instance of DimmerActions");
|
||||
}
|
||||
if (actions.getClass().getName().equals(DimmerActions.class.getName())) {
|
||||
if (actions instanceof IDimmerActions) {
|
||||
return (IDimmerActions) actions;
|
||||
} else {
|
||||
return (IDimmerActions) Proxy.newProxyInstance(IDimmerActions.class.getClassLoader(),
|
||||
new Class[] { IDimmerActions.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 DimmerActions");
|
||||
}
|
||||
}
|
||||
@@ -19,7 +19,7 @@ import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
import org.openhab.binding.lutron.action.DimmerActions;
|
||||
import org.openhab.binding.lutron.internal.action.DimmerActions;
|
||||
import org.openhab.binding.lutron.internal.config.DimmerConfig;
|
||||
import org.openhab.binding.lutron.internal.protocol.LutronCommandType;
|
||||
import org.openhab.binding.lutron.internal.protocol.LutronDuration;
|
||||
|
||||
Reference in New Issue
Block a user