]> git.basschouten.com Git - openhab-addons.git/blob
93bac0845c54fe61347a40c8d6dac6da7745b69a
[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;
14
15 import java.io.IOException;
16 import java.util.HashSet;
17 import java.util.Set;
18
19 import org.jivesoftware.smack.*;
20 import org.jivesoftware.smack.chat2.Chat;
21 import org.jivesoftware.smack.chat2.ChatManager;
22 import org.jivesoftware.smack.chat2.IncomingChatMessageListener;
23 import org.jivesoftware.smack.packet.Message;
24 import org.jivesoftware.smack.tcp.XMPPTCPConnection;
25 import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration;
26 import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
27 import org.jivesoftware.smackx.disco.packet.DiscoverInfo.Identity;
28 import org.jxmpp.jid.EntityBareJid;
29 import org.jxmpp.jid.impl.JidCreate;
30 import org.jxmpp.stringprep.XmppStringprepException;
31 import org.openhab.binding.xmppclient.handler.XMPPClientMessageSubscriber;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 /**
36  * The {@link XMPPClient} is lib for handling XMPP connection and messaging
37  *
38  * @author Pavel Gololobov - Initial contribution
39  */
40 public class XMPPClient implements IncomingChatMessageListener, ConnectionListener {
41     private final Logger logger = LoggerFactory.getLogger(XMPPClient.class);
42     private AbstractXMPPConnection connection;
43     private ChatManager chatManager;
44     private Set<XMPPClientMessageSubscriber> subscribers = new HashSet<>();
45
46     public void subscribe(XMPPClientMessageSubscriber channel) {
47         logger.debug("Channel {} subscribed", channel.getName());
48         subscribers.add(channel);
49     }
50
51     public void unsubscribe(XMPPClientMessageSubscriber channel) {
52         logger.debug("Channel {} unsubscribed", channel.getName());
53         subscribers.remove(channel);
54     }
55
56     public void connect(String host, Integer port, String login, String domain, String password)
57             throws XMPPException, SmackException, IOException {
58         disconnect();
59         String serverHost = domain;
60         if ((host != null) && !host.isEmpty()) {
61             serverHost = host;
62         }
63
64         XMPPTCPConnectionConfiguration config = XMPPTCPConnectionConfiguration.builder() //
65                 .setHost(serverHost) //
66                 .setPort(port) //
67                 .setUsernameAndPassword(login, password) //
68                 .setXmppDomain(domain) //
69                 .build();
70
71         connection = new XMPPTCPConnection(config);
72         connection.addConnectionListener(this);
73
74         ReconnectionManager reconnectionManager = ReconnectionManager.getInstanceFor(connection);
75         reconnectionManager.enableAutomaticReconnection();
76
77         Identity identity = new Identity("client", "openHAB", "bot");
78         ServiceDiscoveryManager sdm = ServiceDiscoveryManager.getInstanceFor(connection);
79         sdm.setIdentity(identity);
80
81         try {
82             connection.connect().login();
83         } catch (InterruptedException ex) {
84         }
85
86         chatManager = ChatManager.getInstanceFor(connection);
87         chatManager.addIncomingListener(this);
88     }
89
90     public void disconnect() {
91         if (connection != null) {
92             connection.disconnect();
93         }
94     }
95
96     public void sendMessage(String to, String message) {
97         if (connection == null) {
98             logger.warn("XMPP connection is null");
99             return;
100         }
101         if (chatManager == null) {
102             logger.warn("XMPP chatManager is null");
103             return;
104         }
105         try {
106             EntityBareJid jid = JidCreate.entityBareFrom(to);
107             Chat chat = chatManager.chatWith(jid);
108             chat.send(message);
109         } catch (XmppStringprepException | SmackException.NotConnectedException | InterruptedException e) {
110             logger.info("XMPP message sending error", e);
111         }
112     }
113
114     @Override
115     public void newIncomingMessage(EntityBareJid from, Message message, Chat chat) {
116         logger.debug("XMPP {} says {}", from.asBareJid().toString(), message.getBody());
117         for (XMPPClientMessageSubscriber subscriber : subscribers) {
118             logger.debug("Push to subscriber {}", subscriber.getName());
119             subscriber.processMessage(from.asBareJid().toString(), message.getBody());
120         }
121     }
122
123     @Override
124     public void connected(XMPPConnection connection) {
125         logger.debug("Connected to XMPP server.");
126     }
127
128     @Override
129     public void authenticated(XMPPConnection connection, boolean resumed) {
130         logger.debug("Authenticated to XMPP server.");
131     }
132
133     @Override
134     public void connectionClosed() {
135         logger.debug("XMPP connection was closed.");
136     }
137
138     @Override
139     public void connectionClosedOnError(Exception e) {
140         logger.debug("Connection to XMPP server was lost.");
141         if (connection != null) {
142             connection.disconnect();
143             try {
144                 connection.connect().login();
145             } catch (SmackException | IOException | XMPPException | InterruptedException ex) {
146                 logger.info("XMPP connection error", ex);
147             }
148         }
149     }
150 }