Skip to content

Commit d650280

Browse files
committed
remove custom work-around for missing arduinojson6 support - it has since been added to async esp core
1 parent 69caa84 commit d650280

17 files changed

+87
-215
lines changed

lib/framework/APStatus.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ APStatus::APStatus(AsyncWebServer* server, SecurityManager* securityManager) {
77
}
88

99
void APStatus::apStatus(AsyncWebServerRequest *request) {
10-
AsyncJsonResponse * response = new AsyncJsonResponse(MAX_AP_STATUS_SIZE);
10+
AsyncJsonResponse * response = new AsyncJsonResponse(false, MAX_AP_STATUS_SIZE);
1111
JsonObject root = response->getRoot();
1212

1313
WiFiMode_t currentWiFiMode = WiFi.getMode();

lib/framework/APStatus.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
#include <ESPAsyncWebServer.h>
1313
#include <ArduinoJson.h>
14-
#include <AsyncArduinoJson6.h>
14+
#include <AsyncJson.h>
1515
#include <IPAddress.h>
1616
#include <SecurityManager.h>
1717

lib/framework/AsyncArduinoJson6.h

Lines changed: 0 additions & 167 deletions
This file was deleted.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#ifndef _AsyncJsonCallbackResponse_H_
2+
#define _AsyncJsonCallbackResponse_H_
3+
4+
#include <ESPAsyncWebServer.h>
5+
#include <AsyncJson.h>
6+
7+
/*
8+
* Listens for a response being destroyed and calls a callback during said distruction.
9+
* used so we can take action after the response has been rendered to the client.
10+
*
11+
* Avoids having to fork ESPAsyncWebServer with a callback feature, but not nice!
12+
*/
13+
14+
typedef std::function<void()> AsyncJsonCallback;
15+
16+
class AsyncJsonCallbackResponse : public AsyncJsonResponse
17+
{
18+
19+
private:
20+
AsyncJsonCallback _callback;
21+
22+
public:
23+
AsyncJsonCallbackResponse(AsyncJsonCallback callback, bool isArray = false, size_t maxJsonBufferSize = DYNAMIC_JSON_DOCUMENT_SIZE)
24+
: AsyncJsonResponse(isArray, maxJsonBufferSize), _callback{callback} {}
25+
~AsyncJsonCallbackResponse()
26+
{
27+
_callback();
28+
}
29+
};
30+
31+
#endif // end _AsyncJsonCallbackResponse_H_

lib/framework/AuthenticationService.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ void AuthenticationService::signIn(AsyncWebServerRequest *request, JsonDocument
3030
Authentication authentication = _securityManager->authenticate(username, password);
3131
if (authentication.isAuthenticated()) {
3232
User* user = authentication.getUser();
33-
AsyncJsonResponse * response = new AsyncJsonResponse(MAX_AUTHENTICATION_SIZE);
33+
AsyncJsonResponse * response = new AsyncJsonResponse(false, MAX_AUTHENTICATION_SIZE);
3434
JsonObject jsonObject = response->getRoot();
3535
jsonObject["access_token"] = _securityManager->generateJWT(user);
3636
response->setLength();

lib/framework/AuthenticationService.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include <SecurityManager.h>
55
#include <ESPAsyncWebServer.h>
66
#include <AsyncJsonWebHandler.h>
7-
#include <AsyncArduinoJson6.h>
7+
#include <AsyncJson.h>
88

99
#define VERIFY_AUTHORIZATION_PATH "/rest/verifyAuthorization"
1010
#define SIGN_IN_PATH "/rest/signIn"

lib/framework/NTPStatus.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ NTPStatus::NTPStatus(AsyncWebServer* server, SecurityManager* securityManager) {
77
}
88

99
void NTPStatus::ntpStatus(AsyncWebServerRequest *request) {
10-
AsyncJsonResponse * response = new AsyncJsonResponse(MAX_NTP_STATUS_SIZE);
10+
AsyncJsonResponse * response = new AsyncJsonResponse(false, MAX_NTP_STATUS_SIZE);
1111
JsonObject root = response->getRoot();
1212

1313
// request time now first, this can sometimes force a sync

lib/framework/NTPStatus.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
#include <ESPAsyncWebServer.h>
1313
#include <ArduinoJson.h>
14-
#include <AsyncArduinoJson6.h>
14+
#include <AsyncJson.h>
1515
#include <TimeLib.h>
1616
#include <NtpClientLib.h>
1717
#include <SecurityManager.h>

lib/framework/SettingsPersistence.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#include <FS.h>
66
#include <ArduinoJson.h>
77
#include <AsyncJsonWebHandler.h>
8-
#include <AsyncArduinoJson6.h>
8+
#include <AsyncJson.h>
99

1010
/**
1111
* At the moment, not expecting settings service to have to deal with large JSON

lib/framework/SettingsService.h

Lines changed: 39 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,78 +2,85 @@
22
#define SettingsService_h
33

44
#if defined(ESP8266)
5-
#include <ESP8266WiFi.h>
6-
#include <ESPAsyncTCP.h>
5+
#include <ESP8266WiFi.h>
6+
#include <ESPAsyncTCP.h>
77
#elif defined(ESP_PLATFORM)
8-
#include <WiFi.h>
9-
#include <AsyncTCP.h>
8+
#include <WiFi.h>
9+
#include <AsyncTCP.h>
1010
#endif
1111

1212
#include <SecurityManager.h>
1313
#include <SettingsPersistence.h>
1414
#include <ESPAsyncWebServer.h>
1515
#include <ArduinoJson.h>
1616
#include <AsyncJsonWebHandler.h>
17-
#include <AsyncArduinoJson6.h>
17+
#include <AsyncJson.h>
18+
#include <AsyncJsonCallbackResponse.h>
1819

1920
/*
2021
* Abstraction of a service which stores it's settings as JSON in a file system.
2122
*/
22-
class SettingsService : public SettingsPersistence {
23+
class SettingsService : public SettingsPersistence
24+
{
2325

24-
public:
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));
2530

26-
SettingsService(AsyncWebServer* server, FS* fs, char const* servicePath, char const* filePath): SettingsPersistence(fs, filePath), _servicePath(servicePath) {
27-
server->on(_servicePath, HTTP_GET, std::bind(&SettingsService::fetchConfig, this, std::placeholders::_1));
28-
29-
_updateHandler.setUri(servicePath);
30-
_updateHandler.setMethod(HTTP_POST);
31-
_updateHandler.setMaxContentLength(MAX_SETTINGS_SIZE);
32-
_updateHandler.onRequest(std::bind(&SettingsService::updateConfig, this, std::placeholders::_1, std::placeholders::_2));
33-
server->addHandler(&_updateHandler);
34-
}
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+
}
3537

36-
virtual ~SettingsService() {}
38+
virtual ~SettingsService() {}
3739

38-
void begin() {
39-
// read the initial data from the file system
40-
readFromFS();
41-
}
40+
void begin()
41+
{
42+
// read the initial data from the file system
43+
readFromFS();
44+
}
4245

4346
protected:
44-
char const* _servicePath;
47+
char const *_servicePath;
4548
AsyncJsonWebHandler _updateHandler;
4649

47-
virtual void fetchConfig(AsyncWebServerRequest *request) {
50+
virtual void fetchConfig(AsyncWebServerRequest *request)
51+
{
4852
// handle the request
49-
AsyncJsonResponse * response = new AsyncJsonResponse(MAX_SETTINGS_SIZE);
50-
JsonObject jsonObject = response->getRoot();
53+
AsyncJsonResponse *response = new AsyncJsonResponse(false, MAX_SETTINGS_SIZE);
54+
JsonObject jsonObject = response->getRoot();
5155
writeToJsonObject(jsonObject);
5256
response->setLength();
5357
request->send(response);
5458
}
5559

56-
virtual void updateConfig(AsyncWebServerRequest *request, JsonDocument &jsonDocument) {
60+
virtual void updateConfig(AsyncWebServerRequest *request, JsonDocument &jsonDocument)
61+
{
5762
// handle the request
58-
if (jsonDocument.is<JsonObject>()){
63+
if (jsonDocument.is<JsonObject>())
64+
{
5965
JsonObject newConfig = jsonDocument.as<JsonObject>();
6066
readFromJsonObject(newConfig);
6167
writeToFS();
6268

6369
// write settings back with a callback to reconfigure the wifi
64-
AsyncJsonCallbackResponse * response = new AsyncJsonCallbackResponse([this] () {onConfigUpdated();}, MAX_SETTINGS_SIZE);
65-
JsonObject jsonObject = response->getRoot();
70+
AsyncJsonCallbackResponse *response = new AsyncJsonCallbackResponse([this]() { onConfigUpdated(); }, false, MAX_SETTINGS_SIZE);
71+
JsonObject jsonObject = response->getRoot();
6672
writeToJsonObject(jsonObject);
6773
response->setLength();
6874
request->send(response);
69-
} else {
75+
}
76+
else
77+
{
7078
request->send(400);
7179
}
7280
}
7381

7482
// implement to perform action when config has been updated
75-
virtual void onConfigUpdated(){}
76-
83+
virtual void onConfigUpdated() {}
7784
};
7885

7986
#endif // end SettingsService

0 commit comments

Comments
 (0)