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

Merge pull request #1732 from node-red/pi-nodes-editable-when-na

let Pi nodes be visible/editable on all platforms
This commit is contained in:
Nick O'Leary 2018-06-13 15:46:21 +01:00 committed by GitHub
commit 56db1da3cf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 293 additions and 169 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 { try {
fs.statSync("/usr/share/doc/python-rpi.gpio"); // test on Raspbian fs.statSync("/usr/share/doc/python-rpi.gpio"); // test on Raspbian
// /usr/lib/python2.7/dist-packages/RPi/GPIO // /usr/lib/python2.7/dist-packages/RPi/GPIO
} catch(err) { } catch(err) {
try { try {
fs.statSync("/usr/lib/python2.7/site-packages/RPi/GPIO"); // test on Arch fs.statSync("/usr/lib/python2.7/site-packages/RPi/GPIO"); // test on Arch
} } 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/dist-packages/RPi/GPIO"); // test on Hypriot
} } catch(err) {
catch(err) { RED.log.warn("rpi-gpio : "+RED._("rpi-gpio.errors.libnotfound"));
RED.log.warn(RED._("rpi-gpio.errors.libnotfound")); allOK = false;
throw "Warning : "+RED._("rpi-gpio.errors.libnotfound");
} }
} }
} }
if ( !(1 & parseInt((fs.statSync(gpioCommand).mode & parseInt("777", 8)).toString(8)[0]) )) { if ( !(1 & parseInt((fs.statSync(gpioCommand).mode & parseInt("777", 8)).toString(8)[0]) )) {
RED.log.error(RED._("rpi-gpio.errors.needtobeexecutable",{command:gpioCommand})); RED.log.warn("rpi-gpio : "+RED._("rpi-gpio.errors.needtobeexecutable",{command:gpioCommand}));
throw "Error : "+RED._("rpi-gpio.errors.mustbeexecutable"); allOK = false;
}
} 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,6 +62,7 @@ module.exports = function(RED) {
} }
} }
if (allOK === true) {
if (node.pin !== undefined) { if (node.pin !== undefined) {
node.child = spawn(gpioCommand, ["in",node.pin,node.intype,node.debounce]); node.child = spawn(gpioCommand, ["in",node.pin,node.intype,node.debounce]);
node.running = true; node.running = true;
@ -104,6 +106,19 @@ module.exports = function(RED) {
else { else {
node.warn(RED._("rpi-gpio.errors.invalidpin")+": "+node.pin); node.warn(RED._("rpi-gpio.errors.invalidpin")+": "+node.pin);
} }
}
else {
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) {
node.status({fill:"grey",shape:"ring",text:"rpi-gpio.status.closed"}); node.status({fill:"grey",shape:"ring",text:"rpi-gpio.status.closed"});
@ -155,6 +170,7 @@ module.exports = function(RED) {
else { node.warn(RED._("rpi-gpio.errors.invalidinput")+": "+out); } else { node.warn(RED._("rpi-gpio.errors.invalidinput")+": "+out); }
} }
if (allOK === true) {
if (node.pin !== undefined) { if (node.pin !== undefined) {
if (node.set && (node.out === "out")) { if (node.set && (node.out === "out")) {
node.child = spawn(gpioCommand, [node.out,node.pin,node.level]); node.child = spawn(gpioCommand, [node.out,node.pin,node.level]);
@ -196,6 +212,13 @@ module.exports = function(RED) {
else { else {
node.warn(RED._("rpi-gpio.errors.invalidpin")+": "+node.pin); 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.on("close", function(done) {
node.status({fill:"grey",shape:"ring",text:"rpi-gpio.status.closed"}); node.status({fill:"grey",shape:"ring",text:"rpi-gpio.status.closed"});
@ -216,12 +239,13 @@ module.exports = function(RED) {
this.butt = n.butt || 7; this.butt = n.butt || 7;
var node = this; var node = this;
if (allOK === true) {
node.child = spawn(gpioCommand+".py", ["mouse",node.butt]); node.child = spawn(gpioCommand+".py", ["mouse",node.butt]);
node.status({fill:"green",shape:"dot",text:"common.status.ok"}); node.status({fill:"green",shape:"dot",text:"common.status.ok"});
node.child.stdout.on('data', function (data) { node.child.stdout.on('data', function (data) {
data = Number(data); data = Number(data);
if (data === 1) { node.send({ topic:"pi/mouse", button:data, payload:1 }); } if (data !== 0) { node.send({ topic:"pi/mouse", button:data, payload:1 }); }
else { node.send({ topic:"pi/mouse", button:data, payload:0 }); } else { node.send({ topic:"pi/mouse", button:data, payload:0 }); }
}); });
@ -256,12 +280,17 @@ 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-mouse",PiMouseNode); RED.nodes.registerType("rpi-mouse",PiMouseNode);
function PiKeyboardNode(n) { function PiKeyboardNode(n) {
RED.nodes.createNode(this,n); RED.nodes.createNode(this,n);
var node = this; var node = this;
if (allOK === true) {
node.child = spawn(gpioCommand+".py", ["kbd","0"]); node.child = spawn(gpioCommand+".py", ["kbd","0"]);
node.status({fill:"green",shape:"dot",text:"common.status.ok"}); node.status({fill:"green",shape:"dot",text:"common.status.ok"});
@ -304,9 +333,14 @@ 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);
var pitype = { type:"" }; var pitype = { type:"" };
if (allOK === true) {
exec(gpioCommand+" info", function(err,stdout,stderr) { exec(gpioCommand+" info", function(err,stdout,stderr) {
if (err) { if (err) {
RED.log.info(RED._("rpi-gpio.errors.version")); RED.log.info(RED._("rpi-gpio.errors.version"));
@ -321,6 +355,7 @@ module.exports = function(RED) {
} }
} }
}); });
}
RED.httpAdmin.get('/rpi-gpio/:id', RED.auth.needsPermission('rpi-gpio.read'), function(req,res) { RED.httpAdmin.get('/rpi-gpio/:id', RED.auth.needsPermission('rpi-gpio.read'), function(req,res) {
res.json(pitype); res.json(pitype);

View File

@ -786,8 +786,8 @@
"na": "N/A : __value__" "na": "N/A : __value__"
}, },
"errors": { "errors": {
"ignorenode": "Ignoring Raspberry Pi specific node", "ignorenode": "Raspberry Pi specific node set inactive",
"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__",

View File

@ -0,0 +1,89 @@
/**
* Copyright JS Foundation and other contributors, http://js.foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
var should = require("should");
var rpi = require("../../../../nodes/core/hardware/36-rpi-gpio.js");
var helper = require("node-red-node-test-helper");
var fs = require("fs");
describe('RPI GPIO Node', function() {
before(function(done) {
helper.startServer(done);
});
after(function(done) {
helper.stopServer(done);
});
afterEach(function() {
helper.unload();
});
var checkIgnore = function(done) {
setTimeout(function() {
try {
var logEvents = helper.log().args.filter(function(evt) {
return ((evt[0].level == 30) && (evt[0].msg.indexOf("rpi-gpio")===0));
});
logEvents[0][0].should.have.a.property('msg');
logEvents[0][0].msg.toString().should.startWith("rpi-gpio : rpi-gpio.errors.ignorenode");
done();
} catch(err) {
done(err);
}
},25);
}
it('should load Input node', function(done) {
var flow = [{id:"n1", type:"rpi-gpio in", name:"rpi-gpio in" }];
helper.load(rpi, flow, function() {
var n1 = helper.getNode("n1");
n1.should.have.property('name', 'rpi-gpio in');
try {
var cpuinfo = fs.readFileSync("/proc/cpuinfo").toString();
if (cpuinfo.indexOf(": BCM") === 1) {
done(); // It's ON a PI ... should really do more tests !
} else {
checkIgnore(done);
}
}
catch(e) {
checkIgnore(done);
}
});
});
it('should load Output node', function(done) {
var flow = [{id:"n1", type:"rpi-gpio out", name:"rpi-gpio out" }];
helper.load(rpi, flow, function() {
var n1 = helper.getNode("n1");
n1.should.have.property('name', 'rpi-gpio out');
try {
var cpuinfo = fs.readFileSync("/proc/cpuinfo").toString();
if (cpuinfo.indexOf(": BCM") === 1) {
done(); // It's ON a PI ... should really do more tests !
} else {
checkIgnore(done);
}
}
catch(e) {
checkIgnore(done);
}
});
});
});