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>
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" ;
96104static 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
99118const 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+
0 commit comments