|
| 1 | +#if defined(ENABLE_DEBUG) && !defined(ENABLE_DEBUG_HTTP_UPATE) |
| 2 | +#undef ENABLE_DEBUG |
| 3 | +#endif |
| 4 | + |
| 5 | +#include "http_update.h" |
| 6 | +#include "lcd.h" |
| 7 | +#include "debug.h" |
| 8 | +#include "emonesp.h" |
| 9 | + |
| 10 | +#include <MongooseHttpClient.h> |
| 11 | +#include <Update.h> |
| 12 | + |
| 13 | +MongooseHttpClient client; |
| 14 | +static int lastPercent = -1; |
| 15 | +static size_t update_total_size = 0; |
| 16 | +static size_t update_position = 0; |
| 17 | + |
| 18 | +bool http_update_from_url(String url, |
| 19 | + std::function<void(size_t complete, size_t total)> progress, |
| 20 | + std::function<void(int)> success, |
| 21 | + std::function<void(int)> error) |
| 22 | +{ |
| 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) |
| 29 | + { |
| 30 | + if(response->respCode() == 200) |
| 31 | + { |
| 32 | + size_t total = response->contentLength(); |
| 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 { |
| 46 | + error(HTTP_UPDATE_ERROR_FAILED_TO_START_UPDATE); |
| 47 | + } |
| 48 | + } else { |
| 49 | + error(response->respCode()); |
| 50 | + } |
| 51 | + request->abort(); |
| 52 | + }); |
| 53 | + |
| 54 | + request->onClose([success, error]() |
| 55 | + { |
| 56 | + if(http_update_end()) |
| 57 | + { |
| 58 | + success(HTTP_UPDATE_OK); |
| 59 | + restart_system(); |
| 60 | + } else { |
| 61 | + error(HTTP_UPDATE_ERROR_FAILED_TO_END_UPDATE); |
| 62 | + } |
| 63 | + }); |
| 64 | + client.send(request); |
| 65 | + |
| 66 | + return true; |
| 67 | + } |
| 68 | + |
| 69 | + return false; |
| 70 | +} |
| 71 | + |
| 72 | +bool http_update_start(String source, size_t total) |
| 73 | +{ |
| 74 | + update_position = 0; |
| 75 | + update_total_size = total; |
| 76 | + if(Update.begin()) |
| 77 | + { |
| 78 | + DEBUG_PORT.printf("Update Start: %s %zu\n", source.c_str(), total); |
| 79 | + |
| 80 | + lcd.display(F("Updating WiFi"), 0, 0, 10 * 1000, LCD_CLEAR_LINE | LCD_DISPLAY_NOW); |
| 81 | + lcd.display(F(""), 0, 1, 10 * 1000, LCD_CLEAR_LINE | LCD_DISPLAY_NOW); |
| 82 | + |
| 83 | + return true; |
| 84 | + } |
| 85 | + |
| 86 | + return false; |
| 87 | +} |
| 88 | + |
| 89 | +bool http_update_write(uint8_t *data, size_t len) |
| 90 | +{ |
| 91 | + DBUGF("Update Writing %u, %u", update_position, len); |
| 92 | + size_t written = Update.write(data, len); |
| 93 | + DBUGVAR(written); |
| 94 | + if(written == len) |
| 95 | + { |
| 96 | + update_position += len; |
| 97 | + if(update_total_size > 0) |
| 98 | + { |
| 99 | + int percent = update_position / (update_total_size / 100); |
| 100 | + DBUGVAR(percent); |
| 101 | + DBUGVAR(lastPercent); |
| 102 | + if(percent != lastPercent) |
| 103 | + { |
| 104 | + String text = String(percent) + F("%"); |
| 105 | + lcd.display(text, 0, 1, 10 * 1000, LCD_DISPLAY_NOW); |
| 106 | + |
| 107 | + DEBUG_PORT.printf("Update: %d%%\n", percent); |
| 108 | + lastPercent = percent; |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + return true; |
| 113 | + } |
| 114 | + |
| 115 | + return false; |
| 116 | +} |
| 117 | + |
| 118 | +bool http_update_end() |
| 119 | +{ |
| 120 | + DBUGLN("Upload finished"); |
| 121 | + if(Update.end(true)) |
| 122 | + { |
| 123 | + DBUGF("Update Success: %u", update_position); |
| 124 | + lcd.display(F("Complete"), 0, 1, 10 * 1000, LCD_CLEAR_LINE | LCD_DISPLAY_NOW); |
| 125 | + return true; |
| 126 | + } else { |
| 127 | + DBUGF("Update failed: %d", Update.getError()); |
| 128 | + lcd.display(F("Error"), 0, 1, 10 * 1000, LCD_CLEAR_LINE | LCD_DISPLAY_NOW); |
| 129 | + } |
| 130 | + |
| 131 | + return false; |
| 132 | +} |
0 commit comments