]> git.basschouten.com Git - openhab-addons.git/blob
9131a3f8a7ab84a5cc7a36c0dd454fd8bb278927
[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.amplipi.internal.discovery;
14
15 import java.net.InetAddress;
16 import java.util.Set;
17
18 import javax.jmdns.ServiceInfo;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.binding.amplipi.internal.AmpliPiBindingConstants;
23 import org.openhab.core.config.discovery.DiscoveryResult;
24 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
25 import org.openhab.core.config.discovery.mdns.MDNSDiscoveryParticipant;
26 import org.openhab.core.thing.ThingTypeUID;
27 import org.openhab.core.thing.ThingUID;
28 import org.osgi.service.component.annotations.Component;
29
30 /**
31  * This is a discovery participant which finds AmpliPis on the local network
32  * through their mDNS announcements.
33  *
34  * @author Kai Kreuzer - Initial contribution
35  *
36  */
37 @NonNullByDefault
38 @Component
39 public class AmpliPiMDNSDiscoveryParticipant implements MDNSDiscoveryParticipant {
40
41     private static final String AMPLIPI_API = "amplipi-api";
42
43     @Override
44     public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
45         return Set.of(AmpliPiBindingConstants.THING_TYPE_CONTROLLER);
46     }
47
48     @Override
49     public String getServiceType() {
50         return "_http._tcp.local.";
51     }
52
53     @Override
54     public @Nullable DiscoveryResult createResult(ServiceInfo service) {
55         ThingUID uid = getThingUID(service);
56         if (uid != null) {
57             DiscoveryResult result = DiscoveryResultBuilder.create(uid).withLabel("AmpliPi Controller")
58                     .withProperty(AmpliPiBindingConstants.CFG_PARAM_HOSTNAME, getIpAddress(service).getHostAddress())
59                     .withRepresentationProperty(AmpliPiBindingConstants.CFG_PARAM_HOSTNAME).build();
60             return result;
61         } else {
62             return null;
63         }
64     }
65
66     @Override
67     public @Nullable ThingUID getThingUID(ServiceInfo service) {
68         if (service.getName().equals(AMPLIPI_API)) {
69             InetAddress ip = getIpAddress(service);
70             if (ip != null) {
71                 String id = ip.toString().substring(1).replaceAll("\\.", "");
72                 return new ThingUID(AmpliPiBindingConstants.THING_TYPE_CONTROLLER, id);
73             }
74         }
75         return null;
76     }
77
78     private @Nullable InetAddress getIpAddress(ServiceInfo service) {
79         if (service.getInet4Addresses().length > 0) {
80             return service.getInet4Addresses()[0];
81         } else {
82             return null;
83         }
84     }
85 }