]> git.basschouten.com Git - openhab-addons.git/blob
5a8eec82b3ef13b524b63248b9573759fad81702
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.ArrayList;
16 import java.util.Collections;
17 import java.util.HashMap;
18 import java.util.List;
19 import java.util.Map;
20 import java.util.Set;
21 import java.util.WeakHashMap;
22 import java.util.stream.Collectors;
23 import java.util.stream.Stream;
24
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;
45
46 /**
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.
50  *
51  * @author David Graeff - Initial contribution
52  */
53 @NonNullByDefault
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<>()));
64
65     private MqttService mqttService;
66
67     @Activate
68     public MqttBrokerHandlerFactory(@Reference MqttService mqttService) {
69         this.mqttService = mqttService;
70     }
71
72     @Override
73     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
74         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
75     }
76
77     /**
78      * Add the given broker connection to all listeners.
79      */
80     protected void createdHandler(AbstractBrokerHandler handler) {
81         handlers.add(handler);
82         discoveryTopics.forEach((topic, listenerList) -> {
83             listenerList.forEach(listener -> {
84                 handler.registerDiscoveryListener(listener, topic);
85             });
86         });
87     }
88
89     @Override
90     protected @Nullable ThingHandler createHandler(Thing thing) {
91         if (mqttService == null) {
92             throw new IllegalStateException("MqttService must be bound, before ThingHandlers can be created");
93         }
94         if (!(thing instanceof Bridge)) {
95             throw new IllegalStateException("A bridge type is expected");
96         }
97         final ThingTypeUID thingTypeUID = thing.getThingTypeUID();
98
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);
104         } else {
105             throw new IllegalStateException("Not supported " + thingTypeUID.toString());
106         }
107         createdHandler(handler);
108         return handler;
109     }
110
111     /**
112      * This factory also implements {@link MQTTTopicDiscoveryService} so consumers can subscribe to
113      * a MQTT topic that is registered on all available broker connections.
114      */
115     @Override
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));
121     }
122
123     /**
124      * Unsubscribe a listener from all available broker connections.
125      */
126     @Override
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));
132         });
133     }
134
135     @Override
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);
140             });
141         });
142     }
143 }