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.HashMap;
22 import javax.jmdns.ServiceInfo;
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.eclipse.jdt.annotation.Nullable;
26 import org.openhab.core.config.discovery.DiscoveryResult;
27 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
28 import org.openhab.core.config.discovery.mdns.MDNSDiscoveryParticipant;
29 import org.openhab.core.thing.ThingTypeUID;
30 import org.openhab.core.thing.ThingUID;
31 import org.osgi.service.component.annotations.Component;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
36 * The Class TiVoDiscoveryParticipant.
39 * @author Jayson Kubilis (DigitalBytes) - Initial contribution
40 * @author Andrew Black (AndyXMB) - minor updates.
41 * @author Michael Lobstein - Updated for OH3
44 @Component(configurationPid = "discovery.tivo")
45 public class TiVoDiscoveryParticipant implements MDNSDiscoveryParticipant {
46 private final Logger logger = LoggerFactory.getLogger(TiVoDiscoveryParticipant.class);
49 public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
50 return Set.of(THING_TYPE_TIVO);
54 public String getServiceType() {
55 return "_tivo-remote._tcp.local.";
59 public @Nullable DiscoveryResult createResult(ServiceInfo service) {
60 DiscoveryResult result = null;
62 ThingUID uid = getThingUID(service);
64 Map<String, Object> properties = new HashMap<>(2);
65 // remove the domain from the name
66 InetAddress ip = getIpAddress(service);
70 String inetAddress = ip.toString().substring(1); // trim leading slash
71 String label = service.getName();
72 int port = service.getPort();
74 properties.put(CONFIG_HOST, inetAddress);
75 properties.put(CONFIG_PORT, port);
77 result = DiscoveryResultBuilder.create(uid).withProperties(properties).withLabel("Tivo: " + label)
78 .withProperty(CONFIG_HOST, inetAddress).withRepresentationProperty(CONFIG_HOST).build();
79 logger.debug("Created {} for TiVo host '{}' name '{}'", result, inetAddress, label);
85 * @see org.openhab.core.config.discovery.mdns.MDNSDiscoveryParticipant#getThingUID(javax.jmdns.ServiceInfo)
88 public @Nullable ThingUID getThingUID(ServiceInfo service) {
89 if (service.getType() != null) {
90 if (service.getType().equals(getServiceType())) {
91 String uidName = getUIDName(service);
92 return new ThingUID(THING_TYPE_TIVO, uidName);
99 * Gets the UID name, replacing any non AlphaNumeric characters with underscores.
101 * @param service the service
102 * @return the UID name
104 private String getUIDName(ServiceInfo service) {
105 return service.getName().replaceAll("[^A-Za-z0-9_]", "_");
109 * {@link InetAddress} gets the IP address of the device in v4 or v6 format.
111 * @param ServiceInfo service
112 * @return InetAddress the IP address
115 private @Nullable InetAddress getIpAddress(ServiceInfo service) {
116 InetAddress address = null;
117 for (InetAddress addr : service.getInet4Addresses()) {
120 // Fall back for Inet6addresses
121 for (InetAddress addr : service.getInet6Addresses()) {