From 738eada16bc741f25287fa435c0367eb537119c4 Mon Sep 17 00:00:00 2001 From: Dave Conway-Jones Date: Sun, 27 Nov 2016 17:03:03 +0000 Subject: [PATCH] Update Mysql Node status reporting --- storage/mysql/68-mysql.js | 19 +++++++++++++++++-- storage/mysql/package.json | 4 ++-- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/storage/mysql/68-mysql.js b/storage/mysql/68-mysql.js index 9dcde8bb..b8fc46eb 100644 --- a/storage/mysql/68-mysql.js +++ b/storage/mysql/68-mysql.js @@ -14,10 +14,12 @@ module.exports = function(RED) { this.connecting = false; this.dbname = n.db; + this.setMaxListeners(0); var node = this; function doConnect() { node.connecting = true; + node.emit("state","connecting"); node.connection = mysqldb.createConnection({ host : node.host, port : node.port, @@ -33,14 +35,17 @@ module.exports = function(RED) { node.connecting = false; if (err) { node.error(err); + node.emit("state",err.code); node.tick = setTimeout(doConnect, reconnect); } else { node.connected = true; + node.emit("state","connected"); } }); node.connection.on('error', function(err) { node.connected = false; + node.emit("state",err.code); if (err.code === 'PROTOCOL_CONNECTION_LOST') { doConnect(); // silently reconnect... } else { @@ -59,6 +64,7 @@ module.exports = function(RED) { this.on('close', function (done) { if (this.tick) { clearTimeout(this.tick); } node.connected = false; + node.emit("state"," "); if (this.connection) { node.connection.end(function(err) { if (err) { node.error(err); } @@ -85,9 +91,18 @@ module.exports = function(RED) { if (this.mydbConfig) { this.mydbConfig.connect(); var node = this; + 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({fill:"green",shape:"dot",text:"connected"}); if (typeof msg.topic === 'string') { //console.log("query:",msg.topic); var bind = Array.isArray(msg.payload) ? msg.payload : []; @@ -109,7 +124,7 @@ module.exports = function(RED) { } else { node.error("Database not connected",msg); - node.status({fill:"grey",shape:"ring",text:"Not connected"}); + node.status({fill:"red",shape:"ring",text:"not yet connected"}); } }); } diff --git a/storage/mysql/package.json b/storage/mysql/package.json index da93005e..1dcc88df 100644 --- a/storage/mysql/package.json +++ b/storage/mysql/package.json @@ -1,9 +1,9 @@ { "name" : "node-red-node-mysql", - "version" : "0.0.11", + "version" : "0.0.12", "description" : "A Node-RED node to read and write to a MySQL database", "dependencies" : { - "mysql" : "2.11.1" + "mysql" : "^2.12.0" }, "repository" : { "type":"git",