From b4d4c5b044816d8076305171e1c5da3aaff9d165 Mon Sep 17 00:00:00 2001 From: Alan Reader Date: Sun, 2 Sep 2018 14:24:33 +1000 Subject: [PATCH 1/3] Adding a pause feature --- mytimeout.js | 79 +++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 63 insertions(+), 16 deletions(-) diff --git a/mytimeout.js b/mytimeout.js index ef3d8ce..b947e53 100644 --- a/mytimeout.js +++ b/mytimeout.js @@ -21,7 +21,7 @@ // } // // { -// "payload": +// "payload": // } // // The above gets treated as an on condition @@ -67,14 +67,16 @@ module.exports = function(RED) { var countdownNode = function(n) { // Local variables var node = this; - + var state = 'stop'; var wflag = false; var ticks = -1; // - var lastPayload = Date.now();; // + var lastPayload = Date.now();; // + var pauseValue = null; + var tick = null; - var timeout = parseInt(n.timer||30); // - var warn = parseInt(n.warning||10); // + var timeout = parseInt(n.timer||30); // + var warn = parseInt(n.warning||10); // var ignoreCase = ''; @@ -194,7 +196,11 @@ module.exports = function(RED) { node.send([msg, null]); state = 'run'; - + if (tick !== null) { + clearInterval(tick); + tick = null; + } + startInterval(); wflag = false; // rest the warning flag } // on(msg) @@ -263,7 +269,10 @@ module.exports = function(RED) { node.send([null, tremain]); break; } - + if (tick !== null) { + clearInterval(tick); + tick = null; + } state = 'stop'; ticks = -1; timeout = parseInt(node.timer); @@ -276,6 +285,41 @@ module.exports = function(RED) { ndebug("cancel!"); stop('cancel'); ticks = -1; + pauseValue = null; + } + + function pause() { + ndebug("pause function!"); + pauseValue = [ticks, node.warnT]; + + timeout = ticks; + var msg = line; + node.status({ + fill : "red", + shape : "dot", + text : `Paused: ${ticks}` // provide a visual countdown + }); + + msg.payload = "pause"; + lastPayload = msg.payload; + + var tremain = { "payload": ticks, "state": "paused", "flag": "pause"}; + node.send([msg, tremain]); + + state = 'pause'; + if (tick !== null) { + clearInterval(tick); + tick = null; + } + } + + function unpause() { + ndebug("unpause!"); + var pausemsg = { + timeout: pauseValue[0], + warning: pauseValue[1] + } + on(pausemsg); } function doNothing() { @@ -381,8 +425,9 @@ module.exports = function(RED) { // Leave this here, need the functions ref from above var states = { // Not sure if this is what I want in the long run but this is good for now - stop: { 0: off, on: on, off: off, stop: doNothing, cancel: doNothing }, - run: { 0: off, on: on, off: off, stop: stop, cancel: cancel } + stop: { 0: off, on: on, pause: off, off: off, stop: doNothing, cancel: doNothing }, + pause: { 0: off, on: unpause, pause: pause, off: off, stop: stop, cancel: cancel }, + run: { 0: off, on: on, pause: pause, off: off, stop: stop, cancel: cancel } }; // ------------------------------------------------------------------------------- @@ -443,6 +488,7 @@ module.exports = function(RED) { // Stop (initial state at start up and when not running) // On (timer reset to default value and running, on sent) + // Pause (timer retains value, return to Stop) // Off (timer off, off sent, return to Stop) // CANCEL (timer off, nothing sent, return to Stop) // Warning (timer still on, warning sent) @@ -481,7 +527,7 @@ module.exports = function(RED) { // then drop it // If the timer goes off then the lastPayload should be cleared - // We only send a simple message ('on', 'off', 'stop' or 'cancel') + // We only send a simple message ('on', 'pause', 'off', 'stop' or 'cancel') var regex = new RegExp('^' + lastPayload + '$', ignoreCase); //if(lastPayload === inMsg.payload) { if(regex.test(inMsg.payload)) { @@ -530,13 +576,14 @@ module.exports = function(RED) { // Once the node is instantiated this keeps running // I'd like to change this to run when only needed - var tick = setInterval(function() { - var msg = { payload:'TIX', topic:""}; - node.emit("TIX", msg); - }, 1000); // trigger every 1 sec - + function startInterval() { + tick = setInterval(function() { + var msg = { payload:'TIX', topic:""}; + node.emit("TIX", msg); + }, 1000); // trigger every 1 sec + } node.on("close", function() { - if (tick) { + if (tick !== null) { clearInterval(tick); } }); From 48dc79bdca1a07b3b9b5df05d9eaa5c939e039f6 Mon Sep 17 00:00:00 2001 From: Alan Reader Date: Sun, 2 Sep 2018 14:25:54 +1000 Subject: [PATCH 2/3] sending message on function start, not waiting 1 second before decrementing ticks --- mytimeout.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/mytimeout.js b/mytimeout.js index b947e53..0a74ab8 100644 --- a/mytimeout.js +++ b/mytimeout.js @@ -577,8 +577,9 @@ module.exports = function(RED) { // Once the node is instantiated this keeps running // I'd like to change this to run when only needed function startInterval() { + var msg = { payload:'TIX', topic:""}; + node.emit("TIX", msg); tick = setInterval(function() { - var msg = { payload:'TIX', topic:""}; node.emit("TIX", msg); }, 1000); // trigger every 1 sec } From 4fd14887befdaaa857691d2954c25fb74176062c Mon Sep 17 00:00:00 2001 From: Alan Reader Date: Mon, 3 Sep 2018 11:14:59 +1000 Subject: [PATCH 3/3] adding suspend, restoring original 'on' functionality --- mytimeout.js | 38 ++++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/mytimeout.js b/mytimeout.js index 0a74ab8..2b970ef 100644 --- a/mytimeout.js +++ b/mytimeout.js @@ -288,14 +288,15 @@ module.exports = function(RED) { pauseValue = null; } - function pause() { + function pause(susp) { + var suspend = susp.payload == "suspend" ? true : false; ndebug("pause function!"); pauseValue = [ticks, node.warnT]; timeout = ticks; var msg = line; node.status({ - fill : "red", + fill : "grey", shape : "dot", text : `Paused: ${ticks}` // provide a visual countdown }); @@ -303,22 +304,22 @@ module.exports = function(RED) { msg.payload = "pause"; lastPayload = msg.payload; - var tremain = { "payload": ticks, "state": "paused", "flag": "pause"}; - node.send([msg, tremain]); - state = 'pause'; - if (tick !== null) { + if (tick !== null && !suspend) { + var tremain = { "payload": ticks, "state": 0, "flag": "pause"}; clearInterval(tick); tick = null; } + node.send([msg, tremain]); } - function unpause() { + function unpause(msg) { ndebug("unpause!"); - var pausemsg = { - timeout: pauseValue[0], - warning: pauseValue[1] - } + + var pausemsg = { + timeout: pauseValue[0], + warning: pauseValue[1] + } on(pausemsg); } @@ -425,15 +426,15 @@ module.exports = function(RED) { // Leave this here, need the functions ref from above var states = { // Not sure if this is what I want in the long run but this is good for now - stop: { 0: off, on: on, pause: off, off: off, stop: doNothing, cancel: doNothing }, - pause: { 0: off, on: unpause, pause: pause, off: off, stop: stop, cancel: cancel }, - run: { 0: off, on: on, pause: pause, off: off, stop: stop, cancel: cancel } + stop: { 0: off, on: on, pause: on, suspend: off, off: off, stop: doNothing, cancel: doNothing }, + pause: { 0: off, on: on, pause: unpause, suspend: unpause, off: off, stop: stop, cancel: cancel }, + run: { 0: off, on: on, pause: pause, suspend: pause, off: off, stop: stop, cancel: cancel } }; // ------------------------------------------------------------------------------- // Commands // TIX - node.on("TIX", function(inMsg) { + node.on("TIX", function(inMsg, state) { lastPayload = Date.now(); var msg = {}; @@ -472,7 +473,8 @@ module.exports = function(RED) { var tremain = { "payload": ticks, "state": 1, "flag": "ticks > 0"}; node.send([null, tremain]); } - ticks--; + //decrement ticks, but if paused, stop decrementing + state !== "pause" ? ticks-- : ''; } else if(ticks == 0){ ndebug("ticks == 0"); stop("off"); @@ -578,9 +580,9 @@ module.exports = function(RED) { // I'd like to change this to run when only needed function startInterval() { var msg = { payload:'TIX', topic:""}; - node.emit("TIX", msg); + node.emit("TIX", msg, state); tick = setInterval(function() { - node.emit("TIX", msg); + node.emit("TIX", msg, state); }, 1000); // trigger every 1 sec } node.on("close", function() {