]> git.basschouten.com Git - openhab-addons.git/blob
7deb5e8d796afab33b34b8cc1035c1957a8d740c
[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.upnpcontrol.internal.discovery;
14
15 import static org.openhab.binding.upnpcontrol.internal.UpnpControlBindingConstants.*;
16
17 import java.net.URL;
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.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;
34
35 /**
36  *
37  * @author Mark Herwege - Initial contribution
38  */
39 @Component(service = { UpnpDiscoveryParticipant.class })
40 @NonNullByDefault
41 public class UpnpControlDiscoveryParticipant implements UpnpDiscoveryParticipant {
42
43     private final Logger logger = LoggerFactory.getLogger(getClass());
44
45     @Override
46     public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
47         return SUPPORTED_THING_TYPES_UIDS;
48     }
49
50     @Override
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());
65             }
66             for (RemoteService service : device.getServices()) {
67                 properties.put(service.getServiceType().getType() + "DescrURI", service.getDescriptorURI().toString());
68             }
69             result = DiscoveryResultBuilder.create(thingUid).withLabel(label).withProperties(properties)
70                     .withRepresentationProperty("udn").build();
71         }
72         return result;
73     }
74
75     @Override
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();
83
84         logger.debug("Device type {}, manufacturer {}, model {}, SN# {}, UDN {}", deviceType, manufacturer, model,
85                 serialNumber, udn);
86
87         if ("MediaRenderer".equalsIgnoreCase(deviceType)) {
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 ("MediaServer".equalsIgnoreCase(deviceType)) {
92             this.logger.debug("Media server found: {}, {}", manufacturer, model);
93             ThingTypeUID thingTypeUID = THING_TYPE_SERVER;
94             result = new ThingUID(thingTypeUID, device.getIdentity().getUdn().getIdentifierString());
95         }
96         return result;
97     }
98 }