Skip to content

Commit

Permalink
Merge pull request #562 from OpenEVSE/jeremypoulter/issue259
Browse files Browse the repository at this point in the history
Support for updating directly from GitHub
  • Loading branch information
jeremypoulter authored Mar 10, 2023
2 parents 2240212 + 0dc35f3 commit 6ecd665
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 9 deletions.
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

0 comments on commit 6ecd665

Please sign in to comment.