Search in sources :

Example 41 with DiscoveryResult

use of org.eclipse.smarthome.config.discovery.DiscoveryResult in project smarthome by eclipse.

the class WemoDiscoveryParticipant method createResult.

@Override
public DiscoveryResult createResult(RemoteDevice device) {
    ThingUID uid = getThingUID(device);
    if (uid != null) {
        Map<String, Object> properties = new HashMap<>(2);
        String label = "WeMo Device";
        try {
            label = device.getDetails().getFriendlyName();
        } catch (Exception e) {
        // ignore and use default label
        }
        properties.put(UDN, device.getIdentity().getUdn().getIdentifierString());
        DiscoveryResult result = DiscoveryResultBuilder.create(uid).withProperties(properties).withLabel(label).withRepresentationProperty(UDN).build();
        logger.debug("Created a DiscoveryResult for device '{}' with UDN '{}'", device.getDetails().getFriendlyName(), device.getIdentity().getUdn().getIdentifierString());
        return result;
    } else {
        return null;
    }
}
Also used : DiscoveryResult(org.eclipse.smarthome.config.discovery.DiscoveryResult) HashMap(java.util.HashMap) ThingUID(org.eclipse.smarthome.core.thing.ThingUID)

Example 42 with DiscoveryResult

use of org.eclipse.smarthome.config.discovery.DiscoveryResult in project smarthome by eclipse.

the class WemoLinkDiscoveryService method startScan.

