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:
@@ -12,9 +12,6 @@
|
||||
*/
|
||||
package org.openhab.binding.automower.internal.actions;
|
||||
|
||||
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.automower.internal.things.AutomowerCommand;
|
||||
@@ -32,7 +29,7 @@ import org.slf4j.LoggerFactory;
|
||||
*/
|
||||
@ThingActionsScope(name = "automower")
|
||||
@NonNullByDefault
|
||||
public class AutomowerActions implements ThingActions, IAutomowerActions {
|
||||
public class AutomowerActions implements ThingActions {
|
||||
private final Logger logger = LoggerFactory.getLogger(AutomowerActions.class);
|
||||
private @Nullable AutomowerHandler handler;
|
||||
|
||||
@@ -46,7 +43,6 @@ public class AutomowerActions implements ThingActions, IAutomowerActions {
|
||||
return handler;
|
||||
}
|
||||
|
||||
@Override
|
||||
@RuleAction(label = "@text/action-start-label", description = "@text/action-start-desc")
|
||||
public void start(
|
||||
@ActionInput(name = "duration", label = "@text/action-input-duration-label", description = "@text/action-input-duration-desc") int durationMin) {
|
||||
@@ -59,10 +55,13 @@ public class AutomowerActions implements ThingActions, IAutomowerActions {
|
||||
}
|
||||
|
||||
public static void start(@Nullable ThingActions actions, int durationMin) {
|
||||
invokeMethodOf(actions).start(durationMin);
|
||||
if (actions instanceof AutomowerActions) {
|
||||
((AutomowerActions) actions).start(durationMin);
|
||||
} else {
|
||||
throw new IllegalArgumentException("Actions is not an instance of AutomowerActions");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@RuleAction(label = "@text/action-pause-label", description = "@text/action-pause-desc")
|
||||
public void pause() {
|
||||
AutomowerHandler automowerHandler = handler;
|
||||
@@ -74,10 +73,13 @@ public class AutomowerActions implements ThingActions, IAutomowerActions {
|
||||
}
|
||||
|
||||
public static void pause(@Nullable ThingActions actions) {
|
||||
invokeMethodOf(actions).pause();
|
||||
if (actions instanceof AutomowerActions) {
|
||||
((AutomowerActions) actions).pause();
|
||||
} else {
|
||||
throw new IllegalArgumentException("Actions is not an instance of AutomowerActions");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@RuleAction(label = "@text/action-parkuntilnextschedule-label", description = "@text/action-parkuntilnextschedule-desc")
|
||||
public void parkUntilNextSchedule() {
|
||||
AutomowerHandler automowerHandler = handler;
|
||||
@@ -89,10 +91,13 @@ public class AutomowerActions implements ThingActions, IAutomowerActions {
|
||||
}
|
||||
|
||||
public static void parkUntilNextSchedule(@Nullable ThingActions actions) {
|
||||
invokeMethodOf(actions).parkUntilNextSchedule();
|
||||
if (actions instanceof AutomowerActions) {
|
||||
((AutomowerActions) actions).parkUntilNextSchedule();
|
||||
} else {
|
||||
throw new IllegalArgumentException("Actions is not an instance of AutomowerActions");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@RuleAction(label = "@text/action-parkuntilfurthernotice-label", description = "@text/action-parkuntilfurthernotice-desc")
|
||||
public void parkUntilFurtherNotice() {
|
||||
AutomowerHandler automowerHandler = handler;
|
||||
@@ -104,10 +109,13 @@ public class AutomowerActions implements ThingActions, IAutomowerActions {
|
||||
}
|
||||
|
||||
public static void parkUntilFurtherNotice(@Nullable ThingActions actions) {
|
||||
invokeMethodOf(actions).parkUntilFurtherNotice();
|
||||
if (actions instanceof AutomowerActions) {
|
||||
((AutomowerActions) actions).parkUntilFurtherNotice();
|
||||
} else {
|
||||
throw new IllegalArgumentException("Actions is not an instance of AutomowerActions");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@RuleAction(label = "@text/action-park-label", description = "@text/action-park-desc")
|
||||
public void park(
|
||||
@ActionInput(name = "duration", label = "@text/action-input-duration-label", description = "@text/action-input-duration-desc") int durationMin) {
|
||||
@@ -120,10 +128,13 @@ public class AutomowerActions implements ThingActions, IAutomowerActions {
|
||||
}
|
||||
|
||||
public static void park(@Nullable ThingActions actions, int durationMin) {
|
||||
invokeMethodOf(actions).park(durationMin);
|
||||
if (actions instanceof AutomowerActions) {
|
||||
((AutomowerActions) actions).park(durationMin);
|
||||
} else {
|
||||
throw new IllegalArgumentException("Actions is not an instance of AutomowerActions");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@RuleAction(label = "@text/action-resumeschedule-label", description = "@text/action-resumeschedule-desc")
|
||||
public void resumeSchedule() {
|
||||
AutomowerHandler automowerHandler = handler;
|
||||
@@ -135,25 +146,10 @@ public class AutomowerActions implements ThingActions, IAutomowerActions {
|
||||
}
|
||||
|
||||
public static void resumeSchedule(@Nullable ThingActions actions) {
|
||||
invokeMethodOf(actions).resumeSchedule();
|
||||
}
|
||||
|
||||
private static IAutomowerActions invokeMethodOf(@Nullable ThingActions actions) {
|
||||
if (actions == null) {
|
||||
throw new IllegalArgumentException("actions cannot be null");
|
||||
if (actions instanceof AutomowerActions) {
|
||||
((AutomowerActions) actions).resumeSchedule();
|
||||
} else {
|
||||
throw new IllegalArgumentException("Actions is not an instance of AutomowerActions");
|
||||
}
|
||||
if (actions.getClass().getName().equals(AutomowerActions.class.getName())) {
|
||||
if (actions instanceof AutomowerActions) {
|
||||
return (IAutomowerActions) actions;
|
||||
} else {
|
||||
return (IAutomowerActions) Proxy.newProxyInstance(IAutomowerActions.class.getClassLoader(),
|
||||
new Class[] { IAutomowerActions.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 IAutomowerActions");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,36 +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.automower.internal.actions;
|
||||
|
||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||
|
||||
/**
|
||||
* Actions that can be executed for an automower
|
||||
*
|
||||
* @author Markus Pfleger - Initial contribution
|
||||
*/
|
||||
@NonNullByDefault
|
||||
public interface IAutomowerActions {
|
||||
|
||||
void resumeSchedule();
|
||||
|
||||
void park(int durationMin);
|
||||
|
||||
void parkUntilFurtherNotice();
|
||||
|
||||
void parkUntilNextSchedule();
|
||||
|
||||
void pause();
|
||||
|
||||
void start(int durationMin);
|
||||
}
|
||||
Reference in New Issue
Block a user