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.enigma2.actions;
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.enigma2.handler.Enigma2Handler;
21 import org.openhab.binding.enigma2.internal.Enigma2BindingConstants;
22 import org.openhab.core.automation.annotation.ActionInput;
23 import org.openhab.core.automation.annotation.RuleAction;
24 import org.openhab.core.thing.binding.ThingActions;
25 import org.openhab.core.thing.binding.ThingActionsScope;
26 import org.openhab.core.thing.binding.ThingHandler;
29 * This is the automation engine actions handler service for the
32 * @author Guido Dolfen - Initial contribution
34 @ThingActionsScope(name = "enigma2")
36 public class Enigma2Actions implements ThingActions, IEnigma2Actions {
37 private @Nullable Enigma2Handler handler;
40 public void setThingHandler(@Nullable ThingHandler handler) {
41 this.handler = (Enigma2Handler) handler;
45 public @Nullable Enigma2Handler getThingHandler() {
50 @RuleAction(label = "@text/actions.enigma2.send-rc-button.label", description = "@text/actions.enigma2.send-rc-button.description")
51 @SuppressWarnings("null")
52 public void sendRcCommand(
53 @ActionInput(name = "rcButton", label = "@text/actions-input.enigma2.rc-button.label", description = "@text/actions-input.enigma2.rc-button.description") String rcButton) {
54 handler.sendRcCommand(rcButton);
58 @RuleAction(label = "@text/actions.enigma2.send-info.label", description = "@text/actions.enigma2.send-info.description")
59 @SuppressWarnings("null")
61 @ActionInput(name = "text", label = "@text/actions-input.enigma2.text.label", description = "@text/actions-input.enigma2.text.description") String text) {
62 handler.sendInfo(Enigma2BindingConstants.MESSAGE_TIMEOUT, text);
66 @RuleAction(label = "@text/actions.enigma2.send-info.label", description = "@text/actions.enigma2.send-info.description")
67 @SuppressWarnings("null")
69 @ActionInput(name = "text", label = "@text/actions-input.enigma2.text.label", description = "@text/actions-input.enigma2.text.description") String text,
70 @ActionInput(name = "timeout", label = "@text/actions-input.enigma2.timeout.label", description = "@text/actions-input.enigma2.timeout.description") int timeout) {
71 handler.sendInfo(timeout, text);
75 @RuleAction(label = "@text/actions.enigma2.send-warning.label", description = "@text/actions.enigma2.send-warning.description")
76 @SuppressWarnings("null")
77 public void sendWarning(
78 @ActionInput(name = "text", label = "@text/actions-input.enigma2.text.label", description = "@text/actions-input.enigma2.text.description") String text) {
79 handler.sendWarning(Enigma2BindingConstants.MESSAGE_TIMEOUT, text);
83 @RuleAction(label = "@text/actions.enigma2.send-warning.label", description = "@text/actions.enigma2.send-warning.description")
84 @SuppressWarnings("null")
85 public void sendWarning(
86 @ActionInput(name = "text", label = "@text/actions-input.enigma2.text.label", description = "@text/actions-input.enigma2.text.description") String text,
87 @ActionInput(name = "timeout", label = "@text/actions-input.enigma2.timeout.label", description = "@text/actions-input.enigma2.timeout.description") int timeout) {
88 handler.sendWarning(timeout, text);
92 @RuleAction(label = "@text/actions.enigma2.send-error.label", description = "@text/actions.enigma2.send-error.description")
93 @SuppressWarnings("null")
94 public void sendError(
95 @ActionInput(name = "text", label = "@text/actions-input.enigma2.text.label", description = "@text/actions-input.enigma2.text.description") String text) {
96 handler.sendError(Enigma2BindingConstants.MESSAGE_TIMEOUT, text);
100 @RuleAction(label = "@text/actions.enigma2.send-error.label", description = "@text/actions.enigma2.send-error.description")
101 @SuppressWarnings("null")
102 public void sendError(
103 @ActionInput(name = "text", label = "@text/actions-input.enigma2.text.label", description = "@text/actions-input.enigma2.text.description") String text,
104 @ActionInput(name = "timeout", label = "@text/actions-input.enigma2.timeout.label", description = "@text/actions-input.enigma2.timeout.description") int timeout) {
105 handler.sendError(timeout, text);
109 @RuleAction(label = "@text/actions.enigma2.send-error.label", description = "@text/actions.enigma2.send-question.description")
110 @SuppressWarnings("null")
111 public void sendQuestion(
112 @ActionInput(name = "text", label = "@text/actions-input.enigma2.text.label", description = "@text/actions-input.enigma2.text.description") String text) {
113 handler.sendQuestion(Enigma2BindingConstants.MESSAGE_TIMEOUT, text);
117 @RuleAction(label = "@text/actions.enigma2.send-error.label", description = "@text/actions.enigma2.send-question.description")
118 @SuppressWarnings("null")
119 public void sendQuestion(
120 @ActionInput(name = "text", label = "@text/actions-input.enigma2.text.label", description = "@text/actions-input.enigma2.text.description") String text,
121 @ActionInput(name = "timeout", label = "@text/actions-input.enigma2.timeout.label", description = "@text/actions-input.enigma2.timeout.description") int timeout) {
122 handler.sendQuestion(timeout, text);
125 // delegation methods for "legacy" rule support
126 public static void sendRcCommand(@Nullable ThingActions actions, String rcButton) {
127 invokeMethodOf(actions).sendRcCommand(rcButton);
130 public static void sendInfo(@Nullable ThingActions actions, String info) {
131 invokeMethodOf(actions).sendInfo(info);
134 public static void sendInfo(@Nullable ThingActions actions, String info, int timeout) {
135 invokeMethodOf(actions).sendInfo(info, timeout);
138 public static void sendWarning(@Nullable ThingActions actions, String warning) {
139 invokeMethodOf(actions).sendWarning(warning);
142 public static void sendWarning(@Nullable ThingActions actions, String warning, int timeout) {
143 invokeMethodOf(actions).sendWarning(warning, timeout);
146 public static void sendError(@Nullable ThingActions actions, String error) {
147 invokeMethodOf(actions).sendError(error);
150 public static void sendError(@Nullable ThingActions actions, String error, int timeout) {
151 invokeMethodOf(actions).sendError(error, timeout);
154 public static void sendQuestion(@Nullable ThingActions actions, String text) {
155 invokeMethodOf(actions).sendQuestion(text);
158 public static void sendQuestion(@Nullable ThingActions actions, String text, int timeout) {
159 invokeMethodOf(actions).sendQuestion(text, timeout);
162 private static IEnigma2Actions invokeMethodOf(@Nullable ThingActions actions) {
163 if (actions == null) {
164 throw new IllegalArgumentException("actions cannot be null");
166 if (actions.getClass().getName().equals(Enigma2Actions.class.getName())) {
167 if (actions instanceof IEnigma2Actions) {
168 return (IEnigma2Actions) actions;
170 return (IEnigma2Actions) Proxy.newProxyInstance(IEnigma2Actions.class.getClassLoader(),
171 new Class[] { IEnigma2Actions.class }, (Object proxy, Method method, Object[] args) -> {
172 Method m = actions.getClass().getDeclaredMethod(method.getName(),
173 method.getParameterTypes());
174 return m.invoke(actions, args);
178 throw new IllegalArgumentException("Actions is not an instance of Enigma2Actions");