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