7
7
#elif defined(ESP_PLATFORM)
8
8
#include < WiFi.h>
9
9
#include < AsyncTCP.h>
10
- #include < SPIFFS.h>
11
10
#endif
12
11
12
+ #include < SettingsPersistence.h>
13
13
#include < ESPAsyncWebServer.h>
14
- #include < FS.h>
15
14
#include < AsyncJson.h>
16
15
#include < ArduinoJson.h>
17
16
#include < AsyncJsonRequestWebHandler.h>
18
17
#include < AsyncJsonCallbackResponse.h>
19
18
20
- /* *
21
- * At the moment, not expecting settings service to have to deal with large JSON
22
- * files this could be made configurable fairly simply, it's exposed on
23
- * AsyncJsonRequestWebHandler with a setter.
24
- */
25
- #define MAX_SETTINGS_SIZE 1024
26
-
27
19
/*
28
- * Abstraction of a service which stores it's settings as JSON in SPIFFS .
20
+ * Abstraction of a service which stores it's settings as JSON in a file system .
29
21
*/
30
- class SettingsService {
22
+ class SettingsService : public SettingsPersistence {
31
23
32
24
private:
33
25
34
- char const * _filePath;
35
-
36
26
AsyncJsonRequestWebHandler _updateHandler;
37
27
38
- bool writeToSPIFFS () {
39
- // create and populate a new json object
40
- DynamicJsonBuffer jsonBuffer;
41
- JsonObject& root = jsonBuffer.createObject ();
42
- writeToJsonObject (root);
43
-
44
- // serialize it to SPIFFS
45
- File configFile = SPIFFS.open (_filePath, " w" );
46
-
47
- // failed to open file, return false
48
- if (!configFile) {
49
- return false ;
50
- }
51
-
52
- root.printTo (configFile);
53
- configFile.close ();
54
-
55
- return true ;
56
- }
57
-
58
- void readFromSPIFFS (){
59
- File configFile = SPIFFS.open (_filePath, " r" );
60
-
61
- // use defaults if no config found
62
- if (configFile) {
63
- // Protect against bad data uploaded to SPIFFS
64
- // We never expect the config file to get very large, so cap it.
65
- size_t size = configFile.size ();
66
- if (size <= MAX_SETTINGS_SIZE) {
67
- DynamicJsonBuffer jsonBuffer;
68
- JsonObject& root = jsonBuffer.parseObject (configFile);
69
- if (root.success ()) {
70
- readFromJsonObject (root);
71
- configFile.close ();
72
- return ;
73
- }
74
- }
75
- configFile.close ();
76
- }
77
-
78
- // If we reach here we have not been successful in loading the config,
79
- // hard-coded emergency defaults are now applied.
80
- applyDefaultConfig ();
81
- }
82
-
83
28
void fetchConfig (AsyncWebServerRequest *request){
84
29
AsyncJsonResponse * response = new AsyncJsonResponse ();
85
30
writeToJsonObject (response->getRoot ());
@@ -91,7 +36,7 @@ class SettingsService {
91
36
if (json.is <JsonObject>()){
92
37
JsonObject& newConfig = json.as <JsonObject>();
93
38
readFromJsonObject (newConfig);
94
- writeToSPIFFS ();
39
+ writeToFS ();
95
40
96
41
// write settings back with a callback to reconfigure the wifi
97
42
AsyncJsonCallbackResponse * response = new AsyncJsonCallbackResponse ([this ] () {onConfigUpdated ();});
@@ -108,28 +53,13 @@ class SettingsService {
108
53
// will serve setting endpoints from here
109
54
AsyncWebServer* _server;
110
55
111
- // will store and retrieve config from the file system
112
- FS* _fs;
113
-
114
- // reads the local config from the
115
- virtual void readFromJsonObject (JsonObject& root){}
116
- virtual void writeToJsonObject (JsonObject& root){}
117
-
118
56
// implement to perform action when config has been updated
119
57
virtual void onConfigUpdated (){}
120
58
121
- // We assume the readFromJsonObject supplies sensible defaults if an empty object
122
- // is supplied, this virtual function allows that to be changed.
123
- virtual void applyDefaultConfig (){
124
- DynamicJsonBuffer jsonBuffer;
125
- JsonObject& root = jsonBuffer.createObject ();
126
- readFromJsonObject (root);
127
- }
128
-
129
59
public:
130
60
131
61
SettingsService (AsyncWebServer* server, FS* fs, char const * servicePath, char const * filePath):
132
- _filePath ( filePath), _server(server), _fs(fs) {
62
+ SettingsPersistence (fs, servicePath, filePath), _server(server) {
133
63
134
64
// configure fetch config handler
135
65
_server->on (servicePath, HTTP_GET, std::bind (&SettingsService::fetchConfig, this , std::placeholders::_1));
@@ -145,7 +75,7 @@ class SettingsService {
145
75
virtual ~SettingsService () {}
146
76
147
77
virtual void begin () {
148
- readFromSPIFFS ();
78
+ readFromFS ();
149
79
}
150
80
151
81
};
0 commit comments