-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwaterfuse.c
343 lines (309 loc) · 8.08 KB
/
waterfuse.c
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/**
* Monitor a water flow meter and make decisions about
* the need for cutting off flow.
*
* An interrupt handler counts clicks from the flow meter
* after so many litres happen
*/
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <wiringPi.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdarg.h>
#include <time.h>
#define FLOW_METER 0 // Pin for flow meter input
#define POWER_RELAY 1 // Pin for relay to pump output
#define RESET_BUTTON 2
#define PRESSURE_SENSOR 3
#define CLICKS_PER_LITRE 450 // Number of pulses per litre
#define MAX_FLOW 200 // Maximum number of litres in a given time period
#define RESET_PERIOD 600 // Quiescent time to reset counters
#define MAX_TIME 900 // Time during which the max flow can be achieved
volatile unsigned int clicks = 0;
volatile unsigned int reset = 0;
unsigned char triggered = 0;
unsigned char counting = 0;
int last_click_time = 0;
int last_click_count = 0;
int first_click_time = 0;
int total_clicks = 0;
int clicks_per_litre = CLICKS_PER_LITRE;
int max_litres = MAX_FLOW;
int reset_period = RESET_PERIOD;
int time_limit = MAX_TIME;
int daemonise = 1;
int verbose = 0;
void
handleClick(void) {
clicks++;
}
void
handleReset(void) {
reset = 1;
}
void rollLog() {
int outfd;
outfd = open("/var/log/waterfuse.log",
O_WRONLY | O_APPEND | O_CREAT,
S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH
);
close(1);
close(2);
dup2(outfd, 1);
dup2(outfd, 2);
close(outfd);
}
void
readConfig(void) {
FILE * cfg;
char buf[1024];
int val;
if ((cfg = fopen("/etc/waterfuse/waterfuse.conf", "r")) != NULL) {
while (fscanf(cfg, "%s %d", &buf, &val) != EOF) {
if (strcmp("reset_period", buf) == 0) {
reset_period = val;
} else if (strcmp("max_time", buf) == 0) {
time_limit = val * 60;
} else if (strcmp("max_litres", buf) == 0) {
max_litres = val;
} else if (strcmp("clicks_per_litre", buf) == 0) {
clicks_per_litre = val;
} else if (strcmp("verbosity", buf) == 0) {
verbose = val;
}
}
}
}
void
printLog(int level, const char * fmt, ...) {
va_list args;
char buf[1024];
struct tm * time_parts;
time_t secs;
if (level > verbose) {
return;
}
// Get the current date/time
time(&secs);
time_parts = localtime(&secs);
// Write out a date string
strftime((char *)&buf, sizeof(buf), "%Y-%m-%d %H:%M:%S ", time_parts);
printf(buf);
va_start(args, fmt);
vprintf(fmt, args);
va_end(args);
fflush(stdout);
}
void
writeState(const char * fmt, ...) {
va_list args;
char buf[1024];
FILE * statefile;
statefile = fopen("/var/run/waterfuse/waterfuse.state", "w");
va_start(args, fmt);
vfprintf(statefile, fmt, args);
va_end(args);
fclose(statefile);
}
void
showStats(int level) {
int now;
now = time(0);
printLog(level, "last_click_time: %d seconds ago\n", now - last_click_time);
printLog(level, "first_click_time: %d seconds ago\n", now - first_click_time);
printLog(level, "last_click_count: %d\n", last_click_count);
printLog(level, "total_litres: %d\n", total_clicks / clicks_per_litre);
}
void
showConfig(void) {
printLog(0, "reset_period: %d\n", reset_period);
printLog(0, "time_limit: %d\n", time_limit);
printLog(0, "max_litres: %d\n", max_litres);
printLog(0, "clicks_per_litre: %d\n", clicks_per_litre);
printLog(0, "verbose: %d\n", verbose);
}
void
signalHandler(int sig) {
switch (sig) {
case SIGHUP:
rollLog();
readConfig();
break;
case SIGUSR1:
reset = 2;
break;
case SIGUSR2:
showStats(0);
break;
case SIGCONT:
digitalWrite(POWER_RELAY, LOW);
triggered=1;
break;
}
}
void
createPidFile(void) {
int pid;
FILE * pidfile;
struct stat st;
pid = getpid();
if (stat("/var/run/waterfuse", &st) < 0) {
mkdir("/var/run/waterfuse", 0755);
}
pidfile = fopen("/var/run/waterfuse/waterfuse.pid", "w");
fprintf(pidfile, "%d\n", pid);
fclose(pidfile);
}
int
main(int argc, char **argv) {
unsigned int litres = 0;
int stop_reason = 0;
int opt;
int now;
int seconds_from_first, seconds, time_periods, new_clicks;
struct sigaction sa;
int total_litres;
int pressure;
char * reset_msg[3] = { "", "button", "signal" };
char * stop_msg[3] = { "", "volume", "time" };
// Grab config from our config file first
readConfig();
// Now allow command-line overrides
while ((opt = getopt(argc, argv, "l:c:r:t:vd")) != -1) {
switch (opt) {
case 'l':
max_litres = atoi(optarg);
break;
case 'c':
clicks_per_litre = atoi(optarg);
break;
case 't':
time_limit = atoi(optarg) * 60;
break;
case 'r':
reset_period = atoi(optarg);
break;
case 'd':
daemonise = 0;
break;
case 'v':
verbose++;
break;
}
}
// Now we switch to daemon;
if (daemonise) {
close(0);
rollLog();
daemon(1, 1);
}
// And print out our config
printLog(0, "Starting\n");
writeState("started\tstartup\n");
showConfig();
// Create pidfile
createPidFile();
// Set up reset handler
sa.sa_handler = signalHandler;
sa.sa_flags = SA_RESTART|SA_NODEFER;
sigaction(SIGHUP, &sa, NULL);
sigaction(SIGUSR1, &sa, NULL);
sigaction(SIGUSR2, &sa, NULL);
sigaction(SIGCONT, &sa, NULL);
// Later versions die internally, no need to check result
wiringPiSetup();
// Set up ISR
if (wiringPiISR(FLOW_METER, INT_EDGE_RISING, &handleClick) < 0) {
fprintf(stderr, "Unable to create flow meter interrupt: %s\n", strerror(errno));
return 1;
}
pinMode(RESET_BUTTON, INPUT);
pullUpDnControl (RESET_BUTTON, PUD_UP);
/*
if (wiringPiISR(RESET_BUTTON, INT_EDGE_FALLING, &handleReset) < 0) {
fprintf(stderr, "Unable to create pushbutton interrupt: %s\n", strerror(errno));
return 1;
}
*/
// Set up output for relay and fire it up
pinMode(POWER_RELAY, OUTPUT);
// pinMode(PRESSURE_SENSOR, INPUT);
digitalWrite(POWER_RELAY, HIGH);
while (1) {
now = time(0);
/*
* When we first fire up - counting is false, also after a rest
* or after a period of inactivity, we set counting to false.
* If we are counting (flow is happening) we need to check against
* time of day and
*/
// pressure = analogRead(PRESSURE_SENSOR);
// printLog(3, "Pressure returns %d\n", pressure);
seconds = now - last_click_time;
new_clicks = clicks - last_click_count;
last_click_count = clicks;
total_clicks += new_clicks;
total_litres = total_clicks / clicks_per_litre;
litres = clicks / clicks_per_litre;
printLog(3, "clicks: %d, litres: %d, triggered=%d, counting=%d, new=%d\n", clicks, litres, triggered, counting, new_clicks);
if (triggered && digitalRead(RESET_BUTTON) == 0) {
reset = 1;
}
if (reset) {
triggered = 0;
clicks = 0;
counting = 0;
last_click_count = 0;
new_clicks = 0;
last_click_time = now;
first_click_time = now;
printLog(2, "Turning pump on after reset by %s\n", reset_msg[reset]);
writeState("started\t%s\n", reset_msg[reset]);
reset = 0;
digitalWrite(POWER_RELAY, HIGH);
}
if (!triggered) {
if (counting) {
if (! new_clicks ) {
if (seconds > reset_period) {
counting = 0;
clicks = 0;
last_click_count = 0;
}
} else {
last_click_time = now;
seconds_from_first = last_click_time - first_click_time;
stop_reason = 0;
if (litres > max_litres) {
stop_reason = 1;
}
if (seconds_from_first > time_limit) {
stop_reason = 2;
}
if (stop_reason) {
triggered = 1;
printLog(2,"Turning pump off (%s) litres:%d, seconds:%d\n", stop_msg[stop_reason], litres, seconds_from_first);
writeState("stopped\t%s\n", stop_msg[stop_reason]);
showStats(2);
digitalWrite(POWER_RELAY, LOW);
}
}
} else { // Not counting yet
if ( new_clicks) {
counting = 1;
first_click_time = now;
last_click_time = now;
}
}
}
delay(1000);
}
writeState("stopped\tshutdown\n");
return 0;
}