]> git.basschouten.com Git - openhab-addons.git/blob
1f98ed65d4f02eb9ecdad8b0a798c9d7dc7ed5fb
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.*;
16 import java.util.stream.Collectors;
17 import java.util.stream.Stream;
18
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;
39
40 /**
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.
44  *
45  * @author David Graeff - Initial contribution
46  */
47 @NonNullByDefault
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<>()));
58
59     private MqttService mqttService;
60
61     @Activate
62     public MqttBrokerHandlerFactory(@Reference MqttService mqttService) {
63         this.mqttService = mqttService;
64     }
65
66     @Override
67     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
68         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
69     }
70
71     /**
72      * Add the given broker connection to all listeners.
73      */
74     protected void createdHandler(AbstractBrokerHandler handler) {
75         handlers.add(handler);
76         discoveryTopics.forEach((topic, listenerList) -> {
77             listenerList.forEach(listener -> {
78                 handler.registerDiscoveryListener(listener, topic);
79             });
80         });
81     }
82
83     @Override
84     protected @Nullable ThingHandler createHandler(Thing thing) {
85         if (mqttService == null) {
86             throw new IllegalStateException("MqttService must be bound, before ThingHandlers can be created");
87         }
88         if (!(thing instanceof Bridge)) {
89             throw new IllegalStateException("A bridge type is expected");
90         }
91         final ThingTypeUID thingTypeUID = thing.getThingTypeUID();
92
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);
98         } else {
99             throw new IllegalStateException("Not supported " + thingTypeUID.toString());
100         }
101         createdHandler(handler);
102         return handler;
103     }
104
105     /**
106      * This factory also implements {@link MQTTTopicDiscoveryService} so consumers can subscribe to
107      * a MQTT topic that is registered on all available broker connections.
108      */
109     @Override
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));
115     }
116
117     /**
118      * Unsubscribe a listener from all available broker connections.
119      */
120     @Override
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));
126         });
127     }
128
129     @Override
130     public void publish(String topic, byte[] payload) {
131         handlers.forEach(handler -> {
132             handler.getConnectionAsync().thenAccept(connection -> {
133                 connection.publish(topic, payload);
134             });
135         });
136     }
137 }