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