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.amplipi.internal.discovery;
15 import java.net.InetAddress;
18 import javax.jmdns.ServiceInfo;
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;
31 * This is a discovery participant which finds AmpliPis on the local network
32 * through their mDNS announcements.
34 * @author Kai Kreuzer - Initial contribution
39 public class AmpliPiMDNSDiscoveryParticipant implements MDNSDiscoveryParticipant {
41 private static final String AMPLIPI_API = "amplipi-api";
44 public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
45 return Set.of(AmpliPiBindingConstants.THING_TYPE_CONTROLLER);
49 public String getServiceType() {
50 return "_http._tcp.local.";
54 public @Nullable DiscoveryResult createResult(ServiceInfo service) {
55 ThingUID uid = getThingUID(service);
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();
67 public @Nullable ThingUID getThingUID(ServiceInfo service) {
68 if (service.getName().equals(AMPLIPI_API)) {
69 InetAddress ip = getIpAddress(service);
71 String id = ip.toString().substring(1).replaceAll("\\.", "");
72 return new ThingUID(AmpliPiBindingConstants.THING_TYPE_CONTROLLER, id);
78 private @Nullable InetAddress getIpAddress(ServiceInfo service) {
79 if (service.getInet4Addresses().length > 0) {
80 return service.getInet4Addresses()[0];