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

@@ -10,22 +10,16 @@
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.avmfritz.actions;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
package org.openhab.binding.avmfritz.internal.actions;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.avmfritz.internal.actions.IAVMFritzHeatingActions;
import org.openhab.binding.avmfritz.internal.handler.AVMFritzHeatingActionsHandler;
import org.openhab.core.automation.annotation.ActionInput;
import org.openhab.core.automation.annotation.RuleAction;
import org.openhab.core.thing.binding.ThingActions;
import org.openhab.core.thing.binding.ThingActionsScope;
import org.openhab.core.thing.binding.ThingHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* The {@link AVMFritzHeatingActions} defines thing actions for heating devices / groups of the avmfritz binding.
@@ -34,9 +28,7 @@ import org.slf4j.LoggerFactory;
*/
@ThingActionsScope(name = "avmfritz")
@NonNullByDefault
public class AVMFritzHeatingActions implements ThingActions, IAVMFritzHeatingActions {
private final Logger logger = LoggerFactory.getLogger(AVMFritzHeatingActions.class);
public class AVMFritzHeatingActions implements ThingActions {
private @Nullable AVMFritzHeatingActionsHandler handler;
@@ -50,7 +42,6 @@ public class AVMFritzHeatingActions implements ThingActions, IAVMFritzHeatingAct
return handler;
}
@Override
@RuleAction(label = "@text/setBoostModeModeActionLabel", description = "@text/setBoostModeActionDescription")
public void setBoostMode(
@ActionInput(name = "Duration", label = "@text/setBoostModeDurationInputLabel", description = "@text/setBoostModeDurationInputDescription", type = "java.lang.Long", required = true) @Nullable Long duration) {
@@ -65,10 +56,13 @@ public class AVMFritzHeatingActions implements ThingActions, IAVMFritzHeatingAct
}
public static void setBoostMode(@Nullable ThingActions actions, @Nullable Long duration) {
invokeMethodOf(actions).setBoostMode(duration);
if (actions instanceof AVMFritzHeatingActions) {
((AVMFritzHeatingActions) actions).setBoostMode(duration);
} else {
throw new IllegalArgumentException("Actions is not an instance of AVMFritzHeatingActions");
}
}
@Override
@RuleAction(label = "@text/setWindowOpenModeActionLabel", description = "@text/setWindowOpenModeActionDescription")
public void setWindowOpenMode(
@ActionInput(name = "Duration", label = "@text/setWindowOpenModeDurationInputLabel", description = "@text/setWindowOpenModeDurationInputDescription", type = "java.lang.Long", required = true) @Nullable Long duration) {
@@ -83,25 +77,10 @@ public class AVMFritzHeatingActions implements ThingActions, IAVMFritzHeatingAct
}
public static void setWindowOpenMode(@Nullable ThingActions actions, @Nullable Long duration) {
invokeMethodOf(actions).setWindowOpenMode(duration);
}
private static IAVMFritzHeatingActions invokeMethodOf(@Nullable ThingActions actions) {
if (actions == null) {
throw new IllegalArgumentException("actions cannot be null");
if (actions instanceof AVMFritzHeatingActions) {
((AVMFritzHeatingActions) actions).setWindowOpenMode(duration);
} else {
throw new IllegalArgumentException("Actions is not an instance of AVMFritzHeatingActions");
}
if (actions.getClass().getName().equals(AVMFritzHeatingActions.class.getName())) {
if (actions instanceof IAVMFritzHeatingActions) {
return (IAVMFritzHeatingActions) actions;
} else {
return (IAVMFritzHeatingActions) Proxy.newProxyInstance(IAVMFritzHeatingActions.class.getClassLoader(),
new Class[] { IAVMFritzHeatingActions.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 AVMFritzHeatingActions");
}
}

View File

@@ -1,31 +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.avmfritz.internal.actions;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.avmfritz.actions.AVMFritzHeatingActions;
/**
* The {@link IAVMFritzHeatingActions} defines the interface for all thing actions supported by the binding.
* These methods, parameters, and return types are explained in {@link AVMFritzHeatingActions}.
*
* @author Christoph Weitkamp - Initial contribution
*/
@NonNullByDefault
public interface IAVMFritzHeatingActions {
void setBoostMode(@Nullable Long duration);
void setWindowOpenMode(@Nullable Long duration);
}

View File

@@ -16,7 +16,7 @@ import java.util.Collection;
import java.util.Collections;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.binding.avmfritz.actions.AVMFritzHeatingActions;
import org.openhab.binding.avmfritz.internal.actions.AVMFritzHeatingActions;
import org.openhab.core.thing.binding.ThingHandler;
import org.openhab.core.thing.binding.ThingHandlerService;

View File

@@ -10,7 +10,7 @@
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.avmfritz.actions;
package org.openhab.binding.avmfritz.internal.actions;
import static org.junit.jupiter.api.Assertions.assertThrows;