Skip to content

Commit

Permalink
Deprecated timeUtils.toDateTime in favor of time.toZDT
Browse files Browse the repository at this point in the history
Signed-off-by: Richard Koshak <[email protected]>
  • Loading branch information
rkoshak committed Aug 30, 2022
1 parent 334aef5 commit f3c4729
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 134 deletions.
36 changes: 18 additions & 18 deletions countdownTimer.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
const {timeUtils, loopingTimer} = require('openhab_rules_tools');
const {time, items} = require('openhab');
const { loopingTimer } = require('openhab_rules_tools');
const { time, items } = require('openhab');

/**
* Timer that updates a passed in Item with the number of seconds reamining on the
* Timer once a second.
* Timer that updates a passed in Item with the number of seconds reamining on the
* Timer once a second.
*/
class CountdownTimer {
/**
* Creates a timer to run func at when and a looping timer to update
* countItem with the seconds remaining every second. Both timers start
* immediately.
* @param {*} when timeUtils.toDateTime compatible time or duration
* @param {function} func function to call at when
* @param {string} countItem name of the Item to update with the seconds remaining
*/

/**
* Creates a timer to run func at when and a looping timer to update
* countItem with the seconds remaining every second. Both timers start
* immediately.
* @param {*} when time.toZDT compatible time or duration
* @param {function} func function to call at when
* @param {string} countItem name of the Item to update with the seconds remaining
*/
constructor(when, func, countItem) {
this.start = time.ZonedDateTime.now();
this.end = timeUtils.toDateTime(when);
this.end = time.toZDT(when);
this.ONE_SEC = time.Duration.ofSeconds(1);

// Create a separate timer to run the func
this.timer = actions.ScriptExecution.createTimer(this.start, func);
this.timeLeft = time.Duration.between(this.start, this.end);
Expand All @@ -31,14 +31,14 @@ class CountdownTimer {
}

/**
* Determines the number of seconds left and updates the count Item. If the
* Determines the number of seconds left and updates the count Item. If the
* time left is less than a second, 0 is the value posted.
* @param {CountdownTimer} ctx Context to access the timer information from inside the countdown Timer's lambda
*/
_updateItem(ctx) {
let left = (ctx.timeLeft.compareTo(ctx.ONE_SEC) < 0) ? 0 : ctx.timeLeft.seconds();
items.getItem(ctx.countItem).postUpdate(left);
}
}

/**
* Drives the looping timer that updates the countItem. Runs once a second until
Expand All @@ -48,7 +48,7 @@ class CountdownTimer {
_iterateGenerator(ctx) {
return () => {
ctx._updateItem(ctx);
if(!ctx.timeLeft.isZero()) {
if (!ctx.timeLeft.isZero()) {
let sleepTime = (ctx.timeLeft.compareTo(ctx.ONE_SEC) < 0) ? ctx.timeLeft : ctx.ONE_SEC;
ctx.timeLeft = ctx.timeLeft.minusDuration(sleepTime);
return '1s';
Expand Down
10 changes: 5 additions & 5 deletions deferred.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const {timeUtils, timerMgr} = require('openhab_rules_tools');
const { timerMgr } = require('openhab_rules_tools');

/**
* Class that can be used to schedule a command or update to be sent to an Item later.
*/
class Deferred {

/**
* Constructor
*/
Expand All @@ -28,12 +28,12 @@ class Deferred {
* is before now, sendCommand or postUpdate immediately.
* @param {string} target name of the Item to update or command
* @param {string} value command or update to sendGatekeeper
* @param {*} when timeUtils.toDateTime compatible duration or date/time
* @param {*} when time.toZDT compatible duration or date/time
* @param {boolean} isCommand when true value is sent as a command
*/
defer(target, value, when, isCommand) {
const triggerTime = timeUtils.toDateTime(when);
if(triggerTime.isBefore(time.ZonedDateTime.now())) {
const triggerTime = time.toZDT(when);
if (triggerTime.isBefore(time.ZonedDateTime.now())) {
triggerTime = time.ZonedDateTime.now();
}
this.timers.cancel(target);
Expand Down
24 changes: 11 additions & 13 deletions gatekeeper.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
const {timeUtils} = require('openhab_rules_tools');

/**
* Class that implements the Gatekeeper design pattern. When the user calls
* 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.
*/
Expand All @@ -10,7 +8,7 @@ class Gatekeeper {
/**
* Creates the Gatekeeper
*/
constructor(){
constructor() {
var ArrayDeque = Java.type('java.util.ArrayDeque');
this.commands = new ArrayDeque();
this.timer = null;
Expand All @@ -24,24 +22,24 @@ class Gatekeeper {
*/
_procCommandGenerator(ctx) {
return () => {

// no more commands
if(ctx.commands.isEmpty()) {
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 pause = time.toZDT(command[0]);
const triggerTime = pause.minus(delta);

ctx.timer = actions.ScriptExecution.createTimer(triggerTime, ctx._procCommandGenerator(ctx));
}

Expand All @@ -51,12 +49,12 @@ class Gatekeeper {
/**
* 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 {*} a date time or duration supported by time.toZDT
* @param {function} a funuction to call
*/
addCommand(pause, command) {
this.commands.add([pause, command]);
if(this.timer === null || this.timer.hasTerminated()) {
if (this.timer === null || this.timer.hasTerminated()) {
this._procCommandGenerator(this)();
}
}
Expand All @@ -65,7 +63,7 @@ class Gatekeeper {
* Cancels all queued commands.
*/
cancelAll() {
if(this.timer !== null) {
if (this.timer !== null) {
this.timer.cancel();
}
this.commands.clear();
Expand Down
20 changes: 9 additions & 11 deletions loopingTimer.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
const {timeUtils} = require('openhab_rules_tools');

/**
* Implements a looping Timer which is passed a function that is expected to return
* a when supported by timeUtils.toDateTime. The loop will reschedule the timer based
* a when supported by time.toZDT. The loop will reschedule the timer based
* on that returned when or, if it return null the looping stops.
*/
class LoopingTimer {
Expand All @@ -17,16 +15,16 @@ class LoopingTimer {
/**
* Kicks off the timer loop. Schedules a timer to call func at when
* @param {function} func function to call at when, must return a when to continue the loop or null to stop
* @param {*} when any of the types supported by timeUtils.toDateTime
* @param {*} when any of the types supported by time.toZDT
*/
loop(func, when) {

this.func = func;
if(!when) this.expired();
if (!when) this.expired();
else {
this.timer = actions.ScriptExecution.createTimer(
timeUtils.toDateTime(when),
() => this.expired());
time.toZDT(when),
() => this.expired());
}
}

Expand All @@ -37,18 +35,18 @@ class LoopingTimer {
*/
expired() {
var when = this.func();
if(when) {
if (when) {
this.timer = actions.ScriptExecution.createTimer(
timeUtils.toDateTime(when),
() => this.expired());
time.toZDT(when),
() => this.expired());
}
}

/**
* Cancels the timer if it exists and hasn't already terminated.
*/
cancel() {
if(this.timer && !this.hasTerminated()) {
if (this.timer && !this.hasTerminated()) {
this.timer.cancel();
}
}
Expand Down
10 changes: 4 additions & 6 deletions rateLimit.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
const {timeUtils} = require('openhab_rules_tools');

/**
* Simple class that keeps track of a time. When run is called, a
* Simple class that keeps track of a time. When run is called, a
* when is passed indicating how much time to wait after that call
* before calling the passed in func again.
*/
Expand All @@ -16,11 +14,11 @@ class RateLimit {

/**
* @param {function()} func action to run if it's been long enough
* @when {*} anything supported by timeUtils.toDateTime
* @when {*} anything supported by time.toZDT
*/
run(func, when) {
if(time.ZonedDateTime.now().isAfter(this.until)) {
this.until = timeUtils.toDateTime(when);
if (time.ZonedDateTime.now().isAfter(this.until)) {
this.until = time.toZDT(when);
func();
}
}
Expand Down
Loading

0 comments on commit f3c4729

Please sign in to comment.