Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for updating directly from GitHub #562

Merged
merged 4 commits into from
Mar 10, 2023
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
3 changes: 3 additions & 0 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ debug_flags =
#-D ENABLE_DEBUG_TESLA_CLIENT
#-D ENABLE_DEBUG_LIMIT
#-D ENABLE_PROFILE
#-D ENABLE_DEBUG_HTTP_UPATE
#-D ENABLE_DEBUG_TIME
#-D ENABLE_NOISY_PROFILE
#-D ENABLE_DEBUG_MICROTASKS
src_build_flags =
Expand Down Expand Up @@ -95,6 +97,7 @@ build_flags =
-D ARDUINO_ARCH_ESP32
-D USE_ESP32
-D USE_ESP32_FRAMEWORK_ARDUINO
-D MG_MAX_HTTP_REQUEST_SIZE=8196
build_partitions = min_spiffs.csv
build_partitions_debug = min_spiffs_debug.csv

Expand Down
36 changes: 30 additions & 6 deletions src/http_update.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,18 @@ bool http_update_from_url(String url,
std::function<void(int)> success,
std::function<void(int)> error)
{
DBUGF("Update from URL: %s", url.c_str());

MongooseHttpClientRequest *request = client.beginRequest(url.c_str());
if(request)
{
request->setMethod(HTTP_GET);

DBUGF("Trying to fetch firmware from %s", url.c_str());

request->onBody([url,progress,error,request](MongooseHttpClientResponse *response)
{
DBUGF("Update onBody %d", response->respCode());
if(response->respCode() == 200)
{
size_t total = response->contentLength();
Expand All @@ -45,20 +50,39 @@ bool http_update_from_url(String url,
} else {
error(HTTP_UPDATE_ERROR_FAILED_TO_START_UPDATE);
}
} else if (300 <= response->respCode() && response->respCode() < 400) {
// handle 3xx redirects (later)
return;
} else {
error(response->respCode());
}
request->abort();
});

request->onResponse([progress, error, success, request](MongooseHttpClientResponse *response)
{
DBUGF("Update onResponse %d", response->respCode());
if(301 == response->respCode() ||
302 == response->respCode())
{
MongooseString location = response->headers("Location");
DBUGVAR(location.toString());
http_update_from_url(location.toString(), progress, success, error);
}
});

request->onClose([success, error]()
{
if(http_update_end())
DBUGLN("Update onClose");
if(Update.isRunning())
{
success(HTTP_UPDATE_OK);
restart_system();
} else {
error(HTTP_UPDATE_ERROR_FAILED_TO_END_UPDATE);
if(http_update_end())
{
success(HTTP_UPDATE_OK);
restart_system();
} else {
error(HTTP_UPDATE_ERROR_FAILED_TO_END_UPDATE);
}
}
});
client.send(request);
Expand Down Expand Up @@ -112,7 +136,7 @@ bool http_update_write(uint8_t *data, size_t len)
lcd.display(text, 0, 1, 10 * 1000, LCD_DISPLAY_NOW);

DEBUG_PORT.printf("Update: %d%%\n", percent);

StaticJsonDocument<128> event;
event["ota_progress"] = percent;
web_server_event(event);
Expand Down
2 changes: 1 addition & 1 deletion src/root_ca.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const char *root_ca = ""
#endif

#ifndef CA_GITHUB
#define CA_GITHUB 0
#define CA_GITHUB 1
#endif

#ifndef CA_AMAZON_1
Expand Down
10 changes: 8 additions & 2 deletions test/update.http
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,24 @@ Content-Type: application/octet-stream
curl -F [email protected]/build/openevse_esp-wrover-kit/firmware.bin {{baseUrl}}/update

###

# Get the latest published relese (not pre-release)
# @name latest
GET https://api.github.com/repos/OpenEVSE/ESP32_WiFi_V4.x/releases/latest
Accept: application/vnd.github.v3+json

###
# Get the latest pre-release
# @name latest
GET https://api.github.com/repos/OpenEVSE/ESP32_WiFi_V4.x/releases/tags/latest
Accept: application/vnd.github.v3+json

###

POST {{baseUrl}}/update
Content-Type: application/javascript

{
"url": "{{latest.response.body.assets[1].browser_download_url}}"
"url": "{{latest.response.body.assets[0].browser_download_url}}"
}

###
Expand Down