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