Merge pull request #392 from hindessm/remove-redundant-null-checks

Remove redundant msg != null checks.
This commit is contained in:
Nick O'Leary 2014-09-08 21:31:55 +01:00
commit 94b196bfcf
7 changed files with 163 additions and 182 deletions

View File

@ -27,60 +27,56 @@ module.exports = function(RED) {
var node = this; var node = this;
this.on("input", function(msg) { this.on("input", function(msg) {
if (msg != null) { node.status({fill:"blue",shape:"dot"});
node.status({fill:"blue",shape:"dot"}); if (this.useSpawn === true) {
if (this.useSpawn === true) { // make the extra args into an array
// make the extra args into an array // then prepend with the msg.payload
// then prepend with the msg.payload if (typeof(msg.payload !== "string")) { msg.payload = msg.payload.toString(); }
if (typeof(msg.payload !== "string")) { msg.payload = msg.payload.toString(); } var arg = [];
var arg = []; if (node.append.length > 0) { arg = node.append.split(","); }
if (node.append.length > 0) { arg = node.append.split(","); } if (msg.payload.trim() !== "") { arg.unshift(msg.payload); }
if (msg.payload.trim() !== "") { arg.unshift(msg.payload); } node.log(node.cmd+" ["+arg+"]");
node.log(node.cmd+" ["+arg+"]"); if (node.cmd.indexOf(" ") == -1) {
if (node.cmd.indexOf(" ") == -1) { var ex = spawn(node.cmd,arg);
var ex = spawn(node.cmd,arg); ex.stdout.on('data', function (data) {
ex.stdout.on('data', function (data) { //console.log('[exec] stdout: ' + data);
//console.log('[exec] stdout: ' + data); msg.payload = data.toString();
msg.payload = data.toString(); node.send([msg,null,null]);
node.send([msg,null,null]); });
}); ex.stderr.on('data', function (data) {
ex.stderr.on('data', function (data) { //console.log('[exec] stderr: ' + data);
//console.log('[exec] stderr: ' + data); msg.payload = data.toString();
msg.payload = data.toString(); node.send([null,msg,null]);
node.send([null,msg,null]); });
}); ex.on('close', function (code) {
ex.on('close', function (code) { //console.log('[exec] result: ' + code);
//console.log('[exec] result: ' + code); msg.payload = code;
msg.payload = code;
node.status({});
node.send([null,null,msg]);
});
ex.on('error', function (code) {
node.warn(code);
});
}
else { node.error("Spawn command must be just the command - no spaces or extra parameters"); }
}
else {
var cl = node.cmd+" "+msg.payload+" "+node.append;
node.log(cl);
var child = exec(cl, function (error, stdout, stderr) {
msg.payload = stdout;
var msg2 = {payload:stderr};
var msg3 = null;
//console.log('[exec] stdout: ' + stdout);
//console.log('[exec] stderr: ' + stderr);
if (error !== null) {
msg3 = {payload:error};
//console.log('[exec] error: ' + error);
}
node.status({}); node.status({});
node.send([msg,msg2,msg3]); node.send([null,null,msg]);
});
ex.on('error', function (code) {
node.warn(code);
}); });
} }
else { node.error("Spawn command must be just the command - no spaces or extra parameters"); }
}
else {
var cl = node.cmd+" "+msg.payload+" "+node.append;
node.log(cl);
var child = exec(cl, function (error, stdout, stderr) {
msg.payload = stdout;
var msg2 = {payload:stderr};
var msg3 = null;
//console.log('[exec] stdout: ' + stdout);
//console.log('[exec] stderr: ' + stderr);
if (error !== null) {
msg3 = {payload:error};
//console.log('[exec] error: ' + error);
}
node.status({});
node.send([msg,msg2,msg3]);
});
} }
}); });
} }

View File

