-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhousesprinkler_interval.c
More file actions
145 lines (124 loc) · 4.46 KB
/
housesprinkler_interval.c
File metadata and controls
145 lines (124 loc) · 4.46 KB
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
/* 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_interval.c - Manage variable schedule intervals.
*
* SYNOPSYS:
*
* void housesprinkler_interval_refresh (void);
*
* This function must be called each time the sprinkler configuration
* has been changed. It converts the configured interval scales into
* activable ones.
*
* int housesprinkler_interval_exists (const char *name);
*
* Return 1 if the named interval scale exists, 0 otherwise. This is
* used to validate any part of the HouseSprinkler's configuration that
* references an interval scale.
*
* int housesprinkler_interval_get (const char *name, int index);
*
* Return the current interval value based on the named scale and the
* current watering index value.
*/
#include <string.h>
#include <stdlib.h>
#include <echttp.h>
#include <echttp_json.h>
#include "houselog.h"
#include "houseconfig.h"
#include "housesprinkler.h"
#include "housesprinkler_interval.h"
#define DEBUG if (sprinkler_isdebug()) printf
#define SCALE_LIMIT 11 // from 0 to 100, in steps of 10.
typedef struct {
const char *name;
int count;
int byindex[SCALE_LIMIT];
} SprinklerIntervals;
static SprinklerIntervals *Intervals = 0;
static int IntervalsCount = 0;
static int housesprinkler_interval_find (const char *name) {
int i;
if (!name) return -1;
for (i = 0; i < IntervalsCount; ++i) {
if (strcmp (Intervals[i].name, name) == 0) return i;
}
return -1;
}
int housesprinkler_interval_exists (const char *name) {
return housesprinkler_interval_find (name) >= 0;
}
void housesprinkler_interval_refresh (void) {
int i, j;
int count;
int content;
// Reload all intervals.
//
if (Intervals) free (Intervals);
Intervals = 0;
IntervalsCount = 0;
content = houseconfig_array (0, ".intervals");
if (content > 0) {
IntervalsCount = houseconfig_array_length (content);
if (IntervalsCount > 0) {
Intervals = calloc (IntervalsCount, sizeof(SprinklerIntervals));
}
}
DEBUG ("Loading %d interval scales\n", IntervalsCount);
int *list = calloc (IntervalsCount, sizeof(int));
houseconfig_enumerate (content, list, IntervalsCount);
for (i = 0; i < IntervalsCount; ++i) {
int scale = houseconfig_object (list[i], 0);
if (scale > 0) {
Intervals[i].name = houseconfig_string (scale, ".name");
if (!Intervals[i].name) continue; // Bad entry.
int index = houseconfig_array (scale, ".byindex");
if (index <= 0) {
DEBUG ("Bad interval scale array\n");
continue;
}
count = houseconfig_array_length(index);
if (count > SCALE_LIMIT) {
DEBUG ("Interval scale %s: array of %d truncated to %d\n",
Intervals[i].name, count, SCALE_LIMIT);
count = SCALE_LIMIT;
}
int innerlist[SCALE_LIMIT];
count = houseconfig_enumerate (index, innerlist, count);
for (j = 0; j < count; ++j) {
Intervals[i].byindex[j] =
houseconfig_positive (innerlist[j], 0);
}
Intervals[i].count = count;
DEBUG ("\tInterval %s loaded (%d items).\n", Intervals[i].name, count);
}
}
free (list);
}
int housesprinkler_interval_get (const char *name, int index) {
int scale = housesprinkler_interval_find (name);
if (scale < 0) return 0; // No interval scale, just assume every day.
index /= 10;
if (index >= SCALE_LIMIT) index = SCALE_LIMIT - 1;
else if (index < 0) index = 0; // Better safe than sorry.
return Intervals[scale].byindex[index];
}