commit
5fa6a1218f
@ -20,12 +20,11 @@
|
|||||||
/*
|
/*
|
||||||
* Abstraction of a service which stores it's settings as JSON in a file system.
|
* Abstraction of a service which stores it's settings as JSON in a file system.
|
||||||
*/
|
*/
|
||||||
class SettingsService : public SettingsPersistence
|
class SettingsService : public SettingsPersistence {
|
||||||
{
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
SettingsService(AsyncWebServer *server, FS *fs, char const *servicePath, char const *filePath) : SettingsPersistence(fs, filePath), _servicePath(servicePath)
|
|
||||||
{
|
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));
|
server->on(_servicePath, HTTP_GET, std::bind(&SettingsService::fetchConfig, this, std::placeholders::_1));
|
||||||
|
|
||||||
_updateHandler.setUri(servicePath);
|
_updateHandler.setUri(servicePath);
|
||||||
@ -37,50 +36,45 @@ public:
|
|||||||
|
|
||||||
virtual ~SettingsService() {}
|
virtual ~SettingsService() {}
|
||||||
|
|
||||||
void begin()
|
void begin() {
|
||||||
{
|
|
||||||
// read the initial data from the file system
|
// read the initial data from the file system
|
||||||
readFromFS();
|
readFromFS();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
char const *_servicePath;
|
char const* _servicePath;
|
||||||
AsyncJsonWebHandler _updateHandler;
|
AsyncJsonWebHandler _updateHandler;
|
||||||
|
|
||||||
virtual void fetchConfig(AsyncWebServerRequest *request)
|
virtual void fetchConfig(AsyncWebServerRequest *request) {
|
||||||
{
|
|
||||||
// handle the request
|
// handle the request
|
||||||
AsyncJsonResponse *response = new AsyncJsonResponse(false, MAX_SETTINGS_SIZE);
|
AsyncJsonResponse * response = new AsyncJsonResponse(false, MAX_SETTINGS_SIZE);
|
||||||
JsonObject jsonObject = response->getRoot();
|
JsonObject jsonObject = response->getRoot();
|
||||||
writeToJsonObject(jsonObject);
|
writeToJsonObject(jsonObject);
|
||||||
response->setLength();
|
response->setLength();
|
||||||
request->send(response);
|
request->send(response);
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual void updateConfig(AsyncWebServerRequest *request, JsonDocument &jsonDocument)
|
virtual void updateConfig(AsyncWebServerRequest *request, JsonDocument &jsonDocument) {
|
||||||
{
|
|
||||||
// handle the request
|
// handle the request
|
||||||
if (jsonDocument.is<JsonObject>())
|
if (jsonDocument.is<JsonObject>()){
|
||||||
{
|
|
||||||
JsonObject newConfig = jsonDocument.as<JsonObject>();
|
JsonObject newConfig = jsonDocument.as<JsonObject>();
|
||||||
readFromJsonObject(newConfig);
|
readFromJsonObject(newConfig);
|
||||||
writeToFS();
|
writeToFS();
|
||||||
|
|
||||||
// write settings back with a callback to reconfigure the wifi
|
// write settings back with a callback to reconfigure the wifi
|
||||||
AsyncJsonCallbackResponse *response = new AsyncJsonCallbackResponse([this]() { onConfigUpdated(); }, false, MAX_SETTINGS_SIZE);
|
AsyncJsonCallbackResponse * response = new AsyncJsonCallbackResponse([this] () {onConfigUpdated();}, false, MAX_SETTINGS_SIZE);
|
||||||
JsonObject jsonObject = response->getRoot();
|
JsonObject jsonObject = response->getRoot();
|
||||||
writeToJsonObject(jsonObject);
|
writeToJsonObject(jsonObject);
|
||||||
response->setLength();
|
response->setLength();
|
||||||
request->send(response);
|
request->send(response);
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
request->send(400);
|
request->send(400);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// implement to perform action when config has been updated
|
// implement to perform action when config has been updated
|
||||||
virtual void onConfigUpdated() {}
|
virtual void onConfigUpdated(){}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // end SettingsService
|
#endif // end SettingsService
|
||||||
|
Loading…
Reference in New Issue
Block a user