@ -39,38 +39,36 @@ module.exports = function(RED) {
try { try {
this.script = vm.createScript(functionText); this.script = vm.createScript(functionText);
this.on("input", function(msg) { this.on("input", function(msg) {
if (msg != null) { try {
try { var start = process.hrtime();
var start = process.hrtime(); context.msg = msg;
context.msg = msg; this.script.runInContext(context);
this.script.runInContext(context); var results = context.results;
var results = context.results; if (results == null) {
if (results == null) { results = [];
results = []; } else if (results.length == null) {
} else if (results.length == null) { results = [results];
results = [results]; }
} if (msg._topic) {
if (msg._topic) { for (var m in results) {
for (var m in results) { if (results[m]) {
if (results[m]) { if (util.isArray(results[m])) {
if (util.isArray(results[m])) { for (var n=0; n < results[m].length; n++) {
for (var n=0; n < results[m].length; n++) { results[m][n]._topic = msg._topic;
results[m][n]._topic = msg._topic;
}
} else {
results[m]._topic = msg._topic;
} }
} else {
results[m]._topic = msg._topic;
} }
} }
} }
this.send(results);
var duration = process.hrtime(start);
if (process.env.NODE_RED_FUNCTION_TIME) {
this.status({fill:"yellow",shape:"dot",text:""+Math.floor((duration[0]* 1e9 + duration[1])/10000)/100});
}
} catch(err) {
this.error(err.toString());
} }
this.send(results);
var duration = process.hrtime(start);
if (process.env.NODE_RED_FUNCTION_TIME) {
this.status({fill:"yellow",shape:"dot",text:""+Math.floor((duration[0]* 1e9 + duration[1])/10000)/100});
}
} catch(err) {
this.error(err.toString());
} }
}); });
} catch(err) { } catch(err) {

View File

@ -47,14 +47,12 @@ module.exports = function(RED) {
} }
node.on("input", function(msg) { node.on("input", function(msg) {
if (msg != null) { try {
try { m = msg;
m = msg; i = 0;
i = 0; rec(msg);
rec(msg); } catch(err) {
} catch(err) { node.error(err.message);
node.error(err.message);
}
} }
}); });
} }

View File

@ -82,24 +82,22 @@ module.exports = function(RED) {
this.client = connectionPool.get(this.brokerConfig.broker,this.brokerConfig.port,this.brokerConfig.clientid,this.brokerConfig.username,this.brokerConfig.password); this.client = connectionPool.get(this.brokerConfig.broker,this.brokerConfig.port,this.brokerConfig.clientid,this.brokerConfig.username,this.brokerConfig.password);
var node = this; var node = this;
this.on("input",function(msg) { this.on("input",function(msg) {
if (msg != null) { if (msg.qos) {
if (msg.qos) { msg.qos = parseInt(msg.qos);
msg.qos = parseInt(msg.qos); if ((msg.qos !== 0) && (msg.qos !== 1) && (msg.qos !== 2)) {
if ((msg.qos !== 0) && (msg.qos !== 1) && (msg.qos !== 2)) { msg.qos = null;
msg.qos = null;
}
} }
msg.qos = Number(node.qos || msg.qos || 0);
msg.retain = node.retain || msg.retain || false;
msg.retain = ((msg.retain === true) || (msg.retain === "true")) || false;
if (node.topic) {
msg.topic = node.topic;
}
if ((msg.hasOwnProperty("topic")) && (typeof msg.topic === "string") && (msg.topic !== "")) { // topic must exist
this.client.publish(msg); // send the message
}
else { node.warn("Invalid topic specified"); }
} }
msg.qos = Number(node.qos || msg.qos || 0);
msg.retain = node.retain || msg.retain || false;
msg.retain = ((msg.retain === true) || (msg.retain === "true")) || false;
if (node.topic) {
msg.topic = node.topic;
}
if ((msg.hasOwnProperty("topic")) && (typeof msg.topic === "string") && (msg.topic !== "")) { // topic must exist
this.client.publish(msg); // send the message
}
else { node.warn("Invalid topic specified"); }
}); });
this.client.on("connectionlost",function() { this.client.on("connectionlost",function() {
node.status({fill:"red",shape:"ring",text:"disconnected"}); node.status({fill:"red",shape:"ring",text:"disconnected"});

View File

@ -244,52 +244,47 @@ module.exports = function(RED) {
access_token_secret: credentials.access_token_secret access_token_secret: credentials.access_token_secret
}); });
node.on("input", function(msg) { node.on("input", function(msg) {
if (msg != null) { node.status({fill:"blue",shape:"dot",text:"tweeting"});
node.status({fill:"blue",shape:"dot",text:"tweeting"}); if (msg.payload.length > 140) {
msg.payload = msg.payload.slice(0,139);
if (msg.payload.length > 140) { node.warn("Tweet greater than 140 : truncated");
msg.payload = msg.payload.slice(0,139); }
node.warn("Tweet greater than 140 : truncated");
} if (msg.media && Buffer.isBuffer(msg.media)) {
var apiUrl = "https://api.twitter.com/1.1/statuses/update_with_media.json";
if (msg.media && Buffer.isBuffer(msg.media)) { var signedUrl = oa.signUrl(apiUrl,
var apiUrl = "https://api.twitter.com/1.1/statuses/update_with_media.json"; credentials.access_token,
var signedUrl = oa.signUrl(apiUrl, credentials.access_token_secret,
credentials.access_token, "POST");
credentials.access_token_secret,
"POST"); var r = request.post(signedUrl,function(err,httpResponse,body) {
if (err) {
var r = request.post(signedUrl,function(err,httpResponse,body) { node.error(err.toString());
if (err) { node.status({fill:"red",shape:"ring",text:"failed"});
node.error(err.toString()); } else {
var response = JSON.parse(body);
if (body.errors) {
var errorList = body.errors.map(function(er) { return er.code+": "+er.message }).join(", ");
node.error("tweet failed: "+errorList);
node.status({fill:"red",shape:"ring",text:"failed"}); node.status({fill:"red",shape:"ring",text:"failed"});
} else { } else {
var response = JSON.parse(body); node.status({});
if (body.errors) {
var errorList = body.errors.map(function(er) { return er.code+": "+er.message }).join(", ");
node.error("tweet failed: "+errorList);
node.status({fill:"red",shape:"ring",text:"failed"});
} else {
node.status({});
}
} }
}); }
var form = r.form(); });
form.append("status",msg.payload); var form = r.form();
form.append("media[]",msg.media,{filename:"image"}); form.append("status",msg.payload);
form.append("media[]",msg.media,{filename:"image"});
} else {
} else { twit.updateStatus(msg.payload, function (err, data) {
twit.updateStatus(msg.payload, function (err, data) { if (err) {
if (err) { node.status({fill:"red",shape:"ring",text:"failed"});
node.status({fill:"red",shape:"ring",text:"failed"}); node.error(err);
node.error(err); }
} node.status({});
node.status({}); });
});
}
} }
}); });
} }

