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