]> git.basschouten.com Git - openhab-addons.git/blob
f65e7e46eadb4eeabe237980156015cc8659642b
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.Collections;
19 import java.util.HashMap;
20 import java.util.Map;
21 import java.util.Set;
22
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;
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  */
42 @NonNullByDefault
43 @Component
44 public class SamsungTvDiscoveryParticipant implements UpnpDiscoveryParticipant {
45     private final Logger logger = LoggerFactory.getLogger(SamsungTvDiscoveryParticipant.class);
46
47     @Override
48     public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
49         return Collections.singleton(SAMSUNG_TV_THING_TYPE);
50     }
51
52     @Override
53     public @Nullable DiscoveryResult createResult(RemoteDevice device) {
54         ThingUID uid = getThingUID(device);
55         if (uid != null) {
56             Map<String, Object> properties = new HashMap<>();
57             properties.put(HOST_NAME, device.getIdentity().getDescriptorURL().getHost());
58
59             DiscoveryResult result = DiscoveryResultBuilder.create(uid).withProperties(properties)
60                     .withRepresentationProperty(HOST_NAME).withLabel(getLabel(device)).build();
61
62             logger.debug("Created a DiscoveryResult for device '{}' with UDN '{}' and properties: {}",
63                     device.getDetails().getModelDetails().getModelName(),
64                     device.getIdentity().getUdn().getIdentifierString(), properties);
65             return result;
66         } else {
67             return null;
68         }
69     }
70
71     private String getLabel(RemoteDevice device) {
72         String label = "Samsung TV";
73         try {
74             label = device.getDetails().getFriendlyName();
75         } catch (Exception e) {
76             // ignore and use the default label
77         }
78         return label;
79     }
80
81     @Override
82     public @Nullable ThingUID getThingUID(RemoteDevice device) {
83         if (device.getDetails() != null && device.getDetails().getManufacturerDetails() != null) {
84             String manufacturer = device.getDetails().getManufacturerDetails().getManufacturer();
85
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.
90
91                 if (device.getType() != null && "MediaRenderer".equals(device.getType().getType())) {
92                     // UDN shouldn't contain '-' characters.
93                     String udn = device.getIdentity().getUdn().getIdentifierString().replace("-", "_");
94
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);
100                     }
101
102                     return new ThingUID(SAMSUNG_TV_THING_TYPE, udn);
103                 }
104             }
105         }
106         return null;
107     }
108 }