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