]> git.basschouten.com Git - openhab-addons.git/blob
81a9ba5f19ea82ecfdd1b1faaaabe2771cfa15c3
[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.twitter.internal.action;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.openhab.binding.twitter.internal.TwitterHandler;
18 import org.openhab.core.automation.annotation.ActionInput;
19 import org.openhab.core.automation.annotation.ActionOutput;
20 import org.openhab.core.automation.annotation.RuleAction;
21 import org.openhab.core.thing.binding.ThingActions;
22 import org.openhab.core.thing.binding.ThingActionsScope;
23 import org.openhab.core.thing.binding.ThingHandler;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 /**
28  * The {@link TweetActions} class defines rule actions for sending tweet
29  *
30  * @author Scott Hanson - Initial contribution
31  */
32 @ThingActionsScope(name = "twitter")
33 @NonNullByDefault
34 public class TwitterActions implements ThingActions {
35
36     private final Logger logger = LoggerFactory.getLogger(TwitterActions.class);
37
38     private @Nullable TwitterHandler handler;
39
40     @RuleAction(label = "@text/sendTweetActionLabel", description = "@text/sendTweetActionDescription")
41     public @ActionOutput(name = "success", type = "java.lang.Boolean") Boolean sendTweet(
42             @ActionInput(name = "text") @Nullable String text) {
43         if (text == null) {
44             logger.warn("Cannot send Tweet as text is missing.");
45             return false;
46         }
47
48         final TwitterHandler handler = this.handler;
49         if (handler == null) {
50             logger.debug("Handler is null, cannot tweet.");
51             return false;
52         } else {
53             return handler.sendTweet(text);
54         }
55     }
56
57     @RuleAction(label = "@text/sendAttachmentTweetActionLabel", description = "@text/sendAttachmentTweetActionDescription")
58     public @ActionOutput(name = "success", type = "java.lang.Boolean") Boolean sendTweetWithAttachment(
59             @ActionInput(name = "text") @Nullable String text, @ActionInput(name = "url") @Nullable String urlString) {
60         if (text == null) {
61             logger.warn("Cannot send Tweet as text is missing.");
62             return false;
63         }
64         if (urlString == null) {
65             logger.warn("Cannot send Tweet as urlString is missing.");
66             return false;
67         }
68
69         final TwitterHandler handler = this.handler;
70         if (handler == null) {
71             logger.debug("Handler is null, cannot tweet.");
72             return false;
73         } else {
74             return handler.sendTweet(text, urlString);
75         }
76     }
77
78     @RuleAction(label = "@text/sendDirectMessageActionLabel", description = "@text/sendDirectMessageActionDescription")
79     public @ActionOutput(name = "success", type = "java.lang.Boolean") Boolean sendDirectMessage(
80             @ActionInput(name = "recipient") @Nullable String recipient,
81             @ActionInput(name = "text") @Nullable String text) {
82         if (recipient == null) {
83             logger.warn("Cannot send Direct Message as recipient is missing.");
84             return false;
85         }
86         if (text == null) {
87             logger.warn("Cannot send Direct Message as text is missing.");
88             return false;
89         }
90
91         final TwitterHandler handler = this.handler;
92         if (handler == null) {
93             logger.debug("Handler is null, cannot tweet.");
94             return false;
95         } else {
96             return handler.sendDirectMessage(recipient, text);
97         }
98     }
99
100     public static boolean sendTweet(ThingActions actions, @Nullable String text) {
101         return ((TwitterActions) actions).sendTweet(text);
102     }
103
104     public static boolean sendTweetWithAttachment(ThingActions actions, @Nullable String text,
105             @Nullable String urlString) {
106         return ((TwitterActions) actions).sendTweetWithAttachment(text, urlString);
107     }
108
109     public static boolean sendDirectMessage(ThingActions actions, @Nullable String recipient, @Nullable String text) {
110         return ((TwitterActions) actions).sendDirectMessage(recipient, text);
111     }
112
113     @Override
114     public void setThingHandler(@Nullable ThingHandler handler) {
115         if (handler instanceof TwitterHandler) {
116             this.handler = (TwitterHandler) handler;
117         }
118     }
119
120     @Override
121     public @Nullable ThingHandler getThingHandler() {
122         return handler;
123     }
124 }