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.Collections;
19 import java.util.HashMap;
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;
37 * Discovers a SqueezeServer on the network using UPNP
39 * @author Dan Cunningham - Initial contribution
40 * @author Mark Hilbush - Add support for LMS authentication
44 public class SqueezeBoxServerDiscoveryParticipant implements UpnpDiscoveryParticipant {
45 private final Logger logger = LoggerFactory.getLogger(SqueezeBoxServerDiscoveryParticipant.class);
48 * Name of a Squeeze Server
50 private static final String MODEL_NAME = "Logitech Media Server";
53 public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
54 return Collections.singleton(SQUEEZEBOXSERVER_THING_TYPE);
58 public DiscoveryResult createResult(RemoteDevice device) {
59 ThingUID uid = getThingUID(device);
61 Map<String, Object> properties = new HashMap<>(3);
63 URI uri = device.getDetails().getPresentationURI();
65 String host = uri.getHost();
66 int webPort = uri.getPort();
68 int defaultCliPort = 9090;
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);
83 String label = device.getDetails().getFriendlyName();
85 String representationPropertyName = "ipAddress";
86 properties.put(representationPropertyName, host);
87 properties.put("webport", Integer.valueOf(webPort));
88 properties.put("cliPort", Integer.valueOf(cliPort));
90 DiscoveryResult result = DiscoveryResultBuilder.create(uid).withProperties(properties)
91 .withRepresentationProperty(representationPropertyName).withLabel(label).build();
93 logger.debug("Created a DiscoveryResult for device '{}' with UDN '{}'",
94 device.getDetails().getFriendlyName(), device.getIdentity().getUdn().getIdentifierString());
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());