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

Add status indicators to serial and tcp (client) nodes

This commit is contained in:
Dave C-J 2014-05-12 16:32:19 +01:00
parent e15a0d545d
commit 7e71a118eb
2 changed files with 52 additions and 31 deletions

View File

@ -70,6 +70,12 @@ module.exports = function(RED) {
} }
}); });
}); });
node.port.on('ready', function() {
node.status({fill:"green",shape:"dot",text:"connected"},true);
});
node.port.on('closed', function() {
node.status({fill:"red",shape:"ring",text:"not connected"},true);
});
} else { } else {
this.error("missing serial config"); this.error("missing serial config");
} }
@ -89,6 +95,7 @@ module.exports = function(RED) {
if (this.serialConfig) { if (this.serialConfig) {
var node = this; var node = this;
node.status({fill:"grey",shape:"dot",text:"unknown"},true);
node.port = serialPool.get(this.serialConfig.serialport, node.port = serialPool.get(this.serialConfig.serialport,
this.serialConfig.serialbaud, this.serialConfig.serialbaud,
this.serialConfig.databits, this.serialConfig.databits,
@ -98,6 +105,12 @@ module.exports = function(RED) {
this.port.on('data', function(msg) { this.port.on('data', function(msg) {
node.send({ "payload": msg }); node.send({ "payload": msg });
}); });
this.port.on('ready', function() {
node.status({fill:"green",shape:"dot",text:"connected"},true);
});
this.port.on('closed', function() {
node.status({fill:"red",shape:"ring",text:"not connected"},true);
});
} else { } else {
this.error("missing serial config"); this.error("missing serial config");
} }
@ -152,6 +165,7 @@ module.exports = function(RED) {
} }
obj.serial.on('error', function(err) { obj.serial.on('error', function(err) {
util.log("[serial] serial port "+port+" error "+err); util.log("[serial] serial port "+port+" error "+err);
obj._emitter.emit('closed');
obj.tout = setTimeout(function() { obj.tout = setTimeout(function() {
setupSerial(); setupSerial();
}, settings.serialReconnectTime); }, settings.serialReconnectTime);
@ -159,6 +173,7 @@ module.exports = function(RED) {
obj.serial.on('close', function() { obj.serial.on('close', function() {
if (!obj._closing) { if (!obj._closing) {
util.log("[serial] serial port "+port+" closed unexpectedly"); util.log("[serial] serial port "+port+" closed unexpectedly");
obj._emitter.emit('closed');
obj.tout = setTimeout(function() { obj.tout = setTimeout(function() {
setupSerial(); setupSerial();
}, settings.serialReconnectTime); }, settings.serialReconnectTime);

View File

@ -40,10 +40,12 @@ module.exports = function(RED) {
var reconnectTimeout; var reconnectTimeout;
function setupTcpClient() { function setupTcpClient() {
node.log("connecting to "+node.host+":"+node.port); node.log("connecting to "+node.host+":"+node.port);
node.status({fill:"grey",shape:"dot",text:"connecting"},true);
var id = (1+Math.random()*4294967295).toString(16); var id = (1+Math.random()*4294967295).toString(16);
client = net.connect(node.port, node.host, function() { client = net.connect(node.port, node.host, function() {
buffer = (node.datatype == 'buffer')? new Buffer(0):""; buffer = (node.datatype == 'buffer')? new Buffer(0):"";
node.log("connected to "+node.host+":"+node.port); node.log("connected to "+node.host+":"+node.port);
node.status({fill:"green",shape:"dot",text:"connected"},true);
}); });
connectionPool[id] = client; connectionPool[id] = client;
@ -85,12 +87,13 @@ module.exports = function(RED) {
client.on('close', function() { client.on('close', function() {
delete connectionPool[id]; delete connectionPool[id];
node.log("connection lost to "+node.host+":"+node.port); node.log("connection lost to "+node.host+":"+node.port);
node.status({fill:"red",shape:"ring",text:"disconnected"});
if (!node.closing) { if (!node.closing) {
reconnectTimeout = setTimeout(setupTcpClient, reconnectTime); reconnectTimeout = setTimeout(setupTcpClient, reconnectTime);
} }
}); });
client.on('error', function(err) { client.on('error', function(err) {
node.log(err); node.log(err);
}); });
} }
setupTcpClient(); setupTcpClient();
@ -194,9 +197,11 @@ module.exports = function(RED) {
function setupTcpClient() { function setupTcpClient() {
node.log("connecting to "+node.host+":"+node.port); node.log("connecting to "+node.host+":"+node.port);
node.status({fill:"grey",shape:"dot",text:"connecting"},true);
client = net.connect(node.port, node.host, function() { client = net.connect(node.port, node.host, function() {
connected = true; connected = true;
node.log("connected to "+node.host+":"+node.port); node.log("connected to "+node.host+":"+node.port);
node.status({fill:"green",shape:"dot",text:"connected"},true);
}); });
client.on('error', function (err) { client.on('error', function (err) {
node.log('error : '+err); node.log('error : '+err);
@ -205,6 +210,7 @@ module.exports = function(RED) {
}); });
client.on('close', function() { client.on('close', function() {
node.log("connection lost to "+node.host+":"+node.port); node.log("connection lost to "+node.host+":"+node.port);
node.status({fill:"red",shape:"ring",text:"disconnected"},true);
connected = false; connected = false;
client.destroy(); client.destroy();
if (!node.closing) { if (!node.closing) {