2 * Copyright (c) 2010-2021 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.*;
23 import org.openhab.binding.xmppclient.internal.XMPPClient;
24 import org.openhab.binding.xmppclient.internal.action.XMPPActions;
25 import org.openhab.core.thing.Bridge;
26 import org.openhab.core.thing.Channel;
27 import org.openhab.core.thing.ChannelUID;
28 import org.openhab.core.thing.ThingStatus;
29 import org.openhab.core.thing.ThingStatusDetail;
30 import org.openhab.core.thing.binding.BaseBridgeHandler;
31 import org.openhab.core.thing.binding.ThingHandlerService;
32 import org.openhab.core.types.Command;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
37 * The {@link XMPPClientHandler} is responsible for handling commands, which are
38 * sent to one of the channels.
40 * @author Pavel Gololobov - Initial contribution
43 public class XMPPClientHandler extends BaseBridgeHandler {
44 private final Logger logger = LoggerFactory.getLogger(XMPPClientHandler.class);
45 private XMPPClient xmppClient;
46 private XMPPClientConfiguration config;
47 private final Map<ChannelUID, PublishTriggerChannel> channelStateByChannelUID = new HashMap<>();
49 public XMPPClientHandler(Bridge thing) {
53 public XMPPClient getXMPPClient() {
58 public Collection<Class<? extends ThingHandlerService>> getServices() {
59 return Collections.singleton(XMPPActions.class);
63 protected void triggerChannel(ChannelUID channelUID, String event) {
64 super.triggerChannel(channelUID, event);
68 public void handleCommand(ChannelUID channelUID, Command command) {
73 public void initialize() {
74 updateStatus(ThingStatus.UNKNOWN);
75 scheduler.schedule(this::doConnect, 0, TimeUnit.SECONDS);
79 public void dispose() {
80 channelStateByChannelUID.values().forEach(c -> c.stop());
81 channelStateByChannelUID.clear();
82 xmppClient.disconnect();
86 private void doConnect() {
87 config = getConfigAs(XMPPClientConfiguration.class);
88 xmppClient = new XMPPClient();
90 xmppClient.connect(config.host, config.port, config.username, config.domain, config.password);
91 } catch (SmackException | IOException | XMPPException e) {
92 logger.info("XMPP connection error", e);
93 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getMessage());
97 for (Channel channel : thing.getChannels()) {
98 final PublishTriggerChannelConfig channelConfig = channel.getConfiguration()
99 .as(PublishTriggerChannelConfig.class);
100 PublishTriggerChannel c = new PublishTriggerChannel(channelConfig, channel.getUID(), xmppClient, this);
101 channelStateByChannelUID.put(channel.getUID(), c);
102 logger.info("XMPP added channel {} payload {}", channel.getUID().toString(), channelConfig.payload);
104 channelStateByChannelUID.values().forEach(c -> c.start());
106 updateStatus(ThingStatus.ONLINE);