]> git.basschouten.com Git - openhab-addons.git/blob
8b4703ef5b5c60f647623b69b2b9440a6d23918a
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.webexteams.internal;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.openhab.core.automation.annotation.ActionInput;
18 import org.openhab.core.automation.annotation.ActionOutput;
19 import org.openhab.core.automation.annotation.RuleAction;
20 import org.openhab.core.thing.binding.ThingActions;
21 import org.openhab.core.thing.binding.ThingActionsScope;
22 import org.openhab.core.thing.binding.ThingHandler;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 /**
27  * The {@link WebexTeamsActions} class defines rule actions for sending messages
28  *
29  * @author Tom Deckers - Initial contribution
30  */
31 @ThingActionsScope(name = "webexteams")
32 @NonNullByDefault
33 public class WebexTeamsActions implements ThingActions {
34
35     private final Logger logger = LoggerFactory.getLogger(WebexTeamsActions.class);
36     private @Nullable WebexTeamsHandler handler;
37
38     @RuleAction(label = "@text/sendMessageActionLabel", description = "@text/sendMessageActionDescription")
39     public @ActionOutput(name = "success", type = "java.lang.Boolean") Boolean sendMessage(
40             @ActionInput(name = "text") @Nullable String text) {
41         if (text == null) {
42             logger.warn("Cannot send Message as text is missing.");
43             return false;
44         }
45
46         final WebexTeamsHandler handler = this.handler;
47         if (handler == null) {
48             logger.debug("Handler is null, cannot send message.");
49             return false;
50         } else {
51             return handler.sendMessage(text);
52         }
53     }
54
55     @RuleAction(label = "@text/sendMessageAttActionLabel", description = "@text/sendMessageAttActionDescription")
56     public @ActionOutput(name = "success", type = "java.lang.Boolean") Boolean sendMessage(
57             @ActionInput(name = "text") @Nullable String text, @ActionInput(name = "attach") @Nullable String attach) {
58         if (text == null) {
59             logger.warn("Cannot send Message as text is missing.");
60             return false;
61         }
62         if (attach == null) {
63             logger.warn("Cannot send Message as attach is missing.");
64             return false;
65         }
66
67         final WebexTeamsHandler handler = this.handler;
68         if (handler == null) {
69             logger.debug("Handler is null, cannot send message.");
70             return false;
71         } else {
72             return handler.sendMessage(text, attach);
73         }
74     }
75
76     @RuleAction(label = "@text/sendRoomMessageActionLabel", description = "@text/sendRoomMessageActionDescription")
77     public @ActionOutput(name = "success", type = "java.lang.Boolean") Boolean sendRoomMessage(
78             @ActionInput(name = "roomId") @Nullable String roomId, @ActionInput(name = "text") @Nullable String text) {
79         if (text == null) {
80             logger.warn("Cannot send Message as text is missing.");
81             return false;
82         }
83         if (roomId == null) {
84             logger.warn("Cannot send Message as roomId is missing.");
85             return false;
86         }
87
88         final WebexTeamsHandler handler = this.handler;
89         if (handler == null) {
90             logger.debug("Handler is null, cannot send message.");
91             return false;
92         } else {
93             return handler.sendRoomMessage(roomId, text);
94         }
95     }
96
97     @RuleAction(label = "@text/sendRoomMessageAttActionLabel", description = "@text/sendRoomMessageAttActionDescription")
98     public @ActionOutput(name = "success", type = "java.lang.Boolean") Boolean sendRoomMessage(
99             @ActionInput(name = "roomId") @Nullable String roomId, @ActionInput(name = "text") @Nullable String text,
100             @ActionInput(name = "attach") @Nullable String attach) {
101         if (text == null) {
102             logger.warn("Cannot send Message as text is missing.");
103             return false;
104         }
105         if (roomId == null) {
106             logger.warn("Cannot send Message as roomId is missing.");
107             return false;
108         }
109         if (attach == null) {
110             logger.warn("Cannot send Message as attach is missing.");
111             return false;
112         }
113         final WebexTeamsHandler handler = this.handler;
114         if (handler == null) {
115             logger.debug("Handler is null, cannot send message.");
116             return false;
117         } else {
118             return handler.sendRoomMessage(roomId, text, attach);
119         }
120     }
121
122     @RuleAction(label = "@text/sendPersonMessageActionLabel", description = "@text/sendPersonMessageActionDescription")
123     public @ActionOutput(name = "success", type = "java.lang.Boolean") Boolean sendPersonMessage(
124             @ActionInput(name = "personEmail") @Nullable String personEmail,
125             @ActionInput(name = "text") @Nullable String text) {
126         if (text == null) {
127             logger.warn("Cannot send Message as text is missing.");
128             return false;
129         }
130         if (personEmail == null) {
131             logger.warn("Cannot send Message as personEmail is missing.");
132             return false;
133         }
134
135         final WebexTeamsHandler handler = this.handler;
136         if (handler == null) {
137             logger.debug("Handler is null, cannot send message.");
138             return false;
139         } else {
140             return handler.sendPersonMessage(personEmail, text);
141         }
142     }
143
144     @RuleAction(label = "@text/sendPersonMessageAttActionLabel", description = "@text/sendPersonMessageAttActionDescription")
145     public @ActionOutput(name = "success", type = "java.lang.Boolean") Boolean sendPersonMessage(
146             @ActionInput(name = "personEmail") @Nullable String personEmail,
147             @ActionInput(name = "text") @Nullable String text, @ActionInput(name = "attach") @Nullable String attach) {
148         if (text == null) {
149             logger.warn("Cannot send Message as text is missing.");
150             return false;
151         }
152         if (personEmail == null) {
153             logger.warn("Cannot send Message as personEmail is missing.");
154             return false;
155         }
156         if (attach == null) {
157             logger.warn("Cannot send Message as attach is missing.");
158             return false;
159         }
160
161         final WebexTeamsHandler handler = this.handler;
162         if (handler == null) {
163             logger.debug("Handler is null, cannot send message.");
164             return false;
165         } else {
166             return handler.sendPersonMessage(personEmail, text, attach);
167         }
168     }
169
170     public static boolean sendMessage(@Nullable ThingActions actions, @Nullable String text) {
171         if (actions instanceof WebexTeamsActions) {
172             return ((WebexTeamsActions) actions).sendMessage(text);
173         } else {
174             throw new IllegalArgumentException("Instance is not a WebexTeamsActions class.");
175         }
176     }
177
178     public static boolean sendMessage(@Nullable ThingActions actions, @Nullable String text, @Nullable String attach) {
179         if (actions instanceof WebexTeamsActions) {
180             return ((WebexTeamsActions) actions).sendMessage(text, attach);
181         } else {
182             throw new IllegalArgumentException("Instance is not a WebexTeamsActions class.");
183         }
184     }
185
186     public static boolean sendRoomMessage(@Nullable ThingActions actions, @Nullable String roomId,
187             @Nullable String text) {
188         if (actions instanceof WebexTeamsActions) {
189             return ((WebexTeamsActions) actions).sendRoomMessage(roomId, text);
190         } else {
191             throw new IllegalArgumentException("Instance is not a WebexTeamsActions class.");
192         }
193     }
194
195     public static boolean sendRoomMessage(@Nullable ThingActions actions, @Nullable String roomId,
196             @Nullable String text, @Nullable String attach) {
197         if (actions instanceof WebexTeamsActions) {
198             return ((WebexTeamsActions) actions).sendRoomMessage(roomId, text, attach);
199         } else {
200             throw new IllegalArgumentException("Instance is not a WebexTeamsActions class.");
201         }
202     }
203
204     public static boolean sendPersonMessage(@Nullable ThingActions actions, @Nullable String personEmail,
205             @Nullable String text) {
206         if (actions instanceof WebexTeamsActions) {
207             return ((WebexTeamsActions) actions).sendPersonMessage(personEmail, text);
208         } else {
209             throw new IllegalArgumentException("Instance is not a WebexTeamsActions class.");
210         }
211     }
212
213     public static boolean sendPersonMessage(@Nullable ThingActions actions, @Nullable String personEmail,
214             @Nullable String text, @Nullable String attach) {
215         if (actions instanceof WebexTeamsActions) {
216             return ((WebexTeamsActions) actions).sendPersonMessage(personEmail, text, attach);
217         } else {
218             throw new IllegalArgumentException("Instance is not a WebexTeamsActions class.");
219         }
220     }
221
222     @Override
223     public void setThingHandler(@Nullable ThingHandler handler) {
224         if (handler instanceof WebexTeamsHandler) {
225             this.handler = (WebexTeamsHandler) handler;
226         }
227     }
228
229     @Override
230     public @Nullable ThingHandler getThingHandler() {
231         return handler;
232     }
233 }