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.doorbird.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.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;
30 * The {@link DoorbirdActions} defines rule actions for the doorbird binding.
32 * @author Mark Hilbush - Initial contribution
34 @ThingActionsScope(name = "doorbird")
36 public class DoorbirdActions implements ThingActions {
37 private static final Logger LOGGER = LoggerFactory.getLogger(DoorbirdActions.class);
39 private @Nullable DoorbellHandler handler;
41 public DoorbirdActions() {
42 LOGGER.debug("DoorbirdActions service created");
46 public void setThingHandler(@Nullable ThingHandler handler) {
47 if (handler instanceof DoorbellHandler) {
48 this.handler = (DoorbellHandler) handler;
53 public @Nullable ThingHandler getThingHandler() {
57 private static IDoorbirdActions invokeMethodOf(@Nullable ThingActions actions) {
58 if (actions == null) {
59 throw new IllegalArgumentException("actions cannot be null");
61 if (actions.getClass().getName().equals(DoorbirdActions.class.getName())) {
62 if (actions instanceof IDoorbirdActions) {
63 return (IDoorbirdActions) actions;
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);
73 throw new IllegalArgumentException("actions is not an instance of DoorbirdActions");
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();
82 LOGGER.info("Doorbird Action service ThingHandler is null!");
86 public static void restart(@Nullable ThingActions actions) {
87 invokeMethodOf(actions).restart();
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();
96 LOGGER.info("Doorbird Action service ThingHandler is null!");
100 public static void sipHangup(@Nullable ThingActions actions) {
101 invokeMethodOf(actions).sipHangup();
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();
110 LOGGER.info("Doorbird Action service ThingHandler is null!");
115 public static String getRingTimeLimit(@Nullable ThingActions actions) {
116 return invokeMethodOf(actions).getRingTimeLimit();
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();
125 LOGGER.info("Doorbird Action service ThingHandler is null!");
130 public static String getCallTimeLimit(@Nullable ThingActions actions) {
131 return invokeMethodOf(actions).getCallTimeLimit();
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();
140 LOGGER.info("Doorbird Action service ThingHandler is null!");
145 public static String getLastErrorCode(@Nullable ThingActions actions) {
146 return invokeMethodOf(actions).getLastErrorCode();
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();
155 LOGGER.info("Doorbird Action service ThingHandler is null!");
160 public static String getLastErrorText(@Nullable ThingActions actions) {
161 return invokeMethodOf(actions).getLastErrorText();