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

536 lines
19 KiB
JavaScript
Raw Permalink Normal View History

var WeMoNG = require('./lib/wemo.js');
2016-04-09 19:06:29 +02:00
var wemo = new WeMoNG();
//this won't work as there is no way to stop it...
//but is that a problem?
var interval = setInterval(wemo.start.bind(wemo), 60000);
wemo.start();
module.exports = function(RED) {
2016-04-09 19:06:29 +02:00
'use strict';
var util = require('util');
var ip = require('ip');
var bodyParser = require('body-parser');
var http = require('http');
var os = require('os');
var settings = RED.settings;
var subscriptions = {};
var sub2dev = {};
var resubscribe = function() {
var subs = Object.keys(subscriptions);
for (var s in subs) {
if (subs.hasOwnProperty(s)) {
var sub = subscriptions[subs[s]];
var dev = wemo.get(subs[s]);
var reSubOptions = {
host: dev.ip,
port: dev.port,
path: dev.device.UDN.indexOf('Bridge-1_0') < 0 ? '/upnp/event/basicevent1' : '/upnp/event/bridge1',
method: 'SUBSCRIBE',
headers: {
'SID': sub.sid,
'TIMEOUT': 'Second-300'
}
};
var resub_request = http.request(reSubOptions, function(res) {
//shoudl raise an error if needed
if (res.statusCode != 200) {
console.log('problem with resubscription %s - %s', res.statusCode, res.statusMessage);
console.log('opts - %s', util.inspect(reSubOptions));
console.log('dev - %s', util.inspect(dev));
delete subscriptions[subs[s]];
2016-04-09 19:06:29 +02:00
delete sub2dev[sub.sid];
subscribe({dev: subs[s]});
2017-01-29 18:45:44 +01:00
}
else {
2016-04-09 19:06:29 +02:00
// console.log("resubscription good %s", res.statusCode);
// console.log("dev - %s", util.inspect(dev));
}
});
resub_request.on('error', function() {
//console.log("failed to resubscribe to %s", dev.name );
//need to find a way to resubsribe
delete subscriptions[subs[s]];
2016-04-09 19:06:29 +02:00
delete sub2dev[sub.sid];
subscribe({dev: subs[s]});
});
resub_request.end();
2016-04-09 19:06:29 +02:00
}
}
2016-04-09 19:06:29 +02:00
};
setInterval(resubscribe, 100000);
2016-04-09 19:06:29 +02:00
var subscribe = function(node) {
var dev = node.dev;
var device = wemo.get(dev);
if (device) {
if (subscriptions[dev]) {
//exists
subscriptions[dev].count++;
2017-01-29 18:45:44 +01:00
}
else {
2016-04-09 19:06:29 +02:00
//new
var ipAddr;
//device.ip
var interfaces = os.networkInterfaces();
var interfaceNames = Object.keys(interfaces);
for (var name in interfaceNames) {
if (interfaceNames.hasOwnProperty(name)) {
var addrs = interfaces[interfaceNames[name]];
for (var add in addrs) {
if (addrs[add].netmask) {
//node 0.12 or better
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))) {
ipAddr = addrs[add].address;
break;
}
}
2017-01-29 18:45:44 +01:00
}
else {
2016-04-09 19:06:29 +02:00
//node 0.10 not great but best we can do
if (!addrs[add].internal && addrs[add].family == 'IPv4') {
ipAddr = addrs[add].address;
break;
}
}
}
if (ipAddr) {
break;
}
}
}
2016-04-09 19:06:29 +02:00
var callback_url = 'http://' + ipAddr + ':' + settings.uiPort;
if (settings.httpAdminRoot) {
callback_url += settings.httpAdminRoot;
}
2016-04-09 19:06:29 +02:00
if (callback_url.lastIndexOf('/') != (callback_url.length - 1)) {
callback_url += '/';
}
2016-04-09 19:06:29 +02:00
callback_url += 'wemoNG/notification';
// console.log('Callback URL = %s',callback_url);
2016-04-09 19:06:29 +02:00
var subscribeOptions = {
host: device.ip,
port: device.port,
path: device.device.UDN.indexOf('Bridge-1_0') < 0 ? '/upnp/event/basicevent1' : '/upnp/event/bridge1',
method: 'SUBSCRIBE',
headers: {
'CALLBACK': '<' + callback_url + '>',
'NT': 'upnp:event',
'TIMEOUT': 'Second-300'
}
};
//console.log(util.inspect(subscribeOptions));
var sub_request = http.request(subscribeOptions, function(res) {
//console.log("subscribe: %s - %s", device.name, res.statusCode);
if (res.statusCode == 200) {
subscriptions[dev] = {'count': 1, 'sid': res.headers.sid};
sub2dev[res.headers.sid] = dev;
2017-01-29 18:45:44 +01:00
}
else {
2016-04-09 19:06:29 +02:00
console.log('failed to subsrcibe');
}
});
sub_request.on('error', function(){
// devie probably offline
// try again after a minute
setTimeout( function() {
subscribe(node)
}, 60000)
})
2016-04-09 19:06:29 +02:00
sub_request.end();
}
}
2016-04-09 19:06:29 +02:00
};
2016-04-09 19:06:29 +02:00
function unsubscribe(node) {
var dev = node.dev;
if (subscriptions[dev]) {
if (subscriptions[dev].count == 1) {
var sid = subscriptions[dev].sid;
var device = wemo.get(dev);
//need to unsubsribe properly here
var unSubOpts = {
host: device.ip,
port: device.port,
path: device.device.UDN.indexOf('Bridge-1_0') < 0 ? '/upnp/event/basicevent1' : '/upnp/event/bridge1',
method: 'UNSUBSCRIBE',
headers: {
'SID': sid
}
};
//console.log(util.inspect(unSubOpts));
var unSubreq = http.request(unSubOpts, function(res) {
//console.log("unsubscribe: %s \n %s", device.name, res.statusCode);
delete subscriptions[dev];
delete sub2dev[sid];
});
unSubreq.end();
2017-01-29 18:45:44 +01:00
}
else {
2016-04-09 19:06:29 +02:00
subscriptions[dev].count--;
}
2017-01-29 18:45:44 +01:00
}
else {
2016-04-09 19:06:29 +02:00
//shouldn't ever get here
}
2016-04-09 19:06:29 +02:00
}
2016-04-09 19:06:29 +02:00
var wemoNGConfig = function(n) {
RED.nodes.createNode(this,n);
this.device = n.device;
};
RED.nodes.registerType('wemo-dev', wemoNGConfig);
var wemoNGNode = function(n) {
RED.nodes.createNode(this,n);
var node = this;
node.device = n.device;
node.name = n.name;
node.dev = RED.nodes.getNode(node.device).device;
node.status({fill: 'red',shape: 'dot',text: 'searching'});
//console.log("Control - %j" ,this.dev);
if (!wemo.get(node.dev)) {
wemo.on('discovered', function(d) {
if (node.dev === d) {
node.status({fill: 'green',shape: 'dot',text: 'found'});
}
});
2017-01-29 18:45:44 +01:00
}
else {
2016-04-09 19:06:29 +02:00
node.status({fill: 'green',shape: 'dot',text: 'found'});
}
node.on('input', function(msg, send, done){
2016-04-09 19:06:29 +02:00
var dev = wemo.get(node.dev);
send = send || function() { node.send.apply(node,arguments) }
2016-04-09 19:06:29 +02:00
if (!dev) {
//need to show that dev not currently found
// console.log('no device found');
if (done) {
done("Device not discovered yet")
} else {
node.error("Device not discovered yet", msg)
}
2016-04-09 19:06:29 +02:00
return;
}
2016-04-09 19:06:29 +02:00
var on = 0;
var capability = '10006';
2016-04-09 19:06:29 +02:00
if (typeof msg.payload === 'string') {
if (msg.payload == 'on' || msg.payload == '1' || msg.payload == 'true') {
on = 1;
2017-01-29 18:45:44 +01:00
}
else if (msg.payload === 'toggle') {
2016-04-09 19:06:29 +02:00
on = 2;
}
} else if (typeof msg.payload === 'number') {
2016-04-09 19:06:29 +02:00
if (msg.payload >= 0 && msg.payload < 3) {
on = msg.payload;
}
} else if (typeof msg.payload === 'object') {
2016-04-09 19:06:29 +02:00
//object need to get complicated here
if (msg.payload.hasOwnProperty('state') && typeof msg.payload.state === 'number') {
if (dev.type === 'socket' || dev.type === 'socket_insight') {
if (msg.payload.state >= 0 && msg.payload.state < 2) {
2016-04-09 19:06:29 +02:00
on = msg.payload.state;
}
2017-01-29 18:45:44 +01:00
}
else if (dev.type === 'light' || dev.type === 'group') {
// if (msg.payload.state >= 0 && msg.payload.state < 3) {
// on = msg.payload.state;
// }
var keys = Object.keys(msg.payload);
var caps = [];
var states = [];
for (var i=0; i<keys.length; i++) {
if (msg.payload.hasOwnProperty(keys[i])) {
if (wemo.reverseCapabilityMap.hasOwnProperty(keys[i])) {
caps.push(wemo.reverseCapabilityMap[keys[i]]);
states.push(msg.payload[keys[i]]);
}
}
}
if (caps.length > 0) {
capability = caps.join(',');
on = states.join(',');
2016-04-09 19:06:29 +02:00
}
}
}
} else if (typeof msg.payload === 'boolean') {
2016-04-09 19:06:29 +02:00
if (msg.payload) {
on = 1;
}
}
if (dev.type == 'socket' || dev.type == 'socket_insight') {
2016-04-09 19:06:29 +02:00
//console.log("socket");
wemo.toggleSocket(dev, on);
} else if (dev.type === 'light') {
wemo.setStatus(dev,capability, on);
} else {
//console.log('group');
wemo.setStatus(dev, capability, on);
2016-04-09 19:06:29 +02:00
}
});
2016-04-09 19:06:29 +02:00
};
RED.nodes.registerType('wemo out', wemoNGNode);
var wemoNGEvent = function(n) {
RED.nodes.createNode(this,n);
var node = this;
node.ipaddr = n.ipaddr;
node.device = n.device;
node.name = n.name;
node.topic = n.topic;
node.dev = RED.nodes.getNode(node.device).device;
node.status({fill: 'red',shape: 'dot',text: 'searching'});
var onEvent = function(notification) {
var d = sub2dev[notification.sid];
if (d == node.dev) {
var dd = wemo.get(node.dev);
notification.type = dd.type;
notification.name = dd.name;
if (!notification.id) {
notification.id = node.dev;
}
2016-04-09 19:06:29 +02:00
var msg = {
topic: node.topic ? node.topic : 'wemo',
payload: notification
};
switch (notification.type){
case 'light':
2017-01-29 18:45:44 +01:00
case 'group': {
2016-04-09 19:06:29 +02:00
if (dd.id === notification.id) {
node.send(msg);
}
break;
2017-01-29 18:45:44 +01:00
}
case 'socket':
case 'socket_insight': {
// For backwards compatibility, always send type 'socket'
notification.type = 'socket';
2016-04-09 19:06:29 +02:00
node.send(msg);
break;
2017-01-29 18:45:44 +01:00
}
default: {}
2016-04-09 19:06:29 +02:00
}
}
};
2016-04-09 19:06:29 +02:00
wemo.on('event', onEvent);
if (node.dev) {
//subscribe to events
if (wemo.get(node.dev)) {
node.status({fill: 'green',shape: 'dot',text: 'found'});
subscribe(node);
2017-01-29 18:45:44 +01:00
}
else {
2016-04-09 19:06:29 +02:00
wemo.on('discovered', function(d) {
if (node.dev === d) {
node.status({fill: 'green',shape: 'dot',text: 'found'});
subscribe(node);
}
});
}
2017-01-29 18:45:44 +01:00
}
else if (node.ipaddr) {
2016-04-09 19:06:29 +02:00
//legacy
var devices = Object.keys(wemo.devices);
for (var d in devices) {
if (devices.hasOwnProperty(d)) {
var device = devices[d];
if (device.ip === node.ipaddr) {
node.dev = device.id;
node.status({fill: 'green',shape: 'circle',text: 'reconfigure'});
subscribe(node);
break;
}
}
}
}
2016-04-09 19:06:29 +02:00
node.on('close', function(done) {
//should un subscribe from events
wemo.removeListener('event', onEvent);
unsubscribe(node);
done();
});
2016-04-09 19:06:29 +02:00
};
RED.nodes.registerType('wemo in', wemoNGEvent);
var wemoNGLookup = function(n) {
RED.nodes.createNode(this,n);
var node = this;
node.device = n.device;
node.name = n.name;
node.dev = RED.nodes.getNode(node.device).device;
node.status({fill: 'red',shape: 'dot',text: 'searching'});
if (!wemo.get(node.dev)) {
wemo.on('discovered', function(d) {
if (node.dev === d) {
node.status({fill: 'green',shape: 'dot',text: 'found'});
}
});
}
else {
node.status({fill: 'green',shape: 'dot',text: 'found'});
}
node.on('input', function(msg, send, done) {
var dev = wemo.get(node.dev);
send = send || function() { node.send.apply(node,arguments) }
if (!dev) {
//need to show that dev not currently found
console.log('Device not discovered yet');
if (done) {
done("Device not discovered yet")
} else {
node.error("Device not discovered yet",msg)
}
return;
}
//console.log(dev.type);
if (dev.type === 'light' || dev.type === 'group') {
//console.log("light");
wemo.getLightStatus(dev)
.then(function(status) {
// if (status.available) {
var caps = status.capabilities.split(',');
var vals = status.state.split(',');
for (var i=0; i<caps.length; i++) {
if (wemo.capabilityMap.hasOwnProperty(caps[i])) {
if (vals[i] !== '' && vals[i].indexOf(':') == -1) {
status[wemo.capabilityMap[caps[i]]] = parseInt(vals[i]);
} else {
status[wemo.capabilityMap[caps[i]]] = parseInt(vals[i].substring(0,vals[i].indexOf(':')));
}
}
}
delete status.capabilities;
// }
msg.payload = status;
send(msg);
if (done) {
done()
}
}).catch(function(err){
if (done) {
done(err)
} else {
node.error(err,msg)
}
});
} else if (dev.type === 'socket_insight') {
console.log("socket_insight");
// insight socket: ask both current switch status AND power measurements
wemo.getInsightParams(dev)
.then(function(insightParameters) {
msg.payload = insightParameters;
// 'state' should be a number for backwards compatibility
msg.payload.state = parseInt(msg.payload.state);
send(msg);
if (done) {
done()
}
}).catch(function(err){
if (done) {
done(err)
} else {
node.error(err,msg)
}
});
} else {
//console.log("socket");
// classic socket: no power measurement, so only request current switch status
wemo.getSocketStatus(dev)
.then(function(status) {
msg.payload = {
state: status
};
send(msg);
if (done) {
done()
}
})
.catch(function(err){
if (done) {
done(err)
} else {
node.error(err,msg)
}
});
}
});
node.on('close', function(done) {
done();
});
};
RED.nodes.registerType("wemo lookup", wemoNGLookup);
2016-04-09 19:06:29 +02:00
RED.httpAdmin.get('/wemoNG/devices', function(req, res) {
res.json(wemo.devices);
});
2016-04-09 19:06:29 +02:00
RED.httpAdmin.use('/wemoNG/notification',bodyParser.raw({type: 'text/xml'}));
2016-04-09 19:06:29 +02:00
RED.httpAdmin.notify('/wemoNG/notification', function(req, res) {
var notification = {
'sid': req.headers.sid
};
//console.log("Incoming Event %s", req.body.toString());
wemo.parseEvent(req.body.toString())
.then(function(evt) {
2016-04-09 19:06:29 +02:00
evt.sid = notification.sid;
wemo.emit('event',evt);
})
.catch(err => {
console.log(err)
2016-04-09 19:06:29 +02:00
});
res.send('');
});
2016-04-09 19:06:29 +02:00
};