2 * Copyright (c) 2010-2023 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.freeboxos.internal.discovery;
15 import static org.openhab.binding.freeboxos.internal.FreeboxOsBindingConstants.*;
16 import static org.openhab.binding.freeboxos.internal.config.FreeboxOsConfiguration.*;
20 import javax.jmdns.ServiceInfo;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.openhab.core.config.discovery.DiscoveryResult;
25 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
26 import org.openhab.core.config.discovery.mdns.MDNSDiscoveryParticipant;
27 import org.openhab.core.thing.ThingTypeUID;
28 import org.openhab.core.thing.ThingUID;
29 import org.osgi.service.component.annotations.Component;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
34 * The {@link ApiDiscoveryParticipant} is responsible for discovering the various servers flavors of bridges thing using
35 * mDNS discovery service
37 * @author Gaƫl L'hopital - Initial contribution
41 public class ApiDiscoveryParticipant implements MDNSDiscoveryParticipant {
42 private static final String DOMAIN_PROPERTY = "api_domain";
43 private static final String PORT_PROPERTY = "https_port";
44 private static final String HTTPS_PROPERTY = "https_available";
46 private final Logger logger = LoggerFactory.getLogger(ApiDiscoveryParticipant.class);
49 public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
50 return BRIDGE_TYPE_UIDS;
54 public String getServiceType() {
55 return "_fbx-api._tcp.local.";
59 public @Nullable DiscoveryResult createResult(ServiceInfo service) {
60 logger.debug("createResult ServiceInfo: {}", service);
61 ThingUID thingUID = getThingUID(service);
62 return thingUID != null
63 ? DiscoveryResultBuilder.create(thingUID).withLabel("Bridge Freebox OS")
64 .withRepresentationProperty(API_DOMAIN)
65 .withProperty(HTTPS_AVAILABLE, "1".equals(service.getPropertyString(HTTPS_PROPERTY)))
66 .withProperty(HTTPS_PORT, service.getPropertyString(PORT_PROPERTY))
67 .withProperty(API_DOMAIN, service.getPropertyString(DOMAIN_PROPERTY)).build()
72 public @Nullable ThingUID getThingUID(ServiceInfo service) {
73 String domain = service.getPropertyString(DOMAIN_PROPERTY);
75 String[] elements = domain.split("\\.");
76 if (elements.length > 0) {
77 return new ThingUID(BRIDGE_TYPE_API, elements[0]);