]> git.basschouten.com Git - openhab-addons.git/blob
648447085ad8e8ec946a99616cd2f77167cc5290
[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.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.jupnp.model.meta.RemoteDevice;
21 import org.openhab.binding.pioneeravr.internal.PioneerAvrBindingConstants;
22 import org.openhab.core.config.discovery.DiscoveryResult;
23 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
24 import org.openhab.core.config.discovery.upnp.UpnpDiscoveryParticipant;
25 import org.openhab.core.thing.ThingTypeUID;
26 import org.openhab.core.thing.ThingUID;
27 import org.osgi.service.component.ComponentContext;
28 import org.osgi.service.component.annotations.Activate;
29 import org.osgi.service.component.annotations.Component;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 /**
34  * An UpnpDiscoveryParticipant which allows to discover Pioneer AVRs.
35  *
36  * @author Antoine Besnard - Initial contribution
37  */
38 @Component
39 public class PioneerAvrDiscoveryParticipant implements UpnpDiscoveryParticipant {
40
41     private final Logger logger = LoggerFactory.getLogger(PioneerAvrDiscoveryParticipant.class);
42
43     private boolean isAutoDiscoveryEnabled;
44     private Set<ThingTypeUID> supportedThingTypes;
45
46     public PioneerAvrDiscoveryParticipant() {
47         this.isAutoDiscoveryEnabled = true;
48         this.supportedThingTypes = PioneerAvrBindingConstants.SUPPORTED_THING_TYPES_UIDS;
49     }
50
51     /**
52      * Called at the service activation.
53      *
54      * @param componentContext
55      */
56     @Activate
57     protected void activate(ComponentContext componentContext) {
58         if (componentContext.getProperties() != null) {
59             String autoDiscoveryPropertyValue = (String) componentContext.getProperties().get("enableAutoDiscovery");
60             if (autoDiscoveryPropertyValue != null && !autoDiscoveryPropertyValue.isEmpty()) {
61                 isAutoDiscoveryEnabled = Boolean.valueOf(autoDiscoveryPropertyValue);
62             }
63         }
64         supportedThingTypes = isAutoDiscoveryEnabled ? PioneerAvrBindingConstants.SUPPORTED_THING_TYPES_UIDS
65                 : new HashSet<>();
66     }
67
68     @Override
69     public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
70         return supportedThingTypes;
71     }
72
73     @Override
74     public DiscoveryResult createResult(RemoteDevice device) {
75         DiscoveryResult result = null;
76         ThingUID thingUid = getThingUID(device);
77         if (thingUid != null) {
78             String friendlyName = device.getDetails().getFriendlyName();
79             String label = friendlyName == null || friendlyName.isEmpty() ? device.getDisplayString() : friendlyName;
80             Map<String, Object> properties = new HashMap<>(2, 1);
81             properties.put(PioneerAvrBindingConstants.HOST_PARAMETER,
82                     device.getIdentity().getDescriptorURL().getHost());
83             properties.put(PioneerAvrBindingConstants.PROTOCOL_PARAMETER, PioneerAvrBindingConstants.IP_PROTOCOL_NAME);
84
85             result = DiscoveryResultBuilder.create(thingUid).withLabel(label).withProperties(properties).build();
86         }
87
88         return result;
89     }
90
91     @Override
92     public ThingUID getThingUID(RemoteDevice device) {
93         ThingUID result = null;
94         if (isAutoDiscoveryEnabled) {
95             String manufacturer = device.getDetails().getManufacturerDetails().getManufacturer();
96             if (manufacturer != null
97                     && manufacturer.toLowerCase().contains(PioneerAvrBindingConstants.MANUFACTURER.toLowerCase())) {
98                 logger.debug("Manufacturer matched: search: {}, device value: {}.",
99                         PioneerAvrBindingConstants.MANUFACTURER, manufacturer);
100                 String type = device.getType().getType();
101                 if (type != null
102                         && type.toLowerCase().contains(PioneerAvrBindingConstants.UPNP_DEVICE_TYPE.toLowerCase())) {
103                     logger.debug("Device type matched: search: {}, device value: {}.",
104                             PioneerAvrBindingConstants.UPNP_DEVICE_TYPE, type);
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 deviceModel != null && !deviceModel.isBlank() && supportedDeviceModels.stream()
154                 .anyMatch(input -> deviceModel.toLowerCase().startsWith(input.toLowerCase()));
155     }
156 }