mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2025-12-06 07:49:52 +03:00
clean-up code
This commit is contained in:
@@ -37,7 +37,6 @@ static bool __output_pins[256];
|
||||
static int __output_level[256];
|
||||
|
||||
int main(int argc __attribute__((unused)), char * argv[] __attribute__((unused))) {
|
||||
|
||||
memset(__output_pins, 0, sizeof(__output_pins));
|
||||
memset(__output_level, 0, sizeof(__output_level));
|
||||
|
||||
|
||||
@@ -3,16 +3,11 @@
|
||||
#define ASYNC_JSON_H_
|
||||
#include <ArduinoJson.h>
|
||||
#include <ESPAsyncWebServer.h>
|
||||
// #include <Print.h>
|
||||
|
||||
#define DYNAMIC_JSON_DOCUMENT_SIZE 1024
|
||||
|
||||
constexpr const char * JSON_MIMETYPE = "application/json";
|
||||
|
||||
/*
|
||||
* Json Response
|
||||
* */
|
||||
|
||||
class ChunkPrint : public Print {
|
||||
private:
|
||||
uint8_t * _destination;
|
||||
@@ -47,7 +42,6 @@ class ChunkPrint : public Print {
|
||||
|
||||
class AsyncJsonResponse {
|
||||
protected:
|
||||
|
||||
DynamicJsonDocument _jsonBuffer;
|
||||
|
||||
JsonVariant _root;
|
||||
@@ -79,9 +73,6 @@ return 0;
|
||||
}
|
||||
|
||||
size_t _fillBuffer(uint8_t * data, size_t len) {
|
||||
// ChunkPrint dest(data, 0, len);
|
||||
|
||||
// serializeJson(_root, dest);
|
||||
return len;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -24,7 +24,6 @@ struct AsyncMqttClientMessageProperties {
|
||||
bool retain;
|
||||
};
|
||||
|
||||
|
||||
namespace AsyncMqttClientInternals {
|
||||
|
||||
typedef std::function<void(bool sessionPresent)> OnConnectUserCallback;
|
||||
|
||||
@@ -95,8 +95,6 @@ class EMSESPSettingsService {
|
||||
void begin();
|
||||
|
||||
private:
|
||||
// HttpEndpoint<EMSESPSettings> _httpEndpoint;
|
||||
// FSPersistence<EMSESPSettings> _fsPersistence;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@@ -131,7 +131,6 @@ class AsyncWebServer {
|
||||
}
|
||||
|
||||
void on(const char * uri, WebRequestMethodComposite method, ArRequestHandlerFunction onRequest){};
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -24,46 +24,13 @@ class FSPersistence {
|
||||
}
|
||||
|
||||
void readFromFS() {
|
||||
/*
|
||||
File settingsFile = _fs->open(_filePath, "r");
|
||||
|
||||
if (settingsFile) {
|
||||
DynamicJsonDocument jsonDocument = DynamicJsonDocument(_bufferSize);
|
||||
DeserializationError error = deserializeJson(jsonDocument, settingsFile);
|
||||
if (error == DeserializationError::Ok && jsonDocument.is<JsonObject>()) {
|
||||
JsonObject jsonObject = jsonDocument.as<JsonObject>();
|
||||
_statefulService->updateWithoutPropagation(jsonObject, _stateUpdater);
|
||||
settingsFile.close();
|
||||
return;
|
||||
}
|
||||
settingsFile.close();
|
||||
}
|
||||
*/
|
||||
// If we reach here we have not been successful in loading the config,
|
||||
// hard-coded emergency defaults are now applied.
|
||||
|
||||
applyDefaults();
|
||||
}
|
||||
|
||||
bool writeToFS() {
|
||||
// create and populate a new json object
|
||||
DynamicJsonDocument jsonDocument = DynamicJsonDocument(_bufferSize);
|
||||
JsonObject jsonObject = jsonDocument.to<JsonObject>();
|
||||
_statefulService->read(jsonObject, _stateReader);
|
||||
|
||||
// serialize it to filesystem
|
||||
/*
|
||||
File settingsFile = _fs->open(_filePath, "w");
|
||||
|
||||
// failed to open file, return false
|
||||
if (!settingsFile) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// serialize the data to the file
|
||||
serializeJson(jsonDocument, settingsFile);
|
||||
settingsFile.close();
|
||||
*/
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -90,8 +57,6 @@ class FSPersistence {
|
||||
update_handler_id_t _updateHandlerId;
|
||||
|
||||
protected:
|
||||
// We assume the updater supplies sensible defaults if an empty object
|
||||
// is supplied, this virtual function allows that to be changed.
|
||||
virtual void applyDefaults() {
|
||||
DynamicJsonDocument jsonDocument = DynamicJsonDocument(_bufferSize);
|
||||
JsonObject jsonObject = jsonDocument.as<JsonObject>();
|
||||
|
||||
@@ -22,12 +22,6 @@ class HttpGetEndpoint {
|
||||
AuthenticationPredicate authenticationPredicate = AuthenticationPredicates::IS_ADMIN,
|
||||
size_t bufferSize = DEFAULT_BUFFER_SIZE) :
|
||||
_stateReader(stateReader), _statefulService(statefulService), _bufferSize(bufferSize) {
|
||||
/*
|
||||
server->on(servicePath.c_str(),
|
||||
HTTP_GET,
|
||||
securityManager->wrapRequest(std::bind(&HttpGetEndpoint::fetchSettings, this, std::placeholders::_1),
|
||||
authenticationPredicate));
|
||||
*/
|
||||
}
|
||||
|
||||
HttpGetEndpoint(JsonStateReader<T> stateReader,
|
||||
@@ -36,9 +30,6 @@ class HttpGetEndpoint {
|
||||
const String& servicePath,
|
||||
size_t bufferSize = DEFAULT_BUFFER_SIZE) :
|
||||
_stateReader(stateReader), _statefulService(statefulService), _bufferSize(bufferSize) {
|
||||
/*
|
||||
server->on(servicePath.c_str(), HTTP_GET, std::bind(&HttpGetEndpoint::fetchSettings, this, std::placeholders::_1));
|
||||
*/
|
||||
}
|
||||
|
||||
protected:
|
||||
@@ -47,12 +38,6 @@ class HttpGetEndpoint {
|
||||
size_t _bufferSize;
|
||||
|
||||
void fetchSettings(AsyncWebServerRequest* request) {
|
||||
// AsyncJsonResponse* response = new AsyncJsonResponse(false, _bufferSize);
|
||||
// JsonObject jsonObject = response->getRoot().to<JsonObject>();
|
||||
// _statefulService->read(jsonObject, _stateReader);
|
||||
|
||||
// response->setLength();
|
||||
// request->send(response);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -70,17 +55,7 @@ class HttpPostEndpoint {
|
||||
_stateReader(stateReader),
|
||||
_stateUpdater(stateUpdater),
|
||||
_statefulService(statefulService),
|
||||
/*
|
||||
_updateHandler(
|
||||
servicePath,
|
||||
securityManager->wrapCallback(
|
||||
std::bind(&HttpPostEndpoint::updateSettings, this, std::placeholders::_1, std::placeholders::_2),
|
||||
authenticationPredicate),
|
||||
bufferSize),
|
||||
*/
|
||||
_bufferSize(bufferSize) {
|
||||
//_updateHandler.setMethod(HTTP_POST);
|
||||
// server->addHandler(&_updateHandler);
|
||||
}
|
||||
|
||||
HttpPostEndpoint(JsonStateReader<T> stateReader,
|
||||
@@ -92,42 +67,26 @@ class HttpPostEndpoint {
|
||||
_stateReader(stateReader),
|
||||
_stateUpdater(stateUpdater),
|
||||
_statefulService(statefulService),
|
||||
/*
|
||||
_updateHandler(servicePath,
|
||||
std::bind(&HttpPostEndpoint::updateSettings, this, std::placeholders::_1, std::placeholders::_2),
|
||||
bufferSize),
|
||||
*/
|
||||
_bufferSize(bufferSize) {
|
||||
// _updateHandler.setMethod(HTTP_POST);
|
||||
// server->addHandler(&_updateHandler);
|
||||
}
|
||||
|
||||
protected:
|
||||
JsonStateReader<T> _stateReader;
|
||||
JsonStateUpdater<T> _stateUpdater;
|
||||
StatefulService<T>* _statefulService;
|
||||
//AsyncCallbackJsonWebHandler _updateHandler;
|
||||
size_t _bufferSize;
|
||||
|
||||
void updateSettings(AsyncWebServerRequest* request, JsonVariant& json) {
|
||||
if (!json.is<JsonObject>()) {
|
||||
// request->send(400);
|
||||
return;
|
||||
}
|
||||
JsonObject jsonObject = json.as<JsonObject>();
|
||||
StateUpdateResult outcome = _statefulService->updateWithoutPropagation(jsonObject, _stateUpdater);
|
||||
if (outcome == StateUpdateResult::ERROR) {
|
||||
// request->send(400);
|
||||
return;
|
||||
}
|
||||
if (outcome == StateUpdateResult::CHANGED) {
|
||||
// request->onDisconnect([this]() { _statefulService->callUpdateHandlers(HTTP_ENDPOINT_ORIGIN_ID); });
|
||||
}
|
||||
// AsyncJsonResponse* response = new AsyncJsonResponse(false, _bufferSize);
|
||||
// jsonObject = response->getRoot().to<JsonObject>();
|
||||
// _statefulService->read(jsonObject, _stateReader);
|
||||
// response->setLength();
|
||||
// request->send(response);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -4,7 +4,6 @@
|
||||
#include <Arduino.h>
|
||||
#include <Features.h>
|
||||
#include <ESPAsyncWebServer.h>
|
||||
// #include <ESPUtils.h>
|
||||
#include <AsyncJson.h>
|
||||
#include <list>
|
||||
|
||||
@@ -27,7 +26,10 @@ class User {
|
||||
bool admin;
|
||||
|
||||
public:
|
||||
User(String username, String password, bool admin) : username(username), password(password), admin(admin) {
|
||||
User(String username, String password, bool admin)
|
||||
: username(username)
|
||||
, password(password)
|
||||
, admin(admin) {
|
||||
}
|
||||
};
|
||||
|
||||
@@ -37,9 +39,13 @@ class Authentication {
|
||||
boolean authenticated;
|
||||
|
||||
public:
|
||||
Authentication(User& user) : user(new User(user)), authenticated(true) {
|
||||
Authentication(User & user)
|
||||
: user(new User(user))
|
||||
, authenticated(true) {
|
||||
}
|
||||
Authentication() : user(nullptr), authenticated(false) {
|
||||
Authentication()
|
||||
: user(nullptr)
|
||||
, authenticated(false) {
|
||||
}
|
||||
~Authentication() {
|
||||
delete (user);
|
||||
@@ -64,39 +70,14 @@ class AuthenticationPredicates {
|
||||
class SecurityManager {
|
||||
public:
|
||||
#if FT_ENABLED(FT_SECURITY)
|
||||
/*
|
||||
* Authenticate, returning the user if found
|
||||
*/
|
||||
virtual Authentication authenticate(const String & username, const String & password) = 0;
|
||||
|
||||
/*
|
||||
* Generate a JWT for the user provided
|
||||
*/
|
||||
virtual String generateJWT(User * user) = 0;
|
||||
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Check the request header for the Authorization token
|
||||
*/
|
||||
virtual Authentication authenticateRequest(AsyncWebServerRequest * request) = 0;
|
||||
|
||||
/**
|
||||
* Filter a request with the provided predicate, only returning true if the predicate matches.
|
||||
*/
|
||||
virtual ArRequestFilterFunction filterRequest(AuthenticationPredicate predicate) = 0;
|
||||
|
||||
/**
|
||||
* Wrap the provided request to provide validation against an AuthenticationPredicate.
|
||||
*/
|
||||
virtual ArRequestHandlerFunction wrapRequest(ArRequestHandlerFunction onRequest,
|
||||
AuthenticationPredicate predicate) = 0;
|
||||
|
||||
/**
|
||||
* Wrap the provided json request callback to provide validation against an AuthenticationPredicate.
|
||||
*/
|
||||
virtual ArJsonRequestHandlerFunction wrapCallback(ArJsonRequestHandlerFunction onRequest,
|
||||
AuthenticationPredicate predicate) = 0;
|
||||
virtual ArRequestHandlerFunction wrapRequest(ArRequestHandlerFunction onRequest, AuthenticationPredicate predicate) = 0;
|
||||
virtual ArJsonRequestHandlerFunction wrapCallback(ArJsonRequestHandlerFunction onRequest, AuthenticationPredicate predicate) = 0;
|
||||
};
|
||||
|
||||
#endif // end SecurityManager_h
|
||||
|
||||
@@ -86,15 +86,7 @@ class SecuritySettingsService : public StatefulService<SecuritySettings>, public
|
||||
ArduinoJsonJWT _jwtHandler;
|
||||
|
||||
void configureJWTHandler();
|
||||
|
||||
/*
|
||||
* Lookup the user by JWT
|
||||
*/
|
||||
Authentication authenticateJWT(String & jwt);
|
||||
|
||||
/*
|
||||
* Verify the payload is correct
|
||||
*/
|
||||
boolean validatePayload(JsonObject & parsedPayload, User * user);
|
||||
};
|
||||
|
||||
|
||||
@@ -31,8 +31,10 @@ typedef struct StateUpdateHandlerInfo {
|
||||
update_handler_id_t _id;
|
||||
StateUpdateCallback _cb;
|
||||
bool _allowRemove;
|
||||
StateUpdateHandlerInfo(StateUpdateCallback cb, bool allowRemove) :
|
||||
_id(++currentUpdatedHandlerId), _cb(cb), _allowRemove(allowRemove){};
|
||||
StateUpdateHandlerInfo(StateUpdateCallback cb, bool allowRemove)
|
||||
: _id(++currentUpdatedHandlerId)
|
||||
, _cb(cb)
|
||||
, _allowRemove(allowRemove){};
|
||||
} StateUpdateHandlerInfo_t;
|
||||
|
||||
template <class T>
|
||||
@@ -40,11 +42,13 @@ class StatefulService {
|
||||
public:
|
||||
template <typename... Args>
|
||||
#ifdef ESP32
|
||||
StatefulService(Args&&... args) :
|
||||
_state(std::forward<Args>(args)...), _accessMutex(xSemaphoreCreateRecursiveMutex()) {
|
||||
StatefulService(Args &&... args)
|
||||
: _state(std::forward<Args>(args)...)
|
||||
, _accessMutex(xSemaphoreCreateRecursiveMutex()) {
|
||||
}
|
||||
#else
|
||||
StatefulService(Args&&... args) : _state(std::forward<Args>(args)...) {
|
||||
StatefulService(Args &&... args)
|
||||
: _state(std::forward<Args>(args)...) {
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
Reference in New Issue
Block a user