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;
}
}
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);
}
}
Aggregations