UNPKG

node-red-contrib-services-mentor

Version:

Mentor Services

157 lines (139 loc) 4.92 kB
module.exports = function(RED) { var log = RED.log; function Node(config) { RED.nodes.createNode(this, config); var node = this; this.mqtt = RED.nodes.getNode(config.mqtt); this.broker = config.mqtt; this.brokerConn = RED.nodes.getNode(this.broker); this.mydb = config.mysql; this.mydbConfig = RED.nodes.getNode(this.mydb); this.channel = 0; this.qos = 2; if (this.mydbConfig) { this.mydbConfig.connect(); var busy = false; var status = {}; 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('close', function () { if (node.tout) { clearTimeout(node.tout); } node.mydbConfig.removeAllListeners(); node.status({}); }); } else { this.error("MySQL database not configured"); } if (this.brokerConn) { this.status({fill:"red",shape:"ring",text:"node-red:common.status.disconnected"}); node.brokerConn.register(this); this.brokerConn.subscribe('mentor_data',this.qos,function(topic,msg,packet) { switch (topic) { case "mentor_data": var obj = JSON.parse(msg.toString()); var mentor_id = obj.mentor_id; node.channel = "mentor_answer_" + mentor_id; obj.alarm = obj.alarm || 0; node.alarm = obj.alarm; existRecord(node, obj, function (exist, err) { if (err) { log.error(err); } else { if (!exist) { saveRecord(node, obj, mentor_id, function (r, err) { if (err) { log.error(err); } else { getAndSendRecord(obj, node); } }); } else {node.send({payload: {msg:"Record already found ", data: obj}}); getAndSendRecord(obj, node);} } } ); break; default: log.error(topic); break; } }, this.id); if (this.brokerConn.connected) { node.status({fill:"green",shape:"dot",text:"node-red:common.status.connected"}); } this.on('close', function(done) { node.brokerConn.deregister(node,done); }); } } function saveRecord(node, obj, mentor_id, callback) { if (node.mydbConfig.connected) { var sql = "INSERT INTO mqtt_lecturas(time, device, content, status, mentor_id, alarm) VALUES ('"+ obj.time +"','"+obj.device+"','"+obj.content+"', 2, "+mentor_id+",'"+obj.alarm+"');"; node.mydbConfig.connection.query(sql, [], function(err, rows) { if (err) { log.error(err); callback(null, err); } else { node.send({payload: {msg:rows.insertId + " Inserted.", data: obj}}); callback (true, null); } }); } else { node.error("Database not connected",msg); status = {fill:"red",shape:"ring",text:"not yet connected"}; callback(null, null); } } function existRecord(node,obj,callback) { if (node.mydbConfig.connected) { var sql = "SELECT count(*) as _count FROM mqtt_lecturas WHERE device='"+obj.device+"' AND time*1="+ obj.time; 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); } }); } else { node.error("Database not connected",null); status = {fill:"red",shape:"ring",text:"not yet connected"}; callback(null, null); } } function getAndSendRecord(obj, node) { var _msg = {}; _msg.device = obj.device; _msg.time = obj.time; _msg.status = 2; if (node.brokerConn) { msg = {}; msg.qos = 2; msg.retain = false; msg.payload = JSON.stringify(_msg); msg.topic = node.channel; msg.alarm = node.alarm; node.brokerConn.publish(msg); node.status({fill:"red",shape:"ring",text:"node-red:common.status.disconnected"}); if (node.brokerConn.connected) { node.status({fill:"green",shape:"dot",text:"node-red:common.status.connected"}); } } } RED.nodes.registerType("Receiver", Node); };