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
// 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
function SampleNode(n) {
// Create a RED node
RED.nodes.createNode(this,n);
// The main node definition - most things happen in here
function SampleNode(n) {
// Create a RED node
RED.nodes.createNode(this,n);
// Store local copies of the node configuration (as defined in the .html)
this.topic = n.topic;
// Store local copies of the node configuration (as defined in the .html)
this.topic = n.topic;
// Do whatever you need to do in here - declare callbacks etc
// Note: this sample doesn't do anything much - it will only send
// this message once at startup...
// Look at other real nodes for some better ideas of what to do....
var msg = {};
msg.topic = this.topic;
msg.payload = "Hello world !"
// Do whatever you need to do in here - declare callbacks etc
// Note: this sample doesn't do anything much - it will only send
// this message once at startup...
// Look at other real nodes for some better ideas of what to do....
var msg = {};
msg.topic = this.topic;
msg.payload = "Hello world !"
// send out the message to the rest of the workspace.
this.send(msg);
// send out the message to the rest of the workspace.
this.send(msg);
this.on("close", function() {
// Called when the node is shutdown - eg on redeploy.
// Allows ports to be closed, connections dropped etc.
// eg: this.client.disconnect();
});
this.on("close", function() {
// Called when the node is shutdown - eg on redeploy.
// Allows ports to be closed, connections dropped etc.
// 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.
**/
var RED = require(process.env.NODE_RED_HOME+"/red/red");
var badwords = require('badwords');
if (badwords.length == 0 ) { return; }
var badwordsRegExp = require('badwords/regexp');
module.exports = function(RED) {
"use strict";
var badwords = require('badwords');
if (badwords.length === 0 ) { return; }
var badwordsRegExp = require('badwords/regexp');
function BadwordsNode(n) {
RED.nodes.createNode(this,n);
var node = this;
this.on("input", function(msg) {
if (typeof msg.payload === "string") {
if ( !badwordsRegExp.test(msg.payload) ) { node.send(msg); }
}
});
function BadwordsNode(n) {
RED.nodes.createNode(this,n);
var node = this;
this.on("input", function(msg) {
if (typeof msg.payload === "string") {
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.
**/
var RED = require(process.env.NODE_RED_HOME+"/red/red");
var WordPos = require('wordpos');
var wordpos = new WordPos();
module.exports = function(RED) {
"use strict";
var WordPos = require('wordpos');
var wordpos = new WordPos();
function WordPOSNode(n) {
RED.nodes.createNode(this,n);
this.on("input", function(msg) {
var node = this;
wordpos.getPOS(msg.payload, function (result) {
msg.pos = result;
node.send(msg);
function WordPOSNode(n) {
RED.nodes.createNode(this,n);
this.on("input", function(msg) {
var node = this;
wordpos.getPOS(msg.payload, function (result) {
msg.pos = result;
node.send(msg);
});
});
});
}
RED.nodes.registerType("wordpos",WordPOSNode);
}
RED.nodes.registerType("wordpos",WordPOSNode);

View File

@ -14,81 +14,83 @@
* limitations under the License.
**/
var RED = require(process.env.NODE_RED_HOME+"/red/red");
var lvldb = require('level');
module.exports = function(RED) {
"use strict";
var lvldb = require('level');
function LevelNode(n) {
RED.nodes.createNode(this,n);
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) {
function LevelNode(n) {
RED.nodes.createNode(this,n);
this.dbname = n.db;
var node = this;
node.on("input", function(msg) {
if (typeof msg.topic === 'string') {
node.levelConfig.db.get(msg.topic, function(err, value) {
if (err) {
//node.warn(err);
// for some reason they treat nothing found as an error...
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");
}
lvldb(this.dbname, function(err, db) {
if (err) { node.error(err); }
node.db = db;
});
this.on('close', function() {
if (node.db) { node.db.close(); }
});
}
else {
this.error("LevelDB database name not configured");
}
}
RED.nodes.registerType("leveldb in",LevelDBNodeIn);
RED.nodes.registerType("leveldbase",LevelNode);
function LevelDBNodeOut(n) {
RED.nodes.createNode(this,n);
this.level = n.level;
this.operation = n.operation;
this.levelConfig = RED.nodes.getNode(this.level);
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;
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);
if (this.levelConfig) {
var node = this;
node.on("input", function(msg) {
if (typeof msg.topic === 'string') {
node.levelConfig.db.get(msg.topic, function(err, value) {
if (err) {
//node.warn(err);
// for some reason they treat nothing found as an error...
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 {
if (typeof msg.topic !== 'string') { node.error("msg.topic (the key is not defined"); }
}
});
}
else {
this.error("LevelDB database name not configured");
}
}
else {
this.error("LevelDB database name not configured");
RED.nodes.registerType("leveldb in",LevelDBNodeIn);
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.
**/
var RED = require(process.env.NODE_RED_HOME+"/red/red");
var reconnect = RED.settings.mysqlReconnectTime || 30000;
var mysqldb = require('mysql');
var querystring = require('querystring');
module.exports = function(RED) {
"use strict";
var reconnect = RED.settings.mysqlReconnectTime || 30000;
var mysqldb = require('mysql');
var querystring = require('querystring');
RED.httpAdmin.get('/MySQLdatabase/:id',function(req,res) {
var credentials = RED.nodes.getCredentials(req.params.id);
if (credentials) {
res.send(JSON.stringify({user:credentials.user,hasPassword:(credentials.password&&credentials.password!="")}));
} else {
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;
RED.httpAdmin.get('/MySQLdatabase/:id',function(req,res) {
var credentials = RED.nodes.getCredentials(req.params.id);
if (credentials) {
res.send(JSON.stringify({user:credentials.user,hasPassword:(credentials.password&&credentials.password!=="")}));
} else {
res.send(JSON.stringify({}));
}
});
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);
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);
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) {
RED.nodes.createNode(this,n);
this.host = n.host;
this.port = n.port;
function MySQLNode(n) {
RED.nodes.createNode(this,n);
this.host = n.host;
this.port = n.port;
this.connected = false;
this.connecting = false;
this.connected = false;
this.connecting = false;
if (n.user) {
var credentials = {};
credentials.user = n.user;
credentials.password = n.pass;
RED.nodes.addCredentials(n.id,credentials);
this.user = n.user;
this.password = n.pass;
} else {
var credentials = RED.nodes.getCredentials(n.id);
if (credentials) {
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;
if (n.user) {
credentials.user = n.user;
credentials.password = n.pass;
RED.nodes.addCredentials(n.id,credentials);
this.user = n.user;
this.password = n.pass;
} else {
credentials = RED.nodes.getCredentials(n.id);
if (credentials) {
this.user = credentials.user;
this.password = credentials.password;
}
});
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 () {
if (this.tick) { clearTimeout(this.tick); }
if (this.connection) {
node.connection.end(function(err) {
if (err) node.error(err);
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();
}
});
}
});
}
RED.nodes.registerType("MySQLdatabase",MySQLNode);
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);
}
});
this.connect = function() {
if (!this.connected && !this.connecting) {
doConnect();
}
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 {
this.error("MySQL database not configured");
RED.nodes.registerType("MySQLdatabase",MySQLNode);
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) {
//RED.nodes.createNode(this,n);
//this.level = n.level;
//this.operation = n.operation;
//this.levelConfig = RED.nodes.getNode(this.level);
//function MysqlDBNodeOut(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);
//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 {
//node.levelConfig.db.put(msg.topic, msg.payload, function(err) {
//if (err) node.error(err);
//});
//if (typeof msg.topic !== 'string') node.error("msg.topic : the key is not defined");
//}
//}
//else {
//if (typeof msg.topic !== 'string') node.error("msg.topic : the key is not defined");
//}
//});
//});
//}
//else {
//this.error("MySQL database not configured");
//}
//}
//else {
//this.error("MySQL database not configured");
//}
//}
//RED.nodes.registerType("mysql out",MysqlDBNodeOut);
//RED.nodes.registerType("mysql out",MysqlDBNodeOut);
}

View File

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