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