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

@@ -12,8 +12,6 @@
*/
package org.openhab.binding.astro.internal.action;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.time.ZonedDateTime;
import javax.measure.quantity.Angle;
@@ -35,18 +33,13 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* The {AstroActions } defines rule actions for the Astro binding.
* <p>
* <b>Note:</b>The static method <b>invokeMethodOf</b> handles the case where
* the test <i>actions instanceof AstroActions</i> fails. This test can fail
* due to an issue in openHAB core v2.5.0 where the {@link AstroActions} class
* can be loaded by a different classloader than the <i>actions</i> instance.
* Defines the automation thing actions for the Astro binding.
*
* @author Gaël L'hopital - Initial contribution
*/
@ThingActionsScope(name = "astro")
@NonNullByDefault
public class AstroActions implements ThingActions, IAstroActions {
public class AstroActions implements ThingActions {
private final Logger logger = LoggerFactory.getLogger(AstroActions.class);
protected @Nullable AstroThingHandler handler;
@@ -64,11 +57,10 @@ public class AstroActions implements ThingActions, IAstroActions {
@Override
public @Nullable ThingHandler getThingHandler() {
return this.handler;
return handler;
}
@Override
@RuleAction(label = "Astro : Get Azimuth", description = "Get the azimuth of the sun for a given time")
@RuleAction(label = "get the azimuth", description = "Get the azimuth for a given time.")
public @Nullable @ActionOutput(name = "getAzimuth", label = "Azimuth", type = "org.openhab.core.library.types.QuantityType<javax.measure.quantity.Angle>") QuantityType<Angle> getAzimuth(
@ActionInput(name = "date", label = "Date", required = false, description = "Considered date") @Nullable ZonedDateTime date) {
logger.debug("Astro action 'getAzimuth' called");
@@ -81,8 +73,7 @@ public class AstroActions implements ThingActions, IAstroActions {
return null;
}
@Override
@RuleAction(label = "Astro : Get Elevation", description = "Get the Elevation of the sun for a given time")
@RuleAction(label = "get the elevation", description = "Get the elevation for a given time.")
public @Nullable @ActionOutput(name = "getElevation", label = "Elevation", type = "org.openhab.core.library.types.QuantityType<javax.measure.quantity.Angle>") QuantityType<Angle> getElevation(
@ActionInput(name = "date", label = "Date", required = false, description = "Considered date") @Nullable ZonedDateTime date) {
logger.debug("Astro action 'getElevation' called");
@@ -95,8 +86,7 @@ public class AstroActions implements ThingActions, IAstroActions {
return null;
}
@Override
@RuleAction(label = "Sun : Get Event Time", description = "Get the date time of a given planet event")
@RuleAction(label = "get the date time of a sun event", description = "Get the date time of a sun event.")
public @Nullable @ActionOutput(name = "getEventTime", type = "java.time.ZonedDateTime") ZonedDateTime getEventTime(
@ActionInput(name = "phaseName", label = "Phase", required = true, description = "Requested phase") String phaseName,
@ActionInput(name = "date", label = "Date", required = false, description = "Considered date") @Nullable ZonedDateTime date,
@@ -124,39 +114,32 @@ public class AstroActions implements ThingActions, IAstroActions {
public static @Nullable QuantityType<Angle> getElevation(@Nullable ThingActions actions,
@Nullable ZonedDateTime date) {
return invokeMethodOf(actions).getElevation(date);
if (actions instanceof AstroActions) {
return ((AstroActions) actions).getElevation(date);
} else {
throw new IllegalArgumentException("Actions is not an instance of AstroActions");
}
}
public static @Nullable QuantityType<Angle> getAzimuth(@Nullable ThingActions actions,
@Nullable ZonedDateTime date) {
return invokeMethodOf(actions).getAzimuth(date);
if (actions instanceof AstroActions) {
return ((AstroActions) actions).getAzimuth(date);
} else {
throw new IllegalArgumentException("Actions is not an instance of AstroActions");
}
}
public static @Nullable ZonedDateTime getEventTime(@Nullable ThingActions actions, @Nullable String phaseName,
@Nullable ZonedDateTime date, @Nullable String moment) {
if (phaseName != null) {
return invokeMethodOf(actions).getEventTime(phaseName, date, moment);
} else {
throw new IllegalArgumentException("phaseName can not be null");
}
}
private static IAstroActions invokeMethodOf(@Nullable ThingActions actions) {
if (actions == null) {
throw new IllegalArgumentException("actions cannot be null");
}
if (actions.getClass().getName().equals(AstroActions.class.getName())) {
if (actions instanceof IAstroActions) {
return (IAstroActions) actions;
if (actions instanceof AstroActions) {
if (phaseName != null) {
return ((AstroActions) actions).getEventTime(phaseName, date, moment);
} else {
return (IAstroActions) Proxy.newProxyInstance(IAstroActions.class.getClassLoader(),
new Class[] { IAstroActions.class }, (Object proxy, Method method, Object[] args) -> {
Method m = actions.getClass().getDeclaredMethod(method.getName(),
method.getParameterTypes());
return m.invoke(actions, args);
});
throw new IllegalArgumentException("phaseName can not be null");
}
} else {
throw new IllegalArgumentException("Actions is not an instance of AstroActions");
}
throw new IllegalArgumentException("Actions is not an instance of AstroActions");
}
}

View File

@@ -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.astro.internal.action;
import java.time.ZonedDateTime;
import javax.measure.quantity.Angle;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.library.types.QuantityType;
/**
* The {@link IAstroActions} defines the interface for all thing actions supported by the binding.
*
* @author Gaël L'hopital - Initial contribution
*/
@NonNullByDefault
public interface IAstroActions {
public @Nullable ZonedDateTime getEventTime(String phaseName, @Nullable ZonedDateTime date,
@Nullable String moment);
public @Nullable QuantityType<Angle> getAzimuth(@Nullable ZonedDateTime date);
public @Nullable QuantityType<Angle> getElevation(@Nullable ZonedDateTime date);
}