1
0
mirror of https://github.com/node-red/node-red-nodes.git synced 2023-10-10 13:36:58 +02:00

fix slight wrinkle in new Wemo node

This commit is contained in:
Dave Conway-Jones 2016-04-09 18:06:29 +01:00
parent 48bcd3c7df
commit c54a019899
2 changed files with 349 additions and 352 deletions

View File

@ -165,12 +165,12 @@
}, },
oneditprepare: function() { oneditprepare: function() {
var devices; var devices;
$.getJSON('wemoNG/devices', function(data){ $.getJSON('wemoNG/devices', function(data) {
devices = data; devices = data;
var devs = Object.keys(data); var devs = Object.keys(data);
if (devs.length !== 0) { if (devs.length !== 0) {
for (var d in devs) { for (var d in devs) {
if (dev.hasOwnProperty(d)) { if (devs.hasOwnProperty(d)) {
$('<option/>',{ $('<option/>',{
'value': devs[d], 'value': devs[d],
'text': data[devs[d]].name 'text': data[devs[d]].name
@ -181,7 +181,7 @@
} }
}); });
$('#node-config-input-device').change(function(){ $('#node-config-input-device').change(function() {
var id = $( "#node-config-input-device option:selected" ).first().val(); var id = $( "#node-config-input-device option:selected" ).first().val();
if (devices) { if (devices) {
$('#node-config-input-type').val(devices[id].type); $('#node-config-input-type').val(devices[id].type);

View File

@ -23,7 +23,7 @@ var interval = setInterval(wemo.start.bind(wemo), 60000);
wemo.start(); wemo.start();
module.exports = function(RED) { module.exports = function(RED) {
"use strict"; 'use strict';
var util = require('util'); var util = require('util');
var ip = require('ip'); var ip = require('ip');
var bodyParser = require('body-parser'); var bodyParser = require('body-parser');
@ -45,7 +45,7 @@ module.exports = function(RED) {
var reSubOptions = { var reSubOptions = {
host: dev.ip, host: dev.ip,
port: dev.port, port: dev.port,
path: dev.device.UDN.indexOf('Bridge-1_0') < 0 ? '/upnp/event/basicevent1': '/upnp/event/bridge1', path: dev.device.UDN.indexOf('Bridge-1_0') < 0 ? '/upnp/event/basicevent1' : '/upnp/event/bridge1',
method: 'SUBSCRIBE', method: 'SUBSCRIBE',
headers: { headers: {
'SID': sub.sid, 'SID': sub.sid,
@ -56,9 +56,9 @@ module.exports = function(RED) {
var resub_request = http.request(reSubOptions, function(res) { var resub_request = http.request(reSubOptions, function(res) {
//shoudl raise an error if needed //shoudl raise an error if needed
if (res.statusCode != 200) { if (res.statusCode != 200) {
console.log("problem with resubscription %s - %s", res.statusCode, res.statusMessage); console.log('problem with resubscription %s - %s', res.statusCode, res.statusMessage);
console.log("opts - %s", util.inspect(reSubOptions)); console.log('opts - %s', util.inspect(reSubOptions));
console.log("dev - %s", util.inspect(dev)); console.log('dev - %s', util.inspect(dev));
delete subscriptions[dev]; delete subscriptions[dev];
delete sub2dev[sub.sid]; delete sub2dev[sub.sid];
subscribe({dev: subs[s]}); subscribe({dev: subs[s]});
@ -68,7 +68,7 @@ module.exports = function(RED) {
} }
}); });
resub_request.on('error', function(){ resub_request.on('error', function() {
//console.log("failed to resubscribe to %s", dev.name ); //console.log("failed to resubscribe to %s", dev.name );
//need to find a way to resubsribe //need to find a way to resubsribe
delete subscriptions[dev]; delete subscriptions[dev];
@ -81,14 +81,14 @@ module.exports = function(RED) {
} }
} }
} };
setInterval(resubscribe, 200000); setInterval(resubscribe, 200000);
var subscribe = function(node) { var subscribe = function(node) {
var dev = node.dev; var dev = node.dev;
var device = wemo.get(dev); var device = wemo.get(dev);
if (device){ if (device) {
if (subscriptions[dev]) { if (subscriptions[dev]) {
//exists //exists
subscriptions[dev].count++; subscriptions[dev].count++;
@ -103,7 +103,7 @@ module.exports = function(RED) {
if (interfaceNames.hasOwnProperty(name)) { if (interfaceNames.hasOwnProperty(name)) {
var addrs = interfaces[interfaceNames[name]]; var addrs = interfaces[interfaceNames[name]];
for (var add in addrs) { for (var add in addrs) {
if (addrs[add].netmask){ if (addrs[add].netmask) {
//node 0.12 or better //node 0.12 or better
if (!addrs[add].internal && addrs[add].family == 'IPv4') { if (!addrs[add].internal && addrs[add].family == 'IPv4') {
if (ip.isEqual(ip.mask(addrs[add].address,addrs[add].netmask),ip.mask(device.ip,addrs[add].netmask))) { if (ip.isEqual(ip.mask(addrs[add].address,addrs[add].netmask),ip.mask(device.ip,addrs[add].netmask))) {
@ -126,22 +126,22 @@ module.exports = function(RED) {
} }
var callback_url = 'http://' + ipAddr + ':' + settings.uiPort; var callback_url = 'http://' + ipAddr + ':' + settings.uiPort;
if(settings.httpAdminRoot) { if (settings.httpAdminRoot) {
callback_url += settings.httpAdminRoot; callback_url += settings.httpAdminRoot;
} }
if (callback_url.lastIndexOf('/') != (callback_url.length -1)) { if (callback_url.lastIndexOf('/') != (callback_url.length - 1)) {
callback_url += '/'; callback_url += '/';
} }
callback_url += 'wemoNG/notification'; callback_url += 'wemoNG/notification';
console.log("Callback URL = %s",callback_url); console.log('Callback URL = %s',callback_url);
var subscribeOptions = { var subscribeOptions = {
host: device.ip, host: device.ip,
port: device.port, port: device.port,
path: device.device.UDN.indexOf('Bridge-1_0') < 0 ? '/upnp/event/basicevent1': '/upnp/event/bridge1', path: device.device.UDN.indexOf('Bridge-1_0') < 0 ? '/upnp/event/basicevent1' : '/upnp/event/bridge1',
method: 'SUBSCRIBE', method: 'SUBSCRIBE',
headers: { headers: {
'CALLBACK': '<' + callback_url + '>', 'CALLBACK': '<' + callback_url + '>',
@ -158,14 +158,14 @@ module.exports = function(RED) {
subscriptions[dev] = {'count': 1, 'sid': res.headers.sid}; subscriptions[dev] = {'count': 1, 'sid': res.headers.sid};
sub2dev[res.headers.sid] = dev; sub2dev[res.headers.sid] = dev;
} else { } else {
console.log("failed to subsrcibe"); console.log('failed to subsrcibe');
} }
}); });
sub_request.end(); sub_request.end();
} }
} }
} };
function unsubscribe(node) { function unsubscribe(node) {
var dev = node.dev; var dev = node.dev;
@ -178,7 +178,7 @@ module.exports = function(RED) {
var unSubOpts = { var unSubOpts = {
host: device.ip, host: device.ip,
port: device.port, port: device.port,
path: device.device.UDN.indexOf('Bridge-1_0') < 0 ? '/upnp/event/basicevent1': '/upnp/event/bridge1', path: device.device.UDN.indexOf('Bridge-1_0') < 0 ? '/upnp/event/basicevent1' : '/upnp/event/bridge1',
method: 'UNSUBSCRIBE', method: 'UNSUBSCRIBE',
headers: { headers: {
'SID': sid 'SID': sid
@ -187,7 +187,7 @@ module.exports = function(RED) {
//console.log(util.inspect(unSubOpts)); //console.log(util.inspect(unSubOpts));
var unSubreq = http.request(unSubOpts, function(res){ var unSubreq = http.request(unSubOpts, function(res) {
//console.log("unsubscribe: %s \n %s", device.name, res.statusCode); //console.log("unsubscribe: %s \n %s", device.name, res.statusCode);
delete subscriptions[dev]; delete subscriptions[dev];
delete sub2dev[sid]; delete sub2dev[sid];
@ -206,8 +206,8 @@ module.exports = function(RED) {
var wemoNGConfig = function(n) { var wemoNGConfig = function(n) {
RED.nodes.createNode(this,n); RED.nodes.createNode(this,n);
this.device = n.device; this.device = n.device;
} };
RED.nodes.registerType("wemo-dev", wemoNGConfig); RED.nodes.registerType('wemo-dev', wemoNGConfig);
var wemoNGNode = function(n) { var wemoNGNode = function(n) {
RED.nodes.createNode(this,n); RED.nodes.createNode(this,n);
@ -215,25 +215,25 @@ module.exports = function(RED) {
node.device = n.device; node.device = n.device;
node.name = n.name; node.name = n.name;
node.dev = RED.nodes.getNode(node.device).device; node.dev = RED.nodes.getNode(node.device).device;
node.status({fill:"red",shape:"dot",text:"searching"}); node.status({fill: 'red',shape: 'dot',text: 'searching'});
//console.log("Control - %j" ,this.dev); //console.log("Control - %j" ,this.dev);
if (!wemo.get(node.dev)){ if (!wemo.get(node.dev)) {
wemo.on('discovered', function(d){ wemo.on('discovered', function(d) {
if (node.dev === d) { if (node.dev === d) {
node.status({fill:"green",shape:"dot",text:"found"}); node.status({fill: 'green',shape: 'dot',text: 'found'});
} }
}); });
} else { } else {
node.status({fill:"green",shape:"dot",text:"found"}); node.status({fill: 'green',shape: 'dot',text: 'found'});
} }
node.on('input', function(msg){ node.on('input', function(msg) {
var dev = wemo.get(node.dev); var dev = wemo.get(node.dev);
if (!dev) { if (!dev) {
//need to show that dev not currently found //need to show that dev not currently found
console.log("no device found"); console.log('no device found');
return; return;
} }
@ -253,7 +253,7 @@ module.exports = function(RED) {
if (msg.payload.state && typeof msg.payload.state === 'number') { if (msg.payload.state && typeof msg.payload.state === 'number') {
if (dev.type === 'socket') { if (dev.type === 'socket') {
if (msg.payload >= 0 && msg.payload < 2) { if (msg.payload >= 0 && msg.payload < 2) {
on = msg.payload.state on = msg.payload.state;
} }
} else if (dev.type === 'light' || dev.type === 'group') { } else if (dev.type === 'light' || dev.type === 'group') {
if (msg.payload >= 0 && msg.payload < 3) { if (msg.payload >= 0 && msg.payload < 3) {
@ -272,14 +272,14 @@ module.exports = function(RED) {
wemo.toggleSocket(dev, on); wemo.toggleSocket(dev, on);
} else if (dev.type === 'light`') { } else if (dev.type === 'light`') {
//console.log("light"); //console.log("light");
wemo.setStatus(dev,"10006", on); wemo.setStatus(dev,'10006', on);
} else { } else {
console.log("group"); console.log('group');
wemo.setStatus(dev, "10006", on); wemo.setStatus(dev, '10006', on);
} }
}); });
} };
RED.nodes.registerType("wemo out", wemoNGNode); RED.nodes.registerType('wemo out', wemoNGNode);
var wemoNGEvent = function(n) { var wemoNGEvent = function(n) {
RED.nodes.createNode(this,n); RED.nodes.createNode(this,n);
@ -290,9 +290,9 @@ module.exports = function(RED) {
node.topic = n.topic; node.topic = n.topic;
node.dev = RED.nodes.getNode(node.device).device; node.dev = RED.nodes.getNode(node.device).device;
node.status({fill:"red",shape:"dot",text:"searching"}); node.status({fill: 'red',shape: 'dot',text: 'searching'});
var onEvent = function(notification){ var onEvent = function(notification) {
var d = sub2dev[notification.sid]; var d = sub2dev[notification.sid];
if (d == node.dev) { if (d == node.dev) {
var dd = wemo.get(node.dev); var dd = wemo.get(node.dev);
@ -328,12 +328,12 @@ module.exports = function(RED) {
if (node.dev) { if (node.dev) {
//subscribe to events //subscribe to events
if (wemo.get(node.dev)) { if (wemo.get(node.dev)) {
node.status({fill:"green",shape:"dot",text:"found"}); node.status({fill: 'green',shape: 'dot',text: 'found'});
subscribe(node); subscribe(node);
} else { } else {
wemo.on('discovered', function(d){ wemo.on('discovered', function(d) {
if (node.dev === d) { if (node.dev === d) {
node.status({fill:"green",shape:"dot",text:"found"}); node.status({fill: 'green',shape: 'dot',text: 'found'});
subscribe(node); subscribe(node);
} }
}); });
@ -346,7 +346,7 @@ module.exports = function(RED) {
var device = devices[d]; var device = devices[d];
if (device.ip === node.ipaddr) { if (device.ip === node.ipaddr) {
node.dev = device.id; node.dev = device.id;
node.status({fill:"green",shape:"circle",text:"reconfigure"}); node.status({fill: 'green',shape: 'circle',text: 'reconfigure'});
subscribe(node); subscribe(node);
break; break;
} }
@ -354,33 +354,30 @@ module.exports = function(RED) {
} }
} }
node.on('close', function(done) {
node.on('close', function(done){
//should un subscribe from events //should un subscribe from events
wemo.removeListener('event', onEvent); wemo.removeListener('event', onEvent);
unsubscribe(node); unsubscribe(node);
done(); done();
}); });
} };
RED.nodes.registerType("wemo in", wemoNGEvent) RED.nodes.registerType('wemo in', wemoNGEvent);
RED.httpAdmin.get('/wemoNG/devices', function(req,res){ RED.httpAdmin.get('/wemoNG/devices', function(req, res) {
res.json(wemo.devices); res.json(wemo.devices);
}); });
RED.httpAdmin.use('/wemoNG/notification',bodyParser.raw({type: 'text/xml'})); RED.httpAdmin.use('/wemoNG/notification',bodyParser.raw({type: 'text/xml'}));
RED.httpAdmin.notify('/wemoNG/notification', function(req, res){ RED.httpAdmin.notify('/wemoNG/notification', function(req, res) {
var notification = { var notification = {
'sid': req.headers.sid 'sid': req.headers.sid
}; };
//console.log("Incoming Event %s", req.body.toString()); //console.log("Incoming Event %s", req.body.toString());
wemo.parseEvent(req.body.toString()).then(function(evt){ wemo.parseEvent(req.body.toString()).then(function(evt) {
evt.sid = notification.sid; evt.sid = notification.sid;
wemo.emit('event',evt); wemo.emit('event',evt);
}); });
res.send(""); res.send('');
}); });
};
}