Allow a bunch of Pi nodes to be visible but not active when used not on a Pi

This commit is contained in:
Dave Conway-Jones
2018-04-15 11:24:56 +01:00
parent ace67b0154
commit acc2dc1a14
15 changed files with 428 additions and 355 deletions

View File

@@ -1,6 +1,6 @@
{
"name" : "node-red-node-pilcd",
"version" : "0.0.8",
"version" : "0.0.9",
"description" : "A Node-RED node for Raspberry Pi to write to HD44780 style LCD panels.",
"dependencies" : {
},

View File

@@ -6,20 +6,26 @@ module.exports = function(RED) {
var fs = require('fs');
var gpioCommand = __dirname + '/nrlcd';
var allOK = true;
if (!fs.existsSync("/dev/ttyAMA0")) { // unlikely if not on a Pi
//util.log("Info : Ignoring Raspberry Pi specific node.");
throw "Info : Ignoring Raspberry Pi specific node.";
try {
var cpuinfo = fs.readFileSync("/proc/cpuinfo").toString();
if (cpuinfo.indexOf(": BCM") === -1) {
RED.log.warn("rpi-lcd : "+RED._("node-red:rpi-gpio.errors.ignorenode"));
allOK = false;
}
else if (!fs.existsSync("/usr/share/doc/python-rpi.gpio")) {
RED.log.warn("rpi-lcd : "+RED._("node-red:rpi-gpio.errors.libnotfound"));
allOK = false;
}
else if (!(1 & parseInt ((fs.statSync(gpioCommand).mode & parseInt ("777", 8)).toString (8)[0]))) {
RED.log.warn("rpi-lcd : "+RED._("node-red:rpi-gpio.errors.needtobeexecutable",{command:gpioCommand}));
allOK = false;
}
}
if (!fs.existsSync("/usr/share/doc/python-rpi.gpio")) {
util.log("[rpi-lcd] Info : Can't find Pi RPi.GPIO python library.");
throw "Warning : Can't find Pi RPi.GPIO python library.";
}
if (!(1 & parseInt ((fs.statSync(gpioCommand).mode & parseInt ("777", 8)).toString (8)[0]))) {
util.log("[rpi-lcd] Error : " + gpioCommand + " needs to be executable.");
throw "Error : " + gpioCommand + " must to be executable.";
catch(err) {
RED.log.warn("rpi-lcd : "+RED._("node-red:rpi-gpio.errors.ignorenode"));
allOK = false;
}
function PiLcdNode(n) {
@@ -33,54 +39,61 @@ module.exports = function(RED) {
else { node.warn("Command not running"); }
}
if (node.pins !== undefined) {
node.child = spawn(gpioCommand, [node.pins]);
node.running = true;
if (RED.settings.verbose) { node.log("pin: " + node.pins + " :"); }
node.on("input", inputlistener);
if (allOK === true) {
if (node.pins !== undefined) {
node.child = spawn(gpioCommand, [node.pins]);
node.running = true;
if (RED.settings.verbose) { node.log("pin: " + node.pins + " :"); }
node.on("input", inputlistener);
node.child.stdout.on('data', function(data) {
if (RED.settings.verbose) { node.log("out: " + data + " :"); }
node.child.stdout.on('data', function(data) {
if (RED.settings.verbose) { node.log("out: " + data + " :"); }
});
node.child.stderr.on('data', function(data) {
if (RED.settings.verbose) { node.log("err: " + data + " :"); }
});
node.child.on('close', function(code) {
if (RED.settings.verbose) { node.log("ret: " + code + " :"); }
node.child = null;
node.running = false;
});
node.child.on('error', function(err) {
if (err.errno === "ENOENT") { node.warn('Command not found'); }
else if (err.errno === "EACCES") { node.warn('Command not executable'); }
else { node.log('error: ' + err); }
});
}
else {
node.error("Invalid GPIO pins: " + node.pin);
}
var wfi = function(done) {
if (!node.running) {
if (RED.settings.verbose) { node.log("end"); }
done();
return;
}
setTimeout(function() { wfi(done); }, 333);
}
node.on("close", function(done) {
if (node.child != null) {
node.child.stdin.write("c:lose" + node.pin);
node.child.kill('SIGKILL');
}
wfi(done);
});
node.child.stderr.on('data', function(data) {
if (RED.settings.verbose) { node.log("err: " + data + " :"); }
});
node.child.on('close', function(code) {
if (RED.settings.verbose) { node.log("ret: " + code + " :"); }
node.child = null;
node.running = false;
});
node.child.on('error', function(err) {
if (err.errno === "ENOENT") { node.warn('Command not found'); }
else if (err.errno === "EACCES") { node.warn('Command not executable'); }
else { node.log('error: ' + err); }
});
}
else {
node.error("Invalid GPIO pins: " + node.pin);
node.status({fill:"grey",shape:"dot",text:"node-red:rpi-gpio.status.not-available"});
node.on("input", function(msg){
node.status({fill:"grey",shape:"dot",text:RED._("node-red:rpi-gpio.status.na",{value:msg.payload.toString()})});
});
}
var wfi = function(done) {
if (!node.running) {
if (RED.settings.verbose) { node.log("end"); }
done();
return;
}
setTimeout(function() { wfi(done); }, 333);
}
node.on("close", function(done) {
if (node.child != null) {
node.child.stdin.write("c:lose" + node.pin);
node.child.kill('SIGKILL');
}
wfi(done);
});
}
RED.nodes.registerType("rpi-lcd", PiLcdNode);
}