Add better sensehat error handling

This commit is contained in:
Nick O'Leary 2016-03-08 15:28:20 +00:00
parent c9faed7876
commit 471840b868
2 changed files with 6 additions and 2 deletions

View File

@ -1,6 +1,6 @@
{ {
"name" : "node-red-node-pi-sense-hat", "name" : "node-red-node-pi-sense-hat",
"version" : "0.0.7", "version" : "0.0.8",
"description" : "A Node-RED node to interact with a Raspberry Pi Sense HAT", "description" : "A Node-RED node to interact with a Raspberry Pi Sense HAT",
"repository" : { "repository" : {
"type":"git", "type":"git",

View File

@ -53,6 +53,7 @@ module.exports = function(RED) {
var reconnectTimer = null; var reconnectTimer = null;
var connect = function() { var connect = function() {
reconnectTimer = null;
var buffer = ""; var buffer = "";
hat = spawn(hatCommand); hat = spawn(hatCommand);
hat.stdout.on('data', function (data) { hat.stdout.on('data', function (data) {
@ -115,7 +116,10 @@ module.exports = function(RED) {
} }
}); });
hat.stderr.on('data', function (data) { hat.stderr.on('data', function (data) {
// Any data on stderr means a bad thing has happened.
// Best to kill it and let it reconnect.
if (RED.settings.verbose) { RED.log.error("err: "+data+" :"); } if (RED.settings.verbose) { RED.log.error("err: "+data+" :"); }
hat.kill('SIGKILL');
}); });
hat.stderr.on('error', function(err) { }); hat.stderr.on('error', function(err) { });
hat.stdin.on('error', function(err) { }); hat.stdin.on('error', function(err) { });
@ -129,7 +133,7 @@ module.exports = function(RED) {
if (onclose) { if (onclose) {
onclose(); onclose();
onclose = null; onclose = null;
} else { } else if (!reconnectTimer) {
reconnectTimer = setTimeout(function() { reconnectTimer = setTimeout(function() {
connect(); connect();
},5000); },5000);