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

Allow debug edit expression to be sent to status

This commit is contained in:
Nick O'Leary 2019-01-17 17:15:53 +00:00
parent 5b2f24f842
commit 1359545e13
No known key found for this signature in database
GPG Key ID: 4F2157149161A6C9
2 changed files with 68 additions and 77 deletions

View File

@ -293,16 +293,18 @@
) { ) {
$("#node-input-typed-complete").typedInput('value','payload'); $("#node-input-typed-complete").typedInput('value','payload');
} }
if ($("#node-input-typed-complete").typedInput('type') === 'msg') { if ($("#node-input-typed-complete").typedInput('type') === 'full') {
$("#node-tostatus-line").hide();
} else {
$("#node-tostatus-line").show(); $("#node-tostatus-line").show();
} }
else { $("#node-tostatus-line").hide(); }
}); });
$("#node-input-complete").on('change',function() { $("#node-input-complete").on('change',function() {
if ($("#node-input-typed-complete").typedInput('type') === 'msg') { if ($("#node-input-typed-complete").typedInput('type') === 'full') {
$("#node-tostatus-line").hide();
} else {
$("#node-tostatus-line").show(); $("#node-tostatus-line").show();
} }
else { $("#node-tostatus-line").hide(); }
}); });
}, },
oneditsave: function() { oneditsave: function() {

View File

@ -8,14 +8,14 @@ module.exports = function(RED) {
util.inspect.styles.boolean = "red"; util.inspect.styles.boolean = "red";
function DebugNode(n) { function DebugNode(n) {
var is_edit = (n.targetType === "jsonata"); var hasEditExpression = (n.targetType === "jsonata");
var edit_exp = is_edit ? n.complete : null; var editExpression = hasEditExpression ? n.complete : null;
RED.nodes.createNode(this,n); RED.nodes.createNode(this,n);
this.name = n.name; this.name = n.name;
this.complete = is_edit ? null : (n.complete||"payload").toString(); this.complete = hasEditExpression ? null : (n.complete||"payload").toString();
if (this.complete === "false") { this.complete = "payload"; } if (this.complete === "false") { this.complete = "payload"; }
this.console = ""+(n.console || false); this.console = ""+(n.console || false);
this.tostatus = n.tostatus || false; this.tostatus = (this.complete !== "true") && (n.tostatus || false);
this.tosidebar = n.tosidebar; this.tosidebar = n.tosidebar;
if (this.tosidebar === undefined) { this.tosidebar = true; } if (this.tosidebar === undefined) { this.tosidebar = true; }
this.severity = n.severity || 40; this.severity = n.severity || 40;
@ -44,48 +44,44 @@ module.exports = function(RED) {
"50": "green", "50": "green",
"60": "blue" "60": "blue"
}; };
var edit = null; var preparedEditExpression = null;
if (edit_exp) { if (editExpression) {
try { try {
edit = RED.util.prepareJSONataExpression(edit_exp, this); preparedEditExpression = RED.util.prepareJSONataExpression(editExpression, this);
} }
catch (e) { catch (e) {
node.error(RED._("debug.invalid-exp", {error: edit_exp})); node.error(RED._("debug.invalid-exp", {error: editExpression}));
return; return;
} }
} }
function editValue(exp, val) { function prepareValue(msg, done) {
return new Promise((resolve, reject) => { // Either apply the jsonata expression or...
if (exp) { if (preparedEditExpression) {
RED.util.evaluateJSONataExpression(exp, val, (err, value) => { RED.util.evaluateJSONataExpression(preparedEditExpression, msg, (err, value) => {
if (err) { if (err) {
reject(RED._("debug.invalid-exp", {error: edit_exp})); done(RED._("debug.invalid-exp", {error: editExpression}));
} else { } else {
resolve(value); done(null,{id:node.id, name:node.name, topic:msg.topic, msg:value, _path:msg._path});
} }
}); });
} else {
// Extract the required message property
var property = "payload";
var output = msg[property];
if (node.complete !== "false" && typeof node.complete !== "undefined") {
property = node.complete;
try {
output = RED.util.getMessageProperty(msg,node.complete);
} catch(err) {
output = undefined;
}
} }
else { done(null,{id:node.id, z:node.z, name:node.name, topic:msg.topic, property:property, msg:output, _path:msg._path});
resolve(val); }
}
});
} }
function output_e(msg) { this.on("input", function(msg) {
editValue(edit, msg).then(val => {
if (this.console === "true") {
node.log("\n"+util.inspect(val, {colors:useColors, depth:10}));
}
if (this.active && this.tosidebar) {
sendDebug({id:node.id, name:node.name, topic:val.topic, msg:val, _path:val._path});
}
}).catch(err => {
node.error(err);
});
}
function output(msg) {
if (this.complete === "true") { if (this.complete === "true") {
// debug complete msg object // debug complete msg object
if (this.console === "true") { if (this.console === "true") {
@ -94,43 +90,36 @@ module.exports = function(RED) {
if (this.active && this.tosidebar) { if (this.active && this.tosidebar) {
sendDebug({id:node.id, name:node.name, topic:msg.topic, msg:msg, _path:msg._path}); sendDebug({id:node.id, name:node.name, topic:msg.topic, msg:msg, _path:msg._path});
} }
} else {
prepareValue(msg,function(err,msg) {
if (err) {
node.error(err);
return;
}
var output = msg.msg;
if (node.console === "true") {
if (typeof output === "string") {
node.log((output.indexOf("\n") !== -1 ? "\n" : "") + output);
} else if (typeof output === "object") {
node.log("\n"+util.inspect(output, {colors:useColors, depth:10}));
} else {
node.log(util.inspect(output, {colors:useColors}));
}
}
if (node.tostatus === true) {
var st = (typeof output === 'string')?output:util.inspect(output);
var severity = node.severity;
if (st.length > 32) { st = st.substr(0,32) + "..."; }
node.status({fill:colors[severity], shape:"dot", text:st});
}
if (node.active) {
if (node.tosidebar == true) {
sendDebug(msg);
}
}
});
} }
else { })
// debug user defined msg property
var property = "payload";
var output = msg[property];
if (this.complete !== "false" && typeof this.complete !== "undefined") {
property = this.complete;
try {
output = RED.util.getMessageProperty(msg,this.complete);
} catch(err) {
output = undefined;
}
}
if (this.console === "true") {
if (typeof output === "string") {
node.log((output.indexOf("\n") !== -1 ? "\n" : "") + output);
} else if (typeof output === "object") {
node.log("\n"+util.inspect(output, {colors:useColors, depth:10}));
} else {
node.log(util.inspect(output, {colors:useColors}));
}
}
if (this.tostatus === true) {
var st = util.inspect(output);
var severity = node.severity;
if (st.length > 32) { st = st.substr(0,32) + "..."; }
node.status({fill:colors[severity], shape:"dot", text:st});
}
if (this.active) {
if (this.tosidebar == true) {
sendDebug({id:node.id, z:node.z, name:node.name, topic:msg.topic, property:property, msg:output, _path:msg._path});
}
}
}
}
this.on("input", (edit_exp ? output_e : output));
} }
RED.nodes.registerType("debug",DebugNode, { RED.nodes.registerType("debug",DebugNode, {