]> git.basschouten.com Git - openhab-addons.git/blob
d6095a8d40deb252597d13738265a510a0b9da5d
[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.doorbird.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.doorbird.internal.handler.DoorbellHandler;
21 import org.openhab.core.automation.annotation.ActionOutput;
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 DoorbirdActions} defines rule actions for the doorbird binding.
31  *
32  * @author Mark Hilbush - Initial contribution
33  */
34 @ThingActionsScope(name = "doorbird")
35 @NonNullByDefault
36 public class DoorbirdActions implements ThingActions {
37     private static final Logger LOGGER = LoggerFactory.getLogger(DoorbirdActions.class);
38
39     private @Nullable DoorbellHandler handler;
40
41     public DoorbirdActions() {
42         LOGGER.debug("DoorbirdActions service created");
43     }
44
45     @Override
46     public void setThingHandler(@Nullable ThingHandler handler) {
47         if (handler instanceof DoorbellHandler) {
48             this.handler = (DoorbellHandler) handler;
49         }
50     }
51
52     @Override
53     public @Nullable ThingHandler getThingHandler() {
54         return this.handler;
55     }
56
57     private static IDoorbirdActions invokeMethodOf(@Nullable ThingActions actions) {
58         if (actions == null) {
59             throw new IllegalArgumentException("actions cannot be null");
60         }
61         if (actions.getClass().getName().equals(DoorbirdActions.class.getName())) {
62             if (actions instanceof IDoorbirdActions) {
63                 return (IDoorbirdActions) actions;
64             } else {
65                 return (IDoorbirdActions) Proxy.newProxyInstance(IDoorbirdActions.class.getClassLoader(),
66                         new Class[] { IDoorbirdActions.class }, (Object proxy, Method method, Object[] args) -> {
67                             Method m = actions.getClass().getDeclaredMethod(method.getName(),
68                                     method.getParameterTypes());
69                             return m.invoke(actions, args);
70                         });
71             }
72         }
73         throw new IllegalArgumentException("actions is not an instance of DoorbirdActions");
74     }
75
76     @RuleAction(label = "Restart Doorbird", description = "Restarts the Doorbird device")
77     public void restart() {
78         LOGGER.debug("Doorbird action 'restart' called");
79         if (handler != null) {
80             handler.actionRestart();
81         } else {
82             LOGGER.info("Doorbird Action service ThingHandler is null!");
83         }
84     }
85
86     public static void restart(@Nullable ThingActions actions) {
87         invokeMethodOf(actions).restart();
88     }
89
90     @RuleAction(label = "SIP Hangup", description = "Hangup SIP call")
91     public void sipHangup() {
92         LOGGER.debug("Doorbird action 'sipHangup' called");
93         if (handler != null) {
94             handler.actionSIPHangup();
95         } else {
96             LOGGER.info("Doorbird Action service ThingHandler is null!");
97         }
98     }
99
100     public static void sipHangup(@Nullable ThingActions actions) {
101         invokeMethodOf(actions).sipHangup();
102     }
103
104     @RuleAction(label = "Get Ring Time Limit", description = "Get the value of RING_TIME_LIMIT")
105     public @ActionOutput(name = "getRingTimeLimit", type = "java.lang.String") String getRingTimeLimit() {
106         LOGGER.debug("Doorbird action 'getRingTimeLimit' called");
107         if (handler != null) {
108             return handler.actionGetRingTimeLimit();
109         } else {
110             LOGGER.info("Doorbird Action service ThingHandler is null!");
111             return "";
112         }
113     }
114
115     public static String getRingTimeLimit(@Nullable ThingActions actions) {
116         return invokeMethodOf(actions).getRingTimeLimit();
117     }
118
119     @RuleAction(label = "Get Call Time Limit", description = "Get the value of CALL_TIME_LIMIT")
120     public @ActionOutput(name = "getCallTimeLimit", type = "java.lang.String") String getCallTimeLimit() {
121         LOGGER.debug("Doorbird action 'getCallTimeLimit' called");
122         if (handler != null) {
123             return handler.actionGetCallTimeLimit();
124         } else {
125             LOGGER.info("Doorbird Action service ThingHandler is null!");
126             return "";
127         }
128     }
129
130     public static String getCallTimeLimit(@Nullable ThingActions actions) {
131         return invokeMethodOf(actions).getCallTimeLimit();
132     }
133
134     @RuleAction(label = "Get Last Error Code", description = "Get the value of LASTERRORCODE")
135     public @ActionOutput(name = "getLastErrorCode", type = "java.lang.String") String getLastErrorCode() {
136         LOGGER.debug("Doorbird action 'getLastErrorCode' called");
137         if (handler != null) {
138             return handler.actionGetLastErrorCode();
139         } else {
140             LOGGER.info("Doorbird Action service ThingHandler is null!");
141             return "";
142         }
143     }
144
145     public static String getLastErrorCode(@Nullable ThingActions actions) {
146         return invokeMethodOf(actions).getLastErrorCode();
147     }
148
149     @RuleAction(label = "Get Last Error Text", description = "Get the value of LASTERRORTEXT")
150     public @ActionOutput(name = "getLastErrorText", type = "java.lang.String") String getLastErrorText() {
151         LOGGER.debug("Doorbird action 'getLastErrorText' called");
152         if (handler != null) {
153             return handler.actionGetLastErrorText();
154         } else {
155             LOGGER.info("Doorbird Action service ThingHandler is null!");
156             return "";
157         }
158     }
159
160     public static String getLastErrorText(@Nullable ThingActions actions) {
161         return invokeMethodOf(actions).getLastErrorText();
162     }
163 }