]> git.basschouten.com Git - openhab-addons.git/blob
3c906906a5f5f85228e7b2466f2347ef04348926
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.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;
36
37 /**
38  * This class identifies SONY products by their Upnp service information.
39  *
40  * @author David Ã…berg - Initial contribution
41  */
42 @Component
43 public class SonyAudioDiscoveryParticipant implements UpnpDiscoveryParticipant {
44
45     private final Logger logger = LoggerFactory.getLogger(SonyAudioDiscoveryParticipant.class);
46
47     private Set<ThingTypeUID> supportedThingTypes;
48
49     public SonyAudioDiscoveryParticipant() {
50         this.supportedThingTypes = SonyAudioBindingConstants.SUPPORTED_THING_TYPES_UIDS;
51     }
52
53     @Override
54     public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
55         return supportedThingTypes;
56     }
57
58     @Override
59     public DiscoveryResult createResult(RemoteDevice device) {
60         DiscoveryResult result = null;
61
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();
69             try {
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) {
75                 return null;
76             }
77         }
78         return result;
79     }
80
81     @Override
82     public ThingUID getThingUID(RemoteDevice device) {
83         ThingUID result = null;
84
85         if (!StringUtils.containsIgnoreCase(device.getDetails().getManufacturerDetails().getManufacturer(),
86                 SonyAudioBindingConstants.MANUFACTURER)) {
87             return result;
88         }
89
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)) {
93             return result;
94         }
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()
100                 : null;
101         logger.debug("Device model: {}.", deviceModel);
102         ThingTypeUID thingTypeUID = findThingType(deviceModel);
103         if (thingTypeUID != null) {
104             result = new ThingUID(thingTypeUID, device.getIdentity().getUdn().getIdentifierString());
105         }
106         return result;
107     }
108
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)) {
113                 return thingType;
114             }
115         }
116
117         return thingTypeUID;
118     }
119
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()))) {
125             String s;
126             StringBuilder builder = new StringBuilder();
127             while ((s = bufferedReader.readLine()) != null) {
128                 builder.append(s);
129             }
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+)([^<]*)");
132
133             Matcher tagmatch = ScalarWebAPImatch.matcher(builder.toString());
134             if (tagmatch.find()) {
135                 Matcher matcher = baseURLmatch.matcher(tagmatch.group());
136                 matcher.find();
137                 // String scalar_host = matcher.group(0);
138                 int scalar_port = Integer.parseInt(matcher.group(2));
139                 String scalar_path = matcher.group(3);
140
141                 properties.put(SonyAudioBindingConstants.SCALAR_PORT_PARAMETER, scalar_port);
142                 properties.put(SonyAudioBindingConstants.SCALAR_PATH_PARAMETER, scalar_path);
143             }
144             return properties;
145         }
146     }
147 }