]> git.basschouten.com Git - openhab-addons.git/blob
0f3624c8ca3c2f388f47e359398f6f0e4416cfcd
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.freeboxos.internal.discovery;
14
15 import static org.openhab.binding.freeboxos.internal.FreeboxOsBindingConstants.*;
16 import static org.openhab.binding.freeboxos.internal.config.FreeboxOsConfiguration.*;
17
18 import java.util.Set;
19
20 import javax.jmdns.ServiceInfo;
21
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;
32
33 /**
34  * The {@link ApiDiscoveryParticipant} is responsible for discovering the various servers flavors of bridges thing using
35  * mDNS discovery service
36  *
37  * @author GaĆ«l L'hopital - Initial contribution
38  */
39 @Component
40 @NonNullByDefault
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";
45
46     private final Logger logger = LoggerFactory.getLogger(ApiDiscoveryParticipant.class);
47
48     @Override
49     public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
50         return BRIDGE_TYPE_UIDS;
51     }
52
53     @Override
54     public String getServiceType() {
55         return "_fbx-api._tcp.local.";
56     }
57
58     @Override
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()
68                 : null;
69     }
70
71     @Override
72     public @Nullable ThingUID getThingUID(ServiceInfo service) {
73         String domain = service.getPropertyString(DOMAIN_PROPERTY);
74         if (domain != null) {
75             String[] elements = domain.split("\\.");
76             if (elements.length > 0) {
77                 return new ThingUID(BRIDGE_TYPE_API, elements[0]);
78             }
79         }
80         return null;
81     }
82 }