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,32 +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.max.internal.actions;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.binding.max.actions.MaxCubeActions;
/**
* The {@link IMaxCubeActions} defines the interface for all thing actions supported by the binding.
* These methods, parameters, and return types are explained in {@link MaxCubeActions}.
*
* @author Christoph Weitkamp - Initial contribution
*/
@NonNullByDefault
public interface IMaxCubeActions {
Boolean backup();
Boolean resetConfig();
Boolean reboot();
}

View File

@@ -1,28 +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.max.internal.actions;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.binding.max.actions.MaxDevicesActions;
/**
* The {@link IMaxDevicesActions} defines the interface for all thing actions supported by the binding.
* These methods, parameters, and return types are explained in {@link MaxDevicesActions}.
*
* @author Christoph Weitkamp - Initial contribution
*/
@NonNullByDefault
public interface IMaxDevicesActions {
Boolean deleteFromCube();
}

View File

@@ -10,14 +10,10 @@
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.max.actions;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
package org.openhab.binding.max.internal.actions;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.max.internal.actions.IMaxCubeActions;
import org.openhab.binding.max.internal.handler.MaxCubeBridgeHandler;
import org.openhab.core.automation.annotation.ActionOutput;
import org.openhab.core.automation.annotation.RuleAction;
@@ -34,7 +30,7 @@ import org.slf4j.LoggerFactory;
*/
@ThingActionsScope(name = "max-cube")
@NonNullByDefault
public class MaxCubeActions implements ThingActions, IMaxCubeActions {
public class MaxCubeActions implements ThingActions {
private final Logger logger = LoggerFactory.getLogger(MaxCubeActions.class);
@@ -49,11 +45,10 @@ public class MaxCubeActions implements ThingActions, IMaxCubeActions {
@Override
public @Nullable ThingHandler getThingHandler() {
return this.handler;
return handler;
}
@Override
@RuleAction(label = "Backup Cube Data", description = "Creates a backup of the MAX! Cube data.")
@RuleAction(label = "backup the Cube data", description = "Creates a backup of the MAX! Cube data.")
public @ActionOutput(name = "success", type = "java.lang.Boolean") Boolean backup() {
MaxCubeBridgeHandler actionsHandler = handler;
if (actionsHandler == null) {
@@ -65,11 +60,14 @@ public class MaxCubeActions implements ThingActions, IMaxCubeActions {
}
public static boolean backup(@Nullable ThingActions actions) {
return invokeMethodOf(actions).backup();
if (actions instanceof MaxCubeActions) {
return ((MaxCubeActions) actions).backup();
} else {
throw new IllegalArgumentException("Actions is not an instance of MaxCubeActions");
}
}
@Override
@RuleAction(label = "Reset Cube Configuration", description = "Resets the MAX! Cube room and device information. Devices will need to be included again!")
@RuleAction(label = "reset the Cube configuration", description = "Resets the MAX! Cube room and device information. Devices will need to be included again!")
public @ActionOutput(name = "success", type = "java.lang.Boolean") Boolean resetConfig() {
MaxCubeBridgeHandler actionsHandler = handler;
if (actionsHandler == null) {
@@ -81,11 +79,14 @@ public class MaxCubeActions implements ThingActions, IMaxCubeActions {
}
public static boolean reset(@Nullable ThingActions actions) {
return invokeMethodOf(actions).resetConfig();
if (actions instanceof MaxCubeActions) {
return ((MaxCubeActions) actions).resetConfig();
} else {
throw new IllegalArgumentException("Actions is not an instance of MaxCubeActions");
}
}
@Override
@RuleAction(label = "Restart Cube", description = "Restarts the MAX! Cube.")
@RuleAction(label = "restart the Cube", description = "Restarts the MAX! Cube.")
public @ActionOutput(name = "success", type = "java.lang.Boolean") Boolean reboot() {
MaxCubeBridgeHandler actionsHandler = handler;
if (actionsHandler == null) {
@@ -97,25 +98,10 @@ public class MaxCubeActions implements ThingActions, IMaxCubeActions {
}
public static boolean reboot(@Nullable ThingActions actions) {
return invokeMethodOf(actions).reboot();
}
private static IMaxCubeActions invokeMethodOf(@Nullable ThingActions actions) {
if (actions == null) {
throw new IllegalArgumentException("actions cannot be null");
if (actions instanceof MaxCubeActions) {
return ((MaxCubeActions) actions).reboot();
} else {
throw new IllegalArgumentException("Actions is not an instance of MaxCubeActions");
}
if (actions.getClass().getName().equals(MaxCubeActions.class.getName())) {
if (actions instanceof IMaxCubeActions) {
return (IMaxCubeActions) actions;
} else {
return (IMaxCubeActions) Proxy.newProxyInstance(IMaxCubeActions.class.getClassLoader(),
new Class[] { IMaxCubeActions.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 MaxCubeActions");
}
}

View File

@@ -10,14 +10,10 @@
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.max.actions;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
package org.openhab.binding.max.internal.actions;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.max.internal.actions.IMaxDevicesActions;
import org.openhab.binding.max.internal.handler.MaxDevicesHandler;
import org.openhab.core.automation.annotation.ActionOutput;
import org.openhab.core.automation.annotation.RuleAction;
@@ -34,7 +30,7 @@ import org.slf4j.LoggerFactory;
*/
@ThingActionsScope(name = "max-devices")
@NonNullByDefault
public class MaxDevicesActions implements ThingActions, IMaxDevicesActions {
public class MaxDevicesActions implements ThingActions {
private final Logger logger = LoggerFactory.getLogger(MaxDevicesActions.class);
@@ -49,11 +45,10 @@ public class MaxDevicesActions implements ThingActions, IMaxDevicesActions {
@Override
public @Nullable ThingHandler getThingHandler() {
return this.handler;
return handler;
}
@Override
@RuleAction(label = "Delete Device from Cube", description = "Deletes the device from the MAX! Cube. Device will need to be included again!")
@RuleAction(label = "delete the device from the Cube", description = "Deletes the device from the MAX! Cube. Device will need to be included again!")
public @ActionOutput(name = "success", type = "java.lang.Boolean") Boolean deleteFromCube() {
MaxDevicesHandler actionsHandler = handler;
if (actionsHandler == null) {
@@ -65,25 +60,10 @@ public class MaxDevicesActions implements ThingActions, IMaxDevicesActions {
}
public static boolean deleteFromCube(@Nullable ThingActions actions) {
return invokeMethodOf(actions).deleteFromCube();
}
private static IMaxDevicesActions invokeMethodOf(@Nullable ThingActions actions) {
if (actions == null) {
throw new IllegalArgumentException("actions cannot be null");
if (actions instanceof MaxDevicesActions) {
return ((MaxDevicesActions) actions).deleteFromCube();
} else {
throw new IllegalArgumentException("Actions is not an instance of MaxDevicesActions");
}
if (actions.getClass().getName().equals(MaxDevicesActions.class.getName())) {
if (actions instanceof IMaxDevicesActions) {
return (IMaxDevicesActions) actions;
} else {
return (IMaxDevicesActions) Proxy.newProxyInstance(IMaxDevicesActions.class.getClassLoader(),
new Class[] { IMaxDevicesActions.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 MaxDevicesActions");
}
}

View File

@@ -49,9 +49,9 @@ import java.util.stream.Stream;
import javax.measure.quantity.Temperature;
import org.openhab.binding.max.actions.MaxCubeActions;
import org.openhab.binding.max.internal.MaxBackupUtils;
import org.openhab.binding.max.internal.MaxBindingConstants;
import org.openhab.binding.max.internal.actions.MaxCubeActions;
import org.openhab.binding.max.internal.command.ACommand;
import org.openhab.binding.max.internal.command.CCommand;
import org.openhab.binding.max.internal.command.CubeCommand;

View File

@@ -27,7 +27,7 @@ import java.util.Map.Entry;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import org.openhab.binding.max.actions.MaxDevicesActions;
import org.openhab.binding.max.internal.actions.MaxDevicesActions;
import org.openhab.binding.max.internal.command.CCommand;
import org.openhab.binding.max.internal.command.QCommand;
import org.openhab.binding.max.internal.command.SConfigCommand;