]> git.basschouten.com Git - openhab-addons.git/blob
692495f895b667c39e83aee8a2c73333e23ea9bb
[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.volumio.internal.discovery;
14
15 import static org.openhab.binding.volumio.internal.VolumioBindingConstants.THING_TYPE_VOLUMIO;
16
17 import java.util.Collections;
18 import java.util.HashMap;
19 import java.util.Map;
20 import java.util.Set;
21
22 import javax.jmdns.ServiceInfo;
23
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;
35
36 /**
37  * @author Patrick Sernetz - Initial contribution
38  * @author Chris Wohlbrecht - Adaption for openHAB 3
39  * @author Michael Loercher - Adaption for openHAB 3
40  */
41 @NonNullByDefault
42 @Component(configurationPid = "discovery.volumio")
43 public class VolumioDiscoveryParticipant implements MDNSDiscoveryParticipant {
44
45     private final Logger logger = LoggerFactory.getLogger(VolumioDiscoveryParticipant.class);
46
47     @Override
48     public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
49         return Set.of(THING_TYPE_VOLUMIO);
50     }
51
52     @Override
53     public String getServiceType() {
54         return VolumioBindingConstants.DISCOVERY_SERVICE_TYPE;
55     }
56
57     @Override
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);
62
63         logger.debug("Service Device: {}", serviceInfo);
64         logger.debug("Thing UID: {}", thingUID);
65
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");
71
72             discoveryResult = DiscoveryResultBuilder.create(thingUID).withProperties(properties).withLabel(volumioName)
73                     .build();
74             logger.debug("DiscoveryResult: {}", discoveryResult);
75         }
76         return discoveryResult;
77     }
78
79     @Override
80     public @Nullable ThingUID getThingUID(ServiceInfo serviceInfo) {
81         Collections.list(serviceInfo.getPropertyNames()).forEach(s -> logger.debug("PropertyName: {}", s));
82
83         String volumioName = serviceInfo.getPropertyString("volumioName");
84         if (volumioName == null) {
85             return null;
86         }
87
88         String uuid = serviceInfo.getPropertyString("UUID");
89         if (uuid == null) {
90             return null;
91         }
92
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);
96     }
97 }