2015-11-01 15:59:21 +01:00
|
|
|
|
|
|
|
module.exports = function(RED) {
|
|
|
|
"use strict";
|
|
|
|
var util = require("util");
|
|
|
|
var spawn = require('child_process').spawn;
|
|
|
|
var fs = require('fs');
|
|
|
|
|
2021-06-18 09:56:15 +02:00
|
|
|
var gpioCommand = __dirname + '/nrsrf';
|
2018-04-15 12:24:56 +02:00
|
|
|
var allOK = true;
|
2015-11-01 15:59:21 +01:00
|
|
|
|
2018-04-15 12:24:56 +02:00
|
|
|
try {
|
|
|
|
var cpuinfo = fs.readFileSync("/proc/cpuinfo").toString();
|
|
|
|
if (cpuinfo.indexOf(": BCM") === -1) {
|
|
|
|
RED.log.warn("rpi-srf : "+RED._("node-red:rpi-gpio.errors.ignorenode"));
|
|
|
|
allOK = false;
|
|
|
|
}
|
|
|
|
else if (!fs.existsSync("/usr/share/doc/python-rpi.gpio")) {
|
|
|
|
RED.log.warn("rpi-srf : "+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-srf : "+RED._("node-red:rpi-gpio.errors.needtobeexecutable",{command:gpioCommand}));
|
|
|
|
allOK = false;
|
|
|
|
}
|
2015-11-01 15:59:21 +01:00
|
|
|
}
|
2018-04-15 12:24:56 +02:00
|
|
|
catch(err) {
|
|
|
|
RED.log.warn("rpi-srf : "+RED._("node-red:rpi-gpio.errors.ignorenode"));
|
|
|
|
allOK = false;
|
2015-11-01 15:59:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function PiSrfNode(n) {
|
|
|
|
RED.nodes.createNode(this, n);
|
2017-05-13 13:46:24 +02:00
|
|
|
this.topic = n.topic;
|
2015-11-01 15:59:21 +01:00
|
|
|
this.pins = n.pins;
|
2017-08-14 10:20:18 +02:00
|
|
|
this.pins += ","+(n.pulse || 0.5);
|
2015-11-01 15:59:21 +01:00
|
|
|
var node = this;
|
|
|
|
|
2018-04-15 12:24:56 +02:00
|
|
|
if (allOK === true) {
|
|
|
|
if (node.pins !== undefined) {
|
|
|
|
node.child = spawn(gpioCommand, [node.pins]);
|
|
|
|
node.running = true;
|
|
|
|
if (RED.settings.verbose) { node.log("parameters: " + node.pins + " :"); }
|
2015-11-01 15:59:21 +01:00
|
|
|
|
2018-04-15 12:24:56 +02:00
|
|
|
node.child.stdout.on('data', function(data) {
|
|
|
|
if (RED.settings.verbose) { node.log("out: " + data + " :"); }
|
|
|
|
data = data.toString().trim();
|
|
|
|
if (data.length > 0) {
|
|
|
|
node.send({topic:node.topic, payload:data});
|
|
|
|
}
|
|
|
|
});
|
2015-11-01 15:59:21 +01:00
|
|
|
|
2018-04-15 12:24:56 +02:00
|
|
|
node.child.stderr.on('data', function(data) {
|
|
|
|
if (RED.settings.verbose) { node.log("err: " + data + " :"); }
|
|
|
|
});
|
2015-11-01 15:59:21 +01:00
|
|
|
|
2018-04-15 12:24:56 +02:00
|
|
|
node.child.on('close', function(code) {
|
|
|
|
if (RED.settings.verbose) { node.log("ret: " + code + " :"); }
|
|
|
|
node.child = null;
|
|
|
|
node.running = false;
|
|
|
|
});
|
2015-11-01 15:59:21 +01:00
|
|
|
|
2018-04-15 12:24:56 +02:00
|
|
|
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); }
|
|
|
|
});
|
2015-11-01 15:59:21 +01:00
|
|
|
|
|
|
|
}
|
2018-04-15 12:24:56 +02:00
|
|
|
else {
|
|
|
|
node.error("Invalid Parameters: " + node.pins);
|
|
|
|
}
|
2015-11-01 15:59:21 +01:00
|
|
|
|
2018-04-15 12:24:56 +02:00
|
|
|
var wfi = function(done) {
|
|
|
|
if (!node.running) {
|
|
|
|
if (RED.settings.verbose) { node.log("end"); }
|
|
|
|
done();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
setTimeout(function() { wfi(done); }, 333);
|
2015-11-01 15:59:21 +01:00
|
|
|
}
|
|
|
|
|
2018-04-15 12:24:56 +02:00
|
|
|
node.on("close", function(done) {
|
|
|
|
if (node.child != null) {
|
|
|
|
node.child.kill('SIGKILL');
|
|
|
|
}
|
|
|
|
wfi(done);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
node.status({fill:"grey",shape:"dot",text:"node-red:rpi-gpio.status.not-available"});
|
|
|
|
}
|
2015-11-01 15:59:21 +01:00
|
|
|
}
|
|
|
|
RED.nodes.registerType("rpi-srf", PiSrfNode);
|
|
|
|
}
|