-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconfig.cpp
83 lines (68 loc) · 1.91 KB
/
config.cpp
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
#include <Arduino.h>
#include <ArduinoJson.h>
#include "config.h"
static FS *_fs;
static StaticJsonDocument < 1024 > _jsonDoc;
static String _config_name;
static String _config_path;
static String _config_page;
static int _version = 0;
void config_begin(FS & fs, String config_name)
{
_fs = &fs;
_config_name = config_name; // e.g. "config.json"
}
static void handleGetConfig(AsyncWebServerRequest *request)
{
// serve the config page from flash
request->send(*_fs, _config_page, "text/html");
}
static void handlePostConfig(AsyncWebServerRequest *request)
{
// put all parameters in the JSON document
for (size_t i = 0; i < request->params(); i++) {
const AsyncWebParameter *param = request->getParam(i);
String name = param->name();
String value = param->value();
_jsonDoc[name] = value;
}
// write the config file
config_save();
// redirect to page with values
request->redirect(_config_path);
}
void config_serve(AsyncWebServer & server, const char *config_path, const char *config_page)
{
_config_path = config_path; // e.g. "/config"
_config_page = config_page; // e.g "/config.html"
// register ourselves with the server
server.on(config_path, HTTP_GET, handleGetConfig);
server.on(config_path, HTTP_POST, handlePostConfig);
}
bool config_load(void)
{
File file = _fs->open(_config_name, "r");
DeserializationError error = deserializeJson(_jsonDoc, file);
file.close();
return error == DeserializationError::Ok;
}
bool config_save(void)
{
File file = _fs->open(_config_name, "w");
size_t size = serializeJson(_jsonDoc, file);
file.close();
_version++;
return size > 0;
}
void config_set_value(String propName, String propValue)
{
_jsonDoc[propName] = propValue;
}
String config_get_value(String propName)
{
return _jsonDoc[propName] | "";
}
int config_get_version(void)
{
return _version;
}