]> git.basschouten.com Git - openhab-addons.git/blob
76d8cdd1e75ab4d9376578d8191a64da19a1a3c0
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.sonyaudio.internal.discovery;
14
15 import java.io.BufferedReader;
16 import java.io.IOException;
17 import java.io.InputStreamReader;
18 import java.net.URL;
19 import java.util.HashMap;
20 import java.util.Map;
21 import java.util.Set;
22 import java.util.regex.Matcher;
23 import java.util.regex.Pattern;
24
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;
35
36 /**
37  * This class identifies SONY products by their Upnp service information.
38  *
39  * @author David Ã…berg - Initial contribution
40  */
41 @Component
42 public class SonyAudioDiscoveryParticipant implements UpnpDiscoveryParticipant {
43
44     private final Logger logger = LoggerFactory.getLogger(SonyAudioDiscoveryParticipant.class);
45
46     private Set<ThingTypeUID> supportedThingTypes;
47
48     public SonyAudioDiscoveryParticipant() {
49         this.supportedThingTypes = SonyAudioBindingConstants.SUPPORTED_THING_TYPES_UIDS;
50     }
51
52     @Override
53     public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
54         return supportedThingTypes;
55     }
56
57     @Override
58     public DiscoveryResult createResult(RemoteDevice device) {
59         DiscoveryResult result = null;
60
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();
69             try {
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) {
74                 return null;
75             }
76         }
77         return result;
78     }
79
80     @Override
81     public ThingUID getThingUID(RemoteDevice device) {
82         ThingUID result = null;
83
84         String manufacturer = device.getDetails().getManufacturerDetails().getManufacturer();
85         if (manufacturer == null
86                 || !manufacturer.toLowerCase().contains(SonyAudioBindingConstants.MANUFACTURER.toLowerCase())) {
87             return result;
88         }
89
90         logger.debug("Manufacturer matched: search: {}, device value: {}.", SonyAudioBindingConstants.MANUFACTURER,
91                 manufacturer);
92         String type = device.getType().getType();
93         if (type == null || !type.toLowerCase().contains(SonyAudioBindingConstants.UPNP_DEVICE_TYPE.toLowerCase())) {
94             return result;
95         }
96         logger.debug("Device type matched: search: {}, device value: {}.", SonyAudioBindingConstants.UPNP_DEVICE_TYPE,
97                 type);
98         logger.debug("Device services: {}", device.getServices().toString());
99         String deviceModel = device.getDetails().getModelDetails() != null
100                 ? device.getDetails().getModelDetails().getModelName()
101                 : null;
102         logger.debug("Device model: {}.", deviceModel);
103         ThingTypeUID thingTypeUID = findThingType(deviceModel);
104         if (thingTypeUID != null) {
105             result = new ThingUID(thingTypeUID, device.getIdentity().getUdn().getIdentifierString());
106         }
107         return result;
108     }
109
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)) {
114                 return thingType;
115             }
116         }
117
118         return thingTypeUID;
119     }
120
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()))) {
126             String s;
127             StringBuilder builder = new StringBuilder();
128             while ((s = bufferedReader.readLine()) != null) {
129                 builder.append(s);
130             }
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+)([^<]*)");
133
134             Matcher tagmatch = ScalarWebAPImatch.matcher(builder.toString());
135             if (tagmatch.find()) {
136                 Matcher matcher = baseURLmatch.matcher(tagmatch.group());
137                 matcher.find();
138                 // String scalar_host = matcher.group(0);
139                 int scalar_port = Integer.parseInt(matcher.group(2));
140                 String scalar_path = matcher.group(3);
141
142                 properties.put(SonyAudioBindingConstants.SCALAR_PORT_PARAMETER, scalar_port);
143                 properties.put(SonyAudioBindingConstants.SCALAR_PATH_PARAMETER, scalar_path);
144             }
145             return properties;
146         }
147     }
148 }