1
0
This repository has been archived on 2025-01-10. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
openhab-js-automation-old/utils/timer.js
2023-08-26 08:11:13 +02:00

60 lines
1.5 KiB
JavaScript

console.loggerName = 'js.timer';
console.log('Load timer module');
class Timer {
#timers = new Object();
constructor() {
console.log('Initialization of timer');
}
create(identifier, timeout, func) {
console.debug(`Create timer with identifier ${identifier}`);
this.#timers[identifier] = actions.ScriptExecution.createTimer(identifier, timeout, func);
}
cancel(identifier) {
// Return if no timer with the respactive identifier is available
if (!this.#timers.hasOwnProperty(identifier)) {
console.debug(`No timer with identifier ${identifier} available to cancel`);
return false;
}
// Check if timer is active
if (!this.#timers[identifier].isActive()) {
console.debug(`Timer with identifier ${identifier} not running. Cancel anyway`);
} else {
console.debug(`Cancel timer with identifier ${identifier}`);
}
// Cancel timer
this.#timers[identifier].cancel();
delete this.#timers[identifier];
}
cancelAll() {
// Fetch timers
let timers = Object.keys(this.#timers);
// Return if no timers available
if (timers.length == 0) {
console.debug('No timers available to cancel');
return false;
}
// Cancel all timers
for (let timer of timers) {
this.cancel(timer);
}
}
}
module.exports = {
Timer,
};