]> git.basschouten.com Git - openhab-addons.git/blob
2475b6578fff5686c2a63b8edb57d55172602710
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.chromecast.internal.discovery;
14
15 import static org.openhab.binding.chromecast.internal.ChromecastBindingConstants.*;
16
17 import java.util.HashMap;
18 import java.util.Map;
19 import java.util.Set;
20
21 import javax.jmdns.ServiceInfo;
22
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.openhab.core.config.discovery.DiscoveryResult;
26 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
27 import org.openhab.core.config.discovery.mdns.MDNSDiscoveryParticipant;
28 import org.openhab.core.thing.ThingTypeUID;
29 import org.openhab.core.thing.ThingUID;
30 import org.osgi.service.component.annotations.Component;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 /**
35  * The {@link ChromecastDiscoveryParticipant} is responsible for discovering Chromecast devices through UPnP.
36  *
37  * @author Kai Kreuzer - Initial contribution
38  * @author Daniel Walters - Change discovery protocol to mDNS
39  */
40 @Component
41 @NonNullByDefault
42 public class ChromecastDiscoveryParticipant implements MDNSDiscoveryParticipant {
43     private final Logger logger = LoggerFactory.getLogger(ChromecastDiscoveryParticipant.class);
44
45     private static final String PROPERTY_MODEL = "md";
46     private static final String PROPERTY_FRIENDLY_NAME = "fn";
47     private static final String PROPERTY_DEVICE_ID = "id";
48     private static final String SERVICE_TYPE = "_googlecast._tcp.local.";
49
50     @Override
51     public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
52         return SUPPORTED_THING_TYPES_UIDS;
53     }
54
55     @Override
56     public String getServiceType() {
57         return SERVICE_TYPE;
58     }
59
60     @Override
61     public @Nullable DiscoveryResult createResult(ServiceInfo service) {
62         final ThingUID uid = getThingUID(service);
63         if (uid == null) {
64             return null;
65         }
66
67         final Map<String, Object> properties = new HashMap<>(5);
68         String host = service.getHostAddresses()[0];
69         properties.put(HOST, host);
70         int port = service.getPort();
71         properties.put(PORT, port);
72         logger.debug("Chromecast Found: {} {}", host, port);
73         String id = service.getPropertyString(PROPERTY_DEVICE_ID);
74         properties.put(DEVICE_ID, id);
75         String friendlyName = service.getPropertyString(PROPERTY_FRIENDLY_NAME); // friendly name;
76
77         final DiscoveryResult result = DiscoveryResultBuilder.create(uid).withThingType(getThingType(service))
78                 .withProperties(properties).withRepresentationProperty(DEVICE_ID).withLabel(friendlyName).build();
79
80         return result;
81     }
82
83     private @Nullable ThingTypeUID getThingType(final ServiceInfo service) {
84         String model = service.getPropertyString(PROPERTY_MODEL); // model
85         logger.debug("Chromecast Type: {}", model);
86         if (model == null) {
87             return null;
88         }
89         switch (model) {
90             case "Chromecast Audio":
91                 return THING_TYPE_AUDIO;
92             case "Google Cast Group":
93                 return THING_TYPE_AUDIOGROUP;
94             default:
95                 return THING_TYPE_CHROMECAST;
96         }
97     }
98
99     @Override
100     public @Nullable ThingUID getThingUID(ServiceInfo service) {
101         ThingTypeUID thingTypeUID = getThingType(service);
102         if (thingTypeUID != null) {
103             String id = service.getPropertyString(PROPERTY_DEVICE_ID); // device id
104             return new ThingUID(thingTypeUID, id);
105         } else {
106             return null;
107         }
108     }
109 }