Allow Serial nodes to close asynchonously

This commit is contained in:
Nick O'Leary 2014-05-14 21:34:17 +01:00
parent c317ccc36d
commit 8f1dd62515
2 changed files with 19 additions and 11 deletions

View File

@ -80,9 +80,11 @@ module.exports = function(RED) {
this.error("missing serial config");
}
this.on("close", function() {
this.on("close", function(done) {
if (this.serialConfig) {
serialPool.close(this.serialConfig.serialport);
serialPool.close(this.serialConfig.serialport,done);
} else {
done();
}
});
}
@ -115,12 +117,11 @@ module.exports = function(RED) {
this.error("missing serial config");
}
this.on("close", function() {
this.on("close", function(done) {
if (this.serialConfig) {
try {
serialPool.close(this.serialConfig.serialport);
} catch(err) {
}
serialPool.close(this.serialConfig.serialport,done);
} else {
done();
}
});
}
@ -203,23 +204,29 @@ module.exports = function(RED) {
}
return connections[id];
},
close: function(port) {
close: function(port,done) {
if (connections[port]) {
if (connections[port].tout != null) clearTimeout(connections[port].tout);
if (connections[port].tout != null) {
clearTimeout(connections[port].tout);
}
connections[port]._closing = true;
try {
connections[port].close(function() {
util.log("[serial] serial port closed");
done();
});
} catch(err) { };
delete connections[port];
} else {
done();
}
delete connections[port];
}
}
}();
RED.httpAdmin.get("/serialports",function(req,res) {
serialp.list(function (err, ports) {
console.log(JSON.stringify(ports));
res.writeHead(200, {'Content-Type': 'text/plain'});
res.write(JSON.stringify(ports));
res.end();

View File

@ -35,11 +35,12 @@ function Node(n) {
}
Node.prototype.on = function(event,callback) {
var node = this;
if (event == "close") {
if (callback.length == 1) {
this.close = function() {
return when.promise(function(resolve) {
callback(function() {
callback.call(node,function() {
resolve();
});
});