]> git.basschouten.com Git - openhab-addons.git/blob
e2806fa66d5f083e4c9b8cdd0544bf344dd5cc1b
[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.enigma2.actions;
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.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;
27
28 /**
29  * This is the automation engine actions handler service for the
30  * enigma2 actions.
31  *
32  * @author Guido Dolfen - Initial contribution
33  */
34 @ThingActionsScope(name = "enigma2")
35 @NonNullByDefault
36 public class Enigma2Actions implements ThingActions, IEnigma2Actions {
37     private @Nullable Enigma2Handler handler;
38
39     @Override
40     public void setThingHandler(@Nullable ThingHandler handler) {
41         this.handler = (Enigma2Handler) handler;
42     }
43
44     @Override
45     public @Nullable Enigma2Handler getThingHandler() {
46         return this.handler;
47     }
48
49     @Override
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);
55     }
56
57     @Override
58     @RuleAction(label = "@text/actions.enigma2.send-info.label", description = "@text/actions.enigma2.send-info.description")
59     @SuppressWarnings("null")
60     public void sendInfo(
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);
63     }
64
65     @Override
66     @RuleAction(label = "@text/actions.enigma2.send-info.label", description = "@text/actions.enigma2.send-info.description")
67     @SuppressWarnings("null")
68     public void sendInfo(
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);
72     }
73
74     @Override
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);
80     }
81
82     @Override
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);
89     }
90
91     @Override
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);
97     }
98
99     @Override
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);
106     }
107
108     @Override
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);
114     }
115
116     @Override
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);
123     }
124
125     // delegation methods for "legacy" rule support
126     public static void sendRcCommand(@Nullable ThingActions actions, String rcButton) {
127         invokeMethodOf(actions).sendRcCommand(rcButton);
128     }
129
130     public static void sendInfo(@Nullable ThingActions actions, String info) {
131         invokeMethodOf(actions).sendInfo(info);
132     }
133
134     public static void sendInfo(@Nullable ThingActions actions, String info, int timeout) {
135         invokeMethodOf(actions).sendInfo(info, timeout);
136     }
137
138     public static void sendWarning(@Nullable ThingActions actions, String warning) {
139         invokeMethodOf(actions).sendWarning(warning);
140     }
141
142     public static void sendWarning(@Nullable ThingActions actions, String warning, int timeout) {
143         invokeMethodOf(actions).sendWarning(warning, timeout);
144     }
145
146     public static void sendError(@Nullable ThingActions actions, String error) {
147         invokeMethodOf(actions).sendError(error);
148     }
149
150     public static void sendError(@Nullable ThingActions actions, String error, int timeout) {
151         invokeMethodOf(actions).sendError(error, timeout);
152     }
153
154     public static void sendQuestion(@Nullable ThingActions actions, String text) {
155         invokeMethodOf(actions).sendQuestion(text);
156     }
157
158     public static void sendQuestion(@Nullable ThingActions actions, String text, int timeout) {
159         invokeMethodOf(actions).sendQuestion(text, timeout);
160     }
161
162     private static IEnigma2Actions invokeMethodOf(@Nullable ThingActions actions) {
163         if (actions == null) {
164             throw new IllegalArgumentException("actions cannot be null");
165         }
166         if (actions.getClass().getName().equals(Enigma2Actions.class.getName())) {
167             if (actions instanceof IEnigma2Actions) {
168                 return (IEnigma2Actions) actions;
169             } else {
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);
175                         });
176             }
177         }
178         throw new IllegalArgumentException("Actions is not an instance of Enigma2Actions");
179     }
180 }