Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions .github/workflows/verify_build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,24 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- name: Checkout Code
uses: actions/checkout@v6
- name: Cache pip
uses: actions/cache@v2
uses: actions/cache@v5
with:
path: ~/.cache/pip
key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }}
restore-keys: |
${{ runner.os }}-pip-
- name: Cache PlatformIO
uses: actions/cache@v2
uses: actions/cache@v5
with:
path: ~/.platformio
key: ${{ runner.os }}-${{ hashFiles('**/lockfiles') }}
- name: Set up Python
uses: actions/setup-python@v2
uses: actions/setup-python@v6
with:
python-version: '3.x'
- name: Install PlatformIO
run: |
python -m pip install --upgrade pip
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# ESP8266 React

[![Build Status](https://travis-ci.org/rjwats/esp8266-react.svg?branch=master)](https://travis-ci.org/rjwats/esp8266-react)
[![Build Framework](https://github.com/rjwats/esp8266-react/actions/workflows/verify_build.yml/badge.svg)](https://github.com/rjwats/esp8266-react/actions/workflows/verify_build.yml)

A simple, secure and extensible framework for IoT projects built on ESP8266/ESP32 platforms with responsive [React](https://reactjs.org/) front-end built with [Material-UI](https://mui.com/).

Expand Down
8 changes: 8 additions & 0 deletions lib/framework/APSettingsService.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
#ifndef APSettingsConfig_h
#define APSettingsConfig_h

#ifdef ESP32
#include <AsyncTCP.h>
#include <WiFi.h>
#elif defined(ESP8266)
#include <ESP8266WiFi.h>
#include <ESPAsyncTCP.h>
#endif

#include <SettingValue.h>
#include <HttpEndpoint.h>
#include <FSPersistence.h>
Expand Down
2 changes: 1 addition & 1 deletion lib/framework/APStatus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ APStatus::APStatus(AsyncWebServer* server, SecurityManager* securityManager, APS
}

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

root["status"] = _apSettingsService->getAPNetworkStatus();
Expand Down
1 change: 0 additions & 1 deletion lib/framework/APStatus.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
#include <SecurityManager.h>
#include <APSettingsService.h>

#define MAX_AP_STATUS_SIZE 1024
#define AP_STATUS_SERVICE_PATH "/rest/apStatus"

class APStatus {
Expand Down
11 changes: 5 additions & 6 deletions lib/framework/AuthenticationService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ AuthenticationService::AuthenticationService(AsyncWebServer* server, SecurityMan
_securityManager(securityManager),
_signInHandler(SIGN_IN_PATH,
std::bind(&AuthenticationService::signIn, this, std::placeholders::_1, std::placeholders::_2)) {
server->on(VERIFY_AUTHORIZATION_PATH,
HTTP_GET,
std::bind(&AuthenticationService::verifyAuthorization, this, std::placeholders::_1));
server->on(VERIFY_AUTHORIZATION_PATH, HTTP_GET, [this](AsyncWebServerRequest* request) {
this->verifyAuthorization(request);
});
_signInHandler.setMethod(HTTP_POST);
_signInHandler.setMaxContentLength(MAX_AUTHENTICATION_SIZE);
server->addHandler(&_signInHandler);
}

Expand All @@ -33,7 +32,7 @@ void AuthenticationService::signIn(AsyncWebServerRequest* request, JsonVariant&
Authentication authentication = _securityManager->authenticate(username, password);
if (authentication.authenticated) {
User* user = authentication.user;
AsyncJsonResponse* response = new AsyncJsonResponse(false, MAX_AUTHENTICATION_SIZE);
AsyncJsonResponse* response = new AsyncJsonResponse(false);
JsonObject jsonObject = response->getRoot();
jsonObject["access_token"] = _securityManager->generateJWT(user);
response->setLength();
Expand All @@ -45,4 +44,4 @@ void AuthenticationService::signIn(AsyncWebServerRequest* request, JsonVariant&
request->send(response);
}

#endif // end FT_ENABLED(FT_SECURITY)
#endif // end FT_ENABLED(FT_SECURITY)
2 changes: 0 additions & 2 deletions lib/framework/AuthenticationService.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
#define VERIFY_AUTHORIZATION_PATH "/rest/verifyAuthorization"
#define SIGN_IN_PATH "/rest/signIn"

#define MAX_AUTHENTICATION_SIZE 256

#if FT_ENABLED(FT_SECURITY)

class AuthenticationService {
Expand Down
2 changes: 1 addition & 1 deletion lib/framework/ESP8266React.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ ESP8266React::ESP8266React(AsyncWebServer* server) :
WWWData::registerRoutes(
[server, this](const String& uri, const String& contentType, const uint8_t* content, size_t len) {
ArRequestHandlerFunction requestHandler = [contentType, content, len](AsyncWebServerRequest* request) {
AsyncWebServerResponse* response = request->beginResponse_P(200, contentType, content, len);
AsyncWebServerResponse* response = request->beginResponse(200, contentType, content, len);
response->addHeader("Content-Encoding", "gzip");
request->send(response);
};
Expand Down
2 changes: 1 addition & 1 deletion lib/framework/ESP8266React.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class ESP8266React {
return &_mqttSettingsService;
}

AsyncMqttClient* getMqttClient() {
espMqttClient* getMqttClient() {
return _mqttSettingsService.getMqttClient();
}
#endif
Expand Down
11 changes: 4 additions & 7 deletions lib/framework/FSPersistence.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,12 @@ class FSPersistence {
JsonStateUpdater<T> stateUpdater,
StatefulService<T>* statefulService,
FS* fs,
const char* filePath,
size_t bufferSize = DEFAULT_BUFFER_SIZE) :
const char* filePath) :
_stateReader(stateReader),
_stateUpdater(stateUpdater),
_statefulService(statefulService),
_fs(fs),
_filePath(filePath),
_bufferSize(bufferSize),
_updateHandlerId(0) {
enableUpdateHandler();
}
Expand All @@ -27,7 +25,7 @@ class FSPersistence {
File settingsFile = _fs->open(_filePath, "r");

if (settingsFile) {
DynamicJsonDocument jsonDocument = DynamicJsonDocument(_bufferSize);
JsonDocument jsonDocument;
DeserializationError error = deserializeJson(jsonDocument, settingsFile);
if (error == DeserializationError::Ok && jsonDocument.is<JsonObject>()) {
JsonObject jsonObject = jsonDocument.as<JsonObject>();
Expand All @@ -47,7 +45,7 @@ class FSPersistence {

bool writeToFS() {
// create and populate a new json object
DynamicJsonDocument jsonDocument = DynamicJsonDocument(_bufferSize);
JsonDocument jsonDocument;
JsonObject jsonObject = jsonDocument.to<JsonObject>();
_statefulService->read(jsonObject, _stateReader);

Expand Down Expand Up @@ -87,7 +85,6 @@ class FSPersistence {
StatefulService<T>* _statefulService;
FS* _fs;
const char* _filePath;
size_t _bufferSize;
update_handler_id_t _updateHandlerId;

// We assume we have a _filePath with format "/directory1/directory2/filename"
Expand All @@ -107,7 +104,7 @@ class FSPersistence {
// 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);
JsonDocument jsonDocument;
JsonObject jsonObject = jsonDocument.as<JsonObject>();
_statefulService->updateWithoutPropagation(jsonObject, _stateUpdater);
}
Expand Down
6 changes: 4 additions & 2 deletions lib/framework/FeaturesService.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
#include <FeaturesService.h>

FeaturesService::FeaturesService(AsyncWebServer* server) {
server->on(FEATURES_SERVICE_PATH, HTTP_GET, std::bind(&FeaturesService::features, this, std::placeholders::_1));
server->on(FEATURES_SERVICE_PATH, HTTP_GET, [this](AsyncWebServerRequest* request) {
this->features(request);
});
}

void FeaturesService::features(AsyncWebServerRequest* request) {
AsyncJsonResponse* response = new AsyncJsonResponse(false, MAX_FEATURES_SIZE);
AsyncJsonResponse* response = new AsyncJsonResponse(false);
JsonObject root = response->getRoot();
#if FT_ENABLED(FT_PROJECT)
root["project"] = true;
Expand Down
1 change: 0 additions & 1 deletion lib/framework/FeaturesService.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
#include <AsyncJson.h>
#include <ESPAsyncWebServer.h>

#define MAX_FEATURES_SIZE 256
#define FEATURES_SERVICE_PATH "/rest/features"

class FeaturesService {
Expand Down
51 changes: 16 additions & 35 deletions lib/framework/HttpEndpoint.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ class HttpGetEndpoint {
AsyncWebServer* server,
const String& servicePath,
SecurityManager* securityManager,
AuthenticationPredicate authenticationPredicate = AuthenticationPredicates::IS_ADMIN,
size_t bufferSize = DEFAULT_BUFFER_SIZE) :
_stateReader(stateReader), _statefulService(statefulService), _bufferSize(bufferSize) {
AuthenticationPredicate authenticationPredicate = AuthenticationPredicates::IS_ADMIN) :
_stateReader(stateReader), _statefulService(statefulService) {
server->on(servicePath.c_str(),
HTTP_GET,
securityManager->wrapRequest(std::bind(&HttpGetEndpoint::fetchSettings, this, std::placeholders::_1),
Expand All @@ -31,19 +30,17 @@ class HttpGetEndpoint {
HttpGetEndpoint(JsonStateReader<T> stateReader,
StatefulService<T>* statefulService,
AsyncWebServer* server,
const String& servicePath,
size_t bufferSize = DEFAULT_BUFFER_SIZE) :
_stateReader(stateReader), _statefulService(statefulService), _bufferSize(bufferSize) {
const String& servicePath) :
_stateReader(stateReader), _statefulService(statefulService) {
server->on(servicePath.c_str(), HTTP_GET, std::bind(&HttpGetEndpoint::fetchSettings, this, std::placeholders::_1));
}

protected:
JsonStateReader<T> _stateReader;
StatefulService<T>* _statefulService;
size_t _bufferSize;

void fetchSettings(AsyncWebServerRequest* request) {
AsyncJsonResponse* response = new AsyncJsonResponse(false, _bufferSize);
AsyncJsonResponse* response = new AsyncJsonResponse(false);
JsonObject jsonObject = response->getRoot().to<JsonObject>();
_statefulService->read(jsonObject, _stateReader);

Expand All @@ -61,18 +58,15 @@ class HttpPostEndpoint {
AsyncWebServer* server,
const String& servicePath,
SecurityManager* securityManager,
AuthenticationPredicate authenticationPredicate = AuthenticationPredicates::IS_ADMIN,
size_t bufferSize = DEFAULT_BUFFER_SIZE) :
AuthenticationPredicate authenticationPredicate = AuthenticationPredicates::IS_ADMIN) :
_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) {
authenticationPredicate)) {
_updateHandler.setMethod(HTTP_POST);
server->addHandler(&_updateHandler);
}
Expand All @@ -81,15 +75,12 @@ class HttpPostEndpoint {
JsonStateUpdater<T> stateUpdater,
StatefulService<T>* statefulService,
AsyncWebServer* server,
const String& servicePath,
size_t bufferSize = DEFAULT_BUFFER_SIZE) :
const String& servicePath) :
_stateReader(stateReader),
_stateUpdater(stateUpdater),
_statefulService(statefulService),
_updateHandler(servicePath,
std::bind(&HttpPostEndpoint::updateSettings, this, std::placeholders::_1, std::placeholders::_2),
bufferSize),
_bufferSize(bufferSize) {
std::bind(&HttpPostEndpoint::updateSettings, this, std::placeholders::_1, std::placeholders::_2)) {
_updateHandler.setMethod(HTTP_POST);
server->addHandler(&_updateHandler);
}
Expand All @@ -99,7 +90,6 @@ class HttpPostEndpoint {
JsonStateUpdater<T> _stateUpdater;
StatefulService<T>* _statefulService;
AsyncCallbackJsonWebHandler _updateHandler;
size_t _bufferSize;

void updateSettings(AsyncWebServerRequest* request, JsonVariant& json) {
if (!json.is<JsonObject>()) {
Expand All @@ -115,7 +105,7 @@ class HttpPostEndpoint {
if (outcome == StateUpdateResult::CHANGED) {
request->onDisconnect([this]() { _statefulService->callUpdateHandlers(HTTP_ENDPOINT_ORIGIN_ID); });
}
AsyncJsonResponse* response = new AsyncJsonResponse(false, _bufferSize);
AsyncJsonResponse* response = new AsyncJsonResponse(false);
jsonObject = response->getRoot().to<JsonObject>();
_statefulService->read(jsonObject, _stateReader);
response->setLength();
Expand All @@ -132,33 +122,24 @@ class HttpEndpoint : public HttpGetEndpoint<T>, public HttpPostEndpoint<T> {
AsyncWebServer* server,
const String& servicePath,
SecurityManager* securityManager,
AuthenticationPredicate authenticationPredicate = AuthenticationPredicates::IS_ADMIN,
size_t bufferSize = DEFAULT_BUFFER_SIZE) :
HttpGetEndpoint<T>(stateReader,
statefulService,
server,
servicePath,
securityManager,
authenticationPredicate,
bufferSize),
AuthenticationPredicate authenticationPredicate = AuthenticationPredicates::IS_ADMIN) :
HttpGetEndpoint<T>(stateReader, statefulService, server, servicePath, securityManager, authenticationPredicate),
HttpPostEndpoint<T>(stateReader,
stateUpdater,
statefulService,
server,
servicePath,
securityManager,
authenticationPredicate,
bufferSize) {
authenticationPredicate) {
}

HttpEndpoint(JsonStateReader<T> stateReader,
JsonStateUpdater<T> stateUpdater,
StatefulService<T>* statefulService,
AsyncWebServer* server,
const String& servicePath,
size_t bufferSize = DEFAULT_BUFFER_SIZE) :
HttpGetEndpoint<T>(stateReader, statefulService, server, servicePath, bufferSize),
HttpPostEndpoint<T>(stateReader, stateUpdater, statefulService, server, servicePath, bufferSize) {
const String& servicePath) :
HttpGetEndpoint<T>(stateReader, statefulService, server, servicePath),
HttpPostEndpoint<T>(stateReader, stateUpdater, statefulService, server, servicePath) {
}
};

Expand Down
Loading