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 {
equipment,
lib,
timer,
watch
} = require('../utils');

View File

@@ -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')
}
}