]> git.basschouten.com Git - openhab-addons.git/blob
dfd8dbce90e909f9daed9b854f1cc19d1d87e2d3
[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.discovery;
14
15 import java.util.HashMap;
16 import java.util.Map;
17 import java.util.stream.Collectors;
18 import java.util.stream.Stream;
19
20 import org.openhab.binding.mqtt.MqttBindingConstants;
21 import org.openhab.core.config.discovery.AbstractDiscoveryService;
22 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
23 import org.openhab.core.config.discovery.DiscoveryService;
24 import org.openhab.core.io.transport.mqtt.MqttBrokerConnection;
25 import org.openhab.core.io.transport.mqtt.MqttService;
26 import org.openhab.core.io.transport.mqtt.MqttServiceObserver;
27 import org.openhab.core.thing.ThingUID;
28 import org.osgi.service.component.annotations.Activate;
29 import org.osgi.service.component.annotations.Component;
30 import org.osgi.service.component.annotations.Deactivate;
31 import org.osgi.service.component.annotations.Reference;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 /**
36  * The {@link MqttServiceDiscoveryService} is responsible for discovering connections on
37  * the MqttService shared connection pool.
38  *
39  * @author David Graeff - Initial contribution
40  */
41 @Component(service = DiscoveryService.class, configurationPid = "discovery.systemmqttbroker")
42 public class MqttServiceDiscoveryService extends AbstractDiscoveryService implements MqttServiceObserver {
43     private final Logger logger = LoggerFactory.getLogger(MqttServiceDiscoveryService.class);
44     MqttService mqttService;
45
46     public MqttServiceDiscoveryService() {
47         super(Stream.of(MqttBindingConstants.BRIDGE_TYPE_SYSTEMBROKER, MqttBindingConstants.BRIDGE_TYPE_BROKER)
48                 .collect(Collectors.toSet()), 0, true);
49     }
50
51     @Override
52     @Activate
53     protected void activate(Map<String, Object> config) {
54         super.activate(config);
55     }
56
57     @Override
58     @Deactivate
59     protected void deactivate() {
60         super.deactivate();
61     }
62
63     @Reference
64     public void setMqttService(MqttService service) {
65         mqttService = service;
66     }
67
68     public void unsetMqttService(MqttService service) {
69         mqttService = null;
70     }
71
72     @Override
73     protected void startScan() {
74         mqttService.addBrokersListener(this);
75         mqttService.getAllBrokerConnections().forEach((brokerId, broker) -> brokerAdded(brokerId, broker));
76         stopScan();
77     }
78
79     @Override
80     protected void startBackgroundDiscovery() {
81         if (mqttService == null) {
82             return;
83         }
84         mqttService.addBrokersListener(this);
85         mqttService.getAllBrokerConnections().forEach((brokerId, broker) -> brokerAdded(brokerId, broker));
86     }
87
88     @Override
89     protected void stopBackgroundDiscovery() {
90         if (mqttService == null) {
91             return;
92         }
93         mqttService.removeBrokersListener(this);
94     }
95
96     @Override
97     public void brokerAdded(String brokerId, MqttBrokerConnection broker) {
98         logger.trace("Found broker connection {}", brokerId);
99
100         Map<String, Object> properties = new HashMap<>();
101         properties.put("host", broker.getHost());
102         properties.put("port", broker.getPort());
103         properties.put("brokerid", brokerId);
104         ThingUID thingUID;
105         thingUID = new ThingUID(MqttBindingConstants.BRIDGE_TYPE_SYSTEMBROKER, brokerId);
106         thingDiscovered(DiscoveryResultBuilder.create(thingUID).withProperties(properties)
107                 .withRepresentationProperty("brokerid").withLabel("MQTT Broker").build());
108     }
109
110     @Override
111     public void brokerRemoved(String brokerId, MqttBrokerConnection broker) {
112         ThingUID thingUID;
113         thingUID = new ThingUID(MqttBindingConstants.BRIDGE_TYPE_SYSTEMBROKER, brokerId);
114         thingRemoved(thingUID);
115     }
116 }