Node-Red configuration
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

loggerNode.js 2.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. const Logger = require("./logger");
  2. const logger = new Logger("logger Node");
  3. logger.sendInfo("Copyright 2020 Jaroslav Peter Prib");
  4. if(logger.active) logger.send("test active");
  5. const adminActions=["setOn","setOff"];
  6. const MaxMsgOutSize=1000;
  7. let maxReportErrors=10;
  8. module.exports = function (RED) {
  9. function loggerNode (n) {
  10. RED.nodes.createNode(this, n);
  11. var node = Object.assign(this, n);
  12. if(node.active) logger.send({label:"initialising",parameters:n});
  13. node.logger=new Logger({
  14. label:"logger Node:"+(node.name||node.id),
  15. count:node.count
  16. });
  17. node.logger.setNodeStatus(node);
  18. node.on('input', function (msg) {
  19. if(node.logger.active) try {
  20. let processed=[];
  21. const msgString=JSON.stringify(msg,(k,v)=>{
  22. if (typeof v === 'object') {
  23. const i=processed.findIndex((c)=>c===v)
  24. if(i>-1) return "<***circular reference***>";
  25. processed.push(v);
  26. }
  27. return v;
  28. })
  29. node.logger.send({label:"input",message:msgString});
  30. if(node.sendOutputLog)
  31. node.send([null,msg]);
  32. if(node.sendConsoleLog)
  33. node.log(msg);
  34. } catch(ex) {
  35. if(maxReportErrors) {
  36. maxReportErrors--;
  37. node.logger.send({label:"input error",error:ex.error,stack:ex.stack});
  38. }
  39. }
  40. node.send(msg);
  41. });
  42. node.on('close', function (removed, done) {
  43. node.connection.close(false, () => {
  44. if(node.logger.active) node.logger.send({label:"close"});
  45. });
  46. done();
  47. });
  48. }
  49. RED.nodes.registerType('Logger', loggerNode);
  50. RED.httpAdmin.get('/Logger/:id/:action/', RED.auth.needsPermission('Logger.write'), function (req, res) {
  51. if(logger.active) logger.send({label:"httpAdmin",request:{params:req.params}});
  52. const node = RED.nodes.getNode(req.params.id)
  53. if (node && node.type === 'Logger') {
  54. try {
  55. if (adminActions.includes(req.params.action)) {
  56. node.logger[req.params.action]();
  57. return;
  58. }
  59. throw Error('unknown action: ' + req.params.action)
  60. } catch (err) {
  61. const reason1 = 'Internal Server Error, ' + req.params.action + ' failed ' + err.toString()
  62. if(node.logger.active) node.logger.send({label:"httpAdmin", request:req.params,error:reason1});
  63. res.status(500).send(reason1);
  64. }
  65. } else {
  66. const reason2 = 'request to ' + req.params.action + ' failed for id:' + req.params.id;
  67. if(logger.active) logger.send({label:"httpAdmin",error:reason2});
  68. res.status(404).send(reason2);
  69. }
  70. });
  71. };