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 sample, analysis, storage and time nodes to use strict and pass jshint scan

This commit is contained in:
Dave C-J 2014-06-28 23:37:19 +01:00
parent 43dfded402
commit ebf950d818
6 changed files with 279 additions and 268 deletions

View File

@ -19,34 +19,36 @@
// Sample Node-RED node file // Sample Node-RED node file
// Require main module // Require main module
var RED = require(process.env.NODE_RED_HOME+"/red/red"); module.exports = function(RED) {
"use strict";
// The main node definition - most things happen in here // The main node definition - most things happen in here
function SampleNode(n) { function SampleNode(n) {
// Create a RED node // Create a RED node
RED.nodes.createNode(this,n); RED.nodes.createNode(this,n);
// Store local copies of the node configuration (as defined in the .html) // Store local copies of the node configuration (as defined in the .html)
this.topic = n.topic; this.topic = n.topic;
// Do whatever you need to do in here - declare callbacks etc // Do whatever you need to do in here - declare callbacks etc
// Note: this sample doesn't do anything much - it will only send // Note: this sample doesn't do anything much - it will only send
// this message once at startup... // this message once at startup...
// Look at other real nodes for some better ideas of what to do.... // Look at other real nodes for some better ideas of what to do....
var msg = {}; var msg = {};
msg.topic = this.topic; msg.topic = this.topic;
msg.payload = "Hello world !" msg.payload = "Hello world !"
// send out the message to the rest of the workspace. // send out the message to the rest of the workspace.
this.send(msg); this.send(msg);
this.on("close", function() { this.on("close", function() {
// Called when the node is shutdown - eg on redeploy. // Called when the node is shutdown - eg on redeploy.
// Allows ports to be closed, connections dropped etc. // Allows ports to be closed, connections dropped etc.
// eg: this.client.disconnect(); // eg: this.client.disconnect();
}); });
}
// Register the node by name. This must be called before overriding any of the
// Node functions.
RED.nodes.registerType("sample",SampleNode);
} }
// Register the node by name. This must be called before overriding any of the
// Node functions.
RED.nodes.registerType("sample",SampleNode);

View File

@ -14,18 +14,20 @@
* limitations under the License. * limitations under the License.
**/ **/
var RED = require(process.env.NODE_RED_HOME+"/red/red"); module.exports = function(RED) {
var badwords = require('badwords'); "use strict";
if (badwords.length == 0 ) { return; } var badwords = require('badwords');
var badwordsRegExp = require('badwords/regexp'); if (badwords.length === 0 ) { return; }
var badwordsRegExp = require('badwords/regexp');
function BadwordsNode(n) { function BadwordsNode(n) {
RED.nodes.createNode(this,n); RED.nodes.createNode(this,n);
var node = this; var node = this;
this.on("input", function(msg) { this.on("input", function(msg) {
if (typeof msg.payload === "string") { if (typeof msg.payload === "string") {
if ( !badwordsRegExp.test(msg.payload) ) { node.send(msg); } if ( !badwordsRegExp.test(msg.payload) ) { node.send(msg); }
} }
}); });
}
RED.nodes.registerType("badwords",BadwordsNode);
} }
RED.nodes.registerType("badwords",BadwordsNode);

View File

@ -14,18 +14,20 @@
* limitations under the License. * limitations under the License.
**/ **/
var RED = require(process.env.NODE_RED_HOME+"/red/red"); module.exports = function(RED) {
var WordPos = require('wordpos'); "use strict";
var wordpos = new WordPos(); var WordPos = require('wordpos');
var wordpos = new WordPos();
function WordPOSNode(n) { function WordPOSNode(n) {
RED.nodes.createNode(this,n); RED.nodes.createNode(this,n);
this.on("input", function(msg) { this.on("input", function(msg) {
var node = this; var node = this;
wordpos.getPOS(msg.payload, function (result) { wordpos.getPOS(msg.payload, function (result) {
msg.pos = result; msg.pos = result;
node.send(msg); node.send(msg);
});
}); });
}); }
RED.nodes.registerType("wordpos",WordPOSNode);
} }
RED.nodes.registerType("wordpos",WordPOSNode);

View File

