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.samsungtv.internal.discovery;
15 import static org.openhab.binding.samsungtv.internal.SamsungTvBindingConstants.SAMSUNG_TV_THING_TYPE;
16 import static org.openhab.binding.samsungtv.internal.config.SamsungTvConfiguration.HOST_NAME;
18 import java.util.Collections;
19 import java.util.HashMap;
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.jupnp.model.meta.RemoteDevice;
26 import org.openhab.core.config.discovery.DiscoveryResult;
27 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
28 import org.openhab.core.config.discovery.upnp.UpnpDiscoveryParticipant;
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 {@link SamsungTvDiscoveryParticipant} is responsible for processing the
37 * results of searched UPnP devices
39 * @author Pauli Anttila - Initial contribution
40 * @author Arjan Mels - Changed to upnp.UpnpDiscoveryParticipant
44 public class SamsungTvDiscoveryParticipant implements UpnpDiscoveryParticipant {
45 private final Logger logger = LoggerFactory.getLogger(SamsungTvDiscoveryParticipant.class);
48 public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
49 return Collections.singleton(SAMSUNG_TV_THING_TYPE);
53 public @Nullable DiscoveryResult createResult(RemoteDevice device) {
54 ThingUID uid = getThingUID(device);
56 Map<String, Object> properties = new HashMap<>();
57 properties.put(HOST_NAME, device.getIdentity().getDescriptorURL().getHost());
59 DiscoveryResult result = DiscoveryResultBuilder.create(uid).withProperties(properties)
60 .withRepresentationProperty(HOST_NAME).withLabel(getLabel(device)).build();
62 logger.debug("Created a DiscoveryResult for device '{}' with UDN '{}' and properties: {}",
63 device.getDetails().getModelDetails().getModelName(),
64 device.getIdentity().getUdn().getIdentifierString(), properties);
71 private String getLabel(RemoteDevice device) {
72 String label = "Samsung TV";
74 label = device.getDetails().getFriendlyName();
75 } catch (Exception e) {
76 // ignore and use the default label
82 public @Nullable ThingUID getThingUID(RemoteDevice device) {
83 if (device.getDetails() != null && device.getDetails().getManufacturerDetails() != null) {
84 String manufacturer = device.getDetails().getManufacturerDetails().getManufacturer();
86 if (manufacturer != null && manufacturer.toUpperCase().contains("SAMSUNG ELECTRONICS")) {
87 // One Samsung TV contains several UPnP devices.
88 // Create unique Samsung TV thing for every MediaRenderer
89 // device and ignore rest of the UPnP devices.
91 if (device.getType() != null && "MediaRenderer".equals(device.getType().getType())) {
92 // UDN shouldn't contain '-' characters.
93 String udn = device.getIdentity().getUdn().getIdentifierString().replace("-", "_");
95 if (logger.isDebugEnabled()) {
96 String modelName = device.getDetails().getModelDetails().getModelName();
97 String friendlyName = device.getDetails().getFriendlyName();
98 logger.debug("Retrieved Thing UID for a Samsung TV '{}' model '{}' thing with UDN '{}'",
99 friendlyName, modelName, udn);
102 return new ThingUID(SAMSUNG_TV_THING_TYPE, udn);