2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.squeezebox.internal.discovery;
15 import static org.openhab.binding.squeezebox.internal.SqueezeBoxBindingConstants.SQUEEZEBOXSERVER_THING_TYPE;
18 import java.util.HashMap;
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;
36 * Discovers a SqueezeServer on the network using UPNP
38 * @author Dan Cunningham - Initial contribution
39 * @author Mark Hilbush - Add support for LMS authentication
43 public class SqueezeBoxServerDiscoveryParticipant implements UpnpDiscoveryParticipant {
44 private final Logger logger = LoggerFactory.getLogger(SqueezeBoxServerDiscoveryParticipant.class);
47 * Name of a Squeeze Server
49 private static final String MODEL_NAME = "Logitech Media Server";
52 public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
53 return Set.of(SQUEEZEBOXSERVER_THING_TYPE);
57 public DiscoveryResult createResult(RemoteDevice device) {
58 ThingUID uid = getThingUID(device);
60 Map<String, Object> properties = new HashMap<>(3);
62 URI uri = device.getDetails().getPresentationURI();
64 String host = uri.getHost();
65 int webPort = uri.getPort();
67 int defaultCliPort = 9090;
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);
82 String label = device.getDetails().getFriendlyName();
84 String representationPropertyName = "ipAddress";
85 properties.put(representationPropertyName, host);
86 properties.put("webport", Integer.valueOf(webPort));
87 properties.put("cliPort", Integer.valueOf(cliPort));
89 DiscoveryResult result = DiscoveryResultBuilder.create(uid).withProperties(properties)
90 .withRepresentationProperty(representationPropertyName).withLabel(label).build();
92 logger.debug("Created a DiscoveryResult for device '{}' with UDN '{}'",
93 device.getDetails().getFriendlyName(), device.getIdentity().getUdn().getIdentifierString());
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());