Skip to content
Open
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
12 changes: 10 additions & 2 deletions src/SettingsESP.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,18 @@ class SettingsESP : public sets::SettingsBase {
HTTPUpload& upload = server.upload();
if (upload.status == UPLOAD_FILE_START) {
sets::beginOta();
if (_onStart)
_onStart();
} else if (upload.status == UPLOAD_FILE_WRITE) {
Update.write(upload.buf, upload.currentSize);
if (_onProgress) {
size_t maxSketchSpace = sets::OtaGetMaxSize();
_onProgress(upload.totalSize, maxSketchSpace);
}
} else if (upload.status == UPLOAD_FILE_END) {
Update.end(true);
bool result = Update.end(true);
if (_onDone)
_onDone(result);
} });

server.onNotFound([this]() {
Expand Down Expand Up @@ -193,4 +201,4 @@ class SettingsESP : public sets::SettingsBase {
server.sendHeader(F("Access-Control-Allow-Methods"), F("*"));
#endif
}
};
};
29 changes: 29 additions & 0 deletions src/core/SettingsBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,19 @@

namespace sets {

typedef void (*onUpdateFWStart_t)();
typedef void (*onUpdateFWProgress_t)(size_t current, size_t final);
typedef void (*onUpdateFWDone_t)(bool success);

class SettingsBase {
static const uint16_t FOCUS_TOUT = 5000;
static const uint16_t DB_WS_UPDATE_PRD = 300;

protected:
sets::onUpdateFWStart_t _onStart = nullptr;
sets::onUpdateFWProgress_t _onProgress = nullptr;
sets::onUpdateFWDone_t _onDone = nullptr;

typedef std::function<void(Builder& b)> BuildCallback;
typedef std::function<void(Updater& upd)> UpdateCallback;
typedef std::function<void(Text path)> FileCallback;
Expand Down Expand Up @@ -116,6 +124,18 @@ class SettingsBase {
}

public:
void onUpdateFWStart(sets::onUpdateFWStart_t fn) {
_onStart = fn;
};

void onUpdateFWProgress(sets::onUpdateFWProgress_t fn) {
_onProgress = fn;
};

void onUpdateFWDone(sets::onUpdateFWDone_t fn) {
_onDone = fn;
};

#ifndef SETT_NO_DB
SettingsBase(const String& title = "", GyverDB* db = nullptr) : _title(title), _db(db) {
#else
Expand Down Expand Up @@ -188,6 +208,11 @@ class SettingsBase {
_focus_cb = cb;
}

// обработчик удаления файлов с устройства типа f(Text path)
void onFileRemove(FileCallback cb) {
_remove_cb = cb;
}

// тикер, вызывать в родительском классе
void tick() {
#ifndef SETT_NO_DB
Expand Down Expand Up @@ -436,6 +461,9 @@ class SettingsBase {
if (granted) {
fs.remove(value.c_str());
_sendFs(true);
if (_remove_cb) {
_remove_cb(value);
}
return;
}
break;
Expand All @@ -462,6 +490,7 @@ class SettingsBase {
}

private:
FileCallback _remove_cb = nullptr;
BuildCallback _build_cb = nullptr;
UpdateCallback _upd_cb = nullptr;
FocusCallback _focus_cb = nullptr;
Expand Down
28 changes: 20 additions & 8 deletions src/core/ota.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,38 @@
#endif

namespace sets {
size_t OtaGetMaxSize(bool ota_flash)
{
size_t ota_size = 0;
if (ota_flash) {
#ifdef ESP8266
ota_size = (size_t)((ESP.getFreeSketchSpace() - 0x1000) & 0xFFFFF000);
#else
ota_size = UPDATE_SIZE_UNKNOWN;
#endif
} else {
#ifdef ESP8266
close_all_fs();
ota_size = (size_t)&_FS_end - (size_t)&_FS_start;
#else
ota_size = UPDATE_SIZE_UNKNOWN;
#endif
}
return ota_size;
}

bool beginOta(bool ota_flash, bool async) {
size_t ota_size = 0;
size_t ota_size = OtaGetMaxSize(ota_flash);
int ota_type = 0;

if (ota_flash) {
ota_type = U_FLASH;
#ifdef ESP8266
ota_size = (size_t)((ESP.getFreeSketchSpace() - 0x1000) & 0xFFFFF000);
#else
ota_size = UPDATE_SIZE_UNKNOWN;
#endif
} else {
#ifdef ESP8266
ota_type = U_FS;
close_all_fs();
ota_size = (size_t)&_FS_end - (size_t)&_FS_start;
#else
ota_type = U_SPIFFS;
ota_size = UPDATE_SIZE_UNKNOWN;
#endif
}

Expand Down
2 changes: 1 addition & 1 deletion src/core/ota.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#endif

namespace sets {

size_t OtaGetMaxSize(bool ota_flash = true);
bool beginOta(bool ota_flash = true, bool async = false);

} // namespace sets