You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

75 lines
2.4KB

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