mirror of
https://github.com/node-red/node-red-nodes.git
synced 2025-03-01 10:37:43 +00:00
added autoconnect config, checks status before action, prevent unexpected error log, updates node.status
added autoconnect config, checks status before action, prevent unexpected error log, updates node.status
This commit is contained in:
parent
542d837c7c
commit
0c0f45b8f5
@ -27,6 +27,7 @@ module.exports = function(RED) {
|
|||||||
this.waitfor = n.waitfor || "";
|
this.waitfor = n.waitfor || "";
|
||||||
this.responsetimeout = n.responsetimeout || 10000;
|
this.responsetimeout = n.responsetimeout || 10000;
|
||||||
this.serialReconnectTime = n.serialReconnectTime || settings.serialReconnectTime || 15000;
|
this.serialReconnectTime = n.serialReconnectTime || settings.serialReconnectTime || 15000;
|
||||||
|
this.autoConnect = (typeof n.autoConnect === "undefined")? true : n.autoConnect;
|
||||||
}
|
}
|
||||||
RED.nodes.registerType("serial-port",SerialPortNode);
|
RED.nodes.registerType("serial-port",SerialPortNode);
|
||||||
|
|
||||||
@ -45,9 +46,11 @@ module.exports = function(RED) {
|
|||||||
if (msg.hasOwnProperty("action") && this.serialConfig) {
|
if (msg.hasOwnProperty("action") && this.serialConfig) {
|
||||||
if(msg.action == "disconnect"){
|
if(msg.action == "disconnect"){
|
||||||
serialPool.disconnect(this.serialConfig.serialport);
|
serialPool.disconnect(this.serialConfig.serialport);
|
||||||
|
node.status({fill:"red",shape:"ring",text:"node-red:common.status.not-connected"});
|
||||||
}else if(msg.action == "connect"){
|
}else if(msg.action == "connect"){
|
||||||
serialPool.connect(this.serialConfig.serialport);
|
serialPool.connect(this.serialConfig.serialport);
|
||||||
}
|
}
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
if (msg.hasOwnProperty("baudrate")) {
|
if (msg.hasOwnProperty("baudrate")) {
|
||||||
var baud = parseInt(msg.baudrate);
|
var baud = parseInt(msg.baudrate);
|
||||||
@ -142,13 +145,15 @@ module.exports = function(RED) {
|
|||||||
node.port = serialPool.get(this.serialConfig);
|
node.port = serialPool.get(this.serialConfig);
|
||||||
// Serial Out
|
// Serial Out
|
||||||
node.on("input",function(msg) {
|
node.on("input",function(msg) {
|
||||||
if (msg.hasOwnProperty("action") && this.serialConfig) {
|
if (msg.hasOwnProperty("action") && this.serialConfig) {
|
||||||
if(msg.action == "disconnect"){
|
if(msg.action == "disconnect"){
|
||||||
serialPool.disconnect(this.serialConfig.serialport);
|
serialPool.disconnect(this.serialConfig.serialport);
|
||||||
|
node.status({fill:"red",shape:"ring",text:"node-red:common.status.not-connected"});
|
||||||
}else if(msg.action == "connect"){
|
}else if(msg.action == "connect"){
|
||||||
serialPool.connect(this.serialConfig.serialport);
|
serialPool.connect(this.serialConfig.serialport);
|
||||||
}
|
}
|
||||||
}
|
return;
|
||||||
|
}
|
||||||
if (msg.hasOwnProperty("baudrate")) {
|
if (msg.hasOwnProperty("baudrate")) {
|
||||||
var baud = parseInt(msg.baudrate);
|
var baud = parseInt(msg.baudrate);
|
||||||
if (isNaN(baud)) {
|
if (isNaN(baud)) {
|
||||||
@ -233,8 +238,7 @@ module.exports = function(RED) {
|
|||||||
waitfor = serialConfig.waitfor,
|
waitfor = serialConfig.waitfor,
|
||||||
binoutput = serialConfig.bin,
|
binoutput = serialConfig.bin,
|
||||||
addchar = serialConfig.addchar,
|
addchar = serialConfig.addchar,
|
||||||
responsetimeout = serialConfig.responsetimeout,
|
responsetimeout = serialConfig.responsetimeout;
|
||||||
serialReconnectTime = serialConfig.serialReconnectTime;
|
|
||||||
var id = port;
|
var id = port;
|
||||||
// just return the connection object if already have one
|
// just return the connection object if already have one
|
||||||
// key is the port (file path)
|
// key is the port (file path)
|
||||||
@ -270,6 +274,10 @@ module.exports = function(RED) {
|
|||||||
var obj = {
|
var obj = {
|
||||||
_emitter: new events.EventEmitter(),
|
_emitter: new events.EventEmitter(),
|
||||||
serial: null,
|
serial: null,
|
||||||
|
isOpen: false,
|
||||||
|
forceDc: false,
|
||||||
|
autoConnect: serialConfig.autoConnect,
|
||||||
|
_autoConnect: serialConfig.autoConnect,
|
||||||
_closing: false,
|
_closing: false,
|
||||||
tout: null,
|
tout: null,
|
||||||
queue: [],
|
queue: [],
|
||||||
@ -367,30 +375,38 @@ module.exports = function(RED) {
|
|||||||
olderr = err.toString();
|
olderr = err.toString();
|
||||||
RED.log.error("[serialconfig:"+serialConfig.id+"] "+RED._("serial.errors.error",{port:port,error:olderr}), {});
|
RED.log.error("[serialconfig:"+serialConfig.id+"] "+RED._("serial.errors.error",{port:port,error:olderr}), {});
|
||||||
}
|
}
|
||||||
obj.tout = setTimeout(function() {
|
if(obj._autoConnect){
|
||||||
setupSerial();
|
obj.tout = setTimeout(function() {
|
||||||
}, serialReconnectTime);
|
setupSerial();
|
||||||
|
}, serialConfig.serialReconnectTime);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
obj.serial.on('error', function(err) {
|
obj.serial.on('error', function(err) {
|
||||||
RED.log.error("[serialconfig:"+serialConfig.id+"] "+RED._("serial.errors.error",{port:port,error:err.toString()}), {});
|
RED.log.error("[serialconfig:"+serialConfig.id+"] "+RED._("serial.errors.error",{port:port,error:err.toString()}), {});
|
||||||
obj._emitter.emit('closed');
|
obj._emitter.emit('closed');
|
||||||
|
obj.isOpen = false;
|
||||||
if (obj.tout) { clearTimeout(obj.tout); }
|
if (obj.tout) { clearTimeout(obj.tout); }
|
||||||
obj.tout = setTimeout(function() {
|
if(obj._autoConnect){
|
||||||
setupSerial();
|
obj.tout = setTimeout(function() {
|
||||||
}, serialReconnectTime);
|
setupSerial();
|
||||||
|
}, serialConfig.serialReconnectTime);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
obj.serial.on('close', function() {
|
obj.serial.on('close', function() {
|
||||||
if (!obj._closing) {
|
if (!obj._closing) {
|
||||||
if (olderr !== "unexpected") {
|
if (olderr !== "unexpected" && obj.forceDc == false) {
|
||||||
olderr = "unexpected";
|
olderr = "unexpected";
|
||||||
RED.log.error("[serialconfig:"+serialConfig.id+"] "+RED._("serial.errors.unexpected-close",{port:port}), {});
|
RED.log.error("[serialconfig:"+serialConfig.id+"] "+RED._("serial.errors.unexpected-close",{port:port}), {});
|
||||||
}
|
}
|
||||||
|
obj.isOpen = false;
|
||||||
obj._emitter.emit('closed');
|
obj._emitter.emit('closed');
|
||||||
if (obj.tout) { clearTimeout(obj.tout); }
|
if(obj._autoConnect){
|
||||||
obj.tout = setTimeout(function() {
|
if (obj.tout) { clearTimeout(obj.tout); }
|
||||||
setupSerial();
|
obj.tout = setTimeout(function() {
|
||||||
}, serialReconnectTime);
|
setupSerial();
|
||||||
|
}, serialConfig.serialReconnectTime);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
obj.serial.on('open',function() {
|
obj.serial.on('open',function() {
|
||||||
@ -405,6 +421,7 @@ module.exports = function(RED) {
|
|||||||
if (dtr != "none" || rts != "none" || cts != "none" || dsr != "none") { obj.serial.set(flags); }
|
if (dtr != "none" || rts != "none" || cts != "none" || dsr != "none") { obj.serial.set(flags); }
|
||||||
if (obj.tout) { clearTimeout(obj.tout); obj.tout = null; }
|
if (obj.tout) { clearTimeout(obj.tout); obj.tout = null; }
|
||||||
//obj.serial.flush();
|
//obj.serial.flush();
|
||||||
|
obj.isOpen = true;
|
||||||
obj._emitter.emit('ready');
|
obj._emitter.emit('ready');
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -502,21 +519,19 @@ module.exports = function(RED) {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
connect: function(port) {
|
connect: function(port) {
|
||||||
if (connections[port]) {
|
if (connections[port] && connections[port].isOpen == false ) {
|
||||||
try {
|
connections[port]._autoConnect = connections[port].autoConnect; // restore original auto connect config
|
||||||
connections[port].connect();
|
connections[port].connect();
|
||||||
}
|
|
||||||
catch(err) { }
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
disconnect: function(port) {
|
disconnect: function(port) {
|
||||||
if (connections[port]) {
|
if (connections[port] && connections[port].isOpen == true) {
|
||||||
try {
|
connections[port]._autoConnect = false; // disable auto connect on manual disconnect
|
||||||
connections[port].disconnect(function() {
|
connections[port].forceDc = true; // to prevent logging as an unexpected error
|
||||||
RED.log.info(RED._("serial.errors.closed",{port:port}), {});
|
connections[port].disconnect(function() {
|
||||||
});
|
RED.log.info(RED._("serial.errors.closed",{port:port}), {});
|
||||||
}
|
connections[port].forceDc = false;
|
||||||
catch(err) { }
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user