]> git.basschouten.com Git - openhab-addons.git/blob
bc73a2f87eedb02e6c8fb19ad0cfd85a21aecd98
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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 org.openhab.binding.xmppclient.internal.XMPPClient;
16 import org.openhab.core.thing.ChannelUID;
17
18 /**
19  * Subscribes to a chat and calls {@link AbstractBrokerHandler#triggerChannel(ChannelUID, String)} if a value has been
20  * received.
21  *
22  * @author Pavel Gololobov - Initial contribution
23  */
24 public class PublishTriggerChannel implements XMPPClientMessageSubscriber {
25     private final XMPPClient connection;
26     private final PublishTriggerChannelConfig config;
27     private final ChannelUID uid;
28     private final XMPPClientHandler handler;
29
30     PublishTriggerChannel(PublishTriggerChannelConfig config, ChannelUID uid, XMPPClient connection,
31             XMPPClientHandler handler) {
32         this.config = config;
33         this.uid = uid;
34         this.connection = connection;
35         this.handler = handler;
36     }
37
38     public void start() {
39         connection.subscribe(this);
40     }
41
42     public void stop() {
43         connection.unsubscribe(this);
44     }
45
46     @Override
47     public void processMessage(String from, String payload) {
48         // Check condition if exists
49         String expectedPayload = config.payload;
50         if ((expectedPayload != null) && (!expectedPayload.isEmpty()) && !payload.equals(expectedPayload)) {
51             return;
52         }
53         String eventValue = "";
54         if (!config.separator.isEmpty()) {
55             eventValue = from + config.separator + payload;
56         } else {
57             eventValue = payload;
58         }
59         handler.triggerChannel(uid, eventValue);
60     }
61
62     @Override
63     public String getName() {
64         return uid.toString();
65     }
66 }