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.upnpcontrol.internal.discovery;
15 import static org.openhab.binding.upnpcontrol.internal.UpnpControlBindingConstants.*;
18 import java.util.HashMap;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.jupnp.model.meta.RemoteDevice;
25 import org.jupnp.model.meta.RemoteService;
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;
37 * @author Mark Herwege - Initial contribution
39 @Component(service = { UpnpDiscoveryParticipant.class })
41 public class UpnpControlDiscoveryParticipant implements UpnpDiscoveryParticipant {
43 private final Logger logger = LoggerFactory.getLogger(getClass());
46 public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
47 return SUPPORTED_THING_TYPES_UIDS;
51 public @Nullable DiscoveryResult createResult(RemoteDevice device) {
52 DiscoveryResult result = null;
53 ThingUID thingUid = getThingUID(device);
54 if (thingUid != null) {
55 String label = device.getDetails().getFriendlyName().isEmpty() ? device.getDisplayString()
56 : device.getDetails().getFriendlyName();
57 Map<String, Object> properties = new HashMap<>();
58 URL descriptorURL = device.getIdentity().getDescriptorURL();
59 properties.put("ipAddress", descriptorURL.getHost());
60 properties.put("udn", device.getIdentity().getUdn().getIdentifierString());
61 properties.put("deviceDescrURL", descriptorURL.toString());
62 URL baseURL = device.getDetails().getBaseURL();
63 if (baseURL != null) {
64 properties.put("baseURL", device.getDetails().getBaseURL().toString());
66 for (RemoteService service : device.getServices()) {
67 properties.put(service.getServiceType().getType() + "DescrURI", service.getDescriptorURI().toString());
69 result = DiscoveryResultBuilder.create(thingUid).withLabel(label).withProperties(properties)
70 .withRepresentationProperty("udn").build();
76 public @Nullable ThingUID getThingUID(RemoteDevice device) {
77 ThingUID result = null;
78 String deviceType = device.getType().getType();
79 String manufacturer = device.getDetails().getManufacturerDetails().getManufacturer();
80 String model = device.getDetails().getModelDetails().getModelName();
81 String serialNumber = device.getDetails().getSerialNumber();
82 String udn = device.getIdentity().getUdn().getIdentifierString();
84 logger.debug("Device type {}, manufacturer {}, model {}, SN# {}, UDN {}", deviceType, manufacturer, model,
87 if (deviceType.equalsIgnoreCase("MediaRenderer")) {
88 this.logger.debug("Media renderer found: {}, {}", manufacturer, model);
89 ThingTypeUID thingTypeUID = THING_TYPE_RENDERER;
90 result = new ThingUID(thingTypeUID, device.getIdentity().getUdn().getIdentifierString());
91 } else if (deviceType.equalsIgnoreCase("MediaServer")) {
92 this.logger.debug("Media server found: {}, {}", manufacturer, model);
93 ThingTypeUID thingTypeUID = THING_TYPE_SERVER;
94 result = new ThingUID(thingTypeUID, device.getIdentity().getUdn().getIdentifierString());