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.

69 lines
1.9KB

  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. }
  22. }
  23. });
  24. node.on('close', function () {
  25. if (node.tBlinker !== null) clearInterval(node.tBlinker);
  26. node.send({ payload: false });
  27. });
  28. function setNodeStatus({ fill, shape, text }) {
  29. var dDate = new Date();
  30. node.status({ fill: fill, shape: shape, text: text + " (" + dDate.getDate() + ", " + dDate.toLocaleTimeString() + ")" })
  31. }
  32. function ToBoolean(value) {
  33. var res = false;
  34. if (typeof value === 'boolean') {
  35. res = value;
  36. }
  37. else if (typeof value === 'number' || typeof value === 'string') {
  38. // Is it formated as a decimal number?
  39. if (decimal.test(value)) {
  40. var v = parseFloat(value);
  41. res = v != 0;
  42. }
  43. else {
  44. res = value.toLowerCase() === "true";
  45. }
  46. }
  47. return res;
  48. };
  49. function handleTimer() {
  50. node.curPayload = !node.curPayload;
  51. node.send({ payload: node.curPayload });
  52. }
  53. }
  54. RED.nodes.registerType("BlinkerUltimate", BlinkerUltimate);
  55. }