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.tivo.internal.discovery;
15 import static org.openhab.binding.tivo.internal.TiVoBindingConstants.*;
17 import java.net.InetAddress;
18 import java.util.Collections;
19 import java.util.HashMap;
23 import javax.jmdns.ServiceInfo;
25 import org.eclipse.jdt.annotation.NonNullByDefault;
26 import org.eclipse.jdt.annotation.Nullable;
27 import org.openhab.core.config.discovery.DiscoveryResult;
28 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
29 import org.openhab.core.config.discovery.mdns.MDNSDiscoveryParticipant;
30 import org.openhab.core.thing.ThingTypeUID;
31 import org.openhab.core.thing.ThingUID;
32 import org.osgi.service.component.annotations.Component;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
37 * The Class TiVoDiscoveryParticipant.
40 * @author Jayson Kubilis (DigitalBytes) - Initial contribution
41 * @author Andrew Black (AndyXMB) - minor updates.
42 * @author Michael Lobstein - Updated for OH3
45 @Component(configurationPid = "discovery.tivo")
46 public class TiVoDiscoveryParticipant implements MDNSDiscoveryParticipant {
47 private final Logger logger = LoggerFactory.getLogger(TiVoDiscoveryParticipant.class);
50 public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
51 return Collections.singleton(THING_TYPE_TIVO);
55 public String getServiceType() {
56 return "_tivo-remote._tcp.local.";
60 public @Nullable DiscoveryResult createResult(ServiceInfo service) {
61 DiscoveryResult result = null;
63 ThingUID uid = getThingUID(service);
65 Map<String, Object> properties = new HashMap<>(2);
66 // remove the domain from the name
67 InetAddress ip = getIpAddress(service);
71 String inetAddress = ip.toString().substring(1); // trim leading slash
72 String label = service.getName();
73 int port = service.getPort();
75 properties.put(CONFIG_HOST, inetAddress);
76 properties.put(CONFIG_PORT, port);
78 result = DiscoveryResultBuilder.create(uid).withProperties(properties).withLabel("Tivo: " + label)
79 .withProperty(CONFIG_HOST, inetAddress).withRepresentationProperty(CONFIG_HOST).build();
80 logger.debug("Created {} for TiVo host '{}' name '{}'", result, inetAddress, label);
86 * @see org.openhab.core.config.discovery.mdns.MDNSDiscoveryParticipant#getThingUID(javax.jmdns.ServiceInfo)
89 public @Nullable ThingUID getThingUID(ServiceInfo service) {
90 if (service.getType() != null) {
91 if (service.getType().equals(getServiceType())) {
92 String uidName = getUIDName(service);
93 return new ThingUID(THING_TYPE_TIVO, uidName);
100 * Gets the UID name, replacing any non AlphaNumeric characters with underscores.
102 * @param service the service
103 * @return the UID name
105 private String getUIDName(ServiceInfo service) {
106 return service.getName().replaceAll("[^A-Za-z0-9_]", "_");
110 * {@link InetAddress} gets the IP address of the device in v4 or v6 format.
112 * @param ServiceInfo service
113 * @return InetAddress the IP address
116 private @Nullable InetAddress getIpAddress(ServiceInfo service) {
117 InetAddress address = null;
118 for (InetAddress addr : service.getInet4Addresses()) {
121 // Fall back for Inet6addresses
122 for (InetAddress addr : service.getInet6Addresses()) {