@ -14,81 +14,83 @@
* limitations under the License. * limitations under the License.
**/ **/
var RED = require(process.env.NODE_RED_HOME+"/red/red"); module.exports = function(RED) {
var lvldb = require('level'); "use strict";
var lvldb = require('level');
function LevelNode(n) { function LevelNode(n) {
RED.nodes.createNode(this,n); RED.nodes.createNode(this,n);
this.dbname = n.db; this.dbname = n.db;
var node = this;
lvldb(this.dbname, function(err, db) {
if (err) node.error(err);
node.db = db;
});
this.on('close', function() {
if (node.db) { node.db.close(); }
});
}
RED.nodes.registerType("leveldbase",LevelNode);
function LevelDBNodeIn(n) {
RED.nodes.createNode(this,n);
this.level = n.level;
this.levelConfig = RED.nodes.getNode(this.level);
if (this.levelConfig) {
var node = this; var node = this;
node.on("input", function(msg) { lvldb(this.dbname, function(err, db) {
if (typeof msg.topic === 'string') { if (err) { node.error(err); }
node.levelConfig.db.get(msg.topic, function(err, value) { node.db = db;
if (err) { });
//node.warn(err); this.on('close', function() {
// for some reason they treat nothing found as an error... if (node.db) { node.db.close(); }
msg.payload = null; // so we should return null
}
else { msg.payload = value; }
node.send(msg);
});
}
else {
if (typeof msg.topic !== 'string') node.error("msg.topic (the key is not defined");
}
}); });
} }
else { RED.nodes.registerType("leveldbase",LevelNode);
this.error("LevelDB database name not configured");
}
}
RED.nodes.registerType("leveldb in",LevelDBNodeIn);
function LevelDBNodeOut(n) { function LevelDBNodeIn(n) {
RED.nodes.createNode(this,n); RED.nodes.createNode(this,n);
this.level = n.level; this.level = n.level;
this.operation = n.operation; this.levelConfig = RED.nodes.getNode(this.level);
this.levelConfig = RED.nodes.getNode(this.level);
if (this.levelConfig) { if (this.levelConfig) {
var node = this; var node = this;
node.on("input", function(msg) { node.on("input", function(msg) {
if (typeof msg.topic === 'string') { if (typeof msg.topic === 'string') {
if (node.operation === "delete") { node.levelConfig.db.get(msg.topic, function(err, value) {
node.levelConfig.db.del(msg.topic); if (err) {
} //node.warn(err);
else { // for some reason they treat nothing found as an error...
node.levelConfig.db.put(msg.topic, msg.payload, function(err) { msg.payload = null; // so we should return null
if (err) node.error(err); }
else { msg.payload = value; }
node.send(msg);
}); });
} }
} else {
else { if (typeof msg.topic !== 'string') { node.error("msg.topic (the key is not defined"); }
if (typeof msg.topic !== 'string') node.error("msg.topic (the key is not defined"); }
} });
}); }
else {
this.error("LevelDB database name not configured");
}
} }
else { RED.nodes.registerType("leveldb in",LevelDBNodeIn);
this.error("LevelDB database name not configured");
function LevelDBNodeOut(n) {
RED.nodes.createNode(this,n);
this.level = n.level;
this.operation = n.operation;
this.levelConfig = RED.nodes.getNode(this.level);
if (this.levelConfig) {
var node = this;
node.on("input", function(msg) {
if (typeof msg.topic === 'string') {
if (node.operation === "delete") {
node.levelConfig.db.del(msg.topic);
}
else {
node.levelConfig.db.put(msg.topic, msg.payload, function(err) {
if (err) { node.error(err); }
});
}
}
else {
if (typeof msg.topic !== 'string') { node.error("msg.topic (the key is not defined"); }
}
});
}
else {
this.error("LevelDB database name not configured");
}
} }
RED.nodes.registerType("leveldb out",LevelDBNodeOut);
} }
RED.nodes.registerType("leveldb out",LevelDBNodeOut);

View File

