From 8bf1a60749eb5ddaac53153a0784fccf4d8b2a8e Mon Sep 17 00:00:00 2001 From: Christian Weimann Date: Fri, 25 Aug 2023 12:57:35 +0200 Subject: [PATCH] Switch to timer module --- utils/watch.js | 53 ++++++++++++++++++++------------------------------ 1 file changed, 21 insertions(+), 32 deletions(-) diff --git a/utils/watch.js b/utils/watch.js index 97dcb30..af98170 100644 --- a/utils/watch.js +++ b/utils/watch.js @@ -6,6 +6,8 @@ class Watch { #watchItem #watchObjects = new Object(); + #timers = new timer.Timer(); + constructor(watchItem) { // Fetch item if provided itemName is a string @@ -64,16 +66,10 @@ class Watch { console.log(`Delete watch object for item ${this.#watchItem.name} with watchUUID ${watchUUID}`); // End repeatAlertTimer if existing - if (this.#watchObjects[watchUUID].hasOwnProperty('repeatAlertTimer') && this.#watchObjects[watchUUID].repeatAlertTimer.isActive()) { - console.log('Cancel repeatAlertTimer for ' + watchUUID); - this.#watchObjects[watchUUID].repeatAlertTimer.cancel(); - } + this.#timers.cancel('repeatAlarm ' + watchUUID); // End startAlertTimer if existing - if (this.#watchObjects[watchUUID].hasOwnProperty('startAlertTimer') && this.#watchObjects[watchUUID].startAlertTimer.isActive()) { - console.log('Cancel startAlertTimer for ' + watchUUID); - this.#watchObjects[watchUUID].startAlertTimer.cancel(); - } + this.#timers.cancel('startAlert ' + watchUUID); delete this.#watchObjects[watchUUID]; @@ -166,16 +162,10 @@ class Watch { } // End repeatAlertTimer if existing - if (this.#watchObjects[watchUUID].hasOwnProperty('repeatAlertTimer') && this.#watchObjects[watchUUID].repeatAlertTimer.isActive()) { - console.log('Cancel repeatAlertTimer'); - this.#watchObjects[watchUUID].repeatAlertTimer.cancel(); - } + this.#timers.cancel('repeatAlarm ' + watchUUID); // End startAlertTimer if startAlert started timer with delay - if (this.#watchObjects[watchUUID].hasOwnProperty('startAlertTimer') && this.#watchObjects[watchUUID].startAlertTimer.isActive()) { - console.log('Cancel startAlertTimer'); - this.#watchObjects[watchUUID].startAlertTimer.cancel(); - } + this.#timers.cancel('startAlert ' + watchUUID); } #processItemEvent(event) { @@ -223,27 +213,26 @@ class Watch { let alertDelayZDT = time.toZDT(alertDelay); alertDelayAbsolute = (alertDelayZDT.getMillisFromNow() / 1000); - this.#watchObjects[watchUUID].startAlertTimer = actions.ScriptExecution.createTimer('startAlert ' + watchUUID, - alertDelayZDT, - () => { - console.log('Run initial alert function for watchObject ' + watchUUID); - initialAlertFunc(); - } - ); + this.#timers.create('startAlert ' + watchUUID, + alertDelayZDT, + () => { + console.log('Run initial alert function for watchObject ' + watchUUID); + initialAlertFunc(); + } + ); } // Create timer to run repeat alert rule if required if (alertRepeat != '') { console.log(`Shedule repeat alert function for watchObject ${watchUUID} with repeat setting ${alertRepeat}`); - this.#watchObjects[watchUUID].repeatAlertTimer = actions.ScriptExecution.createTimer('repeatAlarm ' + watchUUID, - time.toZDT(alertRepeat).plusSeconds(alertDelayAbsolute), - () => { - console.log('Run repeat alert function for watchObject ' + watchUUID); - this.#watchObjects[watchUUID].alertFunc(); - this.#watchObjects[watchUUID].repeatAlertTimer.reschedule(time.toZDT(alertRepeat)); - } - - ); + this.#timers.create('repeatAlarm ' + watchUUID, + time.toZDT(alertRepeat).plusSeconds(alertDelayAbsolute), + () => { + console.log('Run repeat alert function for watchObject ' + watchUUID); + this.#watchObjects[watchUUID].alertFunc(); + this.#watchObjects[watchUUID].repeatAlertTimer.reschedule(time.toZDT(alertRepeat)); + } + ); } } }