]> git.basschouten.com Git - openhab-addons.git/blob
fce31a41df400725206b0b4fbf6a5964342a53a0
[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.squeezebox.internal.discovery;
14
15 import static org.openhab.binding.squeezebox.internal.SqueezeBoxBindingConstants.SQUEEZEBOXSERVER_THING_TYPE;
16
17 import java.net.URI;
18 import java.util.HashMap;
19 import java.util.Map;
20 import java.util.Set;
21
22 import org.jupnp.model.meta.RemoteDevice;
23 import org.openhab.binding.squeezebox.internal.utils.HttpUtils;
24 import org.openhab.binding.squeezebox.internal.utils.SqueezeBoxCommunicationException;
25 import org.openhab.binding.squeezebox.internal.utils.SqueezeBoxNotAuthorizedException;
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  * Discovers a SqueezeServer on the network using UPNP
37  *
38  * @author Dan Cunningham - Initial contribution
39  * @author Mark Hilbush - Add support for LMS authentication
40  *
41  */
42 @Component
43 public class SqueezeBoxServerDiscoveryParticipant implements UpnpDiscoveryParticipant {
44     private final Logger logger = LoggerFactory.getLogger(SqueezeBoxServerDiscoveryParticipant.class);
45
46     /**
47      * Name of a Squeeze Server
48      */
49     private static final String MODEL_NAME = "Logitech Media Server";
50
51     @Override
52     public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
53         return Set.of(SQUEEZEBOXSERVER_THING_TYPE);
54     }
55
56     @Override
57     public DiscoveryResult createResult(RemoteDevice device) {
58         ThingUID uid = getThingUID(device);
59         if (uid != null) {
60             Map<String, Object> properties = new HashMap<>(3);
61
62             URI uri = device.getDetails().getPresentationURI();
63
64             String host = uri.getHost();
65             int webPort = uri.getPort();
66             int cliPort = 0;
67             int defaultCliPort = 9090;
68
69             try {
70                 cliPort = HttpUtils.getCliPort(host, webPort);
71             } catch (SqueezeBoxNotAuthorizedException e) {
72                 logger.debug("Not authorized to query CLI port. Using default of {}", defaultCliPort);
73                 cliPort = defaultCliPort;
74             } catch (NumberFormatException e) {
75                 logger.debug("Badly formed CLI port. Using default of {}", defaultCliPort);
76                 cliPort = defaultCliPort;
77             } catch (SqueezeBoxCommunicationException e) {
78                 logger.debug("Could not get cli port: {}", e.getMessage(), e);
79                 return null;
80             }
81
82             String label = device.getDetails().getFriendlyName();
83
84             String representationPropertyName = "ipAddress";
85             properties.put(representationPropertyName, host);
86             properties.put("webport", Integer.valueOf(webPort));
87             properties.put("cliPort", Integer.valueOf(cliPort));
88
89             DiscoveryResult result = DiscoveryResultBuilder.create(uid).withProperties(properties)
90                     .withRepresentationProperty(representationPropertyName).withLabel(label).build();
91
92             logger.debug("Created a DiscoveryResult for device '{}' with UDN '{}'",
93                     device.getDetails().getFriendlyName(), device.getIdentity().getUdn().getIdentifierString());
94             return result;
95         } else {
96             return null;
97         }
98     }
99
100     @Override
101     public ThingUID getThingUID(RemoteDevice device) {
102         if (device.getDetails().getFriendlyName() != null) {
103             if (device.getDetails().getModelDetails().getModelName().contains(MODEL_NAME)) {
104                 logger.debug("Discovered a {} thing with UDN '{}'", device.getDetails().getFriendlyName(),
105                         device.getIdentity().getUdn().getIdentifierString());
106                 return new ThingUID(SQUEEZEBOXSERVER_THING_TYPE,
107                         device.getIdentity().getUdn().getIdentifierString().toUpperCase());
108             }
109         }
110         return null;
111     }
112 }