2 * Copyright (c) 2010-2021 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.sonyaudio.internal.discovery;
15 import java.io.BufferedReader;
16 import java.io.IOException;
17 import java.io.InputStreamReader;
19 import java.util.HashMap;
22 import java.util.regex.Matcher;
23 import java.util.regex.Pattern;
25 import org.apache.commons.lang.StringUtils;
26 import org.jupnp.model.meta.RemoteDevice;
27 import org.openhab.binding.sonyaudio.internal.SonyAudioBindingConstants;
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.annotations.Component;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
38 * This class identifies SONY products by their Upnp service information.
40 * @author David Ã…berg - Initial contribution
43 public class SonyAudioDiscoveryParticipant implements UpnpDiscoveryParticipant {
45 private final Logger logger = LoggerFactory.getLogger(SonyAudioDiscoveryParticipant.class);
47 private Set<ThingTypeUID> supportedThingTypes;
49 public SonyAudioDiscoveryParticipant() {
50 this.supportedThingTypes = SonyAudioBindingConstants.SUPPORTED_THING_TYPES_UIDS;
54 public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
55 return supportedThingTypes;
59 public DiscoveryResult createResult(RemoteDevice device) {
60 DiscoveryResult result = null;
62 ThingUID thingUid = getThingUID(device);
63 if (thingUid != null) {
64 String label = StringUtils.isEmpty(device.getDetails().getFriendlyName()) ? device.getDisplayString()
65 : device.getDetails().getFriendlyName();
66 String host = device.getIdentity().getDescriptorURL().getHost();
67 int port = device.getIdentity().getDescriptorURL().getPort();
68 String path = device.getIdentity().getDescriptorURL().getPath();
70 Map<String, Object> properties = getDescription(host, port, path);
71 properties.put(SonyAudioBindingConstants.HOST_PARAMETER,
72 device.getIdentity().getDescriptorURL().getHost());
73 result = DiscoveryResultBuilder.create(thingUid).withLabel(label).withProperties(properties).build();
74 } catch (IOException e) {
82 public ThingUID getThingUID(RemoteDevice device) {
83 ThingUID result = null;
85 if (!StringUtils.containsIgnoreCase(device.getDetails().getManufacturerDetails().getManufacturer(),
86 SonyAudioBindingConstants.MANUFACTURER)) {
90 logger.debug("Manufacturer matched: search: {}, device value: {}.", SonyAudioBindingConstants.MANUFACTURER,
91 device.getDetails().getManufacturerDetails().getManufacturer());
92 if (!StringUtils.containsIgnoreCase(device.getType().getType(), SonyAudioBindingConstants.UPNP_DEVICE_TYPE)) {
95 logger.debug("Device type matched: search: {}, device value: {}.", SonyAudioBindingConstants.UPNP_DEVICE_TYPE,
96 device.getType().getType());
97 logger.debug("Device services: {}", device.getServices().toString());
98 String deviceModel = device.getDetails().getModelDetails() != null
99 ? device.getDetails().getModelDetails().getModelName()
101 logger.debug("Device model: {}.", deviceModel);
102 ThingTypeUID thingTypeUID = findThingType(deviceModel);
103 if (thingTypeUID != null) {
104 result = new ThingUID(thingTypeUID, device.getIdentity().getUdn().getIdentifierString());
109 private ThingTypeUID findThingType(String deviceModel) {
110 ThingTypeUID thingTypeUID = null;
111 for (ThingTypeUID thingType : SonyAudioBindingConstants.SUPPORTED_THING_TYPES_UIDS) {
112 if (thingType.getId().equalsIgnoreCase(deviceModel)) {
120 private Map<String, Object> getDescription(String host, int port, String path) throws IOException {
121 Map<String, Object> properties = new HashMap<>(2, 1);
122 URL url = new URL("http", host, port, path);
123 logger.debug("URL: {}", url.toString());
124 try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(url.openStream()))) {
126 StringBuilder builder = new StringBuilder();
127 while ((s = bufferedReader.readLine()) != null) {
130 Pattern ScalarWebAPImatch = Pattern.compile("<av:X_ScalarWebAPI_BaseURL>(.*)</av:X_ScalarWebAPI_BaseURL>");
131 Pattern baseURLmatch = Pattern.compile("http://(\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}):(\\d+)([^<]*)");
133 Matcher tagmatch = ScalarWebAPImatch.matcher(builder.toString());
134 if (tagmatch.find()) {
135 Matcher matcher = baseURLmatch.matcher(tagmatch.group());
137 // String scalar_host = matcher.group(0);
138 int scalar_port = Integer.parseInt(matcher.group(2));
139 String scalar_path = matcher.group(3);
141 properties.put(SonyAudioBindingConstants.SCALAR_PORT_PARAMETER, scalar_port);
142 properties.put(SonyAudioBindingConstants.SCALAR_PATH_PARAMETER, scalar_path);