Skip to content

Commit

Permalink
provide file upload progress over wss
Browse files Browse the repository at this point in the history
  • Loading branch information
acvigue committed Oct 30, 2023
1 parent 94d5f87 commit a694477
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
24 changes: 18 additions & 6 deletions lib/RdRestAPISystem/RestAPISystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ static const char *MODULE_PREFIX = "RestAPISystem: ";
String RestAPISystem::_systemVersion;

RestAPISystem::RestAPISystem(WiFiManager &wifiManager, WireGuardManager &wireGuardManager, RdOTAUpdate &otaUpdate, FileManager &fileManager,
NTPClient &ntpClient, CommandScheduler &commandScheduler, ConfigBase &hwConfig, ConfigBase &tranquilConfig, ConfigBase &securityConfig,
const char *systemType, const char *systemVersion)
NTPClient &ntpClient, CommandScheduler &commandScheduler, ConfigBase &hwConfig, ConfigBase &tranquilConfig,
ConfigBase &securityConfig, const char *systemType, const char *systemVersion, WebServer &webServer)
: _wifiManager(wifiManager),
_wireGuardManager(wireGuardManager),
_otaUpdate(otaUpdate),
Expand All @@ -20,7 +20,8 @@ RestAPISystem::RestAPISystem(WiFiManager &wifiManager, WireGuardManager &wireGua
_commandScheduler(commandScheduler),
_hwConfig(hwConfig),
_tranquilConfig(tranquilConfig),
_securityConfig(securityConfig) {
_securityConfig(securityConfig),
_webServer(webServer) {
_deviceRestartPending = false;
_deviceRestartMs = 0;
_updateCheckPending = false;
Expand Down Expand Up @@ -96,7 +97,7 @@ void RestAPISystem::setup(RestAPIEndpoints &endpoints) {
"Set security configuration", "application/json", NULL, true, NULL,
std::bind(&RestAPISystem::apiPostSecurityConfigBody, this, std::placeholders::_1, std::placeholders::_2,
std::placeholders::_3, std::placeholders::_4, std::placeholders::_5));

// Scheduler settings
endpoints.addEndpoint("settings/scheduler", RestAPIEndpointDef::ENDPOINT_CALLBACK, RestAPIEndpointDef::ENDPOINT_GET,
std::bind(&RestAPISystem::apiGetSchedulerConfig, this, std::placeholders::_1, std::placeholders::_2),
Expand Down Expand Up @@ -334,7 +335,6 @@ void RestAPISystem::apiPostSecurityConfigBody(String &reqStr, uint8_t *pData, si
}
}


// MARK: Tranquil
void RestAPISystem::apiGetTranquilConfig(String &reqStr, String &respStr) {
// Get config
Expand Down Expand Up @@ -449,5 +449,17 @@ void RestAPISystem::apiUploadToFileManComplete(String &reqStr, String &respStr)
// Upload file to file system - part of file (from HTTP POST file)
void RestAPISystem::apiUploadToFileManPart(String &req, String &filename, size_t contentLen, size_t index, uint8_t *data, size_t len,
bool finalBlock) {
if (contentLen > 0) _fileManager.uploadAPIBlockHandler("", req, filename, contentLen, index, data, len, finalBlock);
if (contentLen > 0) {
if (index == 0) {
_lastUploadProg = 0;
}
int prog = (index / contentLen) * 100;
if (prog != _lastUploadProg) {
_lastUploadProg = prog;
char pBuf[25];
snprintf(pBuf, 25, "{\"uploadProgress\": %d}", prog);
_webServer.webSocketSend((uint8_t *)pBuf, strlen(pBuf));
}
_fileManager.uploadAPIBlockHandler("", req, filename, contentLen, index, data, len, finalBlock);
}
}
5 changes: 4 additions & 1 deletion lib/RdRestAPISystem/RestAPISystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "RestAPIEndpoints.h"
#include "WireGuardManager.h"
#include "CommandScheduler.h"
#include "WebServer.h"

class RestAPISystem {
private:
Expand All @@ -20,6 +21,7 @@ class RestAPISystem {
static const int DEVICE_RESTART_DELAY_MS = 1000;
bool _updateCheckPending;
unsigned long _updateCheckMs;
unsigned int _lastUploadProg = 0;
uint8_t _tmpReqBodyBuf[600];

// Delay before starting an update check
Expand All @@ -38,10 +40,11 @@ class RestAPISystem {
ConfigBase &_securityConfig;
String _systemType;
static String _systemVersion;
WebServer &_webServer;

public:
RestAPISystem(WiFiManager &wifiManager, WireGuardManager &wireGuardManager, RdOTAUpdate &otaUpdate, FileManager &fileManager,
NTPClient &ntpClient, CommandScheduler &commandScheduler, ConfigBase &hwConfig, ConfigBase &tranquilConfig, ConfigBase &securityConfig, const char *systemType, const char *systemVersion);
NTPClient &ntpClient, CommandScheduler &commandScheduler, ConfigBase &hwConfig, ConfigBase &tranquilConfig, ConfigBase &securityConfig, const char *systemType, const char *systemVersion, WebServer &webServer);

// Setup and status
void setup(RestAPIEndpoints &endpoints);
Expand Down
2 changes: 1 addition & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ WebServer webServer(securityConfig);
// REST API System
#include "RestAPISystem.h"
RestAPISystem restAPISystem(wifiManager, wireGuardManager, otaUpdate, fileManager, ntpClient, commandScheduler, hwConfig, tranquilConfig,
securityConfig, systemType, systemVersion);
securityConfig, systemType, systemVersion, webServer);

// Config for LED Strip
ConfigNVS ledStripConfig("ledStrip", 200);
Expand Down

0 comments on commit a694477

Please sign in to comment.