-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathValveManager.h
256 lines (240 loc) · 7.17 KB
/
ValveManager.h
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#ifndef WATER_MANAGER_H
#define WATER_MANAGER_H
#include "Arduino.h"
#include "DurationFsm.h"
#include "WaterMeter.h"
#include "Constants.h"
#define UNUSED 255
#define DURATION_WARN_SEC 2U
#define DURATION_WAIT_BEFORE_SEC 60U
#define DURATION_LEAK_CHECK_FILL_MS 3000U
#define DURATION_LEAK_CHECK_WAIT_MS 2000U
#define DEFAULT_DURATION_AUTOMATIC1_SEC 60U * 5U
#define DEFAULT_DURATION_AUTOMATIC2_SEC 60U * 5U
#define DEFAULT_DURATION_AUTOMATIC3_SEC 60U * 1U
#define MAX_ZONE_DURATION 3600U
/**
Definition of a valve with its PIN. Can be switched on/off and queried on its state.
*/
class Valve {
public:
Valve(const byte pin): pin(pin) {
if (pin != UNUSED) {
pinMode(pin, OUTPUT);
}
}
virtual ~Valve() {}
virtual void on() {
if (pin != UNUSED) {
digitalWrite(pin, HIGH);
}
}
virtual void off() {
if (pin != UNUSED) {
digitalWrite(pin, LOW);
}
}
virtual bool isOn() {
if (pin != UNUSED) {
return digitalRead(pin) == HIGH;
} else {
return false;
}
}
private:
const byte pin;
};
/**
Extends a normal Valve and adds the functionality to start/stop measuring its water flow when it is switched on/off.
The main valve is used for this purpose.
Only one valve can be a MeasuredValve.
*/
class MeasuredValve: public Valve {
public:
MeasuredValve(byte pin, WaterMeter *waterMeter): Valve(pin), waterMeter(waterMeter) {
}
virtual ~MeasuredValve() {
delete waterMeter;
}
virtual void on() {
waterMeter->start();
Valve::on();
}
virtual void off() {
boolean wasOn = isOn();
// still switch if off in any case for security reasons
Valve::off();
if (wasOn) {
waterMeter->stop();
Serial.print(F("measured: "));
Serial.println(getTotalCount());
}
}
unsigned long getTotalCount() {
return waterMeter->getTotalCount();
}
private:
WaterMeter * const waterMeter;
};
/**
A superState to switch a value on/off when it enters/exits
*/
class ValveSuperState: public SuperState {
public:
ValveSuperState(Valve * const valve, String name): SuperState(name), valve(valve) {
}
virtual void enter() {
valve->on();
}
virtual void exit() {
valve->off();
}
private:
Valve * const valve;
};
/**
A duration state to switch a valve on and later off again after a given delay.
*/
class ValveState: public DurationState {
public:
ValveState(Valve *valve, unsigned long durationMs, String name, SuperState * const superState): DurationState(durationMs, name, superState), valve(valve) {
}
virtual void enter() {
valve->on();
}
virtual void exit() {
valve->off();
}
private:
Valve * const valve;
};
/**
A state which checks if there are any water meter ticks while it is active.
*/
class LeakCheckState: public DurationState, public Runnable {
public:
LeakCheckState(unsigned long durationMs, String name, SuperState * const superState, Runnable * const listener, WaterMeter *waterMeter):
DurationState(durationMs, name, superState), waterMeter(waterMeter), listener(listener) {
}
virtual void enter() {
startTotalCount = waterMeter->getTotalCount();
scheduler.scheduleDelayed(this, 100);
}
virtual void exit() {
scheduler.removeCallbacks(this);
checkLeak();
}
void run() {
scheduler.scheduleDelayed(this, 100);
checkLeak();
}
private:
WaterMeter * const waterMeter;
Runnable * const listener;
unsigned long startTotalCount;
void checkLeak() {
if (startTotalCount != waterMeter->getTotalCount()) {
scheduler.removeCallbacks(this);
scheduler.schedule(listener);
Serial.print(F("Leak count: "));
Serial.println(waterMeter->getTotalCount() - startTotalCount);
}
}
};
/**
A state which checks if there are any water meter ticks while it is active.
*/
class MeasureStateListener {
public:
virtual void measuredResult(unsigned int tickCount) = 0;
};
class MeasureState: public DurationState, public Runnable {
public:
MeasureState(Valve *valve, const unsigned long durationMs, const String name, SuperState * const superState, MeasureStateListener * const listener, WaterMeter *waterMeter):
DurationState(durationMs, name, superState), valve(valve), waterMeter(waterMeter), listener(listener) {
}
virtual void enter() {
startTotalCount = waterMeter->getTotalCount();
if (valve != NULL) {
valve->on();
}
}
virtual void exit() {
if (valve != NULL) {
valve->off();
}
tickCount = waterMeter->getTotalCount() - startTotalCount;
// need to use scheduler to allow manipulation of the state machine
scheduler.schedule(this);
}
void run() {
listener->measuredResult(tickCount);
}
private:
Valve * const valve;
WaterMeter * const waterMeter;
MeasureStateListener * const listener;
unsigned long startTotalCount;
unsigned int tickCount;
};
class ValveManager {
public:
/**
@param waterMeter the WaterMeter to use for the measured valve. Cannot be null.
@param sensorCheckListener callback to report how many ticks the sensor has reported during a period when water should be flowing
@param leakCheckListener callback executed when a leak is detected (if activated)
*/
ValveManager(WaterMeter *waterMeter,
MeasureStateListener * const waterMeterCheckListener,
Runnable * const leakCheckListener);
~ValveManager();
/**
start automated watering with a warn second before the actual watering.
*/
void startAutomaticWithWarn();
/**
start automated watering without a warn second.
*/
void startAutomatic();
/**
Stop watering, switch all valves off.
*/
void stopAll();
/**
returns true if any watering is currently running. Can also be in a waiting state. False if currently idle.
*/
bool isOn();
/**
Set and store the duration the given zone will be on persistently.
@param zone number of the zone to be set, 1, 2 or 3
@param durationSec duration in seconds how long the zone will be watered on every automatic run
*/
void setZoneDuration(byte zone, unsigned int durationSec);
/**
print the status of ValveManager to serial.
*/
void printStatus();
private:
MeasuredValve *valveMain;
Valve *valveArea1;
Valve *valveArea2;
Valve *valveArea3;
DurationFsm *fsm;
SuperState *superStateMainIdle;
ValveSuperState *superStateMainOn;
DurationState *stateIdle;
DurationState *stateLeakCheckFill;
DurationState *stateLeakCheckWait;
DurationState *stateWarnAutomatic1;
DurationState *stateWaitBeforeAutomatic1;
DurationState *stateAutomatic1;
DurationState *stateBeforeWarnAutomatic2;
DurationState *stateWarnAutomatic2;
DurationState *stateWaitBeforeAutomatic2;
DurationState *stateAutomatic2;
DurationState *stateBeforeWarnAutomatic3;
DurationState *stateWarnAutomatic3;
DurationState *stateWaitBeforeAutomatic3;
DurationState *stateAutomatic3;
};
#endif