-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprogram.js
93 lines (79 loc) · 2.14 KB
/
program.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
const ValveControl = require('./valveControl');
class Program {
constructor({ duration_per_zone, zones, iterations, interval }, valveControl, stopProgramCallback) {
this.valveControl= valveControl;
this.iterations = iterations;
this.interval = interval;
this.duration_per_zone = duration_per_zone;
this.zones = zones;
this.series = null;
this.running = false;
this.paused = false;
this.current_iteration = 1;
this.stopProgramCallback = stopProgramCallback;
this.killSeries = this.killSeries.bind(this);
this.loop = this.loop.bind(this);
this.maybeWait = this.maybeWait.bind(this);
}
loop() {
let index = 0;
let zones = this.zones;
let killSeries = this.killSeries;
let maybeWait = this.maybeWait;
let paused = this.paused;
let valveControl = this.valveControl;
const zoneControl = () => {
if (index != 0) {
console.log('turn off ', zones[index - 1])
valveControl.zoneOff(zones[index - 1])
}
if (index >= zones.length) {
killSeries();
maybeWait();
return;
}
console.log('turn on ', zones[index]);
valveControl.zoneOn(zones[index])
index++;
}
zoneControl();
this.series = setInterval(zoneControl, this.duration_per_zone * 1000 * 60)
}
init(stopEventHandler) {
this.running = true;
this.loop();
}
killSeries() {
clearInterval(this.series);
this.valveControl.allZonesOff();
}
pauseProgram() {
this.paused = true;
}
unpauseProgram() {
this.paused = false;
}
stopProgram() {
this.killSeries();
this.valveControl.allZonesOff();
this.running = false;
this.stopProgramCallback();
console.log('program: stop program')
}
maybeWait() {
console.log('current iteration: ', this.current_iteration)
console.log('iterations: ', this.iterations)
if (this.current_iteration < this.iterations){
this.current_iteration++;
this.series = setTimeout(() => {
this.loop()
},this.interval * 60 * 1000);
} else {
this.stopProgram();
console.log('program ended');
}
}
}
module.exports = {
Program
}