1+ #if defined(ENABLE_DEBUG) && !defined(ENABLE_DEBUG_HTTP_UPATE)
2+ #undef ENABLE_DEBUG
3+ #endif
4+
15#include " http_update.h"
26#include " lcd.h"
37#include " debug.h"
8+ #include " emonesp.h"
49
510#include < MongooseHttpClient.h>
611#include < Update.h>
@@ -15,46 +20,62 @@ bool http_update_from_url(String url,
1520 std::function<void(int )> success,
1621 std::function<void(int )> error)
1722{
18- MongooseHttpClientRequest *request =
19- client.beginRequest (url.c_str ())
20- ->setMethod (HTTP_GET)
21- // ->onBody([progress, error](const uint8_t *data, size_t len) {
22- // if(http_update_write(data, len)) {
23- // progress(len, update_total_size);
24- // } else {
25- // error(HTTP_UPDATE_ERROR_WRITE_FAILED);
26- // }
27- // }
28- ->onResponse ([url,error](MongooseHttpClientResponse *response)
23+ MongooseHttpClientRequest *request = client.beginRequest (url.c_str ());
24+ if (request)
25+ {
26+ request->setMethod (HTTP_GET);
27+
28+ request->onBody ([url,progress,error,request](MongooseHttpClientResponse *response)
2929 {
3030 if (response->respCode () == 200 )
3131 {
3232 size_t total = response->contentLength ();
33- if (!http_update_start (url, total)) {
33+ DBUGVAR (total);
34+ if (Update.isRunning () || http_update_start (url, total))
35+ {
36+ uint8_t *data = (uint8_t *)response->body ().c_str ();
37+ size_t len = response->body ().length ();
38+ if (http_update_write (data, len))
39+ {
40+ progress (len, total);
41+ return ;
42+ } else {
43+ error (HTTP_UPDATE_ERROR_WRITE_FAILED);
44+ }
45+ } else {
3446 error (HTTP_UPDATE_ERROR_FAILED_TO_START_UPDATE);
3547 }
3648 } else {
3749 error (response->respCode ());
3850 }
39- })
40- ->onClose ([success, error](MongooseHttpClientResponse *response) {
41- if (http_update_end ()) {
51+ request->abort ();
52+ });
53+
54+ request->onClose ([success, error]()
55+ {
56+ if (http_update_end ())
57+ {
4258 success (HTTP_UPDATE_OK);
59+ restart_system ();
4360 } else {
4461 error (HTTP_UPDATE_ERROR_FAILED_TO_END_UPDATE);
4562 }
4663 });
47- client.send (request);
64+ client.send (request);
4865
49- return true ;
66+ return true ;
67+ }
68+
69+ return false ;
5070}
5171
5272bool http_update_start (String source, size_t total)
5373{
74+ update_position = 0 ;
5475 update_total_size = total;
5576 if (Update.begin ())
5677 {
57- DEBUG_PORT.printf (" Update Start: %s\n " , source.c_str ());
78+ DEBUG_PORT.printf (" Update Start: %s %zu \n " , source.c_str (), total );
5879
5980 lcd.display (F (" Updating WiFi" ), 0 , 0 , 10 * 1000 , LCD_CLEAR_LINE | LCD_DISPLAY_NOW);
6081 lcd.display (F (" " ), 0 , 1 , 10 * 1000 , LCD_CLEAR_LINE | LCD_DISPLAY_NOW);
@@ -67,8 +88,10 @@ bool http_update_start(String source, size_t total)
6788
6889bool http_update_write (uint8_t *data, size_t len)
6990{
70- DBUGF (" Update Writing %u" , update_position);
71- if (Update.write (data, len) == len)
91+ DBUGF (" Update Writing %u, %u" , update_position, len);
92+ size_t written = Update.write (data, len);
93+ DBUGVAR (written);
94+ if (written == len)
7295 {
7396 update_position += len;
7497 if (update_total_size > 0 )
@@ -80,6 +103,7 @@ bool http_update_write(uint8_t *data, size_t len)
80103 {
81104 String text = String (percent) + F (" %" );
82105 lcd.display (text, 0 , 1 , 10 * 1000 , LCD_DISPLAY_NOW);
106+
83107 DEBUG_PORT.printf (" Update: %d%%\n " , percent);
84108 lastPercent = percent;
85109 }
@@ -96,8 +120,9 @@ bool http_update_end()
96120 DBUGLN (" Upload finished" );
97121 if (Update.end (true ))
98122 {
99- DBUGF (" Update Success: %uB " , update_position);
123+ DBUGF (" Update Success: %u " , update_position);
100124 lcd.display (F (" Complete" ), 0 , 1 , 10 * 1000 , LCD_CLEAR_LINE | LCD_DISPLAY_NOW);
125+ return true ;
101126 } else {
102127 DBUGF (" Update failed: %d" , Update.getError ());
103128 lcd.display (F (" Error" ), 0 , 1 , 10 * 1000 , LCD_CLEAR_LINE | LCD_DISPLAY_NOW);
0 commit comments