1
0

Switch to timer module

This commit is contained in:
2023-08-25 11:04:53 +02:00
parent 7f6cced243
commit cf30bbb371
2 changed files with 30 additions and 32 deletions

View File

@@ -4,6 +4,7 @@ console.log('Load equipmentMgr');
const { const {
equipment, equipment,
lib, lib,
timer,
watch watch
} = require('../utils'); } = require('../utils');

View File

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