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