1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

let Pi nodes be visible/editable on all platforms

even where they are not physically available.
This commit is contained in:
Dave Conway-Jones 2018-05-22 15:48:24 +01:00
parent d4d9190919
commit b761904424
No known key found for this signature in database
GPG Key ID: 9E7F9C73F5168CD4
2 changed files with 189 additions and 156 deletions

View File

@ -6,35 +6,36 @@ module.exports = function(RED) {
var fs = require('fs'); var fs = require('fs');
var gpioCommand = __dirname+'/nrgpio'; var gpioCommand = __dirname+'/nrgpio';
var allOK = true;
try { try {
var cpuinfo = fs.readFileSync("/proc/cpuinfo").toString(); var cpuinfo = fs.readFileSync("/proc/cpuinfo").toString();
if (cpuinfo.indexOf(": BCM") === -1) { throw "Info : "+RED._("rpi-gpio.errors.ignorenode"); } if (cpuinfo.indexOf(": BCM") === -1) {
} catch(err) { allOK = false;
throw "Info : "+RED._("rpi-gpio.errors.ignorenode"); RED.log.warn("rpi-gpio : "+RED._("rpi-gpio.errors.ignorenode"));
} }
try {
fs.statSync("/usr/share/doc/python-rpi.gpio"); // test on Raspbian
// /usr/lib/python2.7/dist-packages/RPi/GPIO
} catch(err) {
try { try {
fs.statSync("/usr/lib/python2.7/site-packages/RPi/GPIO"); // test on Arch fs.statSync("/usr/share/doc/python-rpi.gpio"); // test on Raspbian
} // /usr/lib/python2.7/dist-packages/RPi/GPIO
catch(err) { } catch(err) {
try { try {
fs.statSync("/usr/lib/python2.7/dist-packages/RPi/GPIO"); // test on Hypriot fs.statSync("/usr/lib/python2.7/site-packages/RPi/GPIO"); // test on Arch
} } catch(err) {
catch(err) { try {
RED.log.warn(RED._("rpi-gpio.errors.libnotfound")); fs.statSync("/usr/lib/python2.7/dist-packages/RPi/GPIO"); // test on Hypriot
throw "Warning : "+RED._("rpi-gpio.errors.libnotfound"); } catch(err) {
RED.log.warn("rpi-gpio : "+RED._("rpi-gpio.errors.libnotfound"));
allOK = false;
}
} }
} }
} if ( !(1 & parseInt((fs.statSync(gpioCommand).mode & parseInt("777", 8)).toString(8)[0]) )) {
RED.log.warn("rpi-gpio : "+RED._("rpi-gpio.errors.needtobeexecutable",{command:gpioCommand}));
if ( !(1 & parseInt((fs.statSync(gpioCommand).mode & parseInt("777", 8)).toString(8)[0]) )) { allOK = false;
RED.log.error(RED._("rpi-gpio.errors.needtobeexecutable",{command:gpioCommand})); }
throw "Error : "+RED._("rpi-gpio.errors.mustbeexecutable"); } catch(err) {
allOK = false;
RED.log.warn("rpi-gpio : "+RED._("rpi-gpio.errors.ignorenode"));
} }
// the magic to make python print stuff immediately // the magic to make python print stuff immediately
@ -61,48 +62,62 @@ module.exports = function(RED) {
} }
} }
if (node.pin !== undefined) { if (allOK === true) {
node.child = spawn(gpioCommand, ["in",node.pin,node.intype,node.debounce]); if (node.pin !== undefined) {
node.running = true; node.child = spawn(gpioCommand, ["in",node.pin,node.intype,node.debounce]);
node.status({fill:"green",shape:"dot",text:"common.status.ok"}); node.running = true;
node.status({fill:"green",shape:"dot",text:"common.status.ok"});
node.child.stdout.on('data', function (data) { node.child.stdout.on('data', function (data) {
var d = data.toString().trim().split("\n"); var d = data.toString().trim().split("\n");
for (var i = 0; i < d.length; i++) { for (var i = 0; i < d.length; i++) {
if (d[i] === '') { return; } if (d[i] === '') { return; }
if (node.running && node.buttonState !== -1 && !isNaN(Number(d[i])) && node.buttonState !== d[i]) { if (node.running && node.buttonState !== -1 && !isNaN(Number(d[i])) && node.buttonState !== d[i]) {
node.send({ topic:"pi/"+node.pin, payload:Number(d[i]) }); node.send({ topic:"pi/"+node.pin, payload:Number(d[i]) });
}
node.buttonState = d[i];
node.status({fill:"green",shape:"dot",text:d[i]});
if (RED.settings.verbose) { node.log("out: "+d[i]+" :"); }
} }
node.buttonState = d[i]; });
node.status({fill:"green",shape:"dot",text:d[i]});
if (RED.settings.verbose) { node.log("out: "+d[i]+" :"); }
}
});
node.child.stderr.on('data', function (data) { node.child.stderr.on('data', function (data) {
if (RED.settings.verbose) { node.log("err: "+data+" :"); } if (RED.settings.verbose) { node.log("err: "+data+" :"); }
}); });
node.child.on('close', function (code) { node.child.on('close', function (code) {
node.running = false; node.running = false;
node.child = null; node.child = null;
if (RED.settings.verbose) { node.log(RED._("rpi-gpio.status.closed")); } if (RED.settings.verbose) { node.log(RED._("rpi-gpio.status.closed")); }
if (node.done) { if (node.done) {
node.status({fill:"grey",shape:"ring",text:"rpi-gpio.status.closed"}); node.status({fill:"grey",shape:"ring",text:"rpi-gpio.status.closed"});
node.done(); node.done();
} }
else { node.status({fill:"red",shape:"ring",text:"rpi-gpio.status.stopped"}); } else { node.status({fill:"red",shape:"ring",text:"rpi-gpio.status.stopped"}); }
}); });
node.child.on('error', function (err) { node.child.on('error', function (err) {
if (err.errno === "ENOENT") { node.error(RED._("rpi-gpio.errors.commandnotfound")); } if (err.errno === "ENOENT") { node.error(RED._("rpi-gpio.errors.commandnotfound")); }
else if (err.errno === "EACCES") { node.error(RED._("rpi-gpio.errors.commandnotexecutable")); } else if (err.errno === "EACCES") { node.error(RED._("rpi-gpio.errors.commandnotexecutable")); }
else { node.error(RED._("rpi-gpio.errors.error",{error:err.errno})) } else { node.error(RED._("rpi-gpio.errors.error",{error:err.errno})) }
}); });
}
else {
node.warn(RED._("rpi-gpio.errors.invalidpin")+": "+node.pin);
}
} }
else { else {
node.warn(RED._("rpi-gpio.errors.invalidpin")+": "+node.pin); node.status({fill:"grey",shape:"dot",text:"node-red:rpi-gpio.status.not-available"});
if (node.read === true) {
var val;
if (node.intype == "up") { val = 1; }
if (node.intype == "down") { val = 0; }
setTimeout(function(){
node.send({ topic:"pi/"+node.pin, payload:val });
node.status({fill:"grey",shape:"dot",text:RED._("rpi-gpio.status.na",{value:val})});
},250);
}
} }
node.on("close", function(done) { node.on("close", function(done) {
@ -155,20 +170,83 @@ module.exports = function(RED) {
else { node.warn(RED._("rpi-gpio.errors.invalidinput")+": "+out); } else { node.warn(RED._("rpi-gpio.errors.invalidinput")+": "+out); }
} }
if (node.pin !== undefined) { if (allOK === true) {
if (node.set && (node.out === "out")) { if (node.pin !== undefined) {
node.child = spawn(gpioCommand, [node.out,node.pin,node.level]); if (node.set && (node.out === "out")) {
node.status({fill:"green",shape:"dot",text:node.level}); node.child = spawn(gpioCommand, [node.out,node.pin,node.level]);
} else { node.status({fill:"green",shape:"dot",text:node.level});
node.child = spawn(gpioCommand, [node.out,node.pin,node.freq]); } else {
node.status({fill:"green",shape:"dot",text:"common.status.ok"}); node.child = spawn(gpioCommand, [node.out,node.pin,node.freq]);
} node.status({fill:"green",shape:"dot",text:"common.status.ok"});
node.running = true; }
node.running = true;
node.on("input", inputlistener); node.on("input", inputlistener);
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) {
node.child = null;
node.running = false;
if (RED.settings.verbose) { node.log(RED._("rpi-gpio.status.closed")); }
if (node.done) {
node.status({fill:"grey",shape:"ring",text:"rpi-gpio.status.closed"});
node.done();
}
else { node.status({fill:"red",shape:"ring",text:"rpi-gpio.status.stopped"}); }
});
node.child.on('error', function (err) {
if (err.errno === "ENOENT") { node.error(RED._("rpi-gpio.errors.commandnotfound")); }
else if (err.errno === "EACCES") { node.error(RED._("rpi-gpio.errors.commandnotexecutable")); }
else { node.error(RED._("rpi-gpio.errors.error")+': ' + err.errno); }
});
}
else {
node.warn(RED._("rpi-gpio.errors.invalidpin")+": "+node.pin);
}
}
else {
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._("rpi-gpio.status.na",{value:msg.payload.toString()})});
});
}
node.on("close", function(done) {
node.status({fill:"grey",shape:"ring",text:"rpi-gpio.status.closed"});
delete pinsInUse[node.pin];
if (node.child != null) {
node.done = done;
node.child.stdin.write("close "+node.pin);
node.child.kill('SIGKILL');
}
else { done(); }
});
}
RED.nodes.registerType("rpi-gpio out",GPIOOutNode);
function PiMouseNode(n) {
RED.nodes.createNode(this,n);
this.butt = n.butt || 7;
var node = this;
if (allOK === true) {
node.child = spawn(gpioCommand+".py", ["mouse",node.butt]);
node.status({fill:"green",shape:"dot",text:"common.status.ok"});
node.child.stdout.on('data', function (data) { node.child.stdout.on('data', function (data) {
if (RED.settings.verbose) { node.log("out: "+data+" :"); } data = Number(data);
if (data !== 0) { node.send({ topic:"pi/mouse", button:data, payload:1 }); }
else { node.send({ topic:"pi/mouse", button:data, payload:0 }); }
}); });
node.child.stderr.on('data', function (data) { node.child.stderr.on('data', function (data) {
@ -192,69 +270,19 @@ module.exports = function(RED) {
else { node.error(RED._("rpi-gpio.errors.error")+': ' + err.errno); } else { node.error(RED._("rpi-gpio.errors.error")+': ' + err.errno); }
}); });
node.on("close", function(done) {
node.status({fill:"grey",shape:"ring",text:"rpi-gpio.status.closed"});
if (node.child != null) {
node.done = done;
node.child.kill('SIGINT');
node.child = null;
}
else { done(); }
});
} }
else { else {
node.warn(RED._("rpi-gpio.errors.invalidpin")+": "+node.pin); node.status({fill:"grey",shape:"dot",text:"node-red:rpi-gpio.status.not-available"});
} }
node.on("close", function(done) {
node.status({fill:"grey",shape:"ring",text:"rpi-gpio.status.closed"});
delete pinsInUse[node.pin];
if (node.child != null) {
node.done = done;
node.child.stdin.write("close "+node.pin);
node.child.kill('SIGKILL');
}
else { done(); }
});
}
RED.nodes.registerType("rpi-gpio out",GPIOOutNode);
function PiMouseNode(n) {
RED.nodes.createNode(this,n);
this.butt = n.butt || 7;
var node = this;
node.child = spawn(gpioCommand+".py", ["mouse",node.butt]);
node.status({fill:"green",shape:"dot",text:"common.status.ok"});
node.child.stdout.on('data', function (data) {
data = Number(data);
if (data === 1) { node.send({ topic:"pi/mouse", button:data, payload:1 }); }
else { node.send({ topic:"pi/mouse", button:data, payload:0 }); }
});
node.child.stderr.on('data', function (data) {
if (RED.settings.verbose) { node.log("err: "+data+" :"); }
});
node.child.on('close', function (code) {
node.child = null;
node.running = false;
if (RED.settings.verbose) { node.log(RED._("rpi-gpio.status.closed")); }
if (node.done) {
node.status({fill:"grey",shape:"ring",text:"rpi-gpio.status.closed"});
node.done();
}
else { node.status({fill:"red",shape:"ring",text:"rpi-gpio.status.stopped"}); }
});
node.child.on('error', function (err) {
if (err.errno === "ENOENT") { node.error(RED._("rpi-gpio.errors.commandnotfound")); }
else if (err.errno === "EACCES") { node.error(RED._("rpi-gpio.errors.commandnotexecutable")); }
else { node.error(RED._("rpi-gpio.errors.error")+': ' + err.errno); }
});
node.on("close", function(done) {
node.status({fill:"grey",shape:"ring",text:"rpi-gpio.status.closed"});
if (node.child != null) {
node.done = done;
node.child.kill('SIGINT');
node.child = null;
}
else { done(); }
});
} }
RED.nodes.registerType("rpi-mouse",PiMouseNode); RED.nodes.registerType("rpi-mouse",PiMouseNode);
@ -262,39 +290,40 @@ module.exports = function(RED) {
RED.nodes.createNode(this,n); RED.nodes.createNode(this,n);
var node = this; var node = this;
node.child = spawn(gpioCommand+".py", ["kbd","0"]); if (allOK === true) {
node.status({fill:"green",shape:"dot",text:"common.status.ok"}); node.child = spawn(gpioCommand+".py", ["kbd","0"]);
node.status({fill:"green",shape:"dot",text:"common.status.ok"});
node.child.stdout.on('data', function (data) { node.child.stdout.on('data', function (data) {
var b = data.toString().trim().split(","); var b = data.toString().trim().split(",");
var act = "up"; var act = "up";
if (b[1] === "1") { act = "down"; } if (b[1] === "1") { act = "down"; }
if (b[1] === "2") { act = "repeat"; } if (b[1] === "2") { act = "repeat"; }
node.send({ topic:"pi/key", payload:Number(b[0]), action:act }); node.send({ topic:"pi/key", payload:Number(b[0]), action:act });
}); });
node.child.stderr.on('data', function (data) { node.child.stderr.on('data', function (data) {
if (RED.settings.verbose) { node.log("err: "+data+" :"); } if (RED.settings.verbose) { node.log("err: "+data+" :"); }
}); });
node.child.on('close', function (code) { node.child.on('close', function (code) {
node.running = false; node.running = false;
node.child = null; node.child = null;
if (RED.settings.verbose) { node.log(RED._("rpi-gpio.status.closed")); } if (RED.settings.verbose) { node.log(RED._("rpi-gpio.status.closed")); }
if (node.done) { if (node.done) {
node.status({fill:"grey",shape:"ring",text:"rpi-gpio.status.closed"}); node.status({fill:"grey",shape:"ring",text:"rpi-gpio.status.closed"});
node.done(); node.done();
} }
else { node.status({fill:"red",shape:"ring",text:"rpi-gpio.status.stopped"}); } else { node.status({fill:"red",shape:"ring",text:"rpi-gpio.status.stopped"}); }
}); });
node.child.on('error', function (err) { node.child.on('error', function (err) {
if (err.errno === "ENOENT") { node.error(RED._("rpi-gpio.errors.commandnotfound")); } if (err.errno === "ENOENT") { node.error(RED._("rpi-gpio.errors.commandnotfound")); }
else if (err.errno === "EACCES") { node.error(RED._("rpi-gpio.errors.commandnotexecutable")); } else if (err.errno === "EACCES") { node.error(RED._("rpi-gpio.errors.commandnotexecutable")); }
else { node.error(RED._("rpi-gpio.errors.error")+': ' + err.errno); } else { node.error(RED._("rpi-gpio.errors.error")+': ' + err.errno); }
}); });
node.on("close", function(done) { node.on("close", function(done) {
node.status({}); node.status({});
if (node.child != null) { if (node.child != null) {
node.done = done; node.done = done;
@ -303,6 +332,10 @@ module.exports = function(RED) {
} }
else { done(); } else { done(); }
}); });
}
else {
node.status({fill:"grey",shape:"dot",text:"node-red:rpi-gpio.status.not-available"});
}
} }
RED.nodes.registerType("rpi-keyboard",PiKeyboardNode); RED.nodes.registerType("rpi-keyboard",PiKeyboardNode);

View File

@ -787,7 +787,7 @@
}, },
"errors": { "errors": {
"ignorenode": "Ignoring Raspberry Pi specific node", "ignorenode": "Ignoring Raspberry Pi specific node",
"version": "Version command failed", "version": "Failed to get version from Pi",
"sawpitype": "Saw Pi Type", "sawpitype": "Saw Pi Type",
"libnotfound": "Cannot find Pi RPi.GPIO python library", "libnotfound": "Cannot find Pi RPi.GPIO python library",
"alreadyset": "GPIO pin __pin__ already set as type: __type__", "alreadyset": "GPIO pin __pin__ already set as type: __type__",