2014-12-24 22:51:17 +01:00
|
|
|
|
|
|
|
module.exports = function(RED) {
|
|
|
|
"use strict";
|
|
|
|
var spawn = require('child_process').spawn;
|
|
|
|
|
|
|
|
function DaemonNode(n) {
|
|
|
|
RED.nodes.createNode(this,n);
|
|
|
|
this.cmd = n.command;
|
2016-08-16 11:12:31 +02:00
|
|
|
this.args = n.args.trim().split(" ") || [];
|
2014-12-24 22:51:17 +01:00
|
|
|
this.cr = n.cr;
|
|
|
|
this.op = n.op;
|
|
|
|
this.redo = n.redo;
|
|
|
|
this.running = false;
|
|
|
|
var node = this;
|
|
|
|
|
|
|
|
function inputlistener(msg) {
|
|
|
|
if (msg != null) {
|
|
|
|
if (!Buffer.isBuffer(msg.payload)) {
|
|
|
|
if (typeof msg.payload === "object") { msg.payload = JSON.stringify(msg.payload); }
|
|
|
|
if (typeof msg.payload !== "string") { msg.payload = msg.payload.toString(); }
|
|
|
|
if (node.cr === true) { msg.payload += "\n"; }
|
|
|
|
}
|
|
|
|
if (RED.settings.verbose) { node.log("inp: "+msg.payload); }
|
|
|
|
if (node.child !== null) { node.child.stdin.write(msg.payload); }
|
|
|
|
else { node.warn("Command not running"); }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function runit() {
|
|
|
|
node.child = spawn(node.cmd, node.args);
|
|
|
|
if (RED.settings.verbose) { node.log(node.cmd+" "+JSON.stringify(node.args)); }
|
|
|
|
node.status({fill:"green",shape:"dot",text:"running"});
|
|
|
|
node.running = true;
|
2016-04-18 22:38:44 +02:00
|
|
|
var line = "";
|
2014-12-24 22:51:17 +01:00
|
|
|
|
|
|
|
node.on("input", inputlistener);
|
|
|
|
|
|
|
|
node.child.stdout.on('data', function (data) {
|
2016-04-18 22:38:44 +02:00
|
|
|
if (node.op === "string") { data = data.toString(); }
|
|
|
|
if (node.op === "number") { data = Number(data); }
|
2014-12-24 22:51:17 +01:00
|
|
|
if (RED.settings.verbose) { node.log("out: "+data); }
|
2016-04-18 22:38:44 +02:00
|
|
|
if (node.op === "lines") {
|
|
|
|
line += data.toString();
|
|
|
|
var bits = line.split("\n");
|
|
|
|
while (bits.length > 1) {
|
|
|
|
node.send([{payload:bits.shift()},null,null]);
|
|
|
|
}
|
|
|
|
line = bits[0];
|
2017-01-29 18:46:50 +01:00
|
|
|
}
|
|
|
|
else {
|
2016-10-17 22:35:12 +02:00
|
|
|
if (data && (data.length !== 0)) {
|
2016-04-18 22:38:44 +02:00
|
|
|
node.send([{payload:data},null,null]);
|
|
|
|
}
|
2014-12-24 22:51:17 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
node.child.stderr.on('data', function (data) {
|
2016-04-18 22:38:44 +02:00
|
|
|
if (node.op === "string") { data = data.toString(); }
|
|
|
|
if (node.op === "number") { data = Number(data); }
|
2014-12-24 22:51:17 +01:00
|
|
|
if (RED.settings.verbose) { node.log("err: "+data); }
|
2016-04-18 22:38:44 +02:00
|
|
|
node.send([null,{payload:data},null]);
|
2014-12-24 22:51:17 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
node.child.on('close', function (code) {
|
|
|
|
if (RED.settings.verbose) { node.log("ret: "+code); }
|
2016-04-18 22:38:44 +02:00
|
|
|
node.send([null,null,{payload:code}]);
|
2014-12-24 22:51:17 +01:00
|
|
|
node.child = null;
|
|
|
|
node.running = false;
|
|
|
|
node.status({fill:"red",shape:"ring",text:"stopped"});
|
|
|
|
});
|
|
|
|
|
|
|
|
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); }
|
|
|
|
node.status({fill:"grey",shape:"dot",text:"error"});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (node.redo === true) {
|
|
|
|
var loop = setInterval( function() {
|
|
|
|
if (!node.running) {
|
|
|
|
node.removeListener('input', inputlistener);
|
|
|
|
node.warn("Restarting : " + node.cmd);
|
|
|
|
runit();
|
|
|
|
}
|
|
|
|
}, 10000); // Restart after 10 secs if required
|
|
|
|
}
|
|
|
|
|
2017-01-02 00:34:23 +01:00
|
|
|
node.on("close", function(done) {
|
|
|
|
clearInterval(loop);
|
2014-12-24 22:51:17 +01:00
|
|
|
if (node.child != null) { node.child.kill('SIGKILL'); }
|
|
|
|
if (RED.settings.verbose) { node.log(node.cmd+" stopped"); }
|
2016-04-18 22:38:44 +02:00
|
|
|
node.status({});
|
2017-01-02 00:34:23 +01:00
|
|
|
setTimeout(function() { done(); }, 100);
|
2014-12-24 22:51:17 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
runit();
|
|
|
|
}
|
|
|
|
RED.nodes.registerType("daemon",DaemonNode);
|
|
|
|
}
|