]> git.basschouten.com Git - openhab-addons.git/blob
6dff237e1de276f4d632967742dbf4d2d59f2262
[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             return DiscoveryResultBuilder.create(uid).withLabel("AmpliPi Controller")
58                     .withProperty(AmpliPiBindingConstants.CFG_PARAM_HOSTNAME, getIpAddress(service).getHostAddress())
59                     .withRepresentationProperty(AmpliPiBindingConstants.CFG_PARAM_HOSTNAME).build();
60         } else {
61             return null;
62         }
63     }
64
65     @Override
66     public @Nullable ThingUID getThingUID(ServiceInfo service) {
67         if (service.getName().equals(AMPLIPI_API)) {
68             InetAddress ip = getIpAddress(service);
69             if (ip != null) {
70                 String id = ip.toString().substring(1).replace(".", "");
71                 return new ThingUID(AmpliPiBindingConstants.THING_TYPE_CONTROLLER, id);
72             }
73         }
74         return null;
75     }
76
77     private @Nullable InetAddress getIpAddress(ServiceInfo service) {
78         if (service.getInet4Addresses().length > 0) {
79             return service.getInet4Addresses()[0];
80         } else {
81             return null;
82         }
83     }
84 }