-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhousesprinkler_feed.c
171 lines (153 loc) · 5.36 KB
/
housesprinkler_feed.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
/* 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_feed.c - Control the sprinkler system feeds.
*
* SYNOPSYS:
*
* A feed is meant to turn on and off various devices that are not
* directly sprinkler valves, but are needed for the watering:
* water pumps,
* 24 volt power supply for the solenoids,
* etc.
*
* void housesprinkler_feed_refresh (void);
*
* This function must be called each time the configuration changes.
*
* void housesprinkler_feed_activate (const char *name,
* int pulse, const char *context);
*
* Turn the feed on for the specified time. This activates the specified
* feed, and all feeds chained to this one.
*
* The context string indicates what caused the feed to be turned on. If
* null, the default cause is "manual". The cause is typically a program.
*/
#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_feed.h"
#include "housesprinkler_config.h"
#include "housesprinkler_control.h"
#define DEBUG if (sprinkler_isdebug()) printf
typedef struct {
const char *name;
const char *next;
char manual;
char linger;
} SprinklerFeed;
static SprinklerFeed *Feed = 0;
static int FeedCount = 0;
static SprinklerFeed *housesprinkler_feed_search (const char *name) {
int i;
for (i = 0; i < FeedCount; ++i) {
if (!strcmp(name, Feed[i].name)) return Feed+i;
}
return 0;
}
void housesprinkler_feed_refresh (void) {
int i;
int count;
int content;
char path[128];
int list[256];
// Reload all feed items.
//
if (Feed) free (Feed);
Feed = 0;
FeedCount = 0;
content = housesprinkler_config_array (0, ".feeds");
if (content > 0) {
FeedCount = housesprinkler_config_array_length (content);
if (FeedCount > 0) {
Feed = calloc (FeedCount, sizeof(SprinklerFeed));
DEBUG ("Loading %d feed items\n", FeedCount);
}
}
for (i = 0; i < FeedCount; ++i) {
snprintf (path, sizeof(path), "[%d]", i);
int item = housesprinkler_config_object (content, path);
if (item > 0) {
Feed[i].name = housesprinkler_config_string (item, ".name");
Feed[i].next = housesprinkler_config_string (item, ".next");
Feed[i].linger = housesprinkler_config_integer (item, ".linger");
Feed[i].manual = housesprinkler_config_boolean (item, ".manual");
}
housesprinkler_control_declare (Feed[i].name, "FEED");
housesprinkler_control_event (Feed[i].name, 0, 0);
DEBUG ("\tFeed %s (manual=%s)\n",
Feed[i].name, Feed[i].manual?"true":"false");
}
// Detect loops in chains. Having any is bad.
for (i = 0; i < FeedCount; ++i) {
int loop = 0;
const char *name = Feed[i].next;
const char *previous = Feed[i].name;
while (name && name[0]) {
SprinklerFeed *feed = housesprinkler_feed_search (name);
if (!feed) {
houselog_event
("FEED", previous, "INVALID", "UNKNOWN NEXT %s", name);
break;
}
previous = name;
name = feed->next;
if (++loop >= FeedCount) {
houselog_event
("FEED", Feed[i].name, "INVALID", "INFINITE LOOP IN CHAIN");
break;
}
}
}
}
void housesprinkler_feed_activate (const char *name,
int pulse, const char *context) {
const char *previous = 0;
int loop = 0;
while (name && name[0]) {
SprinklerFeed *feed = housesprinkler_feed_search (name);
if (!feed) {
if (previous)
houselog_event
("FEED", previous, "INVALID", "UNKNOWN NEXT %s", name);
else
houselog_event ("FEED", name, "UNKNOWN", "");
return;
}
if (!feed->manual) {
// No context means manually operated, i.e. a zone test.
// In this case we generate an event (once) to help with
// testing. Otherwise feed events just add noise.
//
if ((!context) || (context[0] == 0))
housesprinkler_control_event (name, 1, 1);
housesprinkler_control_start (name, pulse + feed->linger, context);
}
previous = name;
name = feed->next;
if (++loop >= FeedCount) break; // We went through all feeds.
}
}