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