2 * Copyright (c) 2010-2020 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.ArrayList;
16 import java.util.Collections;
17 import java.util.HashMap;
18 import java.util.List;
21 import java.util.WeakHashMap;
22 import java.util.stream.Collectors;
23 import java.util.stream.Stream;
25 import org.eclipse.jdt.annotation.NonNullByDefault;
26 import org.eclipse.jdt.annotation.Nullable;
27 import org.openhab.binding.mqtt.MqttBindingConstants;
28 import org.openhab.binding.mqtt.discovery.MQTTTopicDiscoveryParticipant;
29 import org.openhab.binding.mqtt.discovery.MQTTTopicDiscoveryService;
30 import org.openhab.binding.mqtt.handler.AbstractBrokerHandler;
31 import org.openhab.binding.mqtt.handler.BrokerHandler;
32 import org.openhab.binding.mqtt.handler.SystemBrokerHandler;
33 import org.openhab.core.io.transport.mqtt.MqttService;
34 import org.openhab.core.thing.Bridge;
35 import org.openhab.core.thing.Thing;
36 import org.openhab.core.thing.ThingTypeUID;
37 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
38 import org.openhab.core.thing.binding.ThingHandler;
39 import org.openhab.core.thing.binding.ThingHandlerFactory;
40 import org.osgi.service.component.annotations.Activate;
41 import org.osgi.service.component.annotations.Component;
42 import org.osgi.service.component.annotations.Reference;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
47 * The {@link MqttBrokerHandlerFactory} is responsible for creating things and thing
48 * handlers. It keeps reference to all handlers and implements the {@link MQTTTopicDiscoveryService} service
49 * interface, so service consumers can subscribe to a topic on all available broker connections.
51 * @author David Graeff - Initial contribution
54 @Component(service = { ThingHandlerFactory.class,
55 MQTTTopicDiscoveryService.class }, configurationPid = "MqttBrokerHandlerFactory")
56 public class MqttBrokerHandlerFactory extends BaseThingHandlerFactory implements MQTTTopicDiscoveryService {
57 private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Stream
58 .of(MqttBindingConstants.BRIDGE_TYPE_SYSTEMBROKER, MqttBindingConstants.BRIDGE_TYPE_BROKER)
59 .collect(Collectors.toSet());
60 private final Logger logger = LoggerFactory.getLogger(MqttBrokerHandlerFactory.class);
61 protected final Map<String, List<MQTTTopicDiscoveryParticipant>> discoveryTopics = new HashMap<>();
62 protected final Set<AbstractBrokerHandler> handlers = Collections
63 .synchronizedSet(Collections.newSetFromMap(new WeakHashMap<>()));
65 private MqttService mqttService;
68 public MqttBrokerHandlerFactory(@Reference MqttService mqttService) {
69 this.mqttService = mqttService;
73 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
74 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
78 * Add the given broker connection to all listeners.
80 protected void createdHandler(AbstractBrokerHandler handler) {
81 handlers.add(handler);
82 discoveryTopics.forEach((topic, listenerList) -> {
83 listenerList.forEach(listener -> {
84 handler.registerDiscoveryListener(listener, topic);
90 protected @Nullable ThingHandler createHandler(Thing thing) {
91 if (mqttService == null) {
92 throw new IllegalStateException("MqttService must be bound, before ThingHandlers can be created");
94 if (!(thing instanceof Bridge)) {
95 throw new IllegalStateException("A bridge type is expected");
97 final ThingTypeUID thingTypeUID = thing.getThingTypeUID();
99 final AbstractBrokerHandler handler;
100 if (thingTypeUID.equals(MqttBindingConstants.BRIDGE_TYPE_SYSTEMBROKER)) {
101 handler = new SystemBrokerHandler((Bridge) thing, mqttService);
102 } else if (thingTypeUID.equals(MqttBindingConstants.BRIDGE_TYPE_BROKER)) {
103 handler = new BrokerHandler((Bridge) thing);
105 throw new IllegalStateException("Not supported " + thingTypeUID.toString());
107 createdHandler(handler);
112 * This factory also implements {@link MQTTTopicDiscoveryService} so consumers can subscribe to
113 * a MQTT topic that is registered on all available broker connections.
116 public void subscribe(MQTTTopicDiscoveryParticipant listener, String topic) {
117 List<MQTTTopicDiscoveryParticipant> listenerList = discoveryTopics.computeIfAbsent(topic,
118 t -> new ArrayList<>());
119 listenerList.add(listener);
120 handlers.forEach(broker -> broker.registerDiscoveryListener(listener, topic));
124 * Unsubscribe a listener from all available broker connections.
127 @SuppressWarnings("null")
128 public void unsubscribe(MQTTTopicDiscoveryParticipant listener) {
129 discoveryTopics.forEach((topic, listenerList) -> {
130 listenerList.remove(listener);
131 handlers.forEach(broker -> broker.unregisterDiscoveryListener(listener, topic));
136 public void publish(String topic, byte[] payload, int qos, boolean retain) {
137 handlers.forEach(handler -> {
138 handler.getConnectionAsync().thenAccept(connection -> {
139 connection.publish(topic, payload, qos, retain);