]> git.basschouten.com Git - openhab-addons.git/blob
2a0b60a7f6b049afae26ac5b6ab6dd8f941d9bf2
[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.core.config.discovery.DiscoveryResult;
26 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
27 import org.openhab.core.config.discovery.upnp.UpnpDiscoveryParticipant;
28 import org.openhab.core.thing.ThingTypeUID;
29 import org.openhab.core.thing.ThingUID;
30 import org.osgi.service.component.annotations.Component;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 /**
35  * The {@link SamsungTvDiscoveryParticipant} is responsible for processing the
36  * results of searched UPnP devices
37  *
38  * @author Pauli Anttila - Initial contribution
39  * @author Arjan Mels - Changed to upnp.UpnpDiscoveryParticipant
40  */
41 @NonNullByDefault
42 @Component
43 public class SamsungTvDiscoveryParticipant implements UpnpDiscoveryParticipant {
44     private final Logger logger = LoggerFactory.getLogger(SamsungTvDiscoveryParticipant.class);
45
46     @Override
47     public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
48         return Set.of(SAMSUNG_TV_THING_TYPE);
49     }
50
51     @Override
52     public @Nullable DiscoveryResult createResult(RemoteDevice device) {
53         ThingUID uid = getThingUID(device);
54         if (uid != null) {
55             Map<String, Object> properties = new HashMap<>();
56             properties.put(HOST_NAME, device.getIdentity().getDescriptorURL().getHost());
57
58             DiscoveryResult result = DiscoveryResultBuilder.create(uid).withProperties(properties)
59                     .withRepresentationProperty(HOST_NAME).withLabel(getLabel(device)).build();
60
61             logger.debug("Created a DiscoveryResult for device '{}' with UDN '{}' and properties: {}",
62                     device.getDetails().getModelDetails().getModelName(),
63                     device.getIdentity().getUdn().getIdentifierString(), properties);
64             return result;
65         } else {
66             return null;
67         }
68     }
69
70     private String getLabel(RemoteDevice device) {
71         String label = "Samsung TV";
72         try {
73             label = device.getDetails().getFriendlyName();
74         } catch (Exception e) {
75             // ignore and use the default label
76         }
77         return label;
78     }
79
80     @Override
81     public @Nullable ThingUID getThingUID(RemoteDevice device) {
82         if (device.getDetails() != null && device.getDetails().getManufacturerDetails() != null) {
83             String manufacturer = device.getDetails().getManufacturerDetails().getManufacturer();
84
85             if (manufacturer != null && manufacturer.toUpperCase().contains("SAMSUNG ELECTRONICS")) {
86                 // One Samsung TV contains several UPnP devices.
87                 // Create unique Samsung TV thing for every MediaRenderer
88                 // device and ignore rest of the UPnP devices.
89
90                 if (device.getType() != null && "MediaRenderer".equals(device.getType().getType())) {
91                     // UDN shouldn't contain '-' characters.
92                     String udn = device.getIdentity().getUdn().getIdentifierString().replace("-", "_");
93
94                     if (logger.isDebugEnabled()) {
95                         String modelName = device.getDetails().getModelDetails().getModelName();
96                         String friendlyName = device.getDetails().getFriendlyName();
97                         logger.debug("Retrieved Thing UID for a Samsung TV '{}' model '{}' thing with UDN '{}'",
98                                 friendlyName, modelName, udn);
99                     }
100
101                     return new ThingUID(SAMSUNG_TV_THING_TYPE, udn);
102                 }
103             }
104         }
105         return null;
106     }
107 }