2 * Copyright (c) 2010-2022 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.jupnp.model.meta.RemoteDevice;
26 import org.openhab.binding.sonyaudio.internal.SonyAudioBindingConstants;
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.annotations.Component;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
37 * This class identifies SONY products by their Upnp service information.
39 * @author David Ã…berg - Initial contribution
42 public class SonyAudioDiscoveryParticipant implements UpnpDiscoveryParticipant {
44 private final Logger logger = LoggerFactory.getLogger(SonyAudioDiscoveryParticipant.class);
46 private Set<ThingTypeUID> supportedThingTypes;
48 public SonyAudioDiscoveryParticipant() {
49 this.supportedThingTypes = SonyAudioBindingConstants.SUPPORTED_THING_TYPES_UIDS;
53 public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
54 return supportedThingTypes;
58 public DiscoveryResult createResult(RemoteDevice device) {
59 DiscoveryResult result = null;
61 ThingUID thingUid = getThingUID(device);
62 if (thingUid != null) {
63 String friendlyName = device.getDetails().getFriendlyName();
64 String label = friendlyName == null || friendlyName.isEmpty() ? device.getDisplayString() : friendlyName;
65 URL descriptorURL = device.getIdentity().getDescriptorURL();
66 String host = descriptorURL.getHost();
67 int port = descriptorURL.getPort();
68 String path = descriptorURL.getPath();
70 Map<String, Object> properties = getDescription(host, port, path);
71 properties.put(SonyAudioBindingConstants.HOST_PARAMETER, descriptorURL.getHost());
72 result = DiscoveryResultBuilder.create(thingUid).withLabel(label).withProperties(properties).build();
73 } catch (IOException e) {
81 public ThingUID getThingUID(RemoteDevice device) {
82 ThingUID result = null;
84 String manufacturer = device.getDetails().getManufacturerDetails().getManufacturer();
85 if (manufacturer == null
86 || !manufacturer.toLowerCase().contains(SonyAudioBindingConstants.MANUFACTURER.toLowerCase())) {
90 logger.debug("Manufacturer matched: search: {}, device value: {}.", SonyAudioBindingConstants.MANUFACTURER,
92 String type = device.getType().getType();
93 if (type == null || !type.toLowerCase().contains(SonyAudioBindingConstants.UPNP_DEVICE_TYPE.toLowerCase())) {
96 logger.debug("Device type matched: search: {}, device value: {}.", SonyAudioBindingConstants.UPNP_DEVICE_TYPE,
98 logger.debug("Device services: {}", device.getServices().toString());
99 String deviceModel = device.getDetails().getModelDetails() != null
100 ? device.getDetails().getModelDetails().getModelName()
102 logger.debug("Device model: {}.", deviceModel);
103 ThingTypeUID thingTypeUID = findThingType(deviceModel);
104 if (thingTypeUID != null) {
105 result = new ThingUID(thingTypeUID, device.getIdentity().getUdn().getIdentifierString());
110 private ThingTypeUID findThingType(String deviceModel) {
111 ThingTypeUID thingTypeUID = null;
112 for (ThingTypeUID thingType : SonyAudioBindingConstants.SUPPORTED_THING_TYPES_UIDS) {
113 if (thingType.getId().equalsIgnoreCase(deviceModel)) {
121 private Map<String, Object> getDescription(String host, int port, String path) throws IOException {
122 Map<String, Object> properties = new HashMap<>(2, 1);
123 URL url = new URL("http", host, port, path);
124 logger.debug("URL: {}", url.toString());
125 try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(url.openStream()))) {
127 StringBuilder builder = new StringBuilder();
128 while ((s = bufferedReader.readLine()) != null) {
131 Pattern ScalarWebAPImatch = Pattern.compile("<av:X_ScalarWebAPI_BaseURL>(.*)</av:X_ScalarWebAPI_BaseURL>");
132 Pattern baseURLmatch = Pattern.compile("http://(\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}):(\\d+)([^<]*)");
134 Matcher tagmatch = ScalarWebAPImatch.matcher(builder.toString());
135 if (tagmatch.find()) {
136 Matcher matcher = baseURLmatch.matcher(tagmatch.group());
138 // String scalar_host = matcher.group(0);
139 int scalar_port = Integer.parseInt(matcher.group(2));
140 String scalar_path = matcher.group(3);
142 properties.put(SonyAudioBindingConstants.SCALAR_PORT_PARAMETER, scalar_port);
143 properties.put(SonyAudioBindingConstants.SCALAR_PATH_PARAMETER, scalar_path);