Updated debug node to output other msg properties

This commit is contained in:
Anna Thomas 2014-09-30 15:15:12 +01:00
parent 4f496c37be
commit f96b40cff2
2 changed files with 110 additions and 47 deletions

View File

@ -16,12 +16,16 @@
<script type="text/x-red" data-template-name="debug"> <script type="text/x-red" data-template-name="debug">
<div class="form-row"> <div class="form-row">
<label for="node-input-complete"><i class="fa fa-list"></i> Output</label> <label for="node-input-select-complete"><i class="fa fa-list"></i> Output</label>
<select type="text" id="node-input-complete" style="display: inline-block; width: 250px; vertical-align: top;"> <select type="text" id="node-input-select-complete" style="display: inline-block; width: 250px; vertical-align: top;">
<option value="false">payload only</option> <option value="false">message property</option>
<option value="true">complete msg object</option> <option value="true">complete msg object</option>
</select> </select>
</div> </div>
<div class="form-row" id="node-prop-row">
<label for="node-input-complete">&nbsp;</label>msg.
<input type="text" style="width:205px" id="node-input-complete">
</div>
<div class="form-row"> <div class="form-row">
<label for="node-input-console"><i class="fa fa-random"></i> to</label> <label for="node-input-console"><i class="fa fa-random"></i> to</label>
<select type="text" id="node-input-console" style="display: inline-block; width: 250px; vertical-align: top;"> <select type="text" id="node-input-console" style="display: inline-block; width: 250px; vertical-align: top;">
@ -36,27 +40,57 @@
</script> </script>
<script type="text/x-red" data-help-name="debug"> <script type="text/x-red" data-help-name="debug">
<p>The Debug node can be connected to the output of any node. It will display the timestamp, <b>msg.topic</b> and <b>msg.payload</b> fields of any messages it receives in the debug tab of the sidebar. <p>The Debug node can be connected to the output of any node. It can be used to display the output of any message property in the debug tab of the sidebar. The default is to display <b>msg.payload</b>.</p>
<br/>The sidebar can be accessed under the options drop-down in the top right corner.</p> <p>Each message will also display the timestamp, <b>msg.topic</b> and the property chosen to output.</p>
<p>The sidebar can be accessed under the options drop-down in the top right corner.</p>
<p>The button to the right of the node will toggle it's output on and off so you can de-clutter the debug window.</p> <p>The button to the right of the node will toggle it's output on and off so you can de-clutter the debug window.</p>
<p>If the payload is an object it will be stringified first for display and indicate that by saying "(Object) ".</p> <p>If the payload is an object or buffer it will be stringified first for display and indicate that by saying "(Object)" or "(Buffer)".</p>
<p>If the payload is a buffer it will be stringified first for display and indicate that by saying "(Buffer) ".</p>
<p>Selecting any particular message will highlight (in red) the debug node that reported it. This is useful if you wire up multiple debug nodes.</p> <p>Selecting any particular message will highlight (in red) the debug node that reported it. This is useful if you wire up multiple debug nodes.</p>
<p>Optionally can show the complete msg object - but the screen can get messy.</p> <p>Optionally can show the complete <b>msg</b> object.</p>
<p>In addition any calls to node.warn or node.error will appear here.</p> <p>In addition any calls to node.warn or node.error will appear here.</p>
</script> </script>
<script type="text/javascript"> <script type="text/javascript">
function oneditprepare() {
if (this.complete === "true") {
// show complete message object
$("#node-input-select-complete").val(this.complete);
$("#node-prop-row").hide();
} else {
// show msg.[ ]
var property = (this.complete === "false") ? "payload" : this.complete;
$("#node-input-select-complete").val("false");
$("#node-input-complete").val(property);
$("#node-prop-row").show();
}
$("#node-input-select-complete").change(function() {
var v = $("#node-input-select-complete option:selected").val();
$("#node-input-complete").val(v);
if (v !== "true") {
$("#node-input-complete").val("payload");
$("#node-prop-row").show();
$("#node-input-complete").focus();
} else {
$("#node-prop-row").hide();
}
});
}
RED.nodes.registerType('debug',{ RED.nodes.registerType('debug',{
category: 'output', category: 'output',
defaults: { defaults: {
name: {value:""}, name: {value:""},
active: {value:true}, active: {value:true},
console: {value:"false"}, console: {value:"false"},
complete: {value:"false"} complete: {value:"false", required:true}
}, },
label: function() { label: function() {
return this.name||"debug"; if (this.complete === "true") {
return this.name||"debug msg";
} else {
return this.name||"debug msg." + this.complete;
}
}, },
labelStyle: function() { labelStyle: function() {
return this.name?"node_label_italic":""; return this.name?"node_label_italic":"";
@ -150,12 +184,17 @@
}; };
var name = (o.name?o.name:o.id).toString().replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;"); var name = (o.name?o.name:o.id).toString().replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;");
var topic = (o.topic||"").toString().replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;"); var topic = (o.topic||"").toString().replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;");
var property = (o.property?o.property:'').replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;");
var payload = (o.msg||"").toString().replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;"); var payload = (o.msg||"").toString().replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;");
msg.className = 'debug-message'+(o.level?(' debug-message-level-'+o.level):''); msg.className = 'debug-message'+(o.level?(' debug-message-level-'+o.level):'');
msg.innerHTML = '<span class="debug-message-date">'+getTimestamp()+'</span>'+ msg.innerHTML = '<span class="debug-message-date">'+
getTimestamp()+'</span>'+
'<span class="debug-message-name">['+name+']</span>'+ '<span class="debug-message-name">['+name+']</span>'+
(o.topic?'<span class="debug-message-topic">'+topic+'</span>':'')+ '<span class="debug-message-topic">'+
'<span class="debug-message-payload">'+payload+'</span>'; (o.topic?topic+' : ':'')+
(o.property?'[msg.'+property+']':'[msg]')+
'</span>'+'<span class="debug-message-payload">'+
payload+'</span>';
var atBottom = (sbc.scrollHeight-messages.offsetHeight-sbc.scrollTop) < 5; var atBottom = (sbc.scrollHeight-messages.offsetHeight-sbc.scrollTop) < 5;
messageCount++; messageCount++;
$(messages).append(msg); $(messages).append(msg);
@ -184,7 +223,8 @@
RED.comms.unsubscribe("debug",this.handleDebugMessage); RED.comms.unsubscribe("debug",this.handleDebugMessage);
RED.sidebar.removeTab("debug"); RED.sidebar.removeTab("debug");
delete RED._debug; delete RED._debug;
} },
oneditprepare: oneditprepare
}); });
</script> </script>

