2 * Copyright (c) 2010-2023 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.handler;
15 import java.io.IOException;
16 import java.util.Collection;
17 import java.util.Collections;
18 import java.util.HashMap;
20 import java.util.concurrent.TimeUnit;
22 import org.jivesoftware.smack.SmackException;
23 import org.jivesoftware.smack.XMPPException;
24 import org.openhab.binding.xmppclient.internal.XMPPClient;
25 import org.openhab.binding.xmppclient.internal.action.XMPPActions;
26 import org.openhab.core.thing.Bridge;
27 import org.openhab.core.thing.Channel;
28 import org.openhab.core.thing.ChannelUID;
29 import org.openhab.core.thing.ThingStatus;
30 import org.openhab.core.thing.ThingStatusDetail;
31 import org.openhab.core.thing.binding.BaseBridgeHandler;
32 import org.openhab.core.thing.binding.ThingHandlerService;
33 import org.openhab.core.types.Command;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
38 * The {@link XMPPClientHandler} is responsible for handling commands, which are
39 * sent to one of the channels.
41 * @author Pavel Gololobov - Initial contribution
44 public class XMPPClientHandler extends BaseBridgeHandler {
45 private final Logger logger = LoggerFactory.getLogger(XMPPClientHandler.class);
46 private XMPPClient xmppClient;
47 private XMPPClientConfiguration config;
48 private final Map<ChannelUID, PublishTriggerChannel> channelStateByChannelUID = new HashMap<>();
50 public XMPPClientHandler(Bridge thing) {
54 public XMPPClient getXMPPClient() {
59 public Collection<Class<? extends ThingHandlerService>> getServices() {
60 return Collections.singleton(XMPPActions.class);
64 protected void triggerChannel(ChannelUID channelUID, String event) {
65 super.triggerChannel(channelUID, event);
69 public void handleCommand(ChannelUID channelUID, Command command) {
74 public void initialize() {
75 updateStatus(ThingStatus.UNKNOWN);
76 scheduler.schedule(this::doConnect, 0, TimeUnit.SECONDS);
80 public void dispose() {
81 channelStateByChannelUID.values().forEach(c -> c.stop());
82 channelStateByChannelUID.clear();
83 xmppClient.disconnect();
87 private void doConnect() {
88 config = getConfigAs(XMPPClientConfiguration.class);
89 xmppClient = new XMPPClient();
91 xmppClient.connect(config.host, config.port, config.username, config.domain, config.password);
92 } catch (SmackException | IOException | XMPPException e) {
93 logger.info("XMPP connection error", e);
94 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getMessage());
98 for (Channel channel : thing.getChannels()) {
99 final PublishTriggerChannelConfig channelConfig = channel.getConfiguration()
100 .as(PublishTriggerChannelConfig.class);
101 PublishTriggerChannel c = new PublishTriggerChannel(channelConfig, channel.getUID(), xmppClient, this);
102 channelStateByChannelUID.put(channel.getUID(), c);
103 logger.info("XMPP added channel {} payload {}", channel.getUID().toString(), channelConfig.payload);
105 channelStateByChannelUID.values().forEach(c -> c.start());
107 updateStatus(ThingStatus.ONLINE);