]> git.basschouten.com Git - openhab-addons.git/blob
79c8196a32dad060b4caedb0862a8f546eec7064
[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.mqtt.internal;
14
15 import java.util.Collections;
16 import java.util.Map;
17 import java.util.Set;
18 import java.util.WeakHashMap;
19 import java.util.concurrent.ConcurrentHashMap;
20 import java.util.stream.Collectors;
21 import java.util.stream.Stream;
22
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.openhab.binding.mqtt.MqttBindingConstants;
26 import org.openhab.binding.mqtt.discovery.MQTTTopicDiscoveryParticipant;
27 import org.openhab.binding.mqtt.discovery.MQTTTopicDiscoveryService;
28 import org.openhab.binding.mqtt.handler.AbstractBrokerHandler;
29 import org.openhab.binding.mqtt.handler.BrokerHandler;
30 import org.openhab.binding.mqtt.handler.SystemBrokerHandler;
31 import org.openhab.core.io.transport.mqtt.MqttService;
32 import org.openhab.core.thing.Bridge;
33 import org.openhab.core.thing.Thing;
34 import org.openhab.core.thing.ThingTypeUID;
35 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
36 import org.openhab.core.thing.binding.ThingHandler;
37 import org.openhab.core.thing.binding.ThingHandlerFactory;
38 import org.osgi.service.component.annotations.Activate;
39 import org.osgi.service.component.annotations.Component;
40 import org.osgi.service.component.annotations.Reference;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43
44 /**
45  * The {@link MqttBrokerHandlerFactory} is responsible for creating things and thing
46  * handlers. It keeps reference to all handlers and implements the {@link MQTTTopicDiscoveryService} service
47  * interface, so service consumers can subscribe to a topic on all available broker connections.
48  *
49  * @author David Graeff - Initial contribution
50  */
51 @NonNullByDefault
52 @Component(service = { ThingHandlerFactory.class,
53         MQTTTopicDiscoveryService.class }, configurationPid = "MqttBrokerHandlerFactory")
54 public class MqttBrokerHandlerFactory extends BaseThingHandlerFactory implements MQTTTopicDiscoveryService {
55
56     private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Stream
57             .of(MqttBindingConstants.BRIDGE_TYPE_SYSTEMBROKER, MqttBindingConstants.BRIDGE_TYPE_BROKER)
58             .collect(Collectors.toSet());
59
60     private final Logger logger = LoggerFactory.getLogger(MqttBrokerHandlerFactory.class);
61
62     /**
63      * This Map provides a lookup between a Topic string (key) and a Set of MQTTTopicDiscoveryParticipants (value),
64      * where the Set itself is a list of participants which are subscribed to the respective Topic.
65      */
66     protected final Map<String, Set<MQTTTopicDiscoveryParticipant>> discoveryTopics = new ConcurrentHashMap<>();
67
68     /**
69      * This Set contains a list of all the Broker handlers that have been created by this factory
70      */
71     protected final Set<AbstractBrokerHandler> handlers = Collections
72             .synchronizedSet(Collections.newSetFromMap(new WeakHashMap<>()));
73
74     private MqttService mqttService;
75
76     @Activate
77     public MqttBrokerHandlerFactory(@Reference MqttService mqttService) {
78         this.mqttService = mqttService;
79     }
80
81     @Override
82     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
83         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
84     }
85
86     /**
87      * Add the given broker handler to the list of known handlers. And then iterate over all topics and their respective
88      * list of listeners, and register the respective new listener and topic with the given new broker handler.
89      */
90     protected void createdHandler(AbstractBrokerHandler handler) {
91         handlers.add(handler);
92         discoveryTopics.forEach((topic, listeners) -> {
93             listeners.forEach(listener -> {
94                 handler.registerDiscoveryListener(listener, topic);
95             });
96         });
97     }
98
99     @Override
100     protected @Nullable ThingHandler createHandler(Thing thing) {
101         if (mqttService == null) {
102             throw new IllegalStateException("MqttService must be bound, before ThingHandlers can be created");
103         }
104         if (!(thing instanceof Bridge)) {
105             throw new IllegalStateException("A bridge type is expected");
106         }
107         final ThingTypeUID thingTypeUID = thing.getThingTypeUID();
108
109         final AbstractBrokerHandler handler;
110         if (thingTypeUID.equals(MqttBindingConstants.BRIDGE_TYPE_SYSTEMBROKER)) {
111             handler = new SystemBrokerHandler((Bridge) thing, mqttService);
112         } else if (thingTypeUID.equals(MqttBindingConstants.BRIDGE_TYPE_BROKER)) {
113             handler = new BrokerHandler((Bridge) thing);
114         } else {
115             throw new IllegalStateException("Not supported " + thingTypeUID.toString());
116         }
117         createdHandler(handler);
118         return handler;
119     }
120
121     /**
122      * This factory also implements {@link MQTTTopicDiscoveryService} so consumers can subscribe to
123      * a MQTT topic that is registered on all available broker connections.
124      *
125      * Checks each topic, and if the listener is not already in the listener list for that topic, adds itself from that
126      * list, and registers itself and the respective topic with all the known brokers.
127      */
128     @Override
129     @SuppressWarnings("null")
130     public void subscribe(MQTTTopicDiscoveryParticipant listener, String topic) {
131         Set<MQTTTopicDiscoveryParticipant> listeners = discoveryTopics.computeIfAbsent(topic,
132                 t -> ConcurrentHashMap.newKeySet());
133         if (listeners.add(listener)) {
134             handlers.forEach(broker -> broker.registerDiscoveryListener(listener, topic));
135         }
136     }
137
138     /**
139      * This factory also implements {@link MQTTTopicDiscoveryService} so consumers can unsubscribe from
140      * a MQTT topic that is registered on all available broker connections.
141      *
142      * Checks each topic, and if the listener is in the listener list for that topic, removes itself from that list, and
143      * unregisters itself and the respective topic from all the known brokers.
144      */
145     @Override
146     public void unsubscribe(MQTTTopicDiscoveryParticipant listener) {
147         discoveryTopics.forEach((topic, listeners) -> {
148             if (listeners.remove(listener)) {
149                 handlers.forEach(broker -> broker.unregisterDiscoveryListener(listener, topic));
150             }
151         });
152     }
153
154     @Override
155     public void publish(String topic, byte[] payload, int qos, boolean retain) {
156         handlers.forEach(handler -> {
157             handler.getConnectionAsync().thenAccept(connection -> {
158                 connection.publish(topic, payload, qos, retain);
159             });
160         });
161     }
162 }