UNPKG

node-red-contrib-services-mentor

Version:

Mentor Services

153 lines (136 loc) 5.58 kB
module.exports = function(RED) { var log = RED.log; function UpdateLastReading(node, msg) { var sql = "UPDATE mqtt_snapshot SET content = '"+ msg.payload + "', time = '" + msg.time + "' WHERE device='"+msg.code + "'"; node.mydbConfig.connection.query(sql, [], function(err, rows) { //node.pool.query(sql, function (err, rows, fields) { if (err) { log.error(err);} else node.send({payload: {msg: rows.affectedRows + " record(s) updated", data:msg}}); }); } function getLastReading(node,msg, callback) { var sql = "SELECT time, device, content from mqtt_snapshot WHERE device='" + msg.code + "' LIMIT 1"; //node.pool.query(sql, function (err, rows, fields) { node.mydbConfig.connection.query(sql, [], function(err, rows) { if (err) { log.error(err); callback(null, err);} else { var content = JSON.parse(rows[0].content); content.time = msg.time; msg.payload = JSON.stringify(content); callback (msg, null); } }); } function existLastReading(node,msg, callback) { var sql = "SELECT count(*) as _count from mqtt_snapshot WHERE device='" + msg.code + "'"; //node.pool.query(sql, function (err, rows, fields) { node.mydbConfig.connection.query(sql, [], function(err, rows) { if (err) { log.error(err); callback(null, err); } else { if (rows[0]._count > 0 ) callback (true, null); else callback(false, null); } }); } function insertMysql(node, msg, table) { var sql = "INSERT INTO " + table + "(mentor_id, time, device, content, result) VALUES ('"+ msg.mentor_id+"','"+ msg.time +"','"+msg.code+"','"+msg.payload+"','"+msg.result+"')"; //node.pool.query(sql, function (err, rows, fields) { node.mydbConfig.connection.query(sql, [], function(err, rows) { if (err) { log.error(err);} else node.send({payload: {msg: rows.affectedRows + " record(s) inserted", data:msg}}); }); } function MysqlDBNodeIn(n) { RED.nodes.createNode(this,n); this.mydb = n.mysql; this.mydbConfig = RED.nodes.getNode(this.mydb); this.mentor = RED.nodes.getNode(n.mentor); var node = this; var busy = false; var status = {}; this.interval = 1000*60*n.interval; this.mentor_id = 0; if (this.mentor) this.mentor_id = this.mentor.mentor_id; if (this.mydbConfig) { this.mydbConfig.connect(); node.mydbConfig.on("state", function(info) { if (info === "connecting") { node.status({fill:"grey",shape:"ring",text:info}); } else if (info === "connected") { node.status({fill:"green",shape:"dot",text:info}); } else { if (info === "ECONNREFUSED") { info = "connection refused"; } if (info === "PROTOCOL_CONNECTION_LOST") { info = "connection lost"; } node.status({fill:"red",shape:"ring",text:info}); } }); node.on("input", function(msg) { if (node.mydbConfig.connected) { node.status({}); var obj = JSON.parse(msg.payload); msg.code = obj.code; msg.time = obj.time; msg.mentor_id = node.mentor_id; msg.result = 1; if(msg.payload.indexOf("Timeout") > -1 || msg.payload.indexOf("NaN") > -1) { getLastReading(node, msg, function (data, err) { if (err) log.error(err); else { msg = data; insertMysql(node, msg, "mqtt_lecturas"); } }); } else { if (node.interval > 0 && msg.time*1 % node.interval == 0 ) insertMysql(node, msg, "mqtt_lecturas"); existLastReading(node, msg, function(exist, err) { if (err) log.error(err); else { if (!exist) { insertMysql(node, msg, "mqtt_snapshot"); } UpdateLastReading(node,msg); status = {fill:"green",shape:"ring",text:"OK"}; } }) } node.send(msg); } else { node.error("Database not connected",msg); status = {fill:"red",shape:"ring",text:"not yet connected"}; } if (!busy) { busy = true; node.status(status); node.tout = setTimeout(function() { busy = false; node.status(status); },500); } }); node.on('close', function () { if (node.tout) { clearTimeout(node.tout); } node.mydbConfig.removeAllListeners(); node.status({}); }); } else { this.error("MySQL database not configured"); } } RED.nodes.registerType("SaveDB",MysqlDBNodeIn); }