Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

InterruptFlowUltimate.js 2.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. module.exports = function (RED) {
  2. function InterruptFlowUltimate(config) {
  3. RED.nodes.createNode(this, config);
  4. this.config = config;
  5. var node = this;
  6. var decimal = /^\s*[+-]{0,1}\s*([\d]+(\.[\d]*)*)\s*$/
  7. setNodeStatus({ fill: "green", shape: "ring", text: "-> pass" });
  8. node.bInviaMessaggio = true; // Send the message or not
  9. node.currentMsg = {}; // Stores current payload
  10. node.sTriggerTopic = node.config.triggertopic.replace(/[`~!@#$%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/]/gi, '') || "trigger"; // Topic controlling the bInviaMessaggio
  11. this.on('input', function (msg) {
  12. var sIncomingTopic = "";
  13. if (msg.hasOwnProperty("topic")) {
  14. // 06/11/2019
  15. sIncomingTopic = msg.topic.replace(/[`~!@#$%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/]/gi, ''); // Cut unwanted Characters
  16. if (sIncomingTopic == node.sTriggerTopic) {
  17. if (msg.hasOwnProperty("play")) {
  18. node.currentMsg.isReplay = true;
  19. setNodeStatus({ fill: "yellow", shape: "dot", text: "-> replay" });
  20. // Restore previous status
  21. setTimeout(() => {
  22. if (node.bInviaMessaggio) {
  23. setNodeStatus({ fill: "green", shape: "dot", text: "-> pass" });
  24. } else {
  25. setNodeStatus({ fill: "red", shape: "dot", text: "|| stop (stored last msg)" });
  26. }
  27. }, 1000)
  28. node.send(node.currentMsg);
  29. return;
  30. } else if (ToBoolean(msg.payload) === true) {
  31. node.bInviaMessaggio = true;
  32. setNodeStatus({ fill: "green", shape: "dot", text: "-> pass" });
  33. return;
  34. } else if (ToBoolean(msg.payload) === false) {
  35. node.bInviaMessaggio = false;
  36. setNodeStatus({ fill: "red", shape: "dot", text: "|| stop (stored last msg)" });
  37. return;
  38. }
  39. }
  40. }
  41. if (node.bInviaMessaggio) {
  42. node.currentMsg = msg;
  43. node.send(msg);
  44. }
  45. });
  46. function setNodeStatus({ fill, shape, text }) {
  47. var dDate = new Date();
  48. node.status({ fill: fill, shape: shape, text: text + " (" + dDate.getDate() + ", " + dDate.toLocaleTimeString() + ")" })
  49. }
  50. function ToBoolean(value) {
  51. var res = false;
  52. if (typeof value === 'boolean') {
  53. res = value;
  54. }
  55. else if (typeof value === 'number' || typeof value === 'string') {
  56. // Is it formated as a decimal number?
  57. if (decimal.test(value)) {
  58. var v = parseFloat(value);
  59. res = v != 0;
  60. }
  61. else {
  62. res = value.toLowerCase() === "true";
  63. }
  64. }
  65. return res;
  66. };
  67. }
  68. RED.nodes.registerType("InterruptFlowUltimate", InterruptFlowUltimate);
  69. }