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

Update node-red-nodes io nodes to use strict and pass jshint scan

This commit is contained in:
Dave C-J 2014-06-28 23:35:33 +01:00
parent 9e0585a721
commit 052a7d587d
4 changed files with 182 additions and 174 deletions

View File

@ -14,42 +14,44 @@
* limitations under the License. * limitations under the License.
**/ **/
var RED = require(process.env.NODE_RED_HOME+"/red/red"); module.exports = function(RED) {
var spawn = require('child_process').spawn; "use strict";
var plat = require('os').platform(); var spawn = require('child_process').spawn;
var plat = require('os').platform();
function PingNode(n) { function PingNode(n) {
RED.nodes.createNode(this,n); RED.nodes.createNode(this,n);
this.host = n.host; this.host = n.host;
this.timer = n.timer * 1000; this.timer = n.timer * 1000;
var node = this; var node = this;
node.tout = setInterval(function() { node.tout = setInterval(function() {
var ex; var ex;
if (plat == "linux") ex = spawn('ping', ['-n', '-w', '5', '-c', '1', node.host]); if (plat == "linux") { ex = spawn('ping', ['-n', '-w', '5', '-c', '1', node.host]); }
else if (plat.match(/^win/)) ex = spawn('ping', ['-n', '1', '-w', '5000', node.host]); else if (plat.match(/^win/)) { ex = spawn('ping', ['-n', '1', '-w', '5000', node.host]); }
else if (plat == "darwin") ex = spawn('ping', ['-n', '-t', '5', '-c', '1', node.host]); else if (plat == "darwin") { ex = spawn('ping', ['-n', '-t', '5', '-c', '1', node.host]); }
else node.error("Sorry - your platform - "+plat+" - is not recognised."); else { node.error("Sorry - your platform - "+plat+" - is not recognised."); }
var res = false; var res = false;
ex.stdout.on('data', function (data) { ex.stdout.on('data', function (data) {
//console.log('[ping] stdout: ' + data.toString()); //console.log('[ping] stdout: ' + data.toString());
var regex = /from.*time.(.*)ms/; var regex = /from.*time.(.*)ms/;
var m = regex.exec(data.toString())||""; var m = regex.exec(data.toString())||"";
if (m != '') { res = Number(m[1]); } if (m !== '') { res = Number(m[1]); }
}); });
ex.stderr.on('data', function (data) { ex.stderr.on('data', function (data) {
//console.log('[ping] stderr: ' + data); //console.log('[ping] stderr: ' + data);
}); });
ex.on('close', function (code) { ex.on('close', function (code) {
//console.log('[ping] result: ' + code); //console.log('[ping] result: ' + code);
var msg = { payload:false, topic:node.host }; var msg = { payload:false, topic:node.host };
if (code == 0) msg = { payload:res, topic:node.host }; if (code === 0) { msg = { payload:res, topic:node.host }; }
node.send(msg); node.send(msg);
}); });
}, node.timer); }, node.timer);
this.on("close", function() { this.on("close", function() {
clearInterval(this.tout); clearInterval(this.tout);
}); });
}
RED.nodes.registerType("ping",PingNode);
} }
RED.nodes.registerType("ping",PingNode);

View File

