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

@ -19,27 +19,27 @@ module.exports = function(RED) {
var events = require("events"); var events = require("events");
var util = require("util"); var util = require("util");
var serialp = require("serialport"); var serialp = require("serialport");
// TODO: 'serialPool' should be encapsulated in SerialPortNode // TODO: 'serialPool' should be encapsulated in SerialPortNode
function SerialPortNode(n) { function SerialPortNode(n) {
RED.nodes.createNode(this,n); RED.nodes.createNode(this,n);
this.serialport = n.serialport; this.serialport = n.serialport;
this.newline = n.newline; this.newline = n.newline;
this.addchar = n.addchar || "false"; this.addchar = n.addchar || "false";
this.serialbaud = parseInt(n.serialbaud) || 57600; this.serialbaud = parseInt(n.serialbaud) || 57600;
this.databits = parseInt(n.databits) || 8; this.databits = parseInt(n.databits) || 8;
this.parity = n.parity || "none"; this.parity = n.parity || "none";
this.stopbits = parseInt(n.stopbits) || 1; this.stopbits = parseInt(n.stopbits) || 1;
} }
RED.nodes.registerType("serial-port",SerialPortNode); RED.nodes.registerType("serial-port",SerialPortNode);
function SerialOutNode(n) { function SerialOutNode(n) {
RED.nodes.createNode(this,n); RED.nodes.createNode(this,n);
this.serial = n.serial; this.serial = n.serial;
this.serialConfig = RED.nodes.getNode(this.serial); this.serialConfig = RED.nodes.getNode(this.serial);
if (this.serialConfig) { if (this.serialConfig) {
var node = this; var node = this;
node.port = serialPool.get(this.serialConfig.serialport, 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 { } else {
this.error("missing serial config"); this.error("missing serial config");
} }
this.on("close", function() { this.on("close", function() {
if (this.serialConfig) { if (this.serialConfig) {
serialPool.close(this.serialConfig.serialport); serialPool.close(this.serialConfig.serialport);
@ -81,14 +87,15 @@ module.exports = function(RED) {
}); });
} }
RED.nodes.registerType("serial out",SerialOutNode); RED.nodes.registerType("serial out",SerialOutNode);
function SerialInNode(n) { function SerialInNode(n) {
RED.nodes.createNode(this,n); RED.nodes.createNode(this,n);
this.serial = n.serial; this.serial = n.serial;
this.serialConfig = RED.nodes.getNode(this.serial); this.serialConfig = RED.nodes.getNode(this.serial);
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,10 +105,16 @@ 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");
} }
this.on("close", function() { this.on("close", function() {
if (this.serialConfig) { if (this.serialConfig) {
try { try {
@ -112,8 +125,8 @@ module.exports = function(RED) {
}); });
} }
RED.nodes.registerType("serial in",SerialInNode); RED.nodes.registerType("serial in",SerialInNode);
var serialPool = function() { var serialPool = function() {
var connections = {}; var connections = {};
return { return {
@ -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);
@ -202,7 +217,7 @@ module.exports = function(RED) {
} }
} }
}(); }();
RED.httpAdmin.get("/serialports",function(req,res) { RED.httpAdmin.get("/serialports",function(req,res) {
serialp.list(function (err, ports) { serialp.list(function (err, ports) {
res.writeHead(200, {'Content-Type': 'text/plain'}); res.writeHead(200, {'Content-Type': 'text/plain'});

View File

@ -18,9 +18,9 @@ module.exports = function(RED) {
var reconnectTime = RED.settings.socketReconnectTime||10000; var reconnectTime = RED.settings.socketReconnectTime||10000;
var socketTimeout = RED.settings.socketTimeout||null; var socketTimeout = RED.settings.socketTimeout||null;
var net = require('net'); var net = require('net');
var connectionPool = {}; var connectionPool = {};
function TcpIn(n) { function TcpIn(n) {
RED.nodes.createNode(this,n); RED.nodes.createNode(this,n);
this.host = n.host; this.host = n.host;
@ -33,20 +33,22 @@ module.exports = function(RED) {
this.server = (typeof n.server == 'boolean')?n.server:(n.server == "server"); this.server = (typeof n.server == 'boolean')?n.server:(n.server == "server");
this.closing = false; this.closing = false;
var node = this; var node = this;
if (!node.server) { if (!node.server) {
var buffer = null; var buffer = null;
var client; var client;
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;
client.on('data', function (data) { client.on('data', function (data) {
if (node.datatype != 'buffer') { if (node.datatype != 'buffer') {
data = data.toString(node.datatype); data = data.toString(node.datatype);
@ -85,16 +87,17 @@ 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();
this.on('close', function() { this.on('close', function() {
this.closing = true; this.closing = true;
client.end(); client.end();
@ -105,13 +108,13 @@ module.exports = function(RED) {
if (socketTimeout !== null) { socket.setTimeout(socketTimeout); } if (socketTimeout !== null) { socket.setTimeout(socketTimeout); }
var id = (1+Math.random()*4294967295).toString(16); var id = (1+Math.random()*4294967295).toString(16);
connectionPool[id] = socket; connectionPool[id] = socket;
var buffer = (node.datatype == 'buffer')? new Buffer(0):""; var buffer = (node.datatype == 'buffer')? new Buffer(0):"";
socket.on('data', function (data) { socket.on('data', function (data) {
if (node.datatype != 'buffer') { if (node.datatype != 'buffer') {
data = data.toString(node.datatype); data = data.toString(node.datatype);
} }
if (node.stream) { if (node.stream) {
if ((typeof data) === "string" && node.newline != "") { if ((typeof data) === "string" && node.newline != "") {
buffer = buffer+data; buffer = buffer+data;
@ -164,7 +167,7 @@ module.exports = function(RED) {
node.error('unable to listen on port '+node.port+' : '+err); node.error('unable to listen on port '+node.port+' : '+err);
} else { } else {
node.log('listening on port '+node.port); node.log('listening on port '+node.port);
node.on('close', function() { node.on('close', function() {
node.closing = true; node.closing = true;
server.close(); server.close();
@ -173,10 +176,10 @@ module.exports = function(RED) {
} }
}); });
} }
} }
RED.nodes.registerType("tcp in",TcpIn); RED.nodes.registerType("tcp in",TcpIn);
function TcpOut(n) { function TcpOut(n) {
RED.nodes.createNode(this,n); RED.nodes.createNode(this,n);
this.host = n.host; this.host = n.host;
@ -186,17 +189,19 @@ module.exports = function(RED) {
this.name = n.name; this.name = n.name;
this.closing = false; this.closing = false;
var node = this; var node = this;
if (!node.beserver||node.beserver=="client") { if (!node.beserver||node.beserver=="client") {
var reconnectTimeout; var reconnectTimeout;
var client = null; var client = null;
var connected = false; var connected = false;
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) {
@ -213,7 +219,7 @@ module.exports = function(RED) {
}); });
} }
setupTcpClient(); setupTcpClient();
node.on("input", function(msg) { node.on("input", function(msg) {
if (connected && msg.payload != null) { if (connected && msg.payload != null) {
if (Buffer.isBuffer(msg.payload)) { if (Buffer.isBuffer(msg.payload)) {
@ -225,13 +231,13 @@ module.exports = function(RED) {
} }
} }
}); });
node.on("close", function() { node.on("close", function() {
this.closing = true; this.closing = true;
client.end(); client.end();
clearTimeout(reconnectTimeout); clearTimeout(reconnectTimeout);
}); });
} else if (node.beserver == "reply") { } else if (node.beserver == "reply") {
node.on("input",function(msg) { node.on("input",function(msg) {
if (msg._session && msg._session.type == "tcp") { if (msg._session && msg._session.type == "tcp") {
@ -282,13 +288,13 @@ module.exports = function(RED) {
} }
} }
}); });
server.on('error', function(err) { server.on('error', function(err) {
if (err) { if (err) {
node.error('unable to listen on port '+node.port+' : '+err); node.error('unable to listen on port '+node.port+' : '+err);
} }
}); });
server.listen(node.port, function(err) { server.listen(node.port, function(err) {
if (err) { if (err) {
node.error('unable to listen on port '+node.port+' : '+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); RED.nodes.registerType("tcp out",TcpOut);
} }