Add line output mode for Daemon node

to address #202
This commit is contained in:
Dave Conway-Jones 2016-04-18 21:38:44 +01:00
parent d8cf46a4ec
commit 8563624983
3 changed files with 27 additions and 17 deletions

View File

@ -1,5 +1,6 @@
<!DOCTYPE html>
<!--
Copyright 2014 IBM Corp.
Copyright 2014,2016 IBM Corp.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
@ -26,7 +27,7 @@
<div class="form-row">
<label>&nbsp;</label>
<input type="checkbox" id="node-input-cr" style="display: inline-block; width: auto; vertical-align: top;">
<label for="node-input-cr" style="width: 70%;">Add [enter] to every message ?</label>
<label for="node-input-cr" style="width: 70%;">Add [enter] to every message sent ?</label>
</div>
<div class="form-row">
<label>&nbsp;</label>
@ -36,9 +37,10 @@
<div class="form-row">
<label for="node-input-op"> and</label>
<select id="node-input-op" style='width:70%'>
<option value="string">format reply as a string</option>
<option value="buffer">leave reply as a buffer</option>
<option value="number">try to convert reply to a number</option>
<option value="buffer">leave reply as a buffer</option>
<option value="string">format reply as a string</option>
<option value="lines">output lines of text</option>
<option value="number">try to convert reply to a number</option>
</select>
</div>
<div class="form-row">

View File

@ -46,31 +46,38 @@ module.exports = function(RED) {
if (RED.settings.verbose) { node.log(node.cmd+" "+JSON.stringify(node.args)); }
node.status({fill:"green",shape:"dot",text:"running"});
node.running = true;
var line = "";
node.on("input", inputlistener);
node.child.stdout.on('data', function (data) {
if (node.op == "string") { data = data.toString(); }
if (node.op == "number") { data = Number(data); }
if (node.op === "string") { data = data.toString(); }
if (node.op === "number") { data = Number(data); }
if (RED.settings.verbose) { node.log("out: "+data); }
if (data && data.trim() !== "") {
var msg = {payload:data};
node.send([msg,null,null]);
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];
} else {
if (data && data.trim() !== "") {
node.send([{payload:data},null,null]);
}
}
});
node.child.stderr.on('data', function (data) {
if (node.op == "string") { data = data.toString(); }
if (node.op == "number") { data = Number(data); }
if (node.op === "string") { data = data.toString(); }
if (node.op === "number") { data = Number(data); }
if (RED.settings.verbose) { node.log("err: "+data); }
var msg = {payload:data};
node.send([null,msg,null]);
node.send([null,{payload:data},null]);
});
node.child.on('close', function (code) {
if (RED.settings.verbose) { node.log("ret: "+code); }
var msg = {payload:code};
node.send([null,null,msg]);
node.send([null,null,{payload:code}]);
node.child = null;
node.running = false;
node.status({fill:"red",shape:"ring",text:"stopped"});
@ -98,6 +105,7 @@ module.exports = function(RED) {
if (node.child != null) { node.child.kill('SIGKILL'); }
if (RED.settings.verbose) { node.log(node.cmd+" stopped"); }
clearInterval(loop);
node.status({});
});
runit();

View File

@ -1,6 +1,6 @@
{
"name" : "node-red-node-daemon",
"version" : "0.0.4",
"version" : "0.0.5",
"description" : "A Node-RED node that runs and monitors a long running system command.",
"dependencies" : {
},