]> git.basschouten.com Git - openhab-addons.git/blob
8987b7eb0095e5195f30b125cc49ac2602475c6b
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.util.HashMap;
18 import java.util.Map;
19 import java.util.Set;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.jupnp.model.meta.RemoteDevice;
24 import org.openhab.core.config.discovery.DiscoveryResult;
25 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
26 import org.openhab.core.config.discovery.upnp.UpnpDiscoveryParticipant;
27 import org.openhab.core.thing.ThingTypeUID;
28 import org.openhab.core.thing.ThingUID;
29 import org.osgi.service.component.annotations.Component;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 /**
34  *
35  * @author Mark Herwege - Initial contribution
36  */
37 @Component(service = { UpnpDiscoveryParticipant.class })
38 @NonNullByDefault
39 public class UpnpControlDiscoveryParticipant implements UpnpDiscoveryParticipant {
40
41     private final Logger logger = LoggerFactory.getLogger(getClass());
42
43     @Override
44     public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
45         return SUPPORTED_THING_TYPES_UIDS;
46     }
47
48     @Override
49     public @Nullable DiscoveryResult createResult(RemoteDevice device) {
50         DiscoveryResult result = null;
51         ThingUID thingUid = getThingUID(device);
52         if (thingUid != null) {
53             String label = device.getDetails().getFriendlyName().isEmpty() ? device.getDisplayString()
54                     : device.getDetails().getFriendlyName();
55             Map<String, Object> properties = new HashMap<>();
56             properties.put("ipAddress", device.getIdentity().getDescriptorURL().getHost());
57             properties.put("udn", device.getIdentity().getUdn().getIdentifierString());
58             result = DiscoveryResultBuilder.create(thingUid).withLabel(label).withProperties(properties)
59                     .withRepresentationProperty("udn").build();
60         }
61         return result;
62     }
63
64     @Override
65     public @Nullable ThingUID getThingUID(RemoteDevice device) {
66         ThingUID result = null;
67         String deviceType = device.getType().getType();
68         String manufacturer = device.getDetails().getManufacturerDetails().getManufacturer();
69         String model = device.getDetails().getModelDetails().getModelName();
70         String serialNumber = device.getDetails().getSerialNumber();
71
72         logger.debug("Device type {}, manufacturer {}, model {}, SN# {}", deviceType, manufacturer, model,
73                 serialNumber);
74
75         if (deviceType.equalsIgnoreCase("MediaRenderer")) {
76             this.logger.debug("Media renderer found: {}, {}", manufacturer, model);
77             ThingTypeUID thingTypeUID = THING_TYPE_RENDERER;
78             result = new ThingUID(thingTypeUID, device.getIdentity().getUdn().getIdentifierString());
79         } else if (deviceType.equalsIgnoreCase("MediaServer")) {
80             this.logger.debug("Media server found: {}, {}", manufacturer, model);
81             ThingTypeUID thingTypeUID = THING_TYPE_SERVER;
82             result = new ThingUID(thingTypeUID, device.getIdentity().getUdn().getIdentifierString());
83         }
84         return result;
85     }
86 }