First commit
This commit is contained in:
73
utils/TimerMgr.js
Normal file
73
utils/TimerMgr.js
Normal file
@@ -0,0 +1,73 @@
|
||||
// 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
|
||||
};
|
||||
7
utils/index.js
Normal file
7
utils/index.js
Normal file
@@ -0,0 +1,7 @@
|
||||
// Logging
|
||||
console.loggerName = "org.openhab.js.utils";
|
||||
console.log("Loading utils");
|
||||
|
||||
module.exports = {
|
||||
get TimerMgr() { return require('./TimerMgr.js').getTimerMgr; }
|
||||
}
|
||||
Reference in New Issue
Block a user