make it work from web (thanks michael)

This commit is contained in:
proddy
2024-08-18 16:17:03 +02:00
parent 92a8a268a7
commit 119dcaa7fc
8 changed files with 68 additions and 123 deletions

View File

@@ -1833,31 +1833,51 @@ String System::getBBQKeesGatewayDetails() {
#endif
}
// Stream from an URL and send straight to OTA uploader
// Stream from an URL and send straight to OTA uploader service.
//
// This function needs to be called twice, once with a url to persist it, and second with no arguments to start the upload
// This is to avoid timeouts in callback functions, like calling from a web hook.
bool System::uploadFirmwareURL(const char * url) {
#ifndef EMSESP_STANDALONE
// configure temporary server and url
HTTPClient http;
http.setFollowRedirects(HTTPC_FORCE_FOLLOW_REDIRECTS); // important for GitHub
http.useHTTP10(true);
http.begin(String(url));
// start connection
static String saved_url;
// if the URL is not empty, save it for later
if (url && strlen(url) > 0) {
saved_url = url;
EMSESP::system_.upload_status(true); // tell EMS-ESP we're ready to start the uploading process
return true;
}
// make sure we have a valid URL
if (saved_url.isEmpty()) {
LOG_ERROR("Firmware upload failed - no URL");
return false;
}
// Configure temporary client
HTTPClient http;
http.setFollowRedirects(HTTPC_FORCE_FOLLOW_REDIRECTS); // important for GitHub 302's
http.useHTTP10(true);
http.begin(saved_url);
// start a connection
int httpCode = http.GET();
if (httpCode != HTTP_CODE_OK) {
LOG_ERROR("Firmware upload failed - HTTP code %u", httpCode);
return false;
}
// check we have enough space
// check we have enough space for the upload in the ota partition
int firmware_size = http.getSize();
LOG_INFO("Firmware uploading (file: %s, size: %d bytes)", url, firmware_size);
LOG_INFO("Firmware uploading (file: %s, size: %d bytes). Please wait...", saved_url.c_str(), firmware_size);
if (!Update.begin(firmware_size)) {
LOG_ERROR("Firmware upload failed - no space");
return false;
}
EMSESP::system_.upload_status(true); // tell EMS-ESP we're uploading
// flush buffers so latest log messages are shown
Shell::loop_all();
// get tcp stream and send it to Updater
WiFiClient * stream = http.getStreamPtr();
@@ -1867,54 +1887,22 @@ bool System::uploadFirmwareURL(const char * url) {
}
if (!Update.end(true)) {
LOG_ERROR("Firmware upload error");
LOG_ERROR("Firmware upload failed - general error");
return false;
}
http.end();
EMSESP::system_.upload_status(false);
saved_url.clear(); // prevent from downloading again
LOG_INFO("Firmware uploaded successfully. Restarting...");
restart_requested(true); // not sure this is needed?
restart_requested(true);
#endif
return true; // OK
/*
TODO backup code to save firmware to LittleFS first, in case of slow networks
// create buffer for reading in 128 byte chunks
const size_t buffer_size = 1024;
uint8_t buff[buffer_size] = {0};
File file = LittleFS.open("/new_firmware", "w"); // TODO find new name
if (!file) {
Serial.println("Failed to open file for writing");
return false;
}
// read all data from server
while (http.connected() && (firmware_size > 0 || firmware_size == -1)) {
// get available data size
size_t size = stream->available();
if (size) {
// read up to 128 byte
int c = stream->readBytes(buff, ((size > sizeof(buff)) ? sizeof(buff) : size));
file.write(buff, c);
if (firmware_size > 0) {
firmware_size -= c;
}
}
yield();
// delay(1); // so not to hurt WTD or timeout
}
file.close();
*/
}
} // namespace emsesp