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];
|
static int __output_level[256];
|
||||||
|
|
||||||
int main(int argc __attribute__((unused)), char * argv[] __attribute__((unused))) {
|
int main(int argc __attribute__((unused)), char * argv[] __attribute__((unused))) {
|
||||||
|
|
||||||
memset(__output_pins, 0, sizeof(__output_pins));
|
memset(__output_pins, 0, sizeof(__output_pins));
|
||||||
memset(__output_level, 0, sizeof(__output_level));
|
memset(__output_level, 0, sizeof(__output_level));
|
||||||
|
|
||||||
|
|||||||
@@ -3,16 +3,11 @@
|
|||||||
#define ASYNC_JSON_H_
|
#define ASYNC_JSON_H_
|
||||||
#include <ArduinoJson.h>
|
#include <ArduinoJson.h>
|
||||||
#include <ESPAsyncWebServer.h>
|
#include <ESPAsyncWebServer.h>
|
||||||
// #include <Print.h>
|
|
||||||
|
|
||||||
#define DYNAMIC_JSON_DOCUMENT_SIZE 1024
|
#define DYNAMIC_JSON_DOCUMENT_SIZE 1024
|
||||||
|
|
||||||
constexpr const char * JSON_MIMETYPE = "application/json";
|
constexpr const char * JSON_MIMETYPE = "application/json";
|
||||||
|
|
||||||
/*
|
|
||||||
* Json Response
|
|
||||||
* */
|
|
||||||
|
|
||||||
class ChunkPrint : public Print {
|
class ChunkPrint : public Print {
|
||||||
private:
|
private:
|
||||||
uint8_t * _destination;
|
uint8_t * _destination;
|
||||||
@@ -47,7 +42,6 @@ class ChunkPrint : public Print {
|
|||||||
|
|
||||||
class AsyncJsonResponse {
|
class AsyncJsonResponse {
|
||||||
protected:
|
protected:
|
||||||
|
|
||||||
DynamicJsonDocument _jsonBuffer;
|
DynamicJsonDocument _jsonBuffer;
|
||||||
|
|
||||||
JsonVariant _root;
|
JsonVariant _root;
|
||||||
@@ -79,9 +73,6 @@ return 0;
|
|||||||
}
|
}
|
||||||
|
|
||||||
size_t _fillBuffer(uint8_t * data, size_t len) {
|
size_t _fillBuffer(uint8_t * data, size_t len) {
|
||||||
// ChunkPrint dest(data, 0, len);
|
|
||||||
|
|
||||||
// serializeJson(_root, dest);
|
|
||||||
return len;
|
return len;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -24,7 +24,6 @@ struct AsyncMqttClientMessageProperties {
|
|||||||
bool retain;
|
bool retain;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
namespace AsyncMqttClientInternals {
|
namespace AsyncMqttClientInternals {
|
||||||
|
|
||||||
typedef std::function<void(bool sessionPresent)> OnConnectUserCallback;
|
typedef std::function<void(bool sessionPresent)> OnConnectUserCallback;
|
||||||
|
|||||||
@@ -95,8 +95,6 @@ class EMSESPSettingsService {
|
|||||||
void begin();
|
void begin();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// HttpEndpoint<EMSESPSettings> _httpEndpoint;
|
|
||||||
// FSPersistence<EMSESPSettings> _fsPersistence;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -131,7 +131,6 @@ class AsyncWebServer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void on(const char * uri, WebRequestMethodComposite method, ArRequestHandlerFunction onRequest){};
|
void on(const char * uri, WebRequestMethodComposite method, ArRequestHandlerFunction onRequest){};
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -24,46 +24,13 @@ class FSPersistence {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void readFromFS() {
|
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();
|
applyDefaults();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool writeToFS() {
|
bool writeToFS() {
|
||||||
// create and populate a new json object
|
|
||||||
DynamicJsonDocument jsonDocument = DynamicJsonDocument(_bufferSize);
|
DynamicJsonDocument jsonDocument = DynamicJsonDocument(_bufferSize);
|
||||||
JsonObject jsonObject = jsonDocument.to<JsonObject>();
|
JsonObject jsonObject = jsonDocument.to<JsonObject>();
|
||||||
_statefulService->read(jsonObject, _stateReader);
|
_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;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -90,8 +57,6 @@ class FSPersistence {
|
|||||||
update_handler_id_t _updateHandlerId;
|
update_handler_id_t _updateHandlerId;
|
||||||
|
|
||||||
protected:
|
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() {
|
virtual void applyDefaults() {
|
||||||
DynamicJsonDocument jsonDocument = DynamicJsonDocument(_bufferSize);
|
DynamicJsonDocument jsonDocument = DynamicJsonDocument(_bufferSize);
|
||||||
JsonObject jsonObject = jsonDocument.as<JsonObject>();
|
JsonObject jsonObject = jsonDocument.as<JsonObject>();
|
||||||
|
|||||||
@@ -22,12 +22,6 @@ class HttpGetEndpoint {
|
|||||||
AuthenticationPredicate authenticationPredicate = AuthenticationPredicates::IS_ADMIN,
|
AuthenticationPredicate authenticationPredicate = AuthenticationPredicates::IS_ADMIN,
|
||||||
size_t bufferSize = DEFAULT_BUFFER_SIZE) :
|
size_t bufferSize = DEFAULT_BUFFER_SIZE) :
|
||||||
_stateReader(stateReader), _statefulService(statefulService), _bufferSize(bufferSize) {
|
_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,
|
HttpGetEndpoint(JsonStateReader<T> stateReader,
|
||||||
@@ -36,9 +30,6 @@ class HttpGetEndpoint {
|
|||||||
const String& servicePath,
|
const String& servicePath,
|
||||||
size_t bufferSize = DEFAULT_BUFFER_SIZE) :
|
size_t bufferSize = DEFAULT_BUFFER_SIZE) :
|
||||||
_stateReader(stateReader), _statefulService(statefulService), _bufferSize(bufferSize) {
|
_stateReader(stateReader), _statefulService(statefulService), _bufferSize(bufferSize) {
|
||||||
/*
|
|
||||||
server->on(servicePath.c_str(), HTTP_GET, std::bind(&HttpGetEndpoint::fetchSettings, this, std::placeholders::_1));
|
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
@@ -47,12 +38,6 @@ class HttpGetEndpoint {
|
|||||||
size_t _bufferSize;
|
size_t _bufferSize;
|
||||||
|
|
||||||
void fetchSettings(AsyncWebServerRequest* request) {
|
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),
|
_stateReader(stateReader),
|
||||||
_stateUpdater(stateUpdater),
|
_stateUpdater(stateUpdater),
|
||||||
_statefulService(statefulService),
|
_statefulService(statefulService),
|
||||||
/*
|
|
||||||
_updateHandler(
|
|
||||||
servicePath,
|
|
||||||
securityManager->wrapCallback(
|
|
||||||
std::bind(&HttpPostEndpoint::updateSettings, this, std::placeholders::_1, std::placeholders::_2),
|
|
||||||
authenticationPredicate),
|
|
||||||
bufferSize),
|
|
||||||
*/
|
|
||||||
_bufferSize(bufferSize) {
|
_bufferSize(bufferSize) {
|
||||||
//_updateHandler.setMethod(HTTP_POST);
|
|
||||||
// server->addHandler(&_updateHandler);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
HttpPostEndpoint(JsonStateReader<T> stateReader,
|
HttpPostEndpoint(JsonStateReader<T> stateReader,
|
||||||
@@ -92,42 +67,26 @@ class HttpPostEndpoint {
|
|||||||
_stateReader(stateReader),
|
_stateReader(stateReader),
|
||||||
_stateUpdater(stateUpdater),
|
_stateUpdater(stateUpdater),
|
||||||
_statefulService(statefulService),
|
_statefulService(statefulService),
|
||||||
/*
|
|
||||||
_updateHandler(servicePath,
|
|
||||||
std::bind(&HttpPostEndpoint::updateSettings, this, std::placeholders::_1, std::placeholders::_2),
|
|
||||||
bufferSize),
|
|
||||||
*/
|
|
||||||
_bufferSize(bufferSize) {
|
_bufferSize(bufferSize) {
|
||||||
// _updateHandler.setMethod(HTTP_POST);
|
|
||||||
// server->addHandler(&_updateHandler);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
JsonStateReader<T> _stateReader;
|
JsonStateReader<T> _stateReader;
|
||||||
JsonStateUpdater<T> _stateUpdater;
|
JsonStateUpdater<T> _stateUpdater;
|
||||||
StatefulService<T>* _statefulService;
|
StatefulService<T>* _statefulService;
|
||||||
//AsyncCallbackJsonWebHandler _updateHandler;
|
|
||||||
size_t _bufferSize;
|
size_t _bufferSize;
|
||||||
|
|
||||||
void updateSettings(AsyncWebServerRequest* request, JsonVariant& json) {
|
void updateSettings(AsyncWebServerRequest* request, JsonVariant& json) {
|
||||||
if (!json.is<JsonObject>()) {
|
if (!json.is<JsonObject>()) {
|
||||||
// request->send(400);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
JsonObject jsonObject = json.as<JsonObject>();
|
JsonObject jsonObject = json.as<JsonObject>();
|
||||||
StateUpdateResult outcome = _statefulService->updateWithoutPropagation(jsonObject, _stateUpdater);
|
StateUpdateResult outcome = _statefulService->updateWithoutPropagation(jsonObject, _stateUpdater);
|
||||||
if (outcome == StateUpdateResult::ERROR) {
|
if (outcome == StateUpdateResult::ERROR) {
|
||||||
// request->send(400);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (outcome == StateUpdateResult::CHANGED) {
|
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 <Arduino.h>
|
||||||
#include <Features.h>
|
#include <Features.h>
|
||||||
#include <ESPAsyncWebServer.h>
|
#include <ESPAsyncWebServer.h>
|
||||||
// #include <ESPUtils.h>
|
|
||||||
#include <AsyncJson.h>
|
#include <AsyncJson.h>
|
||||||
#include <list>
|
#include <list>
|
||||||
|
|
||||||
@@ -27,7 +26,10 @@ class User {
|
|||||||
bool admin;
|
bool admin;
|
||||||
|
|
||||||
public:
|
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;
|
boolean authenticated;
|
||||||
|
|
||||||
public:
|
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() {
|
~Authentication() {
|
||||||
delete (user);
|
delete (user);
|
||||||
@@ -64,39 +70,14 @@ class AuthenticationPredicates {
|
|||||||
class SecurityManager {
|
class SecurityManager {
|
||||||
public:
|
public:
|
||||||
#if FT_ENABLED(FT_SECURITY)
|
#if FT_ENABLED(FT_SECURITY)
|
||||||
/*
|
|
||||||
* Authenticate, returning the user if found
|
|
||||||
*/
|
|
||||||
virtual Authentication authenticate(const String & username, const String & password) = 0;
|
virtual Authentication authenticate(const String & username, const String & password) = 0;
|
||||||
|
|
||||||
/*
|
|
||||||
* Generate a JWT for the user provided
|
|
||||||
*/
|
|
||||||
virtual String generateJWT(User * user) = 0;
|
virtual String generateJWT(User * user) = 0;
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/*
|
|
||||||
* Check the request header for the Authorization token
|
|
||||||
*/
|
|
||||||
virtual Authentication authenticateRequest(AsyncWebServerRequest * request) = 0;
|
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;
|
virtual ArRequestFilterFunction filterRequest(AuthenticationPredicate predicate) = 0;
|
||||||
|
virtual ArRequestHandlerFunction wrapRequest(ArRequestHandlerFunction onRequest, AuthenticationPredicate predicate) = 0;
|
||||||
/**
|
virtual ArJsonRequestHandlerFunction wrapCallback(ArJsonRequestHandlerFunction onRequest, 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;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // end SecurityManager_h
|
#endif // end SecurityManager_h
|
||||||
|
|||||||
@@ -86,15 +86,7 @@ class SecuritySettingsService : public StatefulService<SecuritySettings>, public
|
|||||||
ArduinoJsonJWT _jwtHandler;
|
ArduinoJsonJWT _jwtHandler;
|
||||||
|
|
||||||
void configureJWTHandler();
|
void configureJWTHandler();
|
||||||
|
|
||||||
/*
|
|
||||||
* Lookup the user by JWT
|
|
||||||
*/
|
|
||||||
Authentication authenticateJWT(String & jwt);
|
Authentication authenticateJWT(String & jwt);
|
||||||
|
|
||||||
/*
|
|
||||||
* Verify the payload is correct
|
|
||||||
*/
|
|
||||||
boolean validatePayload(JsonObject & parsedPayload, User * user);
|
boolean validatePayload(JsonObject & parsedPayload, User * user);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -31,8 +31,10 @@ typedef struct StateUpdateHandlerInfo {
|
|||||||
update_handler_id_t _id;
|
update_handler_id_t _id;
|
||||||
StateUpdateCallback _cb;
|
StateUpdateCallback _cb;
|
||||||
bool _allowRemove;
|
bool _allowRemove;
|
||||||
StateUpdateHandlerInfo(StateUpdateCallback cb, bool allowRemove) :
|
StateUpdateHandlerInfo(StateUpdateCallback cb, bool allowRemove)
|
||||||
_id(++currentUpdatedHandlerId), _cb(cb), _allowRemove(allowRemove){};
|
: _id(++currentUpdatedHandlerId)
|
||||||
|
, _cb(cb)
|
||||||
|
, _allowRemove(allowRemove){};
|
||||||
} StateUpdateHandlerInfo_t;
|
} StateUpdateHandlerInfo_t;
|
||||||
|
|
||||||
template <class T>
|
template <class T>
|
||||||
@@ -40,11 +42,13 @@ class StatefulService {
|
|||||||
public:
|
public:
|
||||||
template <typename... Args>
|
template <typename... Args>
|
||||||
#ifdef ESP32
|
#ifdef ESP32
|
||||||
StatefulService(Args&&... args) :
|
StatefulService(Args &&... args)
|
||||||
_state(std::forward<Args>(args)...), _accessMutex(xSemaphoreCreateRecursiveMutex()) {
|
: _state(std::forward<Args>(args)...)
|
||||||
|
, _accessMutex(xSemaphoreCreateRecursiveMutex()) {
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
StatefulService(Args&&... args) : _state(std::forward<Args>(args)...) {
|
StatefulService(Args &&... args)
|
||||||
|
: _state(std::forward<Args>(args)...) {
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user