WordClockESP/lib/framework/SettingsService.h

83 lines
2.5 KiB
C
Raw Normal View History

#ifndef SettingsService_h
#define SettingsService_h
#if defined(ESP8266)
#include <ESP8266WiFi.h>
#include <ESPAsyncTCP.h>
#elif defined(ESP_PLATFORM)
#include <AsyncTCP.h>
#include <WiFi.h>
#endif
#include <ArduinoJson.h>
#include <AsyncJson.h>
#include <AsyncJsonCallbackResponse.h>
#include <AsyncJsonWebHandler.h>
#include <ESPAsyncWebServer.h>
#include <SecurityManager.h>
#include <SettingsPersistence.h>
/*
* Abstraction of a service which stores it's settings as JSON in a file system.
*/
2019-11-30 13:42:47 +00:00
class SettingsService : public SettingsPersistence {
public:
SettingsService(AsyncWebServer* server, FS* fs, char const* servicePath, char const* filePath) :
SettingsPersistence(fs, filePath),
_servicePath(servicePath) {
server->on(_servicePath, HTTP_GET, std::bind(&SettingsService::fetchConfig, this, std::placeholders::_1));
_updateHandler.setUri(servicePath);
_updateHandler.setMethod(HTTP_POST);
_updateHandler.setMaxContentLength(MAX_SETTINGS_SIZE);
_updateHandler.onRequest(
std::bind(&SettingsService::updateConfig, this, std::placeholders::_1, std::placeholders::_2));
server->addHandler(&_updateHandler);
}
virtual ~SettingsService() {
}
2019-11-30 13:42:47 +00:00
void begin() {
// read the initial data from the file system
readFromFS();
}
2019-05-29 22:48:16 +00:00
protected:
2019-11-30 13:42:47 +00:00
char const* _servicePath;
2019-05-18 18:35:27 +00:00
AsyncJsonWebHandler _updateHandler;
virtual void fetchConfig(AsyncWebServerRequest* request) {
2019-05-29 22:48:16 +00:00
// handle the request
AsyncJsonResponse* response = new AsyncJsonResponse(false, MAX_SETTINGS_SIZE);
JsonObject jsonObject = response->getRoot();
writeToJsonObject(jsonObject);
response->setLength();
request->send(response);
}
virtual void updateConfig(AsyncWebServerRequest* request, JsonDocument& jsonDocument) {
2019-05-29 22:48:16 +00:00
// handle the request
if (jsonDocument.is<JsonObject>()) {
JsonObject newConfig = jsonDocument.as<JsonObject>();
readFromJsonObject(newConfig);
writeToFS();
// write settings back with a callback to reconfigure the wifi
AsyncJsonCallbackResponse* response =
new AsyncJsonCallbackResponse([this]() { onConfigUpdated(); }, false, MAX_SETTINGS_SIZE);
JsonObject jsonObject = response->getRoot();
writeToJsonObject(jsonObject);
response->setLength();
request->send(response);
2019-11-30 13:42:47 +00:00
} else {
request->send(400);
}
}
2019-05-29 22:48:16 +00:00
// implement to perform action when config has been updated
virtual void onConfigUpdated() {
}
2019-05-29 22:48:16 +00:00
};
#endif // end SettingsService