@Override
protected void startScan() {
    logger.trace("Starting WeMoEndDevice discovery on WeMo Link {}", wemoBridgeHandler.getThing().getUID());
    try {
        String devUDN = "uuid:" + wemoBridgeHandler.getThing().getConfiguration().get(UDN).toString();
        logger.trace("devUDN = '{}'", devUDN);
        String soapHeader = "\"urn:Belkin:service:bridge:1#GetEndDevices\"";
        String content = "<?xml version=\"1.0\"?>" + "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">" + "<s:Body>" + "<u:GetEndDevices xmlns:u=\"urn:Belkin:service:bridge:1\">" + "<DevUDN>" + devUDN + "</DevUDN><ReqListType>PAIRED_LIST</ReqListType>" + "</u:GetEndDevices>" + "</s:Body>" + "</s:Envelope>";
        URL descriptorURL = service.getDescriptorURL(this);
        if (descriptorURL != null) {
            String deviceURL = StringUtils.substringBefore(descriptorURL.toString(), "/setup.xml");
            String wemoURL = deviceURL + "/upnp/control/bridge1";
            String endDeviceRequest = WemoHttpCall.executeCall(wemoURL, soapHeader, content);
            if (endDeviceRequest != null) {
                logger.trace("endDeviceRequest answered '{}'", endDeviceRequest);
                try {
                    String stringParser = StringUtils.substringBetween(endDeviceRequest, "<DeviceLists>", "</DeviceLists>");
                    stringParser = StringEscapeUtils.unescapeXml(stringParser);
                    // check if there are already paired devices with WeMo Link
                    if ("0".equals(stringParser)) {
                        logger.debug("There are no devices connected with WeMo Link. Exit discovery");
                        return;
                    }
                    // Build parser for received <DeviceList>
                    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                    DocumentBuilder db = dbf.newDocumentBuilder();
                    InputSource is = new InputSource();
                    is.setCharacterStream(new StringReader(stringParser));
                    Document doc = db.parse(is);
                    NodeList nodes = doc.getElementsByTagName("DeviceInfo");
                    // iterate the devices
                    for (int i = 0; i < nodes.getLength(); i++) {
                        Element element = (Element) nodes.item(i);
                        NodeList deviceIndex = element.getElementsByTagName("DeviceIndex");
                        Element line = (Element) deviceIndex.item(0);
                        logger.trace("DeviceIndex: {}", getCharacterDataFromElement(line));
                        NodeList deviceID = element.getElementsByTagName("DeviceID");
                        line = (Element) deviceID.item(0);
                        String endDeviceID = getCharacterDataFromElement(line);
                        logger.trace("DeviceID: {}", endDeviceID);
                        NodeList friendlyName = element.getElementsByTagName("FriendlyName");
                        line = (Element) friendlyName.item(0);
                        String endDeviceName = getCharacterDataFromElement(line);
                        logger.trace("FriendlyName: {}", endDeviceName);
                        NodeList vendor = element.getElementsByTagName("Manufacturer");
                        line = (Element) vendor.item(0);
                        String endDeviceVendor = getCharacterDataFromElement(line);
                        logger.trace("Manufacturer: {}", endDeviceVendor);
                        NodeList model = element.getElementsByTagName("ModelCode");
                        line = (Element) model.item(0);
                        String endDeviceModelID = getCharacterDataFromElement(line);
                        endDeviceModelID = endDeviceModelID.replaceAll(NORMALIZE_ID_REGEX, "_");
                        logger.trace("ModelCode: {}", endDeviceModelID);
                        if (SUPPORTED_THING_TYPES.contains(new ThingTypeUID(BINDING_ID, endDeviceModelID))) {
                            logger.debug("Discovered a WeMo LED Light thing with ID '{}'", endDeviceID);
                            ThingUID bridgeUID = wemoBridgeHandler.getThing().getUID();
                            ThingTypeUID thingTypeUID = new ThingTypeUID(BINDING_ID, endDeviceModelID);
                            if (thingTypeUID.equals(THING_TYPE_MZ100)) {
                                String thingLightId = endDeviceID;
                                ThingUID thingUID = new ThingUID(thingTypeUID, bridgeUID, thingLightId);
                                Map<String, Object> properties = new HashMap<>(1);
                                properties.put(DEVICE_ID, endDeviceID);
                                DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID).withProperties(properties).withBridge(wemoBridgeHandler.getThing().getUID()).withLabel(endDeviceName).build();
                                thingDiscovered(discoveryResult);
                            }
                        } else {
                            logger.debug("Discovered an unsupported device :");
                            logger.debug("DeviceIndex : {}", getCharacterDataFromElement(line));
                            logger.debug("DeviceID    : {}", endDeviceID);
                            logger.debug("FriendlyName: {}", endDeviceName);
                            logger.debug("Manufacturer: {}", endDeviceVendor);
                            logger.debug("ModelCode   : {}", endDeviceModelID);
                        }
                    }
                } catch (Exception e) {
                    logger.error("Failed to parse endDevices for bridge '{}'", wemoBridgeHandler.getThing().getUID(), e);
                }
            }
        }
    } catch (Exception e) {
        logger.error("Failed to get endDevices for bridge '{}'", wemoBridgeHandler.getThing().getUID(), e);
    }
}
Also used : InputSource(org.xml.sax.InputSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) HashMap(java.util.HashMap) NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) Document(org.w3c.dom.Document) URL(java.net.URL) DiscoveryResult(org.eclipse.smarthome.config.discovery.DiscoveryResult) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ThingUID(org.eclipse.smarthome.core.thing.ThingUID) StringReader(java.io.StringReader) ThingTypeUID(org.eclipse.smarthome.core.thing.ThingTypeUID)

Aggregations

DiscoveryResult (org.eclipse.smarthome.config.discovery.DiscoveryResult)42 ThingUID (org.eclipse.smarthome.core.thing.ThingUID)29 HashMap (java.util.HashMap)16 Test (org.junit.Test)14 ThingTypeUID (org.eclipse.smarthome.core.thing.ThingTypeUID)12 Collection (java.util.Collection)4 DiscoveryListener (org.eclipse.smarthome.config.discovery.DiscoveryListener)4 DiscoveryService (org.eclipse.smarthome.config.discovery.DiscoveryService)4 ThingStatusInfo (org.eclipse.smarthome.core.thing.ThingStatusInfo)4 DiscoveryResultFlag (org.eclipse.smarthome.config.discovery.DiscoveryResultFlag)3 InboxPredicates.forThingUID (org.eclipse.smarthome.config.discovery.inbox.InboxPredicates.forThingUID)3 Thing (org.eclipse.smarthome.core.thing.Thing)3 JavaOSGiTest (org.eclipse.smarthome.test.java.JavaOSGiTest)3 Date (java.util.Date)2 List (java.util.List)2 Map (java.util.Map)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 Collectors (java.util.stream.Collectors)2 Nullable (org.eclipse.jdt.annotation.Nullable)2 Configuration (org.eclipse.smarthome.config.core.Configuration)2