diff --git a/equipmentMgr.js b/equipmentMgr.js index 5e2a598..7955521 100644 --- a/equipmentMgr.js +++ b/equipmentMgr.js @@ -4,6 +4,7 @@ console.log('Load equipmentMgr'); const { equipment, lib, + timer, watch } = require('../utils'); diff --git a/utils/equipment.js b/utils/equipment.js index aa338e1..44a1c76 100644 --- a/utils/equipment.js +++ b/utils/equipment.js @@ -27,7 +27,7 @@ class Equipment { this.name = this.equipmentItem.name; this.watch = new Object(); - this.timers = new Object(); + this.timers = new timer.Timer(); // Check if equipment has state item if (this.hasProperty('State')) { @@ -110,6 +110,9 @@ class Equipment { for (let watchItem of Object.keys(this.watch)) { this.watch[watchItem].deleteAll(); } + + // Cancel all timers + this.timers.cancelAll(); } } @@ -176,30 +179,33 @@ class Irrigation extends Equipment { let endTime = time.toZDT(totalDuration); // Set timers for valve - this.valves[currentValve].timers['autoOn'] = actions.ScriptExecution.createTimer( - startTime, - () => { - this.valves[currentValve].stateItem.sendCommandIfDifferent('ON'); - } - ); + this.valves[currentValve].timers.create( + 'autoOn', + startTime, + () => { + this.valves[currentValve].stateItem.sendCommandIfDifferent('ON'); + } + ); - this.valves[currentValve].timers['autoOff'] = actions.ScriptExecution.createTimer( - endTime, - () => { - this.valves[currentValve].stateItem.sendCommandIfDifferent('OFF'); - } - ); + this.valves[currentValve].timers.create( + 'autoOff', + endTime, + () => { + this.valves[currentValve].stateItem.sendCommandIfDifferent('OFF'); + } + ); // Stop irrigation after the last valve if (nextValve == undefined) { console.log('No further valves with autoMode enabled available'); - this.timers['autoOff'] = actions.ScriptExecution.createTimer( - endTime.plusSeconds(5), - () => { - console.debug(`Switch irrigation ${this.name} to off`) - this.stateItem.sendCommandIfDifferent('OFF'); - } - ); + this.timers.create( + 'autoOff', + endTime.plusSeconds(5), + () => { + console.debug(`Switch irrigation ${this.name} to off`) + this.stateItem.sendCommandIfDifferent('OFF'); + } + ); } } } @@ -207,20 +213,11 @@ class Irrigation extends Equipment { #irrigationOff() { console.info(`Irrigation ${this.name} received command off`); - if (this.timers.hasOwnProperty('autoOff') && this.timers['autoOff'].isActive()) { - console.debug(`Cancel stop timer for irrigation`); - this.timers['autoOff'].cancel(); - } + this.timers.cancel('autoOff') for (let valve of Object.keys(this.valves)) { - if (this.valves[valve].timers.hasOwnProperty('autoOn') && this.valves[valve].timers['autoOn'].isActive()) { - console.debug(`Cancel start timer for valve ${valve}`); - this.valves[valve].timers['autoOn'].cancel(); - } - if (this.valves[valve].timers.hasOwnProperty('autoOff') && this.valves[valve].timers['autoOff'].isActive()) { - console.debug(`Cancel stop timer for valve ${valve}`); - this.valves[valve].timers['autoOff'].cancel(); - } + this.valves[valve].timers.cancel('autoOn'); + this.valves[valve].timers.cancel('autoOff'); this.valves[valve].stateItem.sendCommandIfDifferent('OFF') } }