]> git.basschouten.com Git - openhab-addons.git/blob
7edf1c6e4d3711eeed9a47f94f89178af646683a
[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.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 = "xmpp")
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     public static void publishXMPP(@Nullable ThingActions actions, @Nullable String to, @Nullable String text) {
70         if (actions instanceof XMPPActions) {
71             ((XMPPActions) actions).publishXMPP(to, text);
72         } else {
73             throw new IllegalArgumentException("Actions is not an instance of XMPPActions");
74         }
75     }
76 }