]> git.basschouten.com Git - openhab-addons.git/blob
ba3ad4bba6754eac28f01d40d39343a90011b3a8
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.handler;
14
15 import java.io.IOException;
16 import java.util.Collection;
17 import java.util.Collections;
18 import java.util.HashMap;
19 import java.util.Map;
20 import java.util.concurrent.TimeUnit;
21
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;
36
37 /**
38  * The {@link XMPPClientHandler} is responsible for handling commands, which are
39  * sent to one of the channels.
40  *
41  * @author Pavel Gololobov - Initial contribution
42  */
43
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<>();
49
50     public XMPPClientHandler(Bridge thing) {
51         super(thing);
52     }
53
54     public XMPPClient getXMPPClient() {
55         return xmppClient;
56     }
57
58     @Override
59     public Collection<Class<? extends ThingHandlerService>> getServices() {
60         return Collections.singleton(XMPPActions.class);
61     }
62
63     @Override
64     protected void triggerChannel(ChannelUID channelUID, String event) {
65         super.triggerChannel(channelUID, event);
66     }
67
68     @Override
69     public void handleCommand(ChannelUID channelUID, Command command) {
70         // not supported
71     }
72
73     @Override
74     public void initialize() {
75         updateStatus(ThingStatus.UNKNOWN);
76         scheduler.schedule(this::doConnect, 0, TimeUnit.SECONDS);
77     }
78
79     @Override
80     public void dispose() {
81         channelStateByChannelUID.values().forEach(c -> c.stop());
82         channelStateByChannelUID.clear();
83         xmppClient.disconnect();
84         super.dispose();
85     }
86
87     private void doConnect() {
88         config = getConfigAs(XMPPClientConfiguration.class);
89         xmppClient = new XMPPClient();
90         try {
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());
95             return;
96         }
97
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);
104         }
105         channelStateByChannelUID.values().forEach(c -> c.start());
106
107         updateStatus(ThingStatus.ONLINE);
108     }
109 }