View File

@ -69,27 +69,25 @@ module.exports = function(RED) {
}); });
this.on("input", function(msg) { this.on("input", function(msg) {
if (msg != null) { if (smtpTransport) {
if (smtpTransport) { node.status({fill:"blue",shape:"dot",text:"sending"});
node.status({fill:"blue",shape:"dot",text:"sending"}); var payload = RED.util.ensureString(msg.payload);
var payload = RED.util.ensureString(msg.payload); smtpTransport.sendMail({
smtpTransport.sendMail({ from: node.userid, // sender address
from: node.userid, // sender address to: msg.to || node.name, // comma separated list of addressees
to: msg.to || node.name, // comma separated list of addressees subject: msg.topic, // subject line
subject: msg.topic, // subject line text: payload // plaintext body
text: payload // plaintext body }, function(error, info) {
}, function(error, info) { if (error) {
if (error) { node.error(error);
node.error(error); node.status({fill:"red",shape:"ring",text:"send failed"});
node.status({fill:"red",shape:"ring",text:"send failed"}); } else {
} else { node.log("Message sent: " + info.response);
node.log("Message sent: " + info.response); node.status({});
node.status({}); }
} });
});
}
else { node.warn("No Email credentials found. See info panel."); }
} }
else { node.warn("No Email credentials found. See info panel."); }
}); });
} }
RED.nodes.registerType("e-mail",EmailNode,{ RED.nodes.registerType("e-mail",EmailNode,{

View File

@ -79,27 +79,25 @@ module.exports = function(RED) {
}); });
this.on("input", function(msg) { this.on("input", function(msg) {
if (msg != null) { var k = this.key || msg.topic;
var k = this.key || msg.topic; if (k) {
if (k) { if (this.structtype == "string") {
if (this.structtype == "string") { this.client.set(k,RED.util.ensureString(msg.payload));
this.client.set(k,RED.util.ensureString(msg.payload)); } else if (this.structtype == "hash") {
} else if (this.structtype == "hash") { var r = hashFieldRE.exec(msg.payload);
var r = hashFieldRE.exec(msg.payload); if (r) {
if (r) { this.client.hset(k,r[1],r[2]);
this.client.hset(k,r[1],r[2]);
} else {
this.warn("Invalid payload for redis hash");
}
} else if (this.structtype == "set") {
this.client.sadd(k,msg.payload);
} else if (this.structtype == "list") {
this.client.rpush(k,msg.payload);
}
} else { } else {
this.warn("No key or topic set"); this.warn("Invalid payload for redis hash");
} }
} else if (this.structtype == "set") {
this.client.sadd(k,msg.payload);
} else if (this.structtype == "list") {
this.client.rpush(k,msg.payload);
} }
} else {
this.warn("No key or topic set");
}
}); });
this.on("close", function() { this.on("close", function() {
redisConnectionPool.close(node.client); redisConnectionPool.close(node.client);