Skip to content

Commit f59292d

Browse files
Save system-wide variable on change
1 parent f94615d commit f59292d

File tree

4 files changed

+82
-4
lines changed

4 files changed

+82
-4
lines changed

housesprinkler_config.c

Lines changed: 68 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,13 @@
6363
*
6464
* Get the name of the current configuration file, for informational
6565
* purpose (e.g. error messages).
66+
*
67+
* long housesprinkler_config_backup_get (const char *path);
68+
* void housesprinkler_config_backup_set (const char *path, long value);
69+
*
70+
* Read from, and write to, the backup file. This backup file contains
71+
* a handful of saved values that are system-wide and can be changed
72+
* from the user interface. They are all integer type.
6673
*/
6774

6875
#include <string.h>
@@ -76,6 +83,7 @@
7683
#include <echttp_json.h>
7784

7885
#include "houselog.h"
86+
7987
#include "housesprinkler.h"
8088
#include "housesprinkler_config.h"
8189

@@ -95,18 +103,52 @@ static const char FactoryDefaultsConfigFile[] =
95103
"/usr/local/share/house/public/sprinkler/defaults.json";
96104
static int UseFactoryDefaults = 0;
97105

106+
#define BACKUPMAXSIZE 32
107+
108+
static ParserToken BackupParsed[BACKUPMAXSIZE];
109+
static int BackupTokenCount = 0;
110+
static char *BackupText = 0;
111+
112+
static const char *BackupFile = "/etc/house/sprinklerbkp.json";
113+
114+
static const char FactoryBackupFile[] =
115+
"/usr/local/share/house/public/sprinkler/backup.json";
116+
98117

99118
const char *housesprinkler_config_load (int argc, const char **argv) {
100119

101120
struct stat filestat;
102121
int fd;
103-
const char *configname;
104122
char *newconfig;
105123

106124
int i;
107125
for (i = 1; i < argc; ++i) {
108-
if (echttp_option_match ("-config=", argv[i], &configname))
109-
ConfigFile = strdup(configname);
126+
if (echttp_option_match ("-config=", argv[i], &ConfigFile)) continue;
127+
if (echttp_option_match ("-backup=", argv[i], &BackupFile)) continue;
128+
}
129+
130+
DEBUG ("Loading backup from %s\n", BackupFile);
131+
if (BackupText) echttp_parser_free (BackupText);
132+
BackupText = 0;
133+
BackupTokenCount = 0;
134+
newconfig = echttp_parser_load (BackupFile);
135+
if (!newconfig) {
136+
DEBUG ("Loading backup from %s\n", FactoryBackupFile);
137+
newconfig = echttp_parser_load (FactoryBackupFile);
138+
houselog_event ("SYSTEM", "BACKUP", "LOAD",
139+
"FILE %s", FactoryBackupFile);
140+
} else {
141+
houselog_event ("SYSTEM", "BACKUP", "LOAD",
142+
"FILE %s", BackupFile);
143+
}
144+
if (newconfig) {
145+
BackupText = newconfig;
146+
BackupTokenCount = BACKUPMAXSIZE;
147+
if (echttp_json_parse (BackupText, BackupParsed, &BackupTokenCount)) {
148+
echttp_parser_free (BackupText);
149+
BackupText = 0;
150+
BackupTokenCount = 0;
151+
}
110152
}
111153

112154
DEBUG ("Loading config from %s\n", ConfigFile);
@@ -247,3 +289,26 @@ const char *housesprinkler_config_name (void) {
247289
return ConfigFile;
248290
}
249291

292+
long housesprinkler_config_backup_get (const char *path) {
293+
294+
int i = echttp_json_search(BackupParsed, path);
295+
return (i >= 0) ? BackupParsed[i].value.integer : 0;
296+
}
297+
298+
void housesprinkler_config_backup_set (const char *path, long value) {
299+
300+
char buffer[1024];
301+
int i = echttp_json_search(BackupParsed, path);
302+
303+
if (i < 0) return;
304+
BackupParsed[i].value.integer = value;
305+
306+
if (echttp_json_format (BackupParsed, BackupTokenCount,
307+
buffer, sizeof(buffer), 0)) return;
308+
309+
int fd = open (BackupFile, O_WRONLY|O_TRUNC|O_CREAT, 0777);
310+
if (fd < 0) return;
311+
write (fd, buffer, strlen(buffer));
312+
close(fd);
313+
}
314+

housesprinkler_config.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,6 @@ int housesprinkler_config_object (int parent, const char *path);
3838

3939
const char *housesprinkler_config_name (void);
4040

41+
long housesprinkler_config_backup_get (const char *path);
42+
void housesprinkler_config_backup_set (const char *path, long value);
43+

housesprinkler_program.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,9 @@ void housesprinkler_program_refresh (void) {
299299
Programs[i].name, Programs[i].start.hour, Programs[i].start.minute, count);
300300
}
301301
}
302+
303+
SprinklerState = housesprinkler_config_backup_get (".on");
304+
ProgramRainDelay = (time_t)housesprinkler_config_backup_get (".raindelay");
302305
}
303306

304307
void housesprinkler_program_index (int state) {
@@ -315,7 +318,10 @@ void housesprinkler_program_set_index
315318

316319
void housesprinkler_program_rain (int enabled) {
317320
ProgramRainEnabled = enabled;
318-
if (!enabled) ProgramRainDelay;
321+
if (!enabled) {
322+
ProgramRainDelay = 0;
323+
housesprinkler_config_backup_set (".raindelay", 0);
324+
}
319325
}
320326

321327
void housesprinkler_program_set_rain (int delay) {
@@ -337,6 +343,7 @@ void housesprinkler_program_set_rain (int delay) {
337343
// This is an extension to the ongoing rain delay period.
338344
ProgramRainDelay += delay;
339345
}
346+
housesprinkler_config_backup_set (".raindelay", (long)ProgramRainDelay);
340347
}
341348

342349
static void housesprinkler_program_activate
@@ -513,6 +520,7 @@ void housesprinkler_program_switch (void) {
513520
time_t now = time(0);
514521
SprinklerState = !SprinklerState;
515522
houselog_event ("PROGRAM", "SWITCH", SprinklerState?"ON":"OFF", "");
523+
housesprinkler_config_backup_set (".on", SprinklerState);
516524
}
517525

518526
int housesprinkler_program_status (char *buffer, int size) {

public/backup.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
{"on":1,"raindelay":0}
2+

0 commit comments

Comments
 (0)