@ -14,131 +14,135 @@
* limitations under the License. * limitations under the License.
**/ **/
var RED = require(process.env.NODE_RED_HOME+"/red/red"); module.exports = function(RED) {
var settings = RED.settings; "use strict";
var util = require("util"); var settings = RED.settings;
var fs = require('fs'); var util = require("util");
var plat = require('os').platform(); var fs = require('fs');
var pre = "\\\\.\\"; var plat = require('os').platform();
var pre = "\\\\.\\";
if (!plat.match(/^win/)) { if (!plat.match(/^win/)) {
util.log("[26-rawserial.js] Info : only really needed for Windows boxes without serialport npm module installed."); util.log("[26-rawserial.js] Info : only really needed for Windows boxes without serialport npm module installed.");
pre = ""; pre = "";
} }
function RawSerialInNode(n) { function RawSerialInNode(n) {
RED.nodes.createNode(this,n); RED.nodes.createNode(this,n);
this.port = n.port; this.port = n.port;
this.splitc = n.splitc||null; this.splitc = n.splitc||null;
this.out = n.out||"char"; this.out = n.out||"char";
this.bin = n.bin||false; this.bin = n.bin||false;
if (this.splitc == '\\n') this.splitc = "\n"; if (this.splitc == '\\n') { this.splitc = "\n"; }
if (this.splitc == '\\r') this.splitc = "\r"; if (this.splitc == '\\r') { this.splitc = "\r"; }
if (!isNaN(parseInt(this.splitc))) { this.splitc = parseInt(this.splitc); } if (!isNaN(parseInt(this.splitc))) { this.splitc = parseInt(this.splitc); }
var node = this; var node = this;
var setupSerial = function() { var setupSerial = function() {
node.inp = fs.createReadStream(pre+node.port); node.inp = fs.createReadStream(pre+node.port);
node.log("open "+pre+node.port); node.log("open "+pre+node.port);
node.tout = null; node.tout = null;
var line = ""; var line = "";
var buf = new Buffer(32768); var buf = new Buffer(32768);
var i = 0; var i = 0;
node.inp.on('data', function (data) { node.inp.on('data', function (data) {
for (var z = 0; z < data.length; z++) { for (var z = 0; z < data.length; z++) {
if ((node.out === "time") && (node.splitc != 0)) { if ((node.out === "time") && (node.splitc !== 0)) {
if (node.tout) { if (node.tout) {
i += 1; i += 1;
buf[i] = data[z];
}
else {
node.tout = setTimeout(function () {
node.tout = null;
var m = new Buffer(i+1);
buf.copy(m,0,0,i+1);
if (node.bin !== "true") { m = m.toString(); }
node.send({"payload": m});
m = null;
}, node.splitc);
i = 0;
buf[0] = data[z];
}
}
else if ((node.out == "char") && (node.splitc != null)) {
buf[i] = data[z]; buf[i] = data[z];
i += 1;
if ((data[z] === node.splitc.charCodeAt(0)) || (i === 32768)) {
var m = new Buffer(i);
buf.copy(m,0,0,i);
if (node.bin !== "true") { m = m.toString(); }
node.send({"payload":m});
m = null;
i = 0;
}
} }
else { else {
node.tout = setTimeout(function () { if (node.bin !== "true") { node.send({"payload": String.fromCharCode(data[z])}); }
node.tout = null; else { node.send({"payload": new Buffer([data[z]])});}
var m = new Buffer(i+1);
buf.copy(m,0,0,i+1);
if (node.bin !== "true") { m = m.toString(); }
node.send({"payload": m});
}, node.splitc);
i = 0;
buf[0] = data[z];
} }
} }
else if ((node.out == "char") && (node.splitc != null)) { });
buf[i] = data[z]; //node.inp.on('end', function (error) {console.log("End", error);});
i += 1; node.inp.on('close', function (error) {
if ((data[z] === node.splitc.charCodeAt(0)) || (i === 32768)) { node.log(node.port+" closed");
var m = new Buffer(i); node.tout = setTimeout(function() {
buf.copy(m,0,0,i); setupSerial();
if (node.bin !== "true") { m = m.toString(); } },settings.serialReconnectTime);
node.send({"payload":m}); });
i = 0; node.inp.on('error', function(error) {
} if (error.code == "ENOENT") { node.log(node.port+" not found"); }
} else { node.log(node.port+" error "+error); }
else { node.tout = setTimeout(function() {
if (node.bin !== "true") { node.send({"payload": String.fromCharCode(data[z])}); } setupSerial();
else { node.send({"payload": new Buffer([data[z]])});} },settings.serialReconnectTime);
} });
} }
setupSerial();
node.on('close', function() {
if (node.tout) { clearTimeout(node.tout); }
if (node.inp) { node.inp.pause(); }
}); });
//node.inp.on('end', function (error) {console.log("End", error);});
node.inp.on('close', function (error) { }
node.log(node.port+" closed"); RED.nodes.registerType("rawserial in",RawSerialInNode);
node.tout = setTimeout(function() {
setupSerial();
},settings.serialReconnectTime); function RawSerialOutNode(n) {
}); RED.nodes.createNode(this,n);
node.inp.on('error', function(error) { this.port = n.port;
if (error.code == "ENOENT") { node.log(node.port+" not found"); } var node = this;
else { node.log(node.port+" error "+error); }
node.tout = setTimeout(function() { var setupSerial = function() {
setupSerial(); node.oup = fs.createWriteStream(pre+node.port,{ flags:'w', encoding:'utf8', mode:'0666' });
},settings.serialReconnectTime); node.on("input", function(msg) {
if (msg.payload != null) {
node.oup.write(msg.payload);
}
});
node.oup.on('open', function (error) { node.log("opened "+node.port); });
node.oup.on('end', function (error) { node.log("end :"+error); });
node.oup.on('close', function (error) {
node.log(node.port+" closed");
node.tout = setTimeout(function() {
setupSerial();
},settings.serialReconnectTime);
});
node.oup.on('error', function(error) {
if (error.code == "EACCES") { node.log("can't access port "+node.port); }
else if (error.code == "EIO") { node.log("can't write to port "+node.port); }
else { node.log(node.port+" error "+error); }
node.tout = setTimeout(function() {
setupSerial();
},settings.serialReconnectTime);
});
}
setupSerial();
node.on('close', function() {
if (node.tout) { clearTimeout(node.tout); }
}); });
} }
setupSerial(); RED.nodes.registerType("rawserial out",RawSerialOutNode);
node.on('close', function() {
if (node.tout) { clearTimeout(node.tout); }
if (node.inp) { node.inp.pause(); }
});
} }
RED.nodes.registerType("rawserial in",RawSerialInNode);
function RawSerialOutNode(n) {
RED.nodes.createNode(this,n);
this.port = n.port;
var node = this;
var setupSerial = function() {
node.oup = fs.createWriteStream(pre+node.port,{ flags:'w', encoding:'utf8', mode:0666 });
node.on("input", function(msg) {
if (msg.payload != null) {
node.oup.write(msg.payload);
}
});
node.oup.on('open', function (error) { node.log("opened "+node.port); });
node.oup.on('end', function (error) { node.log("end :"+error); });
node.oup.on('close', function (error) {
node.log(node.port+" closed");
node.tout = setTimeout(function() {
setupSerial();
},settings.serialReconnectTime);
});
node.oup.on('error', function(error) {
if (error.code == "EACCES") { node.log("can't access port "+node.port); }
else if (error.code == "EIO") { node.log("can't write to port "+node.port); }
else { node.log(node.port+" error "+error); }
node.tout = setTimeout(function() {
setupSerial();
},settings.serialReconnectTime);
});
}
setupSerial();
node.on('close', function() {
if (node.tout) { clearTimeout(node.tout); }
});
}
RED.nodes.registerType("rawserial out",RawSerialOutNode);

