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.

78 lines
2.1KB

  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.on('input', function (msg) {
  11. if (msg.hasOwnProperty("payload")) {
  12. // 06/11/2019
  13. if (ToBoolean(msg.payload) === true) {
  14. if (node.tBlinker !== null) clearInterval(node.tBlinker);
  15. node.tBlinker = setInterval(handleTimer, node.blinkfrequency); // Start the timer that handles the queue of telegrams
  16. setNodeStatus({ fill: "green", shape: "dot", text: "-> On" });
  17. } else {
  18. if (node.tBlinker !== null) clearInterval(node.tBlinker);
  19. setNodeStatus({ fill: "red", shape: "dot", text: "|| Off" });
  20. node.send({ payload: false });
  21. node.curPayload = false;
  22. }
  23. }
  24. if (msg.hasOwnProperty("interval")) {
  25. try {
  26. node.blinkfrequency = msg.interval;
  27. } catch (error) {
  28. node.blinkfrequency = 500;
  29. setNodeStatus({ fill: "red", shape: "dot", text: "Invalid interval received" });
  30. }
  31. }
  32. });
  33. node.on('close', function () {
  34. if (node.tBlinker !== null) clearInterval(node.tBlinker);
  35. node.send({ payload: false });
  36. });
  37. function setNodeStatus({ fill, shape, text }) {
  38. var dDate = new Date();
  39. node.status({ fill: fill, shape: shape, text: text + " (" + dDate.getDate() + ", " + dDate.toLocaleTimeString() + ")" })
  40. }
  41. function ToBoolean(value) {
  42. var res = false;
  43. if (typeof value === 'boolean') {
  44. res = value;
  45. }
  46. else if (typeof value === 'number' || typeof value === 'string') {
  47. // Is it formated as a decimal number?
  48. if (decimal.test(value)) {
  49. var v = parseFloat(value);
  50. res = v != 0;
  51. }
  52. else {
  53. res = value.toLowerCase() === "true";
  54. }
  55. }
  56. return res;
  57. };
  58. function handleTimer() {
  59. node.curPayload = !node.curPayload;
  60. node.send({ payload: node.curPayload });
  61. }
  62. }
  63. RED.nodes.registerType("BlinkerUltimate", BlinkerUltimate);
  64. }