2 * Copyright (c) 2010-2022 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.discovery;
15 import java.util.HashMap;
17 import java.util.stream.Collectors;
18 import java.util.stream.Stream;
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;
36 * The {@link MqttServiceDiscoveryService} is responsible for discovering connections on
37 * the MqttService shared connection pool.
39 * @author David Graeff - Initial contribution
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;
46 public MqttServiceDiscoveryService() {
47 super(Stream.of(MqttBindingConstants.BRIDGE_TYPE_SYSTEMBROKER, MqttBindingConstants.BRIDGE_TYPE_BROKER)
48 .collect(Collectors.toSet()), 0, true);
53 protected void activate(Map<String, Object> config) {
54 super.activate(config);
59 protected void deactivate() {
64 public void setMqttService(MqttService service) {
65 mqttService = service;
68 public void unsetMqttService(MqttService service) {
73 protected void startScan() {
74 mqttService.addBrokersListener(this);
75 mqttService.getAllBrokerConnections().forEach((brokerId, broker) -> brokerAdded(brokerId, broker));
80 protected void startBackgroundDiscovery() {
81 if (mqttService == null) {
84 mqttService.addBrokersListener(this);
85 mqttService.getAllBrokerConnections().forEach((brokerId, broker) -> brokerAdded(brokerId, broker));
89 protected void stopBackgroundDiscovery() {
90 if (mqttService == null) {
93 mqttService.removeBrokersListener(this);
97 public void brokerAdded(String brokerId, MqttBrokerConnection broker) {
98 logger.trace("Found broker connection {}", brokerId);
100 Map<String, Object> properties = new HashMap<>();
101 properties.put("host", broker.getHost());
102 properties.put("port", broker.getPort());
103 properties.put("brokerid", brokerId);
105 thingUID = new ThingUID(MqttBindingConstants.BRIDGE_TYPE_SYSTEMBROKER, brokerId);
106 thingDiscovered(DiscoveryResultBuilder.create(thingUID).withProperties(properties)
107 .withRepresentationProperty("brokerid").withLabel("MQTT Broker").build());
111 public void brokerRemoved(String brokerId, MqttBrokerConnection broker) {
113 thingUID = new ThingUID(MqttBindingConstants.BRIDGE_TYPE_SYSTEMBROKER, brokerId);
114 thingRemoved(thingUID);