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.

BlinkerUltimate.js 2.6KB

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