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

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"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.
@ -26,7 +27,7 @@
<div class="form-row"> <div class="form-row">
<label>&nbsp;</label> <label>&nbsp;</label>
<input type="checkbox" id="node-input-cr" style="display: inline-block; width: auto; vertical-align: top;"> <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>
<div class="form-row"> <div class="form-row">
<label>&nbsp;</label> <label>&nbsp;</label>
@ -36,8 +37,9 @@
<div class="form-row"> <div class="form-row">
<label for="node-input-op"> and</label> <label for="node-input-op"> and</label>
<select id="node-input-op" style='width:70%'> <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="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> <option value="number">try to convert reply to a number</option>
</select> </select>
</div> </div>

View File

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

View File

@ -1,6 +1,6 @@
{ {
"name" : "node-red-node-daemon", "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.", "description" : "A Node-RED node that runs and monitors a long running system command.",
"dependencies" : { "dependencies" : {
}, },