View File

@ -35,7 +35,7 @@ module.exports = function(RED) {
RED.httpAdmin.get('/stomp-server/:id',function(req,res) { RED.httpAdmin.get('/stomp-server/:id',function(req,res) {
var credentials = RED.nodes.getCredentials(req.params.id); var credentials = RED.nodes.getCredentials(req.params.id);
if (credentials) { if (credentials) {
res.send(JSON.stringify({user:credentials.user,hasPassword:(credentials.password&&credentials.password!="")})); res.send(JSON.stringify({user:credentials.user,hasPassword:(credentials.password&&credentials.password!=="")}));
} else { } else {
res.send(JSON.stringify({})); res.send(JSON.stringify({}));
} }
@ -54,12 +54,12 @@ module.exports = function(RED) {
req.on('end', function(){ req.on('end', function(){
var newCreds = querystring.parse(body); var newCreds = querystring.parse(body);
var credentials = RED.nodes.getCredentials(req.params.id)||{}; var credentials = RED.nodes.getCredentials(req.params.id)||{};
if (newCreds.user == null || newCreds.user == "") { if (newCreds.user == null || newCreds.user === "") {
delete credentials.user; delete credentials.user;
} else { } else {
credentials.user = newCreds.user; credentials.user = newCreds.user;
} }
if (newCreds.password == "") { if (newCreds.password === "") {
delete credentials.password; delete credentials.password;
} else { } else {
credentials.password = newCreds.password||credentials.password; credentials.password = newCreds.password||credentials.password;

View File

@ -14,28 +14,30 @@
* limitations under the License. * limitations under the License.
**/ **/
var RED = require(process.env.NODE_RED_HOME+"/red/red"); module.exports = function(RED) {
var wol = require('wake_on_lan'); "use strict";
var chk = /^([0-9A-F]{2}[:-]){5}([0-9A-F]{2})$/; var wol = require('wake_on_lan');
var chk = /^([0-9A-F]{2}[:-]){5}([0-9A-F]{2})$/;
function WOLnode(n) { function WOLnode(n) {
RED.nodes.createNode(this,n); RED.nodes.createNode(this,n);
this.mac = n.mac.trim(); this.mac = n.mac.trim();
var node = this; var node = this;
this.on("input", function(msg) { this.on("input", function(msg) {
if (msg != null) { if (msg != null) {
var mac = this.mac || msg.mac || null; var mac = this.mac || msg.mac || null;
if (mac != null) { if (mac != null) {
if (chk.test(mac)) { if (chk.test(mac)) {
wol.wake(mac, function(error) { wol.wake(mac, function(error) {
if (error) { node.warn(error); } if (error) { node.warn(error); }
}); });
}
else { node.warn('WOL: bad mac address "'+mac+'"'); }
} }
else { node.warn('WOL: bad mac address "'+mac+'"'); } else { node.warn("WOL: no mac address specified"); }
} }
else { node.warn("WOL: no mac address specified"); } });
} }
}); RED.nodes.registerType("wake on lan",WOLnode);
} }
RED.nodes.registerType("wake on lan",WOLnode);