mirror of
https://github.com/node-red/node-red.git
synced 2023-10-10 13:36:53 +02:00
parent
0b8e8de260
commit
069f42f0c5
@ -17,9 +17,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="icon-list"></i> Output</label>
|
<label for="node-input-complete"><i class="icon-list"></i> Output</label>
|
||||||
<select type="text" id="node-input-complete" style="display: inline-block; width: auto; vertical-align: top;">
|
<select type="text" id="node-input-complete" style="display: inline-block; width: 250px; vertical-align: top;">
|
||||||
<option value=false>Payload only</option>
|
<option value=false>payload only</option>
|
||||||
<option value=true>Complete msg object</option>
|
<option value=true>complete msg object</option>
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
<div class="form-row">
|
||||||
|
<label for="node-input-console"><i class="icon-random"></i> to</label>
|
||||||
|
<select type="text" id="node-input-console" style="display: inline-block; width: 250px; vertical-align: top;">
|
||||||
|
<option value=false>debug tab</option>
|
||||||
|
<option value=true>debug tab and console</option>
|
||||||
</select>
|
</select>
|
||||||
</div>
|
</div>
|
||||||
<div class="form-row">
|
<div class="form-row">
|
||||||
@ -45,6 +52,7 @@
|
|||||||
defaults: {
|
defaults: {
|
||||||
name: {value:""},
|
name: {value:""},
|
||||||
active: {value:true},
|
active: {value:true},
|
||||||
|
console: {value:false},
|
||||||
complete: {value:false}
|
complete: {value:false}
|
||||||
},
|
},
|
||||||
label: function() {
|
label: function() {
|
||||||
|
@ -15,67 +15,82 @@
|
|||||||
**/
|
**/
|
||||||
|
|
||||||
var RED = require(process.env.NODE_RED_HOME+"/red/red");
|
var RED = require(process.env.NODE_RED_HOME+"/red/red");
|
||||||
|
|
||||||
var util = require("util");
|
var util = require("util");
|
||||||
var ws = require('ws');
|
var ws = require('ws');
|
||||||
var events = require("events");
|
var events = require("events");
|
||||||
var debuglength = RED.settings.debugMaxLength||1000;
|
var debuglength = RED.settings.debugMaxLength||1000;
|
||||||
|
var useColors = false;
|
||||||
|
|
||||||
|
util.inspect.styles.boolean = "red";
|
||||||
|
|
||||||
function DebugNode(n) {
|
function DebugNode(n) {
|
||||||
RED.nodes.createNode(this,n);
|
RED.nodes.createNode(this,n);
|
||||||
this.name = n.name;
|
this.name = n.name;
|
||||||
this.complete = n.complete;
|
this.complete = n.complete;
|
||||||
this.active = (n.active == null)||n.active;
|
this.console = n.console;
|
||||||
|
this.active = (n.active == null)||n.active;
|
||||||
|
var node = this;
|
||||||
|
|
||||||
this.on("input",function(msg) {
|
this.on("input",function(msg) {
|
||||||
if (this.active) {
|
if (this.complete == "true") { // debug complete msg object
|
||||||
if (msg.payload instanceof Buffer) {
|
if (this.console == "true") {
|
||||||
msg.payload = "(Buffer) "+msg.payload.toString();
|
node.log("\n"+util.inspect(msg, {colors:useColors, depth:10}));
|
||||||
}
|
}
|
||||||
if (this.complete == "true") {
|
if (msg.payload instanceof Buffer) { msg.payload = "(Buffer) "+msg.payload.toString('hex'); }
|
||||||
DebugNode.send({id:this.id,name:this.name,topic:msg.topic,msg:msg,_path:msg._path});
|
if (this.active) {
|
||||||
} else {
|
DebugNode.send({id:this.id,name:this.name,topic:msg.topic,msg:msg,_path:msg._path});
|
||||||
if (typeof msg.payload !== "undefined") {
|
}
|
||||||
DebugNode.send({id:this.id,name:this.name,topic:msg.topic,msg:msg.payload,_path:msg._path});
|
} else { // debug just the msg.payload
|
||||||
}
|
if (this.console == "true") {
|
||||||
}
|
if (typeof msg.payload === "string") {
|
||||||
}
|
if (msg.payload.indexOf("\n") != -1) { msg.payload = "\n"+msg.payload; }
|
||||||
});
|
node.log(msg.payload);
|
||||||
|
}
|
||||||
|
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 (typeof msg.payload === "undefined") { msg.payload = "(undefined)"; }
|
||||||
|
if (msg.payload instanceof Buffer) { msg.payload = "(Buffer) "+msg.payload.toString('hex'); }
|
||||||
|
if (this.active) {
|
||||||
|
DebugNode.send({id:this.id,name:this.name,topic:msg.topic,msg:msg.payload,_path:msg._path});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
RED.nodes.registerType("debug",DebugNode);
|
RED.nodes.registerType("debug",DebugNode);
|
||||||
|
|
||||||
DebugNode.send = function(msg) {
|
DebugNode.send = function(msg) {
|
||||||
if (msg.msg instanceof Error) {
|
if (msg.msg instanceof Error) {
|
||||||
msg.msg = msg.msg.toString();
|
msg.msg = msg.msg.toString();
|
||||||
}
|
}
|
||||||
else if (typeof msg.msg === 'object') {
|
else if (typeof msg.msg === 'object') {
|
||||||
var seen = [];
|
var seen = [];
|
||||||
msg.msg = "(Object) " + JSON.stringify(msg.msg, function(key, value) {
|
msg.msg = "(Object) " + JSON.stringify(msg.msg, function(key, value) {
|
||||||
if (typeof value === 'object' && value !== null) {
|
if (typeof value === 'object' && value !== null) {
|
||||||
if (seen.indexOf(value) !== -1) { return "[circular]"; }
|
if (seen.indexOf(value) !== -1) { return "[circular]"; }
|
||||||
seen.push(value);
|
seen.push(value);
|
||||||
}
|
}
|
||||||
return value;
|
return value;
|
||||||
}," ");
|
}," ");
|
||||||
seen = null;
|
seen = null;
|
||||||
}
|
}
|
||||||
else if (typeof msg.msg === "boolean") msg.msg = "(boolean) "+msg.msg.toString();
|
else if (typeof msg.msg === "boolean") msg.msg = "(boolean) "+msg.msg.toString();
|
||||||
else if (msg.msg === 0) msg.msg = "0";
|
else if (msg.msg === 0) msg.msg = "0";
|
||||||
|
|
||||||
if (msg.msg.length > debuglength) {
|
if (msg.msg.length > debuglength) {
|
||||||
msg.msg = msg.msg.substr(0,debuglength) +" ....";
|
msg.msg = msg.msg.substr(0,debuglength) +" ....";
|
||||||
}
|
}
|
||||||
|
|
||||||
for (var i in DebugNode.activeConnections) {
|
for (var i in DebugNode.activeConnections) {
|
||||||
var ws = DebugNode.activeConnections[i];
|
var ws = DebugNode.activeConnections[i];
|
||||||
try {
|
try {
|
||||||
var p = JSON.stringify(msg);
|
var p = JSON.stringify(msg);
|
||||||
ws.send(p);
|
ws.send(p);
|
||||||
} catch(err) {
|
} catch(err) {
|
||||||
util.log("[debug] ws error : "+err);
|
util.log("[debug] ws error : "+err);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
DebugNode.activeConnections = [];
|
DebugNode.activeConnections = [];
|
||||||
@ -85,18 +100,18 @@ path = path + (path.slice(-1) == "/" ? "":"/") + "debug/ws";
|
|||||||
|
|
||||||
DebugNode.wsServer = new ws.Server({server:RED.server,path:path});
|
DebugNode.wsServer = new ws.Server({server:RED.server,path:path});
|
||||||
DebugNode.wsServer.on('connection',function(ws) {
|
DebugNode.wsServer.on('connection',function(ws) {
|
||||||
DebugNode.activeConnections.push(ws);
|
DebugNode.activeConnections.push(ws);
|
||||||
ws.on('close',function() {
|
ws.on('close',function() {
|
||||||
for (var i in DebugNode.activeConnections) {
|
for (var i in DebugNode.activeConnections) {
|
||||||
if (DebugNode.activeConnections[i] === ws) {
|
if (DebugNode.activeConnections[i] === ws) {
|
||||||
DebugNode.activeConnections.splice(i,1);
|
DebugNode.activeConnections.splice(i,1);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
ws.on('error', function(err) {
|
ws.on('error', function(err) {
|
||||||
util.log("[debug] ws error : "+err);
|
util.log("[debug] ws error : "+err);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
DebugNode.wsServer.on('error', function(err) {
|
DebugNode.wsServer.on('error', function(err) {
|
||||||
@ -105,26 +120,26 @@ DebugNode.wsServer.on('error', function(err) {
|
|||||||
|
|
||||||
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") {
|
||||||
DebugNode.send(msg);
|
DebugNode.send(msg);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
RED.nodes.addLogHandler(DebugNode.logHandler);
|
RED.nodes.addLogHandler(DebugNode.logHandler);
|
||||||
|
|
||||||
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) {
|
||||||
if (state === "enable") {
|
if (state === "enable") {
|
||||||
node.active = true;
|
node.active = true;
|
||||||
res.send(200);
|
res.send(200);
|
||||||
} else if (state === "disable") {
|
} else if (state === "disable") {
|
||||||
node.active = false;
|
node.active = false;
|
||||||
res.send(201);
|
res.send(201);
|
||||||
} else {
|
} else {
|
||||||
res.send(404);
|
res.send(404);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
res.send(404);
|
res.send(404);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user