]> git.basschouten.com Git - openhab-addons.git/blob
2284ad7fc1ccc8ebaabe09d2f8423088afd5fc04
[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.yamahareceiver.internal.discovery;
14
15 import static org.openhab.binding.yamahareceiver.internal.YamahaReceiverBindingConstants.*;
16 import static org.openhab.binding.yamahareceiver.internal.YamahaReceiverBindingConstants.Configs.CONFIG_HOST_NAME;
17
18 import java.net.URL;
19 import java.util.Collections;
20 import java.util.HashMap;
21 import java.util.Map;
22 import java.util.Set;
23
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.eclipse.jdt.annotation.Nullable;
26 import org.jupnp.model.meta.RemoteDevice;
27 import org.openhab.core.config.discovery.DiscoveryResult;
28 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
29 import org.openhab.core.config.discovery.upnp.UpnpDiscoveryParticipant;
30 import org.openhab.core.thing.ThingTypeUID;
31 import org.openhab.core.thing.ThingUID;
32 import org.osgi.service.component.annotations.Component;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 /**
37  * The {@link YamahaDiscoveryParticipant} is responsible for processing the
38  * results of searched UPnP devices
39  *
40  * @author David Graeff - Initial contribution
41  * @author Tomasz Maruszak - Introduced config object, migrated to newer UPnP api
42  */
43 @Component
44 @NonNullByDefault
45 public class YamahaDiscoveryParticipant implements UpnpDiscoveryParticipant {
46
47     private final Logger logger = LoggerFactory.getLogger(YamahaDiscoveryParticipant.class);
48
49     private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Collections.singleton(BRIDGE_THING_TYPE);
50
51     @Override
52     public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
53         return SUPPORTED_THING_TYPES;
54     }
55
56     @Override
57     public @Nullable DiscoveryResult createResult(RemoteDevice device) {
58         ThingUID uid = getThingUID(device);
59         if (uid == null) {
60             return null;
61         }
62
63         Map<String, Object> properties = new HashMap<>(3);
64
65         String label = "Yamaha Receiver";
66         try {
67             label += " " + device.getDetails().getModelDetails().getModelName();
68         } catch (Exception e) {
69             // ignore and use the default label
70         }
71
72         URL url = device.getIdentity().getDescriptorURL();
73         properties.put(CONFIG_HOST_NAME, url.getHost());
74
75         // The port via UPNP is unreliable, sometimes it is 8080, on some models 49154.
76         // But so far the API was always reachable via port 80.
77         // We provide the port config therefore, if the user ever needs to adjust the port.
78         // Note the port is set in the thing-types.xml to 80 by default.
79
80         DiscoveryResult result = DiscoveryResultBuilder.create(uid).withTTL(MIN_MAX_AGE_SECS).withProperties(properties)
81                 .withLabel(label).withRepresentationProperty(CONFIG_HOST_NAME).build();
82
83         logger.debug("Discovered a Yamaha Receiver '{}' model '{}' thing with UDN '{}'",
84                 device.getDetails().getFriendlyName(), device.getDetails().getModelDetails().getModelName(),
85                 device.getIdentity().getUdn().getIdentifierString());
86
87         return result;
88     }
89
90     public static @Nullable ThingUID getThingUID(@Nullable String manufacturer, @Nullable String deviceType,
91             String udn) {
92         if (manufacturer == null || deviceType == null) {
93             return null;
94         }
95
96         if (manufacturer.toUpperCase().contains(UPNP_MANUFACTURER) && deviceType.equals(UPNP_TYPE)) {
97             return new ThingUID(BRIDGE_THING_TYPE, udn);
98         } else {
99             return null;
100         }
101     }
102
103     @Override
104     public @Nullable ThingUID getThingUID(RemoteDevice device) {
105         String manufacturer = device.getDetails().getManufacturerDetails().getManufacturer();
106         String deviceType = device.getType().getType();
107
108         // UDN shouldn't contain '-' characters.
109         return getThingUID(manufacturer, deviceType,
110                 device.getIdentity().getUdn().getIdentifierString().replace("-", "_"));
111     }
112 }