]> git.basschouten.com Git - openhab-addons.git/blob
129c1baff9c0a8c64aa3040c2118c4efa52f490a
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.util.Set;
16
17 import javax.jmdns.ServiceInfo;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.amplipi.internal.AmpliPiBindingConstants;
22 import org.openhab.core.config.discovery.DiscoveryResult;
23 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
24 import org.openhab.core.config.discovery.mdns.MDNSDiscoveryParticipant;
25 import org.openhab.core.thing.ThingTypeUID;
26 import org.openhab.core.thing.ThingUID;
27
28 /**
29  * This is a discovery participant which finds AmpliPis on the local network
30  * through their mDNS announcements.
31  *
32  * @author Kai Kreuzer - Initial contribution
33  *
34  */
35 @NonNullByDefault
36 public class AmpliPiMDNSDiscoveryParticipant implements MDNSDiscoveryParticipant {
37
38     @Override
39     public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
40         return Set.of(AmpliPiBindingConstants.THING_TYPE_CONTROLLER);
41     }
42
43     @Override
44     public String getServiceType() {
45         return "_http._tcp";
46     }
47
48     @Override
49     public @Nullable DiscoveryResult createResult(ServiceInfo service) {
50         ThingUID uid = getThingUID(service);
51         if (uid != null) {
52             DiscoveryResult result = DiscoveryResultBuilder.create(uid).withLabel(service.getName())
53                     .withProperty(AmpliPiBindingConstants.CFG_PARAM_HOSTNAME,
54                             service.getInet4Addresses()[0].getHostAddress())
55                     .withRepresentationProperty(AmpliPiBindingConstants.CFG_PARAM_HOSTNAME).build();
56             return result;
57         } else {
58             return null;
59         }
60     }
61
62     @Override
63     public @Nullable ThingUID getThingUID(ServiceInfo service) {
64         // TODO: Currently, the AmpliPi does not seem to announce any services.
65         return null;
66     }
67 }