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.

87 line
2.6KB

  1. module.exports = function (RED) {
  2. function BlinkerUltimate(config) {
  3. RED.nodes.createNode(this, config);
  4. this.config = config;
  5. var node = this;
  6. setNodeStatus({ fill: "grey", shape: "ring", text: "|| Off" });
  7. node.tBlinker = null;// Timer Blinker
  8. node.blinkfrequency = typeof config.blinkfrequency === "undefined" ? 500 : config.blinkfrequency;
  9. node.curPayload = false;
  10. node.isBlinking = false; // Is the timer running?
  11. node.on('input', function (msg) {
  12. if (msg.hasOwnProperty("interval")) {
  13. try {
  14. node.blinkfrequency = msg.interval;
  15. if (node.isBlinking) // 29/05/2020 If was blinking, restart the timer with the new interval
  16. {
  17. if (node.tBlinker !== null) clearInterval(node.tBlinker);
  18. node.tBlinker = setInterval(handleTimer, node.blinkfrequency); // Start the timer that handles the queue of telegrams
  19. }
  20. } catch (error) {
  21. node.blinkfrequency = 500;
  22. setNodeStatus({ fill: "red", shape: "dot", text: "Invalid interval received" });
  23. }
  24. }
  25. if (msg.hasOwnProperty("payload")) {
  26. // 06/11/2019
  27. if (ToBoolean(msg.payload) === true) {
  28. if (node.tBlinker !== null) clearInterval(node.tBlinker);
  29. node.tBlinker = setInterval(handleTimer, node.blinkfrequency); // Start the timer that handles the queue of telegrams
  30. node.isBlinking = true;
  31. setNodeStatus({ fill: "green", shape: "dot", text: "-> On" });
  32. } else {
  33. if (node.tBlinker !== null) clearInterval(node.tBlinker);
  34. node.isBlinking = false;
  35. setNodeStatus({ fill: "red", shape: "dot", text: "|| Off" });
  36. node.send({ payload: false });
  37. node.curPayload = false;
  38. }
  39. }
  40. });
  41. node.on('close', function () {
  42. if (node.tBlinker !== null) clearInterval(node.tBlinker);
  43. node.isBlinking = false;
  44. node.send({ payload: false });
  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. function handleTimer() {
  68. node.curPayload = !node.curPayload;
  69. node.send({ payload: node.curPayload });
  70. }
  71. }
  72. RED.nodes.registerType("BlinkerUltimate", BlinkerUltimate);
  73. }