1
0
mirror of https://github.com/node-red/node-red-nodes.git synced 2023-10-10 13:36:58 +02:00

Update Mysql Node status reporting

This commit is contained in:
Dave Conway-Jones 2016-11-27 17:03:03 +00:00
parent fcc1fcfca8
commit 738eada16b
2 changed files with 19 additions and 4 deletions

View File

@ -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"});
}
});
}

View File

@ -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",