2 * Copyright (c) 2010-2024 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.xmppclient.internal.action;
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;
30 * This is the automation engine action handler service for the publishXMPP action.
32 * @author Pavel Gololobov - Initial contribution
34 @Component(scope = ServiceScope.PROTOTYPE, service = XMPPActions.class)
35 @ThingActionsScope(name = "xmppclient")
37 public class XMPPActions implements ThingActions {
38 private static final Logger logger = LoggerFactory.getLogger(XMPPActions.class);
39 private @Nullable XMPPClientHandler handler;
42 public void setThingHandler(@Nullable ThingHandler handler) {
43 this.handler = (XMPPClientHandler) handler;
47 public @Nullable ThingHandler getThingHandler() {
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");
60 XMPPClient connection = clientHandler.getXMPPClient();
61 if (connection == null) {
62 logger.warn("XMPP ThingHandler connection is null");
65 if ((to == null) || (text == null)) {
66 logger.info("Skipping XMPP messaging to {} value {}", to, text);
69 connection.sendMessage(to, text);
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");
82 XMPPClient connection = clientHandler.getXMPPClient();
83 if (connection == null) {
84 logger.warn("XMPP ThingHandler connection is null");
87 if ((to == null) || (filename == null)) {
88 logger.warn("Skipping XMPP messaging to {} value {}", to, filename);
91 connection.sendImageByHTTP(to, filename);
94 public static void publishXMPP(ThingActions actions, @Nullable String to, @Nullable String text) {
95 ((XMPPActions) actions).publishXMPP(to, text);
98 public static void publishXMPPImageByHTTP(ThingActions actions, @Nullable String to, @Nullable String filename) {
99 ((XMPPActions) actions).publishXMPPImageByHTTP(to, filename);