]> git.basschouten.com Git - openhab-addons.git/blob
fa53624de5d53f4574155968714d9e73cb7845cf
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.samsungtv.internal.discovery;
14
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;
17
18 import java.util.HashMap;
19 import java.util.Map;
20 import java.util.Set;
21
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.jupnp.model.meta.RemoteDevice;
25 import org.openhab.binding.samsungtv.internal.Utils;
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;
34
35 /**
36  * The {@link SamsungTvDiscoveryParticipant} is responsible for processing the
37  * results of searched UPnP devices
38  *
39  * @author Pauli Anttila - Initial contribution
40  * @author Arjan Mels - Changed to upnp.UpnpDiscoveryParticipant
41  * @author Nick Waterton - use Utils class
42  */
43 @NonNullByDefault
44 @Component
45 public class SamsungTvDiscoveryParticipant implements UpnpDiscoveryParticipant {
46     private final Logger logger = LoggerFactory.getLogger(SamsungTvDiscoveryParticipant.class);
47
48     @Override
49     public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
50         return Set.of(SAMSUNG_TV_THING_TYPE);
51     }
52
53     @Override
54     public @Nullable DiscoveryResult createResult(RemoteDevice device) {
55         ThingUID uid = getThingUID(device);
56         if (uid != null) {
57             Map<String, Object> properties = new HashMap<>();
58             properties.put(HOST_NAME, Utils.getHost(device));
59
60             DiscoveryResult result = DiscoveryResultBuilder.create(uid).withProperties(properties)
61                     .withRepresentationProperty(HOST_NAME).withLabel(getLabel(device)).build();
62
63             logger.debug("Created a DiscoveryResult for device '{}' with UDN '{}' and properties: {}",
64                     Utils.getModelName(device), Utils.getUdn(device), properties);
65             return result;
66         }
67         return null;
68     }
69
70     private String getLabel(RemoteDevice device) {
71         String label = Utils.getFriendlyName(device);
72         return label.isBlank() ? "Samsung TV" : label;
73     }
74
75     @Override
76     public @Nullable ThingUID getThingUID(RemoteDevice device) {
77         if (Utils.getManufacturer(device).toUpperCase().contains("SAMSUNG ELECTRONICS")) {
78             // One Samsung TV contains several UPnP devices.
79             // Create unique Samsung TV thing for every MediaRenderer
80             // device and ignore rest of the UPnP devices.
81             // use MediaRenderer udn for ThingID.
82
83             if ("MediaRenderer".equals(Utils.getType(device))) {
84                 String udn = Utils.getUdn(device);
85                 if (logger.isDebugEnabled()) {
86                     logger.debug("Retrieved Thing UID for a Samsung TV '{}' model '{}' thing with UDN '{}'",
87                             Utils.getFriendlyName(device), Utils.getModelName(device), udn);
88                 }
89
90                 return new ThingUID(SAMSUNG_TV_THING_TYPE, udn);
91             }
92         }
93         return null;
94     }
95 }