2 * Copyright (c) 2010-2020 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.zoneminder.action;
15 import java.lang.reflect.Method;
16 import java.lang.reflect.Proxy;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.openhab.binding.zoneminder.internal.handler.ZmMonitorHandler;
21 import org.openhab.core.automation.annotation.ActionInput;
22 import org.openhab.core.automation.annotation.RuleAction;
23 import org.openhab.core.thing.binding.ThingActions;
24 import org.openhab.core.thing.binding.ThingActionsScope;
25 import org.openhab.core.thing.binding.ThingHandler;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
30 * The {@link ZmActions} defines the thing actions provided by this binding.
32 * <b>Note:</b>The static method <b>invokeMethodOf</b> handles the case where
33 * the test <i>actions instanceof ZmActions</i> fails. This test can fail
34 * due to an issue in openHAB core v2.5.0 where the {@link ZmActions} class
35 * can be loaded by a different classloader than the <i>actions</i> instance.
37 * @author Mark Hilbush - Initial contribution
39 @ThingActionsScope(name = "zm")
41 public class ZmActions implements ThingActions, IZmActions {
42 private final Logger logger = LoggerFactory.getLogger(ZmActions.class);
44 private @Nullable ZmMonitorHandler handler;
47 public @Nullable ThingHandler getThingHandler() {
52 public void setThingHandler(@Nullable ThingHandler handler) {
53 if (handler instanceof ZmMonitorHandler) {
54 this.handler = (ZmMonitorHandler) handler;
58 private static IZmActions invokeMethodOf(@Nullable ThingActions actions) {
59 if (actions == null) {
60 throw new IllegalArgumentException("actions cannot be null");
62 if (actions.getClass().getName().equals(ZmActions.class.getName())) {
63 if (actions instanceof IZmActions) {
64 return (IZmActions) actions;
66 return (IZmActions) Proxy.newProxyInstance(IZmActions.class.getClassLoader(),
67 new Class[] { IZmActions.class }, (Object proxy, Method method, Object[] args) -> {
68 Method m = actions.getClass().getDeclaredMethod(method.getName(),
69 method.getParameterTypes());
70 return m.invoke(actions, args);
74 throw new IllegalArgumentException("Actions is not an instance of ZmActions");
78 * The Trigger Alarm function triggers an alarm that will run for the number of seconds
79 * specified by the supplied parameter duration.
82 @RuleAction(label = "TriggerAlarm", description = "Trigger an alarm on the monitor.")
83 public void triggerAlarm(
84 @ActionInput(name = "duration", description = "The duration of the alarm in seconds.") @Nullable Number duration) {
85 logger.debug("ZmActions: Action 'TriggerAlarm' called");
86 ZmMonitorHandler localHandler = handler;
87 if (localHandler == null) {
88 logger.warn("ZmActions: Action service ThingHandler is null!");
91 localHandler.actionTriggerAlarm(duration);
94 public static void triggerAlarm(@Nullable ThingActions actions, @Nullable Number alarmDuration) {
95 invokeMethodOf(actions).triggerAlarm(alarmDuration);
99 * The Trigger Alarm function triggers an alarm that will run for the number of seconds
100 * specified in the thing configuration.
103 @RuleAction(label = "TriggerAlarm", description = "Trigger an alarm on the monitor.")
104 public void triggerAlarm() {
105 logger.debug("ZmActions: Action 'TriggerAlarm' called");
106 ZmMonitorHandler localHandler = handler;
107 if (localHandler == null) {
108 logger.warn("ZmActions: Action service ThingHandler is null!");
111 localHandler.actionTriggerAlarm();
114 public static void triggerAlarm(@Nullable ThingActions actions) {
115 invokeMethodOf(actions).triggerAlarm();
119 * The Cancel Alarm function cancels a running alarm.
122 @RuleAction(label = "CancelAlarm", description = "Cancel a running alarm.")
123 public void cancelAlarm() {
124 logger.debug("ZmActions: Action 'CancelAlarm' called");
125 ZmMonitorHandler localHandler = handler;
126 if (localHandler == null) {
127 logger.warn("ZmActions: Action service ThingHandler is null!");
130 localHandler.actionCancelAlarm();
133 public static void cancelAlarm(@Nullable ThingActions actions) {
134 invokeMethodOf(actions).cancelAlarm();