]> git.basschouten.com Git - openhab-addons.git/blob
45291bc66d660dddf16d9a50af2f2688d8496233
[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.kodi.internal.discovery;
14
15 import static org.openhab.binding.kodi.internal.KodiBindingConstants.*;
16
17 import java.util.Dictionary;
18 import java.util.HashMap;
19 import java.util.Map;
20 import java.util.Set;
21
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.jupnp.model.meta.RemoteDevice;
25 import org.openhab.core.config.discovery.DiscoveryResult;
26 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
27 import org.openhab.core.config.discovery.upnp.UpnpDiscoveryParticipant;
28 import org.openhab.core.thing.ThingTypeUID;
29 import org.openhab.core.thing.ThingUID;
30 import org.osgi.service.component.ComponentContext;
31 import org.osgi.service.component.annotations.Activate;
32 import org.osgi.service.component.annotations.Component;
33 import org.osgi.service.component.annotations.Modified;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 /**
38  * An UpnpDiscoveryParticipant which allows to discover Kodi AVRs.
39  *
40  * @author Paul Frank - Initial contribution
41  * @author Christoph Weitkamp - Use "discovery.kodi:background=false" to disable discovery service
42  */
43 @Component(configurationPid = "discovery.kodi")
44 @NonNullByDefault
45 public class KodiUpnpDiscoveryParticipant implements UpnpDiscoveryParticipant {
46
47     private Logger logger = LoggerFactory.getLogger(KodiUpnpDiscoveryParticipant.class);
48
49     private boolean isAutoDiscoveryEnabled = true;
50
51     @Activate
52     protected void activate(ComponentContext componentContext) {
53         activateOrModifyService(componentContext);
54     }
55
56     @Modified
57     protected void modified(ComponentContext componentContext) {
58         activateOrModifyService(componentContext);
59     }
60
61     private void activateOrModifyService(ComponentContext componentContext) {
62         Dictionary<String, @Nullable Object> properties = componentContext.getProperties();
63         String autoDiscoveryPropertyValue = (String) properties.get("background");
64         if (autoDiscoveryPropertyValue != null && !autoDiscoveryPropertyValue.isEmpty()) {
65             isAutoDiscoveryEnabled = Boolean.valueOf(autoDiscoveryPropertyValue);
66         }
67     }
68
69     @Override
70     public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
71         return SUPPORTED_THING_TYPES_UIDS;
72     }
73
74     @Override
75     public @Nullable DiscoveryResult createResult(RemoteDevice device) {
76         if (isAutoDiscoveryEnabled) {
77             ThingUID thingUid = getThingUID(device);
78             if (thingUid != null) {
79                 String friendlyName = device.getDetails().getFriendlyName();
80                 String label = friendlyName == null || friendlyName.isEmpty() ? device.getDisplayString()
81                         : friendlyName;
82                 Map<String, Object> properties = new HashMap<>();
83                 properties.put(HOST_PARAMETER, device.getIdentity().getDescriptorURL().getHost());
84
85                 return DiscoveryResultBuilder.create(thingUid).withLabel(label).withProperties(properties)
86                         .withRepresentationProperty(HOST_PARAMETER).build();
87             }
88         }
89         return null;
90     }
91
92     @Override
93     public @Nullable ThingUID getThingUID(RemoteDevice device) {
94         String manufacturer = device.getDetails().getManufacturerDetails().getManufacturer();
95         if (containsIgnoreCase(manufacturer, MANUFACTURER)) {
96             logger.debug("Manufacturer matched: search: {}, device value: {}.", MANUFACTURER, manufacturer);
97             String type = device.getType().getType();
98             if (containsIgnoreCase(type, UPNP_DEVICE_TYPE)) {
99                 logger.debug("Device type matched: search: {}, device value: {}.", UPNP_DEVICE_TYPE, type);
100                 return new ThingUID(THING_TYPE_KODI, device.getIdentity().getUdn().getIdentifierString());
101             }
102         }
103         return null;
104     }
105
106     private boolean containsIgnoreCase(final @Nullable String str, final String searchStr) {
107         return str != null && str.toLowerCase().contains(searchStr.toLowerCase());
108     }
109 }