2 * Copyright (c) 2010-2022 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.mqtt.internal;
15 import java.util.Collections;
18 import java.util.WeakHashMap;
19 import java.util.concurrent.ConcurrentHashMap;
20 import java.util.stream.Collectors;
21 import java.util.stream.Stream;
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.core.thing.Bridge;
31 import org.openhab.core.thing.Thing;
32 import org.openhab.core.thing.ThingTypeUID;
33 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
34 import org.openhab.core.thing.binding.ThingHandler;
35 import org.openhab.core.thing.binding.ThingHandlerFactory;
36 import org.osgi.service.component.annotations.Component;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
41 * The {@link MqttBrokerHandlerFactory} is responsible for creating things and thing
42 * handlers. It keeps reference to all handlers and implements the {@link MQTTTopicDiscoveryService} service
43 * interface, so service consumers can subscribe to a topic on all available broker connections.
45 * @author David Graeff - Initial contribution
48 @Component(service = { ThingHandlerFactory.class,
49 MQTTTopicDiscoveryService.class }, configurationPid = "MqttBrokerHandlerFactory")
50 public class MqttBrokerHandlerFactory extends BaseThingHandlerFactory implements MQTTTopicDiscoveryService {
52 private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Stream
53 .of(MqttBindingConstants.BRIDGE_TYPE_BROKER).collect(Collectors.toSet());
55 private final Logger logger = LoggerFactory.getLogger(MqttBrokerHandlerFactory.class);
58 * This Map provides a lookup between a Topic string (key) and a Set of MQTTTopicDiscoveryParticipants (value),
59 * where the Set itself is a list of participants which are subscribed to the respective Topic.
61 protected final Map<String, Set<MQTTTopicDiscoveryParticipant>> discoveryTopics = new ConcurrentHashMap<>();
64 * This Set contains a list of all the Broker handlers that have been created by this factory
66 protected final Set<AbstractBrokerHandler> handlers = Collections
67 .synchronizedSet(Collections.newSetFromMap(new WeakHashMap<>()));
70 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
71 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
75 * Add the given broker handler to the list of known handlers. And then iterate over all topics and their respective
76 * list of listeners, and register the respective new listener and topic with the given new broker handler.
78 protected void createdHandler(AbstractBrokerHandler handler) {
79 handlers.add(handler);
80 discoveryTopics.forEach((topic, listeners) -> {
81 listeners.forEach(listener -> {
82 handler.registerDiscoveryListener(listener, topic);
88 protected @Nullable ThingHandler createHandler(Thing thing) {
89 if (!(thing instanceof Bridge)) {
90 throw new IllegalStateException("A bridge type is expected");
92 final ThingTypeUID thingTypeUID = thing.getThingTypeUID();
94 final AbstractBrokerHandler handler;
95 if (thingTypeUID.equals(MqttBindingConstants.BRIDGE_TYPE_BROKER)) {
96 handler = new BrokerHandler((Bridge) thing);
98 throw new IllegalStateException("Not supported " + thingTypeUID.toString());
100 createdHandler(handler);
105 * This factory also implements {@link MQTTTopicDiscoveryService} so consumers can subscribe to
106 * a MQTT topic that is registered on all available broker connections.
108 * Checks each topic, and if the listener is not already in the listener list for that topic, adds itself from that
109 * list, and registers itself and the respective topic with all the known brokers.
112 @SuppressWarnings("null")
113 public void subscribe(MQTTTopicDiscoveryParticipant listener, String topic) {
114 Set<MQTTTopicDiscoveryParticipant> listeners = discoveryTopics.computeIfAbsent(topic,
115 t -> ConcurrentHashMap.newKeySet());
116 if (listeners.add(listener)) {
117 handlers.forEach(broker -> broker.registerDiscoveryListener(listener, topic));
122 * This factory also implements {@link MQTTTopicDiscoveryService} so consumers can unsubscribe from
123 * a MQTT topic that is registered on all available broker connections.
125 * Checks each topic, and if the listener is in the listener list for that topic, removes itself from that list, and
126 * unregisters itself and the respective topic from all the known brokers.
129 public void unsubscribe(MQTTTopicDiscoveryParticipant listener) {
130 discoveryTopics.forEach((topic, listeners) -> {
131 if (listeners.remove(listener)) {
132 handlers.forEach(broker -> broker.unregisterDiscoveryListener(listener, topic));
138 public void publish(String topic, byte[] payload, int qos, boolean retain) {
139 handlers.forEach(handler -> {
140 handler.getConnectionAsync().thenAccept(connection -> {
141 connection.publish(topic, payload, qos, retain);