2 * Copyright (c) 2010-2020 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.pioneeravr.internal.discovery;
15 import java.util.HashMap;
16 import java.util.HashSet;
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;
35 * An UpnpDiscoveryParticipant which allows to discover Pioneer AVRs.
37 * @author Antoine Besnard - Initial contribution
39 @Component(immediate = true)
40 public class PioneerAvrDiscoveryParticipant implements UpnpDiscoveryParticipant {
42 private final Logger logger = LoggerFactory.getLogger(PioneerAvrDiscoveryParticipant.class);
44 private boolean isAutoDiscoveryEnabled;
45 private Set<ThingTypeUID> supportedThingTypes;
47 public PioneerAvrDiscoveryParticipant() {
48 this.isAutoDiscoveryEnabled = true;
49 this.supportedThingTypes = PioneerAvrBindingConstants.SUPPORTED_THING_TYPES_UIDS;
53 * Called at the service activation.
55 * @param componentContext
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);
65 supportedThingTypes = isAutoDiscoveryEnabled ? PioneerAvrBindingConstants.SUPPORTED_THING_TYPES_UIDS
70 public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
71 return supportedThingTypes;
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);
86 result = DiscoveryResultBuilder.create(thingUid).withLabel(label).withProperties(properties).build();
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());
106 String deviceModel = device.getDetails().getModelDetails() != null
107 ? device.getDetails().getModelDetails().getModelName()
110 ThingTypeUID thingTypeUID = PioneerAvrBindingConstants.IP_AVR_THING_TYPE;
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;
138 result = new ThingUID(thingTypeUID, device.getIdentity().getUdn().getIdentifierString());
147 * Return true only if the given device model is supported.
152 private boolean isSupportedDeviceModel(String deviceModel, Set<String> supportedDeviceModels) {
153 return StringUtils.isNotBlank(deviceModel) && supportedDeviceModels.stream()
154 .anyMatch(input -> StringUtils.startsWithIgnoreCase(deviceModel, input));