Add ability to pass in wemo address as msg.ipaddr.

This commit is contained in:
Kerber 2015-05-11 00:03:22 -05:00
parent 36ac3817a7
commit e96c65c0bc

View File

@ -20,17 +20,34 @@ module.exports = function(RED) {
function WemoOut(n) { function WemoOut(n) {
RED.nodes.createNode(this,n); RED.nodes.createNode(this,n);
this.ipaddr = n.ipaddr; this.devices = [];
this.wemoSwitch = new Wemo(n.ipaddr); if (n.ipaddr) {
this.ipaddr = n.ipaddr;
this.devices[this.ipaddr] = new Wemo(n.ipaddr);
}
var node = this; var node = this;
this.on("input", function(msg) { this.on("input", function(msg) {
var state = 0; if (msg != null) {
if ( msg.payload == 1 || msg.payload === true || msg.payload == "on" ) { state = 1; } var device;
node.wemoSwitch.setBinaryState(state, function(err, result) { if (msg.ipaddr) {
if (err) { node.warn(err); } if (!this.devices[msg.ipaddr]) {
//else { node.log(result); } this.devices[msg.ipaddr]=new Wemo(msg.ipaddr);
}); }
device=this.devices[msg.ipaddr];
}
else if (this.ipaddr) {
device=this.devices[this.ipaddr];
}
if (device) {
var state = 0;
if ( msg.payload == 1 || msg.payload === true || msg.payload == "on" ) { state = 1; }
device.setBinaryState(state, function(err, result) {
if (err) { node.warn(err); }
//else { node.log(result); }
});
}
}
}); });
} }
RED.nodes.registerType("wemo out",WemoOut); RED.nodes.registerType("wemo out",WemoOut);