-
Notifications
You must be signed in to change notification settings - Fork 0
/
housesprinkler_schedule.c
414 lines (348 loc) · 13.8 KB
/
housesprinkler_schedule.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
/* housesprinkler - A simple home web server for sprinkler control
*
* Copyright 2023, 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_schedule.c - Manage the watering schedules.
*
* A watering schedule defines when each program is launched (automatic
* start). That launch is subject to a few conditions: on/off and rain delay.
*
* SYNOPSYS:
*
* void housesprinkler_schedule_refresh (void);
*
* This function must be called each time the sprinkler configuration
* has been changed. It converts the configured schedules into activable
* ones.
*
* void housesprinkler_schedule_switch (void);
*
* Alternate the sprinkler system between on and off.
*
* void housesprinkler_schedule_rain (int enabled);
*
* Enable or disable the rain delay feature. This is independent of
* the current rain delay value, which might be calculated automatically.
*
* void housesprinkler_schedule_set_rain (int delay);
*
* Add to the rain delay used to cancel scheduled programs during rain
* periods.
*
* void housesprinkler_schedule_periodic (time_t now);
*
* This is the heart of the sprinkler function: activate watering
* programs automatically, based on the schedule.
*
* int housesprinkler_schedule_status (char *buffer, int size);
*
* Report the status of this module as a JSON string.
*/
#include <string.h>
#include <stdlib.h>
#include <uuid/uuid.h>
#include <echttp.h>
#include <echttp_json.h>
#include "houselog.h"
#include "housesprinkler.h"
#include "housesprinkler_time.h"
#include "housesprinkler_config.h"
#include "housesprinkler_program.h"
#include "housesprinkler_schedule.h"
#define DEBUG if (sprinkler_isdebug()) printf
static int SprinklerOn = 1;
typedef struct {
uuid_t id;
const char *program;
char disabled;
time_t begin;
time_t until;
struct {
short hour;
short minute;
} start;
char days[7];
char interval;
time_t lastlaunch;
} SprinklerSchedule;
static SprinklerSchedule *Schedules = 0;
static int SchedulesCount = 0;
static int WateringIndexState = 1;
static int WateringIndex = 100;
static const char *WateringIndexOrigin = 0;
static int WateringIndexTimestamp = 0;
static int RainDelayEnabled = 1;
static time_t RainDelay = 0;
static time_t housesprinkler_schedule_time (int index, const char *path) {
struct tm local = {0};
const char *p;
const char *date = housesprinkler_config_string (index, path);
if (date) {
local.tm_mon = atoi(date) - 1;
p = strchr (date, '/');
if (p) {
local.tm_mday = atoi (p+1);
p = strchr (p+1, '/');
if (p) {
local.tm_year = atoi(p+1);
if (local.tm_year < 100) {
local.tm_year += 100; // Post 2000.
} else if (local.tm_year >= 2000) {
local.tm_year -= 1900; // 4 digits year.
}
local.tm_isdst = -1;
return mktime (&local);
}
}
}
return 0;
}
void housesprinkler_schedule_refresh (void) {
int i, j;
int count;
int content;
char path[128];
const char *programname = ".program";
housesprinkler_config_backup_register (housesprinkler_schedule_status);
// Keep the old schedule set on the side, to recover some live data.
SprinklerSchedule *oldschedules = Schedules;
int oldschedulescount = SchedulesCount;
// Recalculate all watering schedules.
Schedules = 0;
SchedulesCount = 0;
content = housesprinkler_config_array (0, ".schedules");
if (content <= 0) {
// Compatibility with previous generation of HouseSprinkler configs.
DEBUG ("No schedules, loading from programs\n");
content = housesprinkler_config_array (0, ".programs");
programname = ".name";
}
if (content > 0) {
SchedulesCount = housesprinkler_config_array_length (content);
if (SchedulesCount > 0) {
Schedules = calloc (SchedulesCount, sizeof(SprinklerSchedule));
DEBUG ("Loading %d schedules\n", SchedulesCount);
}
}
for (i = 0; i < SchedulesCount; ++i) {
snprintf (path, sizeof(path), "[%d]", i);
int schedule = housesprinkler_config_object (content, path);
if (schedule <= 0) {
DEBUG ("\tNo schedule at index %d\n", i);
continue;
}
Schedules[i].program = housesprinkler_config_string (schedule, programname);
if (!Schedules[i].program) {
DEBUG ("\tSchedule with no name at index %d\n", i);
continue;
}
// Retrieve the schedule's ID. If none can be recovered, just
// generate a new ID so that there is always one.
//
const char *id = housesprinkler_config_string (schedule, ".id");
if (id) {
if (uuid_parse (id, Schedules[i].id)) {
uuid_generate_random (Schedules[i].id);
}
} else {
uuid_generate_random (Schedules[i].id);
}
int days = housesprinkler_config_array (schedule, ".days");
j = 0;
if (days > 0) {
int daylist[8];
int daycount = housesprinkler_config_enumerate (days, daylist);
for (j = 0; j < daycount; ++j) {
Schedules[i].days[j] =
housesprinkler_config_boolean (daylist[j], "");
}
}
for (; j < 8; ++j) Schedules[i].days[j] = 0;
Schedules[i].interval = housesprinkler_config_integer (schedule, ".interval");
Schedules[i].begin = housesprinkler_schedule_time (schedule, ".begin");
Schedules[i].until = housesprinkler_schedule_time (schedule, ".until");
const char *s = housesprinkler_config_string (schedule, ".start");
if (s) {
Schedules[i].start.hour = atoi(s);
const char *m = strchr (s, ':');
if (m) Schedules[i].start.minute = atoi(m+1);
} else {
Schedules[i].start.hour = -1; // Will never start, basically.
}
Schedules[i].lastlaunch = 0;
Schedules[i].disabled =
housesprinkler_config_boolean (schedule, ".disabled");
DEBUG ("\tSchedule program %s at %02d:%02d\n",
Schedules[i].program, Schedules[i].start.hour, Schedules[i].start.minute);
}
SprinklerOn = housesprinkler_config_backup_get (".on");
RainDelay = (time_t)housesprinkler_config_backup_get (".raindelay");
if (RainDelay < time(0)) RainDelay = 0; // Expired.
// Last step: recover the last launch time of already existing schedules.
//
if (oldschedules) {
for (i = 0; i < oldschedulescount; ++i) {
if (oldschedules[i].disabled) continue; // Nothing to recover.
for (j = 0; j < SchedulesCount; ++j) {
if (uuid_compare (oldschedules[i].id, Schedules[j].id)) continue;
Schedules[j].lastlaunch = oldschedules[i].lastlaunch;
DEBUG ("Schedule %d (%s at %02d:%02d) recovers data from %d (%s at %02d:%02d)\n", j, Schedules[j].program, Schedules[j].start.hour, Schedules[j].start.minute, i, oldschedules[i].program, oldschedules[i].start.hour, oldschedules[i].start.minute);
break;
}
}
free (oldschedules);
return;
}
// Program start (this is the first time the configuration is loaded):
// recover the last launch times from the backup.
//
i = 0;
for (;;) {
uuid_t uuid;
snprintf (path, sizeof(path), ".schedule[%d].id", i);
const char *id = housesprinkler_config_backup_get_string (path);
if (!id) break;
uuid_parse (id, uuid);
for (j = 0; j < SchedulesCount; ++j) {
if (uuid_compare (uuid, Schedules[j].id)) continue;
snprintf (path, sizeof(path), ".schedule[%d].launched", i);
Schedules[j].lastlaunch = housesprinkler_config_backup_get (path);
DEBUG ("Schedule %d (%s at %02d:%02d) recovers data from backup: lastlaunch = %ld\n", j, Schedules[j].program, Schedules[j].start.hour, Schedules[j].start.minute, (long)(Schedules[j].lastlaunch));
break;
}
i += 1;
}
}
void housesprinkler_schedule_switch (void) {
time_t now = time(0);
SprinklerOn = !SprinklerOn;
houselog_event ("PROGRAM", "SWITCH", SprinklerOn?"ON":"OFF", "");
housesprinkler_config_backup_changed();
}
void housesprinkler_schedule_rain (int enabled) {
if (RainDelayEnabled == enabled) return; // No change.
RainDelayEnabled = enabled;
if (!enabled) {
if (RainDelay > time(0)) {
RainDelay = 0;
housesprinkler_config_backup_changed();
}
}
houselog_event ("SYSTEM", "RAIN DELAY", enabled?"ENABLED":"DISABLED", "");
}
void housesprinkler_schedule_set_rain (int delay) {
if (!RainDelayEnabled) return;
time_t now = time(0);
if (delay == 0) {
RainDelay = 0; // Cancel.
houselog_event ("SYSTEM", "RAIN DELAY", "OFF", "");
} else if (RainDelay < now) {
// This is a new rain delay period.
RainDelay = now + delay;
houselog_event ("SYSTEM", "RAIN DELAY", "ON",
housesprinkler_time_delta_printable (now, RainDelay));
} else {
// This is an extension to the ongoing rain delay period.
RainDelay += delay;
houselog_event ("SYSTEM", "RAIN DELAY", "EXTENDED",
housesprinkler_time_delta_printable (now, RainDelay));
}
housesprinkler_config_backup_changed();
}
void housesprinkler_schedule_periodic (time_t now) {
static char lasthour = -1;
static char lastminute = -1;
if (!SprinklerOn) return;
struct tm local = *localtime(&now);
if (local.tm_hour == lasthour && local.tm_min == lastminute) return;
lasthour = local.tm_hour;
lastminute = local.tm_min;
if ((RainDelay > 0) && (RainDelay < now)) {
RainDelay = 0; // No need to save: what was saved is an expired value anyway.
houselog_event ("SYSTEM", "RAIN DELAY", "EXPIRED", "");
}
if (RainDelay > 0) return;
DEBUG ("== Checking schedule at %02d:%02d\n", local.tm_hour, local.tm_min);
int i;
for (i = 0; i < SchedulesCount; ++i) {
SprinklerSchedule *schedule = Schedules + i;
DEBUG ("== Checking schedule for program %s\n", schedule->program);
if (schedule->disabled) continue;
DEBUG ("== program %s is enabled\n", schedule->program);
if (housesprinkler_program_running(schedule->program)) continue;
DEBUG ("== Program %s is not running\n", schedule->program);
// Start only at the time specified.
//
DEBUG ("== Program %s: must start at %02d:%02d\n", schedule->program, schedule->start.hour, schedule->start.minute);
if (local.tm_hour != schedule->start.hour
|| local.tm_min != schedule->start.minute) continue;
DEBUG ("== Program %s: time of day matches\n", schedule->program);
// Start only if the schedule entry is active at that time.
//
if (schedule->begin > now) continue;
if (schedule->until > 0 && schedule->until < now) continue;
DEBUG ("== Program %s: schedule is active\n", schedule->program);
// Start only on the days specified.
//
if (!schedule->days[local.tm_wday]) continue;
DEBUG ("== Program %s: schedule is for this day\n", schedule->program);
// Start only after the specified day interval has passed.
// We use a 6 hours (21600 sec) leniency to account for changes
// to the schedule start time, for example when the start time is
// changed to be a few hours early.
//
if (schedule->interval > 1) {
if (((now - schedule->lastlaunch + 21600) / 86400) < schedule->interval) continue;
}
DEBUG ("== Program %s: interval %d has passed\n", schedule->program, schedule->interval);
DEBUG ("== Program %s activated at %ld\n", schedule->program, (long)now);
housesprinkler_program_start_scheduled (schedule->program);
schedule->lastlaunch = now;
housesprinkler_config_backup_changed();
}
}
int housesprinkler_schedule_status (char *buffer, int size) {
int cursor = 0;
cursor = snprintf (buffer, size,
"\"on\":%s", SprinklerOn?"true":"false");
if (cursor >= size) goto overflow;
if (RainDelayEnabled) {
cursor += snprintf (buffer+cursor, size-cursor,
",\"raindelay\":%d", RainDelay);
if (cursor >= size) goto overflow;
}
int i;
char ascii[40];
const char *sep = ",\"schedule\":[";
for (i = 0; i < SchedulesCount; ++i) {
if (!Schedules[i].lastlaunch) continue;
uuid_unparse (Schedules[i].id, ascii);
cursor += snprintf (buffer+cursor, size-cursor, "%s{\"id\":\"%s\",\"launched\":%ld}",
sep, ascii, (long)(Schedules[i].lastlaunch));
if (cursor >= size) return cursor;
sep = ",";
}
if (sep[1] == 0)
cursor += snprintf (buffer+cursor,size-cursor,"]");
return cursor;
overflow:
houselog_trace (HOUSE_FAILURE, "BUFFER", "overflow");
buffer[0] = 0;
return 0;
}