1
0

Switch to timer module

This commit is contained in:
2023-08-25 12:57:35 +02:00
parent cf30bbb371
commit 8bf1a60749

View File

@@ -6,6 +6,8 @@ class Watch {
#watchItem #watchItem
#watchObjects = new Object(); #watchObjects = new Object();
#timers = new timer.Timer();
constructor(watchItem) { constructor(watchItem) {
// Fetch item if provided itemName is a string // 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}`); console.log(`Delete watch object for item ${this.#watchItem.name} with watchUUID ${watchUUID}`);
// End repeatAlertTimer if existing // End repeatAlertTimer if existing
if (this.#watchObjects[watchUUID].hasOwnProperty('repeatAlertTimer') && this.#watchObjects[watchUUID].repeatAlertTimer.isActive()) { this.#timers.cancel('repeatAlarm ' + watchUUID);
console.log('Cancel repeatAlertTimer for ' + watchUUID);
this.#watchObjects[watchUUID].repeatAlertTimer.cancel();
}
// End startAlertTimer if existing // End startAlertTimer if existing
if (this.#watchObjects[watchUUID].hasOwnProperty('startAlertTimer') && this.#watchObjects[watchUUID].startAlertTimer.isActive()) { this.#timers.cancel('startAlert ' + watchUUID);
console.log('Cancel startAlertTimer for ' + watchUUID);
this.#watchObjects[watchUUID].startAlertTimer.cancel();
}
delete this.#watchObjects[watchUUID]; delete this.#watchObjects[watchUUID];
@@ -166,16 +162,10 @@ class Watch {
} }
// End repeatAlertTimer if existing // End repeatAlertTimer if existing
if (this.#watchObjects[watchUUID].hasOwnProperty('repeatAlertTimer') && this.#watchObjects[watchUUID].repeatAlertTimer.isActive()) { this.#timers.cancel('repeatAlarm ' + watchUUID);
console.log('Cancel repeatAlertTimer');
this.#watchObjects[watchUUID].repeatAlertTimer.cancel();
}
// End startAlertTimer if startAlert started timer with delay // End startAlertTimer if startAlert started timer with delay
if (this.#watchObjects[watchUUID].hasOwnProperty('startAlertTimer') && this.#watchObjects[watchUUID].startAlertTimer.isActive()) { this.#timers.cancel('startAlert ' + watchUUID);
console.log('Cancel startAlertTimer');
this.#watchObjects[watchUUID].startAlertTimer.cancel();
}
} }
#processItemEvent(event) { #processItemEvent(event) {
@@ -223,7 +213,7 @@ class Watch {
let alertDelayZDT = time.toZDT(alertDelay); let alertDelayZDT = time.toZDT(alertDelay);
alertDelayAbsolute = (alertDelayZDT.getMillisFromNow() / 1000); alertDelayAbsolute = (alertDelayZDT.getMillisFromNow() / 1000);
this.#watchObjects[watchUUID].startAlertTimer = actions.ScriptExecution.createTimer('startAlert ' + watchUUID, this.#timers.create('startAlert ' + watchUUID,
alertDelayZDT, alertDelayZDT,
() => { () => {
console.log('Run initial alert function for watchObject ' + watchUUID); console.log('Run initial alert function for watchObject ' + watchUUID);
@@ -235,14 +225,13 @@ class Watch {
// Create timer to run repeat alert rule if required // Create timer to run repeat alert rule if required
if (alertRepeat != '') { if (alertRepeat != '') {
console.log(`Shedule repeat alert function for watchObject ${watchUUID} with repeat setting ${alertRepeat}`); console.log(`Shedule repeat alert function for watchObject ${watchUUID} with repeat setting ${alertRepeat}`);
this.#watchObjects[watchUUID].repeatAlertTimer = actions.ScriptExecution.createTimer('repeatAlarm ' + watchUUID, this.#timers.create('repeatAlarm ' + watchUUID,
time.toZDT(alertRepeat).plusSeconds(alertDelayAbsolute), time.toZDT(alertRepeat).plusSeconds(alertDelayAbsolute),
() => { () => {
console.log('Run repeat alert function for watchObject ' + watchUUID); console.log('Run repeat alert function for watchObject ' + watchUUID);
this.#watchObjects[watchUUID].alertFunc(); this.#watchObjects[watchUUID].alertFunc();
this.#watchObjects[watchUUID].repeatAlertTimer.reschedule(time.toZDT(alertRepeat)); this.#watchObjects[watchUUID].repeatAlertTimer.reschedule(time.toZDT(alertRepeat));
} }
); );
} }
} }