Search in sources :

Example 61 with ThingTypeUID

use of org.eclipse.smarthome.core.thing.ThingTypeUID in project smarthome by eclipse.

the class WemoHandlerFactory method createHandler.

@Override
protected ThingHandler createHandler(Thing thing) {
    ThingTypeUID thingTypeUID = thing.getThingTypeUID();
    if (thingTypeUID != null) {
        logger.debug("Trying to create a handler for ThingType '{}", thingTypeUID);
        if (thingTypeUID.equals(WemoBindingConstants.THING_TYPE_BRIDGE)) {
            logger.debug("Creating a WemoBridgeHandler for thing '{}' with UDN '{}'", thing.getUID(), thing.getConfiguration().get(UDN));
            WemoBridgeHandler handler = new WemoBridgeHandler((Bridge) thing);
            registerDeviceDiscoveryService(handler);
            return handler;
        } else if (thingTypeUID.equals(WemoBindingConstants.THING_TYPE_MAKER)) {
            logger.debug("Creating a WemoMakerHandler for thing '{}' with UDN '{}'", thing.getUID(), thing.getConfiguration().get(UDN));
            return new WemoMakerHandler(thing, upnpIOService);
        } else if (WemoBindingConstants.SUPPORTED_DEVICE_THING_TYPES.contains(thing.getThingTypeUID())) {
            logger.debug("Creating a WemoHandler for thing '{}' with UDN '{}'", thing.getUID(), thing.getConfiguration().get(UDN));
            return new WemoHandler(thing, upnpIOService);
        } else if (thingTypeUID.equals(WemoBindingConstants.THING_TYPE_COFFEE)) {
            logger.debug("Creating a WemoCoffeeHandler for thing '{}' with UDN '{}'", thing.getUID(), thing.getConfiguration().get(UDN));
            return new WemoCoffeeHandler(thing, upnpIOService);
        } else if (thingTypeUID.equals(WemoBindingConstants.THING_TYPE_MZ100)) {
            return new WemoLightHandler(thing, upnpIOService);
        } else {
            logger.warn("ThingHandler not found for {}", thingTypeUID);
            return null;
        }
    }
    return null;
}
Also used : WemoBridgeHandler(org.eclipse.smarthome.binding.wemo.handler.WemoBridgeHandler) WemoMakerHandler(org.eclipse.smarthome.binding.wemo.handler.WemoMakerHandler) WemoCoffeeHandler(org.eclipse.smarthome.binding.wemo.handler.WemoCoffeeHandler) WemoHandler(org.eclipse.smarthome.binding.wemo.handler.WemoHandler) WemoLightHandler(org.eclipse.smarthome.binding.wemo.handler.WemoLightHandler) ThingTypeUID(org.eclipse.smarthome.core.thing.ThingTypeUID)

Example 62 with ThingTypeUID

use of org.eclipse.smarthome.core.thing.ThingTypeUID 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)

Example 63 with ThingTypeUID

use of org.eclipse.smarthome.core.thing.ThingTypeUID in project smarthome by eclipse.

the class WemoHandler method onSubscription.

private synchronized void onSubscription() {
    if (service.isRegistered(this)) {
        logger.debug("Checking WeMo GENA subscription for '{}'", this);
        ThingTypeUID thingTypeUID = thing.getThingTypeUID();
        String subscription = "basicevent1";
        if ((subscriptionState.get(subscription) == null) || !subscriptionState.get(subscription).booleanValue()) {
            logger.debug("Setting up GENA subscription {}: Subscribing to service {}...", getUDN(), subscription);
            service.addSubscription(this, subscription, SUBSCRIPTION_DURATION);
            subscriptionState.put(subscription, true);
        }
        if (thingTypeUID.equals(THING_TYPE_INSIGHT)) {
            subscription = "insight1";
            if ((subscriptionState.get(subscription) == null) || !subscriptionState.get(subscription).booleanValue()) {
                logger.debug("Setting up GENA subscription {}: Subscribing to service {}...", getUDN(), subscription);
                service.addSubscription(this, subscription, SUBSCRIPTION_DURATION);
                subscriptionState.put(subscription, true);
            }
        }
    } else {
        logger.debug("Setting up WeMo GENA subscription for '{}' FAILED - service.isRegistered(this) is FALSE", this);
    }
}
Also used : ThingTypeUID(org.eclipse.smarthome.core.thing.ThingTypeUID)

Aggregations

ThingTypeUID (org.eclipse.smarthome.core.thing.ThingTypeUID)63 ThingUID (org.eclipse.smarthome.core.thing.ThingUID)27 Test (org.junit.Test)27 JavaOSGiTest (org.eclipse.smarthome.test.java.JavaOSGiTest)25 Thing (org.eclipse.smarthome.core.thing.Thing)12 DiscoveryResult (org.eclipse.smarthome.config.discovery.DiscoveryResult)11 Locale (java.util.Locale)10 HashMap (java.util.HashMap)9 Collection (java.util.Collection)5 DiscoveryService (org.eclipse.smarthome.config.discovery.DiscoveryService)5 Before (org.junit.Before)5 Configuration (org.eclipse.smarthome.config.core.Configuration)4 DiscoveryListener (org.eclipse.smarthome.config.discovery.DiscoveryListener)4 ApiOperation (io.swagger.annotations.ApiOperation)3 ApiResponses (io.swagger.annotations.ApiResponses)3 Nullable (org.eclipse.jdt.annotation.Nullable)3 ThingRegistry (org.eclipse.smarthome.core.thing.ThingRegistry)3 BaseThingHandlerFactory (org.eclipse.smarthome.core.thing.binding.BaseThingHandlerFactory)3 Firmware (org.eclipse.smarthome.core.thing.binding.firmware.Firmware)3 ArrayList (java.util.ArrayList)2