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