Create a gist now

Instantly share code, notes, and snippets.

@hardillb /bridge.js
Last active Mar 17, 2017

What would you like to do?
A short script to do admin tasks on the WeMo Link ZigBee bridge
var http = require('http');
var util = require('util');
var postbodyheader = [
'<?xml version="1.0" encoding="utf-8"?>',
'<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">',
'<s:Body>'].join('\n');
var postbodyfooter = ['</s:Body>',
'</s:Envelope>'
].join('\n');
// These details need updating to match your setup, the devs.js script can help you discover the values
var ipAddr = "192.168.1.52";
var port = 49153;
var udn = "uuid:Bridge-1_0-231442B0100xxxx";
var getEndDevices = {
method: 'POST',
path: '/upnp/control/bridge1',
action: '"urn:Belkin:service:bridge:1#GetEndDevices"',
body: [
postbodyheader,
'<u:GetEndDevices xmlns:u="urn:Belkin:service:bridge:1">',
'<DevUDN>%s</DevUDN>',
'<ReqListType>ALL_LIST</ReqListType>',
'</u:GetEndDevices>',
postbodyfooter
].join('\n')
}
var scanDevices = {
method: 'POST',
path: '/upnp/control/bridge1',
action: '"urn:Belkin:service:bridge:1#GetEndDevices"',
body: [
postbodyheader,
'<u:GetEndDevices xmlns:u="urn:Belkin:service:bridge:1">',
'<DevUDN>%s</DevUDN>',
'<ReqListType>SCAN_LIST</ReqListType>',
'</u:GetEndDevices>',
postbodyfooter
].join('\n')
}
var unpairedDevices = {
method: 'POST',
path: '/upnp/control/bridge1',
action: '"urn:Belkin:service:bridge:1#GetEndDevices"',
body: [
postbodyheader,
'<u:GetEndDevices xmlns:u="urn:Belkin:service:bridge:1">',
'<DevUDN>%s</DevUDN>',
'<ReqListType>UNPAIRED_LIST</ReqListType>',
'</u:GetEndDevices>',
postbodyfooter
].join('\n')
}
var addDevice = {
method: 'POST',
path: '/upnp/control/bridge1',
action: '"urn:Belkin:service:bridge:1#AddDevice"',
body: [
postbodyheader,
'<u:AddDevice xmlns:u="urn:Belkin:service:bridge:1">',
'<DeviceIDs>%s</DeviceIDs>',
'</u:AddDevice>',
postbodyfooter
].join('\n')
}
var openNetwork = {
method: 'POST',
path: '/upnp/control/bridge1',
action: '"urn:Belkin:service:bridge:1#OpenNetwork"',
body: [
postbodyheader,
'<u:OpenNetwork xmlns:u="urn:Belkin:service:bridge:1">',
'<DevUDN>%s</DevUDN>',
'</u:OpenNetwork>',
postbodyfooter
].join('\n')
}
var closeNetwork = {
method: 'POST',
path: '/upnp/control/bridge1',
action: '"urn:Belkin:service:bridge:1#CloseNetwork"',
body: [
postbodyheader,
'<u:CloseNetwork xmlns:u="urn:Belkin:service:bridge:1">',
'<DevUDN>%s</DevUDN>',
'</u:CloseNetwork>',
postbodyfooter
].join('\n')
}
var identifyDevice = {
method: 'POST',
path: '/upnp/control/bridge1',
action: '"urn:Belkin:service:bridge:1#IdentifyDevice"',
body: [
postbodyheader,
'<u:IdentifyDevice xmlns:u="urn:Belkin:service:bridge:1">',
'<DeviceID>%s</DeviceID>',
'</u:IdentifyDevice>',
postbodyfooter
].join('\n')
}
var createGroup = {
method: 'POST',
path: '/upnp/control/bridge1',
action: '"urn:Belkin:service:bridge:1#CreateGroup"',
body: [
postbodyheader,
'<u:CreateGroup xmlns:u="urn:Belkin:service:bridge:1">',
'<ReqCreateGroup>',
'&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;&lt;CreateGroup&gt;&lt;GroupID&gt;%d&lt;/GroupID&gt;&lt;GroupName&gt;%s&lt;/GroupName&gt;&lt;DeviceIDList&gt;%s&lt;/DeviceIDList&gt;&lt;GroupCapabilityIDs&gt;%s&lt;/GroupCapabilityIDs&gt;&lt;GroupCapabilityValues&gt;%s&lt;/GroupCapabilityValues&gt;&lt;/CreateGroup&gt;',
'</ReqCreateGroup>',
'</u:CreateGroup>',
postbodyfooter
].join('\n')
}
var getCapabilitiesList = {
method: 'POST',
path: '/upnp/control/bridge1',
action: '"urn:Belkin:service:bridge:1#GetCapabilityProfileIDList"',
body: [
postbodyheader,
'<u:GetCapabilityProfileIDList xmlns:u="urn:Belkin:service:bridge:1">',
'<DevUDN>%s</DevUDN>',
'</u:GetCapabilityProfileIDList>',
postbodyfooter
].join('\n')
}
var getDevCapabilities = {
method: 'POST',
path: '/upnp/control/bridge1',
action: '"urn:Belkin:service:bridge:1#GetDeviceCapabilities"',
body: [
postbodyheader,
'<u:GetDeviceCapabilities xmlns:u="urn:Belkin:service:bridge:1">',
'<DeviceIDs>%s</DeviceIDs>',
'</u:GetDeviceCapabilities>',
postbodyfooter
].join('\n')
}
//this isn't right just yet
var getCapabilitiesProfileList = {
method: 'POST',
path: '/upnp/control/bridge1',
action: '"urn:Belkin:service:bridge:1#GetCapabilityProfileList"',
body: [
postbodyheader,
'<u:GetCapabilityProfileList xmlns:u="urn:Belkin:service:bridge:1">',
'<CapabilityIDs>10006,10008,10300,30008,30009,3000A,30301</CapabilityIDs>',
'</u:GetCapabilityProfileList>',
postbodyfooter
].join('\n')
}
var command = getEndDevices;
console.log(process.argv.length);
var body;
if (process.argv.length === 2) {
console.log("hello");
command = getEndDevices;
body = util.format(command.body, udn);
} else if (process.argv.length === 3) {
console.log("bar");
if (process.argv[2] === 'devices') {
command = getEndDevices;
udn = "";
} else if (process.argv[2] === 'open') {
command = openNetwork;
} else if (process.argv[2] === 'close') {
command = closeNetwork;
} else if (process.argv[2] === 'capabilities') {
command = getCapabilitiesList;
} else if (process.argv[2] === 'capabilityDetails') {
command = getCapabilitiesProfileList;
udn = "";
} else if (process.argv[2] === 'scan') {
command = scanDevices;
} else if (process.argv[2] === 'unpaired') {
command = unpairedDevices;
}
body = util.format(command.body, udn);
} else if (process.argv.length === 4) {
console.log("foo");
if (process.argv[2] === 'identify') {
command = identifyDevice;
} else if (process.argv[2] === 'deviceCapabilities') {
command = getDevCapabilities;
} else if (process.argv[2] == 'add') {
command = addDevice;
}
body = util.format(command.body, process.argv[3]);
} else if (process.argv.length === 8) {
if (process.argv[2] === 'createGroup') {
command = createGroup;
body = util.format(command.body,
process.argv[3],
process.argv[4],
process.argv[5],
process.argv[6]);
}
}
console.log("%s\n", body);
var options = {
host: ipAddr,
port: port,
path: command.path,
method: command.method,
headers: {
'SOAPACTION': command.action,
'Content-Type': 'text/xml; charset="utf-8"',
'Accept': ''
}
};
var request = http.request(options, function(res){
var data = "";
res.setEncoding('utf8');
res.on('data', function(chunk){
data += chunk;
});
res.on('end', function(){
console.log(data);
});
});
request.on('error', function(e){
console.log(e);
});
request.write(body);
request.end();
var Client = require('node-ssdp').Client;
var client = new Client();
var urn = 'urn:Belkin:service:basicevent:1';
client.setMaxListeners(0);
client.on('response', function (headers, statusCode, rinfo) {
console.log("%j",headers);
});
client.search(urn);
setTimeout(function(){
client._stop();
},5000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment