From 7e71a118ebfc2359565a68bd6e66902753e6aeb3 Mon Sep 17 00:00:00 2001 From: Dave C-J Date: Mon, 12 May 2014 16:32:19 +0100 Subject: [PATCH] Add status indicators to serial and tcp (client) nodes --- nodes/core/io/25-serial.js | 39 ++++++++++++++++++++++----------- nodes/core/io/31-tcpin.js | 44 ++++++++++++++++++++++---------------- 2 files changed, 52 insertions(+), 31 deletions(-) diff --git a/nodes/core/io/25-serial.js b/nodes/core/io/25-serial.js index 7b5da685e..44d054888 100644 --- a/nodes/core/io/25-serial.js +++ b/nodes/core/io/25-serial.js @@ -19,27 +19,27 @@ module.exports = function(RED) { var events = require("events"); var util = require("util"); var serialp = require("serialport"); - + // TODO: 'serialPool' should be encapsulated in SerialPortNode - + function SerialPortNode(n) { RED.nodes.createNode(this,n); this.serialport = n.serialport; this.newline = n.newline; this.addchar = n.addchar || "false"; - + this.serialbaud = parseInt(n.serialbaud) || 57600; this.databits = parseInt(n.databits) || 8; this.parity = n.parity || "none"; this.stopbits = parseInt(n.stopbits) || 1; } RED.nodes.registerType("serial-port",SerialPortNode); - + function SerialOutNode(n) { RED.nodes.createNode(this,n); this.serial = n.serial; this.serialConfig = RED.nodes.getNode(this.serial); - + if (this.serialConfig) { var node = this; node.port = serialPool.get(this.serialConfig.serialport, @@ -70,10 +70,16 @@ 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 { this.error("missing serial config"); } - + this.on("close", function() { if (this.serialConfig) { serialPool.close(this.serialConfig.serialport); @@ -81,14 +87,15 @@ module.exports = function(RED) { }); } RED.nodes.registerType("serial out",SerialOutNode); - + function SerialInNode(n) { RED.nodes.createNode(this,n); this.serial = n.serial; this.serialConfig = RED.nodes.getNode(this.serial); - + if (this.serialConfig) { var node = this; + node.status({fill:"grey",shape:"dot",text:"unknown"},true); node.port = serialPool.get(this.serialConfig.serialport, this.serialConfig.serialbaud, this.serialConfig.databits, @@ -98,10 +105,16 @@ module.exports = function(RED) { this.port.on('data', function(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 { this.error("missing serial config"); } - + this.on("close", function() { if (this.serialConfig) { try { @@ -112,8 +125,8 @@ module.exports = function(RED) { }); } RED.nodes.registerType("serial in",SerialInNode); - - + + var serialPool = function() { var connections = {}; return { @@ -152,6 +165,7 @@ module.exports = function(RED) { } obj.serial.on('error', function(err) { util.log("[serial] serial port "+port+" error "+err); + obj._emitter.emit('closed'); obj.tout = setTimeout(function() { setupSerial(); }, settings.serialReconnectTime); @@ -159,6 +173,7 @@ module.exports = function(RED) { obj.serial.on('close', function() { if (!obj._closing) { util.log("[serial] serial port "+port+" closed unexpectedly"); + obj._emitter.emit('closed'); obj.tout = setTimeout(function() { setupSerial(); }, settings.serialReconnectTime); @@ -202,7 +217,7 @@ module.exports = function(RED) { } } }(); - + RED.httpAdmin.get("/serialports",function(req,res) { serialp.list(function (err, ports) { res.writeHead(200, {'Content-Type': 'text/plain'}); diff --git a/nodes/core/io/31-tcpin.js b/nodes/core/io/31-tcpin.js index e121488e2..11ee43267 100644 --- a/nodes/core/io/31-tcpin.js +++ b/nodes/core/io/31-tcpin.js @@ -18,9 +18,9 @@ module.exports = function(RED) { var reconnectTime = RED.settings.socketReconnectTime||10000; var socketTimeout = RED.settings.socketTimeout||null; var net = require('net'); - + var connectionPool = {}; - + function TcpIn(n) { RED.nodes.createNode(this,n); this.host = n.host; @@ -33,20 +33,22 @@ module.exports = function(RED) { this.server = (typeof n.server == 'boolean')?n.server:(n.server == "server"); this.closing = false; var node = this; - + if (!node.server) { var buffer = null; var client; var reconnectTimeout; function setupTcpClient() { 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); client = net.connect(node.port, node.host, function() { buffer = (node.datatype == 'buffer')? new Buffer(0):""; node.log("connected to "+node.host+":"+node.port); + node.status({fill:"green",shape:"dot",text:"connected"},true); }); connectionPool[id] = client; - + client.on('data', function (data) { if (node.datatype != 'buffer') { data = data.toString(node.datatype); @@ -85,16 +87,17 @@ module.exports = function(RED) { client.on('close', function() { delete connectionPool[id]; node.log("connection lost to "+node.host+":"+node.port); + node.status({fill:"red",shape:"ring",text:"disconnected"}); if (!node.closing) { reconnectTimeout = setTimeout(setupTcpClient, reconnectTime); } }); client.on('error', function(err) { - node.log(err); + node.log(err); }); } setupTcpClient(); - + this.on('close', function() { this.closing = true; client.end(); @@ -105,13 +108,13 @@ module.exports = function(RED) { if (socketTimeout !== null) { socket.setTimeout(socketTimeout); } var id = (1+Math.random()*4294967295).toString(16); connectionPool[id] = socket; - + var buffer = (node.datatype == 'buffer')? new Buffer(0):""; socket.on('data', function (data) { if (node.datatype != 'buffer') { data = data.toString(node.datatype); } - + if (node.stream) { if ((typeof data) === "string" && node.newline != "") { buffer = buffer+data; @@ -164,7 +167,7 @@ module.exports = function(RED) { node.error('unable to listen on port '+node.port+' : '+err); } else { node.log('listening on port '+node.port); - + node.on('close', function() { node.closing = true; server.close(); @@ -173,10 +176,10 @@ module.exports = function(RED) { } }); } - + } RED.nodes.registerType("tcp in",TcpIn); - + function TcpOut(n) { RED.nodes.createNode(this,n); this.host = n.host; @@ -186,17 +189,19 @@ module.exports = function(RED) { this.name = n.name; this.closing = false; var node = this; - + if (!node.beserver||node.beserver=="client") { var reconnectTimeout; var client = null; var connected = false; - + function setupTcpClient() { 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() { connected = true; node.log("connected to "+node.host+":"+node.port); + node.status({fill:"green",shape:"dot",text:"connected"},true); }); client.on('error', function (err) { node.log('error : '+err); @@ -205,6 +210,7 @@ module.exports = function(RED) { }); client.on('close', function() { node.log("connection lost to "+node.host+":"+node.port); + node.status({fill:"red",shape:"ring",text:"disconnected"},true); connected = false; client.destroy(); if (!node.closing) { @@ -213,7 +219,7 @@ module.exports = function(RED) { }); } setupTcpClient(); - + node.on("input", function(msg) { if (connected && msg.payload != null) { if (Buffer.isBuffer(msg.payload)) { @@ -225,13 +231,13 @@ module.exports = function(RED) { } } }); - + node.on("close", function() { this.closing = true; client.end(); clearTimeout(reconnectTimeout); }); - + } else if (node.beserver == "reply") { node.on("input",function(msg) { if (msg._session && msg._session.type == "tcp") { @@ -282,13 +288,13 @@ module.exports = function(RED) { } } }); - + server.on('error', function(err) { if (err) { node.error('unable to listen on port '+node.port+' : '+err); } }); - + server.listen(node.port, function(err) { if (err) { node.error('unable to listen on port '+node.port+' : '+err); @@ -302,6 +308,6 @@ module.exports = function(RED) { }); } } - + RED.nodes.registerType("tcp out",TcpOut); }