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;
16 import java.util.stream.Collectors;
17 import java.util.stream.Stream;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.mqtt.MqttBindingConstants;
22 import org.openhab.binding.mqtt.discovery.MQTTTopicDiscoveryParticipant;
23 import org.openhab.binding.mqtt.discovery.MQTTTopicDiscoveryService;
24 import org.openhab.binding.mqtt.handler.AbstractBrokerHandler;
25 import org.openhab.binding.mqtt.handler.BrokerHandler;
26 import org.openhab.binding.mqtt.handler.SystemBrokerHandler;
27 import org.openhab.core.io.transport.mqtt.MqttService;
28 import org.openhab.core.thing.Bridge;
29 import org.openhab.core.thing.Thing;
30 import org.openhab.core.thing.ThingTypeUID;
31 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
32 import org.openhab.core.thing.binding.ThingHandler;
33 import org.openhab.core.thing.binding.ThingHandlerFactory;
34 import org.osgi.service.component.annotations.Activate;
35 import org.osgi.service.component.annotations.Component;
36 import org.osgi.service.component.annotations.Reference;
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 {
51 private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Stream
52 .of(MqttBindingConstants.BRIDGE_TYPE_SYSTEMBROKER, MqttBindingConstants.BRIDGE_TYPE_BROKER)
53 .collect(Collectors.toSet());
54 private final Logger logger = LoggerFactory.getLogger(MqttBrokerHandlerFactory.class);
55 protected final Map<String, List<MQTTTopicDiscoveryParticipant>> discoveryTopics = new HashMap<>();
56 protected final Set<AbstractBrokerHandler> handlers = Collections
57 .synchronizedSet(Collections.newSetFromMap(new WeakHashMap<>()));
59 private MqttService mqttService;
62 public MqttBrokerHandlerFactory(@Reference MqttService mqttService) {
63 this.mqttService = mqttService;
67 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
68 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
72 * Add the given broker connection to all listeners.
74 protected void createdHandler(AbstractBrokerHandler handler) {
75 handlers.add(handler);
76 discoveryTopics.forEach((topic, listenerList) -> {
77 listenerList.forEach(listener -> {
78 handler.registerDiscoveryListener(listener, topic);
84 protected @Nullable ThingHandler createHandler(Thing thing) {
85 if (mqttService == null) {
86 throw new IllegalStateException("MqttService must be bound, before ThingHandlers can be created");
88 if (!(thing instanceof Bridge)) {
89 throw new IllegalStateException("A bridge type is expected");
91 final ThingTypeUID thingTypeUID = thing.getThingTypeUID();
93 final AbstractBrokerHandler handler;
94 if (thingTypeUID.equals(MqttBindingConstants.BRIDGE_TYPE_SYSTEMBROKER)) {
95 handler = new SystemBrokerHandler((Bridge) thing, mqttService);
96 } else if (thingTypeUID.equals(MqttBindingConstants.BRIDGE_TYPE_BROKER)) {
97 handler = new BrokerHandler((Bridge) thing);
99 throw new IllegalStateException("Not supported " + thingTypeUID.toString());
101 createdHandler(handler);
106 * This factory also implements {@link MQTTTopicDiscoveryService} so consumers can subscribe to
107 * a MQTT topic that is registered on all available broker connections.
110 public void subscribe(MQTTTopicDiscoveryParticipant listener, String topic) {
111 List<MQTTTopicDiscoveryParticipant> listenerList = discoveryTopics.computeIfAbsent(topic,
112 t -> new ArrayList<>());
113 listenerList.add(listener);
114 handlers.forEach(broker -> broker.registerDiscoveryListener(listener, topic));
118 * Unsubscribe a listener from all available broker connections.
121 @SuppressWarnings("null")
122 public void unsubscribe(MQTTTopicDiscoveryParticipant listener) {
123 discoveryTopics.forEach((topic, listenerList) -> {
124 listenerList.remove(listener);
125 handlers.forEach(broker -> broker.unregisterDiscoveryListener(listener, topic));
130 public void publish(String topic, byte[] payload) {
131 handlers.forEach(handler -> {
132 handler.getConnectionAsync().thenAccept(connection -> {
133 connection.publish(topic, payload);