]> git.basschouten.com Git - openhab-addons.git/blob
6976178eb7031b2e7b01927448e3297a58470b58
[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.xmppclient.internal.action;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.openhab.binding.xmppclient.internal.XMPPClient;
18 import org.openhab.binding.xmppclient.internal.handler.XMPPClientHandler;
19 import org.openhab.core.automation.annotation.ActionInput;
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  * This is the automation engine action handler service for the publishXMPP action.
29  *
30  * @author Pavel Gololobov - Initial contribution
31  */
32 @ThingActionsScope(name = "xmppclient")
33 @NonNullByDefault
34 public class XMPPActions implements ThingActions {
35     private static final Logger logger = LoggerFactory.getLogger(XMPPActions.class);
36     private @Nullable XMPPClientHandler handler;
37
38     @Override
39     public void setThingHandler(@Nullable ThingHandler handler) {
40         this.handler = (XMPPClientHandler) handler;
41     }
42
43     @Override
44     public @Nullable ThingHandler getThingHandler() {
45         return handler;
46     }
47
48     @RuleAction(label = "publish a message", description = "Publish a message using XMPP.")
49     public void publishXMPP(@ActionInput(name = "to", label = "To", description = "Send to") @Nullable String to,
50             @ActionInput(name = "text", label = "Text", description = "Message text") @Nullable String text) {
51         XMPPClientHandler clientHandler = handler;
52         if (clientHandler == null) {
53             logger.warn("XMPP ThingHandler is null");
54             return;
55         }
56
57         XMPPClient connection = clientHandler.getXMPPClient();
58         if (connection == null) {
59             logger.warn("XMPP ThingHandler connection is null");
60             return;
61         }
62         if ((to == null) || (text == null)) {
63             logger.info("Skipping XMPP messaging to {} value {}", to, text);
64             return;
65         }
66         connection.sendMessage(to, text);
67     }
68
69     @RuleAction(label = "publish an image by HTTP", description = "Publish an image by HTTP using XMPP.")
70     public void publishXMPPImageByHTTP(
71             @ActionInput(name = "to", label = "To", description = "Send to") @Nullable String to,
72             @ActionInput(name = "filename", label = "Filename", description = "Image Filename") @Nullable String filename) {
73         XMPPClientHandler clientHandler = handler;
74         if (clientHandler == null) {
75             logger.warn("XMPP ThingHandler is null");
76             return;
77         }
78
79         XMPPClient connection = clientHandler.getXMPPClient();
80         if (connection == null) {
81             logger.warn("XMPP ThingHandler connection is null");
82             return;
83         }
84         if ((to == null) || (filename == null)) {
85             logger.warn("Skipping XMPP messaging to {} value {}", to, filename);
86             return;
87         }
88         connection.sendImageByHTTP(to, filename);
89     }
90
91     public static void publishXMPP(ThingActions actions, @Nullable String to, @Nullable String text) {
92         ((XMPPActions) actions).publishXMPP(to, text);
93     }
94
95     public static void publishXMPPImageByHTTP(ThingActions actions, @Nullable String to, @Nullable String filename) {
96         ((XMPPActions) actions).publishXMPPImageByHTTP(to, filename);
97     }
98 }