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.zoneminder.action;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
/**
* The {@link IZmActions} defines the interface for all thing actions supported by the binding.
* These methods, parameters, and return types are explained in {@link ZmActions}.
*
* @author Mark Hilbush - Initial contribution
*/
@NonNullByDefault
public interface IZmActions {
public void triggerAlarm(@Nullable Number alarmDuration);
public void triggerAlarm();
public void cancelAlarm();
}

View File

@@ -10,10 +10,7 @@
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.zoneminder.action;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
package org.openhab.binding.zoneminder.internal.action;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
@@ -29,23 +26,18 @@ import org.slf4j.LoggerFactory;
/**
* The {@link ZmActions} defines the thing actions provided by this binding.
*
* <b>Note:</b>The static method <b>invokeMethodOf</b> handles the case where
* the test <i>actions instanceof ZmActions</i> fails. This test can fail
* due to an issue in openHAB core v2.5.0 where the {@link ZmActions} class
* can be loaded by a different classloader than the <i>actions</i> instance.
*
* @author Mark Hilbush - Initial contribution
*/
@ThingActionsScope(name = "zm")
@NonNullByDefault
public class ZmActions implements ThingActions, IZmActions {
public class ZmActions implements ThingActions {
private final Logger logger = LoggerFactory.getLogger(ZmActions.class);
private @Nullable ZmMonitorHandler handler;
@Override
public @Nullable ThingHandler getThingHandler() {
return this.handler;
return handler;
}
@Override
@@ -55,31 +47,11 @@ public class ZmActions implements ThingActions, IZmActions {
}
}
private static IZmActions invokeMethodOf(@Nullable ThingActions actions) {
if (actions == null) {
throw new IllegalArgumentException("actions cannot be null");
}
if (actions.getClass().getName().equals(ZmActions.class.getName())) {
if (actions instanceof IZmActions) {
return (IZmActions) actions;
} else {
return (IZmActions) Proxy.newProxyInstance(IZmActions.class.getClassLoader(),
new Class[] { IZmActions.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 ZmActions");
}
/**
* The Trigger Alarm function triggers an alarm that will run for the number of seconds
* specified by the supplied parameter duration.
*/
@Override
@RuleAction(label = "TriggerAlarm", description = "Trigger an alarm on the monitor.")
@RuleAction(label = "trigger an alarm", description = "Trigger an alarm on the monitor.")
public void triggerAlarm(
@ActionInput(name = "duration", description = "The duration of the alarm in seconds.") @Nullable Number duration) {
logger.debug("ZmActions: Action 'TriggerAlarm' called");
@@ -92,15 +64,18 @@ public class ZmActions implements ThingActions, IZmActions {
}
public static void triggerAlarm(@Nullable ThingActions actions, @Nullable Number alarmDuration) {
invokeMethodOf(actions).triggerAlarm(alarmDuration);
if (actions instanceof ZmActions) {
((ZmActions) actions).triggerAlarm(alarmDuration);
} else {
throw new IllegalArgumentException("Actions is not an instance of ZmActions");
}
}
/**
* The Trigger Alarm function triggers an alarm that will run for the number of seconds
* specified in the thing configuration.
*/
@Override
@RuleAction(label = "TriggerAlarm", description = "Trigger an alarm on the monitor.")
@RuleAction(label = "trigger an alarm", description = "Trigger an alarm on the monitor.")
public void triggerAlarm() {
logger.debug("ZmActions: Action 'TriggerAlarm' called");
ZmMonitorHandler localHandler = handler;
@@ -112,14 +87,17 @@ public class ZmActions implements ThingActions, IZmActions {
}
public static void triggerAlarm(@Nullable ThingActions actions) {
invokeMethodOf(actions).triggerAlarm();
if (actions instanceof ZmActions) {
((ZmActions) actions).triggerAlarm();
} else {
throw new IllegalArgumentException("Actions is not an instance of ZmActions");
}
}
/**
* The Cancel Alarm function cancels a running alarm.
*/
@Override
@RuleAction(label = "CancelAlarm", description = "Cancel a running alarm.")
@RuleAction(label = "cancel a running alarm", description = "Cancel a running alarm.")
public void cancelAlarm() {
logger.debug("ZmActions: Action 'CancelAlarm' called");
ZmMonitorHandler localHandler = handler;
@@ -131,6 +109,10 @@ public class ZmActions implements ThingActions, IZmActions {
}
public static void cancelAlarm(@Nullable ThingActions actions) {
invokeMethodOf(actions).cancelAlarm();
if (actions instanceof ZmActions) {
((ZmActions) actions).cancelAlarm();
} else {
throw new IllegalArgumentException("Actions is not an instance of ZmActions");
}
}
}

View File

@@ -26,7 +26,7 @@ import javax.measure.quantity.Time;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.zoneminder.action.ZmActions;
import org.openhab.binding.zoneminder.internal.action.ZmActions;
import org.openhab.binding.zoneminder.internal.config.ZmMonitorConfig;
import org.openhab.core.i18n.TimeZoneProvider;
import org.openhab.core.library.types.DateTimeType;