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.volumio.internal.discovery;
15 import static org.openhab.binding.volumio.internal.VolumioBindingConstants.THING_TYPE_VOLUMIO;
17 import java.util.Collections;
18 import java.util.HashMap;
22 import javax.jmdns.ServiceInfo;
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.eclipse.jdt.annotation.Nullable;
26 import org.openhab.binding.volumio.internal.VolumioBindingConstants;
27 import org.openhab.core.config.discovery.DiscoveryResult;
28 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
29 import org.openhab.core.config.discovery.mdns.MDNSDiscoveryParticipant;
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 * @author Patrick Sernetz - Initial contribution
38 * @author Chris Wohlbrecht - Adaption for openHAB 3
39 * @author Michael Loercher - Adaption for openHAB 3
42 @Component(configurationPid = "discovery.volumio")
43 public class VolumioDiscoveryParticipant implements MDNSDiscoveryParticipant {
45 private final Logger logger = LoggerFactory.getLogger(VolumioDiscoveryParticipant.class);
48 public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
49 return Set.of(THING_TYPE_VOLUMIO);
53 public String getServiceType() {
54 return VolumioBindingConstants.DISCOVERY_SERVICE_TYPE;
58 public @Nullable DiscoveryResult createResult(ServiceInfo serviceInfo) {
59 String volumioName = serviceInfo.getPropertyString(VolumioBindingConstants.DISCOVERY_NAME_PROPERTY);
60 Map<String, Object> properties = new HashMap<>();
61 ThingUID thingUID = getThingUID(serviceInfo);
63 logger.debug("Service Device: {}", serviceInfo);
64 logger.debug("Thing UID: {}", thingUID);
66 DiscoveryResult discoveryResult = null;
67 if (thingUID != null) {
68 properties.put("hostname", serviceInfo.getServer());
69 properties.put("port", serviceInfo.getPort());
70 properties.put("protocol", "http");
72 discoveryResult = DiscoveryResultBuilder.create(thingUID).withProperties(properties).withLabel(volumioName)
74 logger.debug("DiscoveryResult: {}", discoveryResult);
76 return discoveryResult;
80 public @Nullable ThingUID getThingUID(ServiceInfo serviceInfo) {
81 Collections.list(serviceInfo.getPropertyNames()).forEach(s -> logger.debug("PropertyName: {}", s));
83 String volumioName = serviceInfo.getPropertyString("volumioName");
84 if (volumioName == null) {
88 String uuid = serviceInfo.getPropertyString("UUID");
93 String uuidAndServername = String.format("%s-%s", uuid, volumioName);
94 logger.debug("return new ThingUID({}, {});", THING_TYPE_VOLUMIO, uuidAndServername);
95 return new ThingUID(THING_TYPE_VOLUMIO, uuidAndServername);