-
Notifications
You must be signed in to change notification settings - Fork 0
/
housesprinkler_control.c
454 lines (396 loc) · 14.2 KB
/
housesprinkler_control.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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
/* housesprinkler - A simple home web server for sprinkler control
*
* Copyright 2020, Pascal Martin
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
*
* housesprinkler_control.c - Interface with the control servers.
*
* SYNOPSYS:
*
* This module handles detection of, and communication with, the control
* servers:
* - Run periodic discoveries to find which server handles each control.
* - Handle the HTTP control requests (and redirects).
*
* Each control is independent of each other: see the zone and feed
* modules for the application logic that applies to controls.
*
* This module remembers which controls are active, so that it does not
* have to stop every known control on cancel.
*
* void housesprinkler_control_reset (void);
*
* Erases the list of known control points.
* This function must be called before applying a new configuration.
*
* void housesprinkler_control_declare (const char *name, const char *type);
*
* Declare a new control point to be discovered.
*
* void housesprinkler_control_event (const char *name, int enable, int once);
*
* Enable or disable activation events for the specified control:
* - If enable and once are both true, events are automatically disabled
* after one event has been issued.
* - If enable is true and once is false, events are enabled until
* explicitly disabled.
* - If enable is false, once is ignored and events are disabled until
* explicitly enabled.
*
* This has no impact on "unusual" events like discovery or stop.
*
* void housesprinkler_control_start (const char *name,
* int pulse, const char *context);
*
* Activate one control for the duration set by pulse. The context is
* typically the name of the schedule, or 0 for manual activation.
*
* void housesprinkler_control_cancel (const char *name);
*
* Immediately stop a control, or all active controls if name is null.
*
* char housesprinkler_control_state (const char *name);
*
* Return the current state of the control.
*
* void housesprinkler_control_periodic (time_t now);
*
* The periodic function that detects the control servers.
*
* int housesprinkler_control_status (char *buffer, int size);
*
* Return the status of control points in JSON format.
*/
#include <string.h>
#include <stdlib.h>
#include <echttp.h>
#include <echttp_json.h>
#include "houselog.h"
#include "housediscover.h"
#include "housesprinkler.h"
#include "housesprinkler_time.h"
#include "housesprinkler_control.h"
#define DEBUG if (sprinkler_isdebug()) printf
static char **Providers = 0;
static int ProvidersCount = 0;
static int ProvidersAllocated = 0;
typedef struct {
const char *name;
const char *type;
char status;
char event;
char once;
time_t deadline;
char url[256];
} SprinklerControl;
static SprinklerControl *Controls = 0;
static int ControlsCount = 0;
static int ControlsSize = 0;
static int ControlsActive = 0;
static SprinklerControl *housesprinkler_control_search (const char *name) {
int i;
if (!Controls) return 0;
for (i = 0; i < ControlsCount; ++i) {
if (!strcmp (name, Controls[i].name)) return Controls+i;
}
return 0;
}
void housesprinkler_control_reset (void) {
ControlsCount = 0;
}
void housesprinkler_control_declare (const char *name, const char *type) {
if (!housesprinkler_control_search(name)) {
if (ControlsCount >= ControlsSize) {
ControlsSize += 16;
Controls = realloc (Controls, ControlsSize*sizeof(SprinklerControl));
if (!Controls) {
houselog_trace (HOUSE_FAILURE, name, "no more memory");
ControlsSize = ControlsCount = 0;
return;
}
}
Controls[ControlsCount].name = name;
Controls[ControlsCount].type = type;
Controls[ControlsCount].status = 'u';
Controls[ControlsCount].event = 1; // enabled.
Controls[ControlsCount].once = 0; // .. until explicitly disabled.
Controls[ControlsCount].deadline = 0;
Controls[ControlsCount].url[0] = 0; // Need to (re)learn.
ControlsCount += 1;
}
}
void housesprinkler_control_event (const char *name, int enable, int once) {
SprinklerControl *control = housesprinkler_control_search(name);
if (control) {
control->event = enable;
control->once = once;
}
}
static void housesprinkler_control_result
(void *origin, int status, char *data, int length) {
SprinklerControl *control = (SprinklerControl *)origin;
status = echttp_redirected("GET");
if (!status) {
echttp_submit (0, 0, housesprinkler_control_result, origin);
return;
}
if (status != 200) {
if (control->status != 'e')
houselog_trace (HOUSE_FAILURE, control->name, "HTTP code %d", status);
control->status = 'e';
control->deadline = 0;
}
}
int housesprinkler_control_start (const char *name,
int pulse, const char *context) {
time_t now = time(0);
SprinklerControl *control = housesprinkler_control_search (name);
if (!control) {
houselog_event ("CONTROL", name, "UNKNOWN", "");
return 0;
}
DEBUG ("%ld: Start %s %s for %d seconds\n", now, control->type, name, pulse);
if (control->url[0]) {
if (!context || context[0] == 0) context = "MANUAL";
if (control->event) {
houselog_event (control->type, name, "ACTIVATED",
"FOR %s USING %s (%s)",
housesprinkler_time_period_printable(pulse),
control->url, context);
if (control->once) {
control->event = 0;
control->once = 0;
}
}
static char url[256];
static char cause[256];
int l = snprintf (cause, sizeof(cause), "%s", "SPRINKLER%20");
echttp_escape (context, cause+l, sizeof(cause)-l);
snprintf (url, sizeof(url),
"%s/set?point=%s&state=on&pulse=%d&cause=%s",
control->url, name, pulse, cause);
const char *error = echttp_client ("GET", url);
if (error) {
houselog_trace (HOUSE_FAILURE, name, "cannot create socket for %s, %s", url, error);
return 0;
}
DEBUG ("GET %s\n", url);
echttp_submit (0, 0, housesprinkler_control_result, (void *)control);
control->deadline = now + pulse;
control->status = 'a';
ControlsActive = 1;
return 1;
}
return 0;
}
static void housesprinkler_control_stop (SprinklerControl *control) {
if (control->url[0]) {
static char url[256];
snprintf (url, sizeof(url),
"%s/set?point=%s&state=off", control->url, control->name);
const char *error = echttp_client ("GET", url);
if (error) {
houselog_trace (HOUSE_FAILURE, control->name, "cannot create socket for %s, %s", url, error);
return;
}
DEBUG ("GET %s\n", url);
echttp_submit (0, 0, housesprinkler_control_result, (void *)control);
control->status = 'i';
}
}
void housesprinkler_control_cancel (const char *name) {
int i;
time_t now = time(0);
if (name) {
SprinklerControl *control = housesprinkler_control_search (name);
if (control) {
houselog_event (control->type, name, "CANCEL", "MANUAL");
housesprinkler_control_stop (control);
control->deadline = 0;
}
return;
}
DEBUG ("%ld: Cancel all zones and feeds\n", now);
for (i = 0; i < ControlsCount; ++i) {
if (Controls[i].deadline) {
housesprinkler_control_stop ( Controls + i);
Controls[i].deadline = 0;
}
}
ControlsActive = 0;
}
char housesprinkler_control_state (const char *name) {
SprinklerControl *control = housesprinkler_control_search (name);
if (!control) return 'e';
return control->status;
}
static void housesprinkler_control_discovered
(void *origin, int status, char *data, int length) {
const char *provider = (const char *) origin;
ParserToken tokens[100];
int innerlist[100];
char path[256];
int count = 100;
int i;
status = echttp_redirected("GET");
if (!status) {
echttp_submit (0, 0, housesprinkler_control_discovered, origin);
return;
}
if (status != 200) {
houselog_trace (HOUSE_FAILURE, provider, "HTTP error %d", status);
return;
}
// Analyze the answer and retrieve the control points matching ours.
const char *error = echttp_json_parse (data, tokens, &count);
if (error) {
houselog_trace
(HOUSE_FAILURE, provider, "JSON syntax error, %s", error);
return;
}
if (count <= 0) {
houselog_trace (HOUSE_FAILURE, provider, "no data");
return;
}
int controls = echttp_json_search (tokens, ".control.status");
if (controls <= 0) {
houselog_trace (HOUSE_FAILURE, provider, "no control data");
return;
}
int n = tokens[controls].length;
if (n <= 0) {
houselog_trace (HOUSE_FAILURE, provider, "empty control data");
return;
}
error = echttp_json_enumerate (tokens+controls, innerlist);
if (error) {
houselog_trace (HOUSE_FAILURE, path, "%s", error);
return;
}
for (i = 0; i < n; ++i) {
ParserToken *inner = tokens + controls + innerlist[i];
SprinklerControl *control = housesprinkler_control_search (inner->key);
if (!control) continue;
if (strcmp (control->url, provider)) {
snprintf (control->url, sizeof(control->url), provider);
control->status = 'i';
houselog_event (control->type, control->name, "ROUTE",
"TO %s", control->url);
}
}
}
static void housesprinkler_control_scan_server
(const char *service, void *context, const char *provider) {
char url[256];
if (ProvidersCount >= ProvidersAllocated) {
ProvidersAllocated += 64;
Providers = realloc (Providers, ProvidersAllocated*(sizeof(char *)));
}
Providers[ProvidersCount++] = strdup(provider); // Keep the string.
snprintf (url, sizeof(url), "%s/status", provider);
DEBUG ("Attempting discovery at %s\n", url);
const char *error = echttp_client ("GET", url);
if (error) {
houselog_trace (HOUSE_FAILURE, provider, "%s", error);
return;
}
echttp_submit (0, 0, housesprinkler_control_discovered, (void *)provider);
}
static void housesprinkler_control_discover (time_t now) {
static time_t latestdiscovery = 0;
if (!now) { // This is a manual reset (force a discovery refresh)
latestdiscovery = 0;
return;
}
// If any new service was detected, force a scan now.
//
if ((latestdiscovery > 0) &&
housediscover_changed ("control", latestdiscovery)) {
latestdiscovery = 0;
}
// Even if nothing new was detected, still scan every minute, in case
// the configuration of a service was changed.
//
if (now <= latestdiscovery + 60) return;
latestdiscovery = now;
// Rebuild the list of control servers, and then launch a discovery
// refresh. This way we don't walk a stale cache while doing discovery.
//
DEBUG ("Reset providers cache\n");
int i;
for (i = 0; i < ProvidersCount; ++i) {
if (Providers[i]) free(Providers[i]);
Providers[i] = 0;
}
ProvidersCount = 0;
DEBUG ("Proceeding with discovery\n");
housediscovered ("control", 0, housesprinkler_control_scan_server);
}
void housesprinkler_control_periodic (time_t now) {
int i;
if (ControlsCount <= 0) return;
if (ControlsActive) {
ControlsActive = 0;
for (i = 0; i < ControlsCount; ++i) {
if (Controls[i].deadline) {
if (Controls[i].deadline < now) {
// No request: it automatically stops on end of pulse.
Controls[i].deadline = 0;
Controls[i].status = 'i';
} else {
ControlsActive = 1;
}
}
}
}
housesprinkler_control_discover (now);
}
int housesprinkler_control_status (char *buffer, int size) {
int i;
int cursor = 0;
const char *prefix = "";
cursor = snprintf (buffer, size, "\"servers\":[");
if (cursor >= size) goto overflow;
for (i = 0; i < ProvidersCount; ++i) {
cursor += snprintf (buffer+cursor, size-cursor,
"%s\"%s\"", prefix, Providers[i]);
if (cursor >= size) goto overflow;
prefix = ",";
}
cursor += snprintf (buffer+cursor, size-cursor, "]");
if (cursor >= size) goto overflow;
cursor += snprintf (buffer+cursor, size-cursor, ",\"controls\":[");
if (cursor >= size) goto overflow;
prefix = "";
time_t now = time(0);
for (i = 0; i < ControlsCount; ++i) {
int remaining =
(Controls[i].status == 'a')?(int)(Controls[i].deadline - now):0;
cursor += snprintf (buffer+cursor, size-cursor, "%s[\"%s\",\"%s\",\"%c\",\"%s\",%d]",
prefix, Controls[i].name, Controls[i].type, Controls[i].status, Controls[i].url, remaining);
if (cursor >= size) goto overflow;
prefix = ",";
}
cursor += snprintf (buffer+cursor, size-cursor, "]");
if (cursor >= size) goto overflow;
return cursor;
overflow:
houselog_trace (HOUSE_FAILURE, "BUFFER", "overflow");
buffer[0] = 0;
return 0;
}