|
20 | 20 | /*
|
21 | 21 | * Abstraction of a service which stores it's settings as JSON in a file system.
|
22 | 22 | */
|
23 |
| -class SettingsService : public SettingsPersistence |
24 |
| -{ |
| 23 | +class SettingsService : public SettingsPersistence { |
25 | 24 |
|
26 |
| -public: |
27 |
| - SettingsService(AsyncWebServer *server, FS *fs, char const *servicePath, char const *filePath) : SettingsPersistence(fs, filePath), _servicePath(servicePath) |
28 |
| - { |
29 |
| - server->on(_servicePath, HTTP_GET, std::bind(&SettingsService::fetchConfig, this, std::placeholders::_1)); |
| 25 | + public: |
30 | 26 |
|
31 |
| - _updateHandler.setUri(servicePath); |
32 |
| - _updateHandler.setMethod(HTTP_POST); |
33 |
| - _updateHandler.setMaxContentLength(MAX_SETTINGS_SIZE); |
34 |
| - _updateHandler.onRequest(std::bind(&SettingsService::updateConfig, this, std::placeholders::_1, std::placeholders::_2)); |
35 |
| - server->addHandler(&_updateHandler); |
36 |
| - } |
| 27 | + SettingsService(AsyncWebServer* server, FS* fs, char const* servicePath, char const* filePath): SettingsPersistence(fs, filePath), _servicePath(servicePath) { |
| 28 | + server->on(_servicePath, HTTP_GET, std::bind(&SettingsService::fetchConfig, this, std::placeholders::_1)); |
37 | 29 |
|
38 |
| - virtual ~SettingsService() {} |
| 30 | + _updateHandler.setUri(servicePath); |
| 31 | + _updateHandler.setMethod(HTTP_POST); |
| 32 | + _updateHandler.setMaxContentLength(MAX_SETTINGS_SIZE); |
| 33 | + _updateHandler.onRequest(std::bind(&SettingsService::updateConfig, this, std::placeholders::_1, std::placeholders::_2)); |
| 34 | + server->addHandler(&_updateHandler); |
| 35 | + } |
39 | 36 |
|
40 |
| - void begin() |
41 |
| - { |
42 |
| - // read the initial data from the file system |
43 |
| - readFromFS(); |
44 |
| - } |
| 37 | + virtual ~SettingsService() {} |
| 38 | + |
| 39 | + void begin() { |
| 40 | + // read the initial data from the file system |
| 41 | + readFromFS(); |
| 42 | + } |
45 | 43 |
|
46 | 44 | protected:
|
47 |
| - char const *_servicePath; |
| 45 | + char const* _servicePath; |
48 | 46 | AsyncJsonWebHandler _updateHandler;
|
49 | 47 |
|
50 |
| - virtual void fetchConfig(AsyncWebServerRequest *request) |
51 |
| - { |
| 48 | + virtual void fetchConfig(AsyncWebServerRequest *request) { |
52 | 49 | // handle the request
|
53 |
| - AsyncJsonResponse *response = new AsyncJsonResponse(false, MAX_SETTINGS_SIZE); |
54 |
| - JsonObject jsonObject = response->getRoot(); |
| 50 | + AsyncJsonResponse * response = new AsyncJsonResponse(false, MAX_SETTINGS_SIZE); |
| 51 | + JsonObject jsonObject = response->getRoot(); |
55 | 52 | writeToJsonObject(jsonObject);
|
56 | 53 | response->setLength();
|
57 | 54 | request->send(response);
|
58 | 55 | }
|
59 | 56 |
|
60 |
| - virtual void updateConfig(AsyncWebServerRequest *request, JsonDocument &jsonDocument) |
61 |
| - { |
| 57 | + virtual void updateConfig(AsyncWebServerRequest *request, JsonDocument &jsonDocument) { |
62 | 58 | // handle the request
|
63 |
| - if (jsonDocument.is<JsonObject>()) |
64 |
| - { |
| 59 | + if (jsonDocument.is<JsonObject>()){ |
65 | 60 | JsonObject newConfig = jsonDocument.as<JsonObject>();
|
66 | 61 | readFromJsonObject(newConfig);
|
67 | 62 | writeToFS();
|
68 | 63 |
|
69 | 64 | // write settings back with a callback to reconfigure the wifi
|
70 |
| - AsyncJsonCallbackResponse *response = new AsyncJsonCallbackResponse([this]() { onConfigUpdated(); }, false, MAX_SETTINGS_SIZE); |
71 |
| - JsonObject jsonObject = response->getRoot(); |
| 65 | + AsyncJsonCallbackResponse * response = new AsyncJsonCallbackResponse([this] () {onConfigUpdated();}, false, MAX_SETTINGS_SIZE); |
| 66 | + JsonObject jsonObject = response->getRoot(); |
72 | 67 | writeToJsonObject(jsonObject);
|
73 | 68 | response->setLength();
|
74 | 69 | request->send(response);
|
75 |
| - } |
76 |
| - else |
77 |
| - { |
| 70 | + } else { |
78 | 71 | request->send(400);
|
79 | 72 | }
|
80 | 73 | }
|
81 | 74 |
|
82 | 75 | // implement to perform action when config has been updated
|
83 |
| - virtual void onConfigUpdated() {} |
| 76 | + virtual void onConfigUpdated(){} |
| 77 | + |
84 | 78 | };
|
85 | 79 |
|
86 | 80 | #endif // end SettingsService
|
0 commit comments