View File

@ -26,27 +26,50 @@ module.exports = function(RED) {
this.name = n.name; this.name = n.name;
this.complete = n.complete; this.complete = n.complete;
this.console = n.console; this.console = n.console;
this.active = (n.active == null)||n.active; this.active = (n.active === null || typeof n.active === "undefined") || n.active;
var node = this; var node = this;
this.on("input",function(msg) { this.on("input",function(msg) {
if (this.complete == "true") { // debug complete msg object if (this.complete === "true") {
if (this.console == "true") { // debug complete msg object
if (this.console === "true") {
node.log("\n"+util.inspect(msg, {colors:useColors, depth:10})); node.log("\n"+util.inspect(msg, {colors:useColors, depth:10}));
} }
if (this.active) { if (this.active) {
sendDebug({id:this.id,name:this.name,topic:msg.topic,msg:msg,_path:msg._path}); sendDebug({id:this.id,name:this.name,topic:msg.topic,msg:msg,_path:msg._path});
} }
} else { // debug just the msg.payload } else {
if (this.console == "true") { // debug user defined msg property
if (typeof msg.payload === "string") { var property = "payload";
node.log((msg.payload.indexOf("\n") != -1 ? "\n" : "") + msg.payload); var output = msg[property];
if (this.complete !== "false" && typeof this.complete !== "undefined") {
property = this.complete;
var propertyParts = property.split(".");
try {
output = propertyParts.reduce(function (obj, i) {
return obj[i];
}, msg);
} catch (err) {
node.warn(err);
return;
}
if (!output) {
node.warn("msg." + this.complete + " does not exist");
return;
}
}
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}));
} }
else if (typeof msg.payload === "object") { node.log("\n"+util.inspect(msg.payload, {colors:useColors, depth:10})); }
else { node.log(util.inspect(msg.payload, {colors:useColors})); }
} }
if (this.active) { if (this.active) {
sendDebug({id:this.id,name:this.name,topic:msg.topic,msg:msg.payload,_path:msg._path}); sendDebug({id:this.id,name:this.name,topic:msg.topic,property:property,msg:output,_path:msg._path});
} }
} }
}); });
@ -75,7 +98,7 @@ module.exports = function(RED) {
msg.msg = "(boolean) "+msg.msg.toString(); msg.msg = "(boolean) "+msg.msg.toString();
} else if (msg.msg === 0) { } else if (msg.msg === 0) {
msg.msg = "0"; msg.msg = "0";
} else if (msg.msg == null) { } else if (msg.msg === null || typeof msg.msg === "undefined") {
msg.msg = "(undefined)"; msg.msg = "(undefined)";
} }
@ -88,7 +111,7 @@ module.exports = function(RED) {
DebugNode.logHandler = new events.EventEmitter(); DebugNode.logHandler = new events.EventEmitter();
DebugNode.logHandler.on("log",function(msg) { DebugNode.logHandler.on("log",function(msg) {
if (msg.level == "warn" || msg.level == "error") { if (msg.level === "warn" || msg.level === "error") {
sendDebug(msg); sendDebug(msg);
} }
}); });
@ -97,7 +120,7 @@ module.exports = function(RED) {
RED.httpAdmin.post("/debug/:id/:state", function(req,res) { RED.httpAdmin.post("/debug/:id/:state", function(req,res) {
var node = RED.nodes.getNode(req.params.id); var node = RED.nodes.getNode(req.params.id);
var state = req.params.state; var state = req.params.state;
if (node != null) { if (node !== null && typeof node !== "undefined" ) {
if (state === "enable") { if (state === "enable") {
node.active = true; node.active = true;
res.send(200); res.send(200);
@ -111,4 +134,4 @@ module.exports = function(RED) {
res.send(404); res.send(404);
} }
}); });
} };