forked from rkoshak/openhab-rules-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatekeeper.js
executable file
·77 lines (66 loc) · 1.99 KB
/
gatekeeper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
const {timeUtils} = require('openhab_rules_tools');
/**
* Class that implements the Gatekeeper design pattern. When the user calls
* addCommand, it will queue them up so that a new command is not called until
* the time specified by the previous command has passed.
*/
class Gatekeeper {
/**
* Creates the Gatekeeper
*/
constructor(){
var ArrayDeque = Java.type('java.util.ArrayDeque');
this.commands = new ArrayDeque();
this.timer = null;
}
/**
* Processes a command from the queue and creates a timer to call the next
* command.
* @parm {*} ctx pointer to the Gatekeeper OPbject.
* @returns {function} function called by the timer to process the next command.
*/
_procCommandGenerator(ctx) {
return () => {
// no more commands
if(ctx.commands.isEmpty()) {
ctx.timer = null;
}
// pop the command and run it
else {
const command = ctx.commands.pop();
const func = command[1];
const before = time.ZonedDateTime.now();
func();
const after = time.ZonedDateTime.now();
const delta = time.Duration.between(before, after);
const pause = timeUtils.toDateTime(command[0]);
const triggerTime = pause.minus(delta);
ctx.timer = actions.ScriptExecution.createTimer(triggerTime, ctx._procCommandGenerator(ctx));
}
};
}
/**
* Add a command to the queue of commands. Gatekeeper will wait until pause
* before it will call the next command in the queue.
* @param {*} a date time or duration supported by timeUtils.toDateTime
* @param {function} a funuction to call
*/
addCommand(pause, command) {
this.commands.add([pause, command]);
if(this.timer === null || this.timer.hasTerminated()) {
this._procCommandGenerator(this)();
}
}
/**
* Cancels all queued commands.
*/
cancelAll() {
if(this.timer !== null) {
this.timer.cancel();
}
this.commands.clear();
}
}
module.exports = {
Gatekeeper
}