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
#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));
}
);
}
}
}