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

120 lines
3.2 KiB
JavaScript
Raw Normal View History

2013-09-05 16:02:48 +02:00
/**
* Copyright 2013 IBM Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
2013-09-13 23:55:16 +02:00
2013-09-05 16:02:48 +02:00
var RED = require("../../red/red");
2013-09-13 23:55:16 +02:00
2013-09-05 16:02:48 +02:00
var util = require("util");
var ws = require('ws');
var events = require("events");
var debuglength = RED.settings.debugMaxLength||1000;
2013-09-05 16:02:48 +02:00
function DebugNode(n) {
RED.nodes.createNode(this,n);
this.name = n.name;
this.complete = n.complete;
this.active = (n.active == null)||n.active;
2013-09-05 16:02:48 +02:00
this.on("input",function(msg) {
if (this.active) {
if (msg.payload instanceof Buffer) {
2013-09-14 00:28:39 +02:00
msg.payload = "(Buffer) "+msg.payload.toString();
2013-09-05 16:02:48 +02:00
}
if (this.complete == "true") {
2013-09-14 00:28:39 +02:00
DebugNode.send({id:this.id,name:this.name,topic:msg.topic,msg:msg,_path:msg._path});
2013-09-05 16:02:48 +02:00
} else {
if (typeof msg.payload !== "undefined") {
DebugNode.send({id:this.id,name:this.name,topic:msg.topic,msg:msg.payload,_path:msg._path});
}
2013-09-05 16:02:48 +02:00
}
}
});
}
RED.nodes.registerType("debug",DebugNode);
DebugNode.send = function(msg) {
if (msg.msg instanceof Error) {
msg.msg = msg.msg.toString();
2013-09-14 00:28:39 +02:00
}
else if (typeof msg.msg === 'object') {
var seen = [];
msg.msg = "(Object) " + JSON.stringify(msg.msg, function(key, value) {
if (typeof value === 'object' && value !== null) {
if (seen.indexOf(value) !== -1) { return "[circular]"; }
seen.push(value);
}
return value;
}," ");
seen = null;
2013-09-14 00:28:39 +02:00
}
else if (typeof msg.msg === "boolean") msg.msg = "(boolean) "+msg.msg.toString();
else if (msg.msg === 0) msg.msg = "0";
2013-09-05 16:02:48 +02:00
if (msg.msg.length > debuglength) {
msg.msg = msg.msg.substr(0,debuglength) +" ....";
}
2013-09-05 16:02:48 +02:00
for (var i in DebugNode.activeConnections) {
var ws = DebugNode.activeConnections[i];
try {
var p = JSON.stringify(msg);
ws.send(p);
} catch(err) {
util.log("[debug] ws error : "+err);
}
}
}
DebugNode.activeConnections = [];
DebugNode.wsServer = new ws.Server({server:RED.server});
DebugNode.wsServer.on('connection',function(ws) {
DebugNode.activeConnections.push(ws);
ws.on('close',function() {
for (var i in DebugNode.activeConnections) {
if (DebugNode.activeConnections[i] === ws) {
DebugNode.activeConnections.splice(i,1);
break;
}
}
});
});
DebugNode.logHandler = new events.EventEmitter();
DebugNode.logHandler.on("log",function(msg) {
if (msg.level == "warn" || msg.level == "error") {
DebugNode.send(msg);
}
});
RED.nodes.addLogHandler(DebugNode.logHandler);
RED.app.post("/debug/:id/:state", function(req,res) {
2013-09-05 16:02:48 +02:00
var node = RED.nodes.getNode(req.params.id);
var state = req.params.state;
2013-09-05 16:02:48 +02:00
if (node != null) {
if (state === "enable") {
node.active = true;
2013-09-14 00:28:39 +02:00
res.send(201);
} else if (state === "disable") {
node.active = false;
2013-09-14 00:28:39 +02:00
res.send(200);
} else {
res.send(404);
}
2013-09-05 16:02:48 +02:00
} else {
res.send(404);
}
});