]> git.basschouten.com Git - openhab-addons.git/blob
05752c5a074876f11e5d62bfb5d2fb87a8c7a843
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.zoneminder.action;
14
15 import java.lang.reflect.Method;
16 import java.lang.reflect.Proxy;
17
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;
28
29 /**
30  * The {@link ZmActions} defines the thing actions provided by this binding.
31  *
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.
36  *
37  * @author Mark Hilbush - Initial contribution
38  */
39 @ThingActionsScope(name = "zm")
40 @NonNullByDefault
41 public class ZmActions implements ThingActions, IZmActions {
42     private final Logger logger = LoggerFactory.getLogger(ZmActions.class);
43
44     private @Nullable ZmMonitorHandler handler;
45
46     @Override
47     public @Nullable ThingHandler getThingHandler() {
48         return this.handler;
49     }
50
51     @Override
52     public void setThingHandler(@Nullable ThingHandler handler) {
53         if (handler instanceof ZmMonitorHandler) {
54             this.handler = (ZmMonitorHandler) handler;
55         }
56     }
57
58     private static IZmActions invokeMethodOf(@Nullable ThingActions actions) {
59         if (actions == null) {
60             throw new IllegalArgumentException("actions cannot be null");
61         }
62         if (actions.getClass().getName().equals(ZmActions.class.getName())) {
63             if (actions instanceof IZmActions) {
64                 return (IZmActions) actions;
65             } else {
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);
71                         });
72             }
73         }
74         throw new IllegalArgumentException("Actions is not an instance of ZmActions");
75     }
76
77     /**
78      * The Trigger Alarm function triggers an alarm that will run for the number of seconds
79      * specified by the supplied parameter duration.
80      */
81     @Override
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!");
89             return;
90         }
91         localHandler.actionTriggerAlarm(duration);
92     }
93
94     public static void triggerAlarm(@Nullable ThingActions actions, @Nullable Number alarmDuration) {
95         invokeMethodOf(actions).triggerAlarm(alarmDuration);
96     }
97
98     /**
99      * The Trigger Alarm function triggers an alarm that will run for the number of seconds
100      * specified in the thing configuration.
101      */
102     @Override
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!");
109             return;
110         }
111         localHandler.actionTriggerAlarm();
112     }
113
114     public static void triggerAlarm(@Nullable ThingActions actions) {
115         invokeMethodOf(actions).triggerAlarm();
116     }
117
118     /**
119      * The Cancel Alarm function cancels a running alarm.
120      */
121     @Override
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!");
128             return;
129         }
130         localHandler.actionCancelAlarm();
131     }
132
133     public static void cancelAlarm(@Nullable ThingActions actions) {
134         invokeMethodOf(actions).cancelAlarm();
135     }
136 }