Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pause #14

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 68 additions & 18 deletions mytimeout.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
// }
//
// {
// "payload": <Don't care>
// "payload": <Don't care>
// }
//
// The above gets treated as an on condition
Expand Down Expand Up @@ -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 = '';

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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);
Expand All @@ -276,6 +285,42 @@ module.exports = function(RED) {
ndebug("cancel!");
stop('cancel');
ticks = -1;
pauseValue = null;
}

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 : "grey",
shape : "dot",
text : `Paused: ${ticks}` // provide a visual countdown
});

msg.payload = "pause";
lastPayload = msg.payload;

state = 'pause';
if (tick !== null && !suspend) {
var tremain = { "payload": ticks, "state": 0, "flag": "pause"};
clearInterval(tick);
tick = null;
}
node.send([msg, tremain]);
}

function unpause(msg) {
ndebug("unpause!");

var pausemsg = {
timeout: pauseValue[0],
warning: pauseValue[1]
}
on(pausemsg);
}

function doNothing() {
Expand Down Expand Up @@ -381,14 +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, off: off, stop: doNothing, cancel: doNothing },
run: { 0: off, on: on, 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 = {};

Expand Down Expand Up @@ -427,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");
Expand All @@ -443,6 +490,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)
Expand Down Expand Up @@ -481,7 +529,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)) {
Expand Down Expand Up @@ -530,13 +578,15 @@ 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() {
var msg = { payload:'TIX', topic:""};
node.emit("TIX", msg, state);
tick = setInterval(function() {
node.emit("TIX", msg, state);
}, 1000); // trigger every 1 sec
}
node.on("close", function() {
if (tick) {
if (tick !== null) {
clearInterval(tick);
}
});
Expand Down