]> git.basschouten.com Git - openhab-addons.git/blob
7d0ecea0d303844c909c38cd8a73f4c10b293326
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.onkyo.internal.discovery;
14
15 import static org.openhab.binding.onkyo.internal.OnkyoBindingConstants.*;
16
17 import java.util.Arrays;
18 import java.util.HashMap;
19 import java.util.HashSet;
20 import java.util.Map;
21 import java.util.Set;
22
23 import org.apache.commons.lang.StringUtils;
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.eclipse.jdt.annotation.Nullable;
26 import org.jupnp.model.meta.RemoteDevice;
27 import org.openhab.binding.onkyo.internal.OnkyoModel;
28 import org.openhab.core.config.discovery.DiscoveryResult;
29 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
30 import org.openhab.core.config.discovery.upnp.UpnpDiscoveryParticipant;
31 import org.openhab.core.thing.ThingTypeUID;
32 import org.openhab.core.thing.ThingUID;
33 import org.osgi.service.component.ComponentContext;
34 import org.osgi.service.component.annotations.Activate;
35 import org.osgi.service.component.annotations.Component;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 /**
40  * An UpnpDiscoveryParticipant which allows to discover Onkyo AVRs.
41  *
42  * @author Paul Frank - Initial contribution
43  */
44 @NonNullByDefault
45 @Component(immediate = true)
46 public class OnkyoUpnpDiscoveryParticipant implements UpnpDiscoveryParticipant {
47
48     private final Logger logger = LoggerFactory.getLogger(OnkyoUpnpDiscoveryParticipant.class);
49
50     private boolean isAutoDiscoveryEnabled;
51     private Set<ThingTypeUID> supportedThingTypes;
52
53     public OnkyoUpnpDiscoveryParticipant() {
54         this.isAutoDiscoveryEnabled = true;
55         this.supportedThingTypes = SUPPORTED_THING_TYPES_UIDS;
56     }
57
58     /**
59      * Called at the service activation.
60      *
61      * @param componentContext
62      */
63     @Activate
64     protected void activate(ComponentContext componentContext) {
65         if (componentContext.getProperties() != null) {
66             String autoDiscoveryPropertyValue = (String) componentContext.getProperties().get("enableAutoDiscovery");
67             if (StringUtils.isNotEmpty(autoDiscoveryPropertyValue)) {
68                 isAutoDiscoveryEnabled = Boolean.valueOf(autoDiscoveryPropertyValue);
69             }
70         }
71         supportedThingTypes = isAutoDiscoveryEnabled ? SUPPORTED_THING_TYPES_UIDS : new HashSet<>();
72     }
73
74     @Override
75     public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
76         return supportedThingTypes;
77     }
78
79     @Override
80     public @Nullable DiscoveryResult createResult(RemoteDevice device) {
81         DiscoveryResult result = null;
82         ThingUID thingUid = getThingUID(device);
83         if (thingUid != null) {
84             String label = StringUtils.isEmpty(device.getDetails().getFriendlyName()) ? device.getDisplayString()
85                     : device.getDetails().getFriendlyName();
86             Map<String, Object> properties = new HashMap<>(2, 1);
87             properties.put(HOST_PARAMETER, device.getIdentity().getDescriptorURL().getHost());
88             properties.put(UDN_PARAMETER, device.getIdentity().getUdn().getIdentifierString());
89
90             result = DiscoveryResultBuilder.create(thingUid).withLabel(label).withProperties(properties).build();
91         }
92
93         return result;
94     }
95
96     @Override
97     public @Nullable ThingUID getThingUID(RemoteDevice device) {
98         ThingUID result = null;
99         if (isAutoDiscoveryEnabled) {
100             if (StringUtils.containsIgnoreCase(device.getDetails().getManufacturerDetails().getManufacturer(),
101                     MANUFACTURER)) {
102                 logger.debug("Manufacturer matched: search: {}, device value: {}.", MANUFACTURER,
103                         device.getDetails().getManufacturerDetails().getManufacturer());
104                 if (StringUtils.containsIgnoreCase(device.getType().getType(), UPNP_DEVICE_TYPE)) {
105                     logger.debug("Device type matched: search: {}, device value: {}.", UPNP_DEVICE_TYPE,
106                             device.getType().getType());
107
108                     String deviceModel = device.getDetails().getModelDetails() != null
109                             ? device.getDetails().getModelDetails().getModelName()
110                             : null;
111
112                     logger.debug("Device model: {}.", deviceModel);
113
114                     ThingTypeUID thingTypeUID = findThingType(deviceModel);
115                     result = new ThingUID(thingTypeUID, device.getIdentity().getUdn().getIdentifierString());
116                 }
117             }
118         }
119
120         return result;
121     }
122
123     private ThingTypeUID findThingType(@Nullable String deviceModel) {
124         ThingTypeUID thingTypeUID = THING_TYPE_ONKYO_UNSUPPORTED;
125
126         for (ThingTypeUID thingType : SUPPORTED_THING_TYPES_UIDS) {
127             if (thingType.getId().equalsIgnoreCase(deviceModel)) {
128                 return thingType;
129             }
130         }
131
132         if (isSupportedDeviceModel(deviceModel)) {
133             thingTypeUID = THING_TYPE_ONKYOAV;
134         }
135
136         return thingTypeUID;
137     }
138
139     /**
140      * Return true only if the given device model is supported.
141      *
142      * @param deviceModel
143      * @return
144      */
145     private boolean isSupportedDeviceModel(final @Nullable String deviceModel) {
146         return StringUtils.isNotBlank(deviceModel) && Arrays.stream(OnkyoModel.values())
147                 .anyMatch(model -> StringUtils.startsWithIgnoreCase(deviceModel, model.getId()));
148     }
149 }