@ -14,182 +14,184 @@
* limitations under the License. * limitations under the License.
**/ **/
var RED = require(process.env.NODE_RED_HOME+"/red/red"); module.exports = function(RED) {
var reconnect = RED.settings.mysqlReconnectTime || 30000; "use strict";
var mysqldb = require('mysql'); var reconnect = RED.settings.mysqlReconnectTime || 30000;
var querystring = require('querystring'); var mysqldb = require('mysql');
var querystring = require('querystring');
RED.httpAdmin.get('/MySQLdatabase/:id',function(req,res) { RED.httpAdmin.get('/MySQLdatabase/: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({}));
} }
});
RED.httpAdmin.delete('/MySQLdatabase/:id',function(req,res) {
RED.nodes.deleteCredentials(req.params.id);
res.send(200);
});
RED.httpAdmin.post('/MySQLdatabase/:id',function(req,res) {
var body = "";
req.on('data', function(chunk) {
body+=chunk;
}); });
req.on('end', function(){
var newCreds = querystring.parse(body); RED.httpAdmin.delete('/MySQLdatabase/:id',function(req,res) {
var credentials = RED.nodes.getCredentials(req.params.id)||{}; RED.nodes.deleteCredentials(req.params.id);
if (newCreds.user == null || newCreds.user == "") {
delete credentials.user;
} else {
credentials.user = newCreds.user;
}
if (newCreds.password == "") {
delete credentials.password;
} else {
credentials.password = newCreds.password||credentials.password;
}
RED.nodes.addCredentials(req.params.id,credentials);
res.send(200); res.send(200);
}); });
});
RED.httpAdmin.post('/MySQLdatabase/:id',function(req,res) {
var body = "";
req.on('data', function(chunk) {
body+=chunk;
});
req.on('end', function(){
var newCreds = querystring.parse(body);
var credentials = RED.nodes.getCredentials(req.params.id)||{};
if (newCreds.user == null || newCreds.user === "") {
delete credentials.user;
} else {
credentials.user = newCreds.user;
}
if (newCreds.password === "") {
delete credentials.password;
} else {
credentials.password = newCreds.password||credentials.password;
}
RED.nodes.addCredentials(req.params.id,credentials);
res.send(200);
});
});
function MySQLNode(n) { function MySQLNode(n) {
RED.nodes.createNode(this,n); RED.nodes.createNode(this,n);
this.host = n.host; this.host = n.host;
this.port = n.port; this.port = n.port;
this.connected = false; this.connected = false;
this.connecting = false; this.connecting = false;
if (n.user) {
var credentials = {}; var credentials = {};
credentials.user = n.user; if (n.user) {
credentials.password = n.pass; credentials.user = n.user;
RED.nodes.addCredentials(n.id,credentials); credentials.password = n.pass;
this.user = n.user; RED.nodes.addCredentials(n.id,credentials);
this.password = n.pass; this.user = n.user;
} else { this.password = n.pass;
var credentials = RED.nodes.getCredentials(n.id); } else {
if (credentials) { credentials = RED.nodes.getCredentials(n.id);
this.user = credentials.user; if (credentials) {
this.password = credentials.password; this.user = credentials.user;
} this.password = credentials.password;
}
this.dbname = n.db;
var node = this;
function doConnect() {
node.connecting = true;
node.connection = mysqldb.createConnection({
host : node.host,
port : node.port,
user : node.user,
password : node.password,
database : node.dbname,
insecureAuth: true
});
node.connection.connect(function(err) {
node.connecting = false;
if (err) {
node.warn(err);
node.tick = setTimeout(doConnect, reconnect);
} else {
node.connected = true;
} }
});
node.connection.on('error', function(err) {
node.connected = false;
if (err.code === 'PROTOCOL_CONNECTION_LOST') {
doConnect(); // silently reconnect...
} else {
node.error(err);
doConnect();
}
});
}
this.connect = function() {
if (!this.connected && !this.connecting) {
doConnect();
} }
}
this.on('close', function () { this.dbname = n.db;
if (this.tick) { clearTimeout(this.tick); } var node = this;
if (this.connection) {
node.connection.end(function(err) { function doConnect() {
if (err) node.error(err); node.connecting = true;
node.connection = mysqldb.createConnection({
host : node.host,
port : node.port,
user : node.user,
password : node.password,
database : node.dbname,
insecureAuth: true
});
node.connection.connect(function(err) {
node.connecting = false;
if (err) {
node.warn(err);
node.tick = setTimeout(doConnect, reconnect);
} else {
node.connected = true;
}
});
node.connection.on('error', function(err) {
node.connected = false;
if (err.code === 'PROTOCOL_CONNECTION_LOST') {
doConnect(); // silently reconnect...
} else {
node.error(err);
doConnect();
}
}); });
} }
});
}
RED.nodes.registerType("MySQLdatabase",MySQLNode);
this.connect = function() {
function MysqlDBNodeIn(n) { if (!this.connected && !this.connecting) {
RED.nodes.createNode(this,n); doConnect();
this.mydb = n.mydb;
this.mydbConfig = RED.nodes.getNode(this.mydb);
if (this.mydbConfig) {
this.mydbConfig.connect();
var node = this;
node.on("input", function(msg) {
if (typeof msg.topic === 'string') {
//console.log("query:",msg.topic);
node.mydbConfig.connection.query(msg.topic, function(err, rows) {
if (err) { node.warn(err); }
else {
msg.payload = rows;
node.send(msg);
}
});
} }
else { }
if (typeof msg.topic !== 'string') node.error("msg.topic : the query is not defined as a string");
this.on('close', function () {
if (this.tick) { clearTimeout(this.tick); }
if (this.connection) {
node.connection.end(function(err) {
if (err) { node.error(err); }
});
} }
}); });
} }
else { RED.nodes.registerType("MySQLdatabase",MySQLNode);
this.error("MySQL database not configured");
function MysqlDBNodeIn(n) {
RED.nodes.createNode(this,n);
this.mydb = n.mydb;
this.mydbConfig = RED.nodes.getNode(this.mydb);
if (this.mydbConfig) {
this.mydbConfig.connect();
var node = this;
node.on("input", function(msg) {
if (typeof msg.topic === 'string') {
//console.log("query:",msg.topic);
node.mydbConfig.connection.query(msg.topic, function(err, rows) {
if (err) { node.warn(err); }
else {
msg.payload = rows;
node.send(msg);
}
});
}
else {
if (typeof msg.topic !== 'string') { node.error("msg.topic : the query is not defined as a string"); }
}
});
}
else {
this.error("MySQL database not configured");
}
} }
} RED.nodes.registerType("mysql",MysqlDBNodeIn);
RED.nodes.registerType("mysql",MysqlDBNodeIn);
//function MysqlDBNodeOut(n) { //function MysqlDBNodeOut(n) {
//RED.nodes.createNode(this,n); //RED.nodes.createNode(this,n);
//this.level = n.level; //this.level = n.level;
//this.operation = n.operation; //this.operation = n.operation;
//this.levelConfig = RED.nodes.getNode(this.level); //this.levelConfig = RED.nodes.getNode(this.level);
//if (this.levelConfig) { //if (this.levelConfig) {
//var node = this; //var node = this;
//node.on("input", function(msg) { //node.on("input", function(msg) {
//if (typeof msg.topic === 'string') { //if (typeof msg.topic === 'string') {
//if (node.operation === "delete") { //if (node.operation === "delete") {
//node.levelConfig.db.del(msg.topic); //node.levelConfig.db.del(msg.topic);
//}
//else {
//node.levelConfig.db.put(msg.topic, msg.payload, function(err) {
//if (err) node.error(err);
//});
//}
//} //}
//else { //else {
//node.levelConfig.db.put(msg.topic, msg.payload, function(err) { //if (typeof msg.topic !== 'string') node.error("msg.topic : the key is not defined");
//if (err) node.error(err);
//});
//} //}
//} //});
//else { //}
//if (typeof msg.topic !== 'string') node.error("msg.topic : the key is not defined"); //else {
//} //this.error("MySQL database not configured");
//}); //}
//} //}
//else { //RED.nodes.registerType("mysql out",MysqlDBNodeOut);
//this.error("MySQL database not configured"); }
//}
//}
//RED.nodes.registerType("mysql out",MysqlDBNodeOut);

View File

@ -15,6 +15,7 @@
**/ **/
module.exports = function(RED) { module.exports = function(RED) {
"use strict";
var SunCalc = require('suncalc'); var SunCalc = require('suncalc');
function SunNode(n) { function SunNode(n) {