123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- /*
- * Copyright (c) 2021 Thomas Buck <thomas@xythobuz.de>
- *
- * This file is part of Giess-o-mat.
- *
- * Giess-o-mat is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * Giess-o-mat is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with Giess-o-mat. If not, see <https://www.gnu.org/licenses/>.
- */
-
- #if defined(ARDUINO_ARCH_ESP8266)
- #include <ESP8266HTTPUpdateServer.h>
- #elif defined(ARDUINO_ARCH_ESP32)
- #include <Update.h>
- #endif
-
- #include "SimpleUpdater.h"
-
- #if defined(ARDUINO_ARCH_ESP32)
-
- void SimpleUpdater::get(void) {
- String uploadPage = F(
- "<!DOCTYPE html>\n"
- "<html><head>\n"
- "<meta charset='utf-8'/>\n"
- "<meta name='viewport' content='width=device-width, initial-scale=1'/>\n"
- "<script src='https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js'></script>\n"
- "<title>SimpleUpdater ESP32</title>\n"
- "<style>\n"
- "@media (prefers-color-scheme: dark) {\n"
- "body {\n"
- "background-color: black;\n"
- "color: white;\n"
- "}\n"
- "}\n"
- "</style>\n"
- "</head><body>\n"
-
- "<h1>SimpleUpdater</h1>\n"
- "<p>Select the update file. If you have built this project with PlatformIO, you can find a firmware.bin in the .pio folder.</p>\n"
- "<form method='POST' action='#' enctype='multipart/form-data' id='upload_form'>\n"
- "<input type='file' name='update' accept='.bin'>\n"
- "<input type='submit' value='Update'>\n"
- "</form><br>\n"
- "<div id='prg'>progress: 0%</div>\n"
- "<p>After the update is finished, you will automatically be redirected to the main page.</p>\n"
- "<a href=\"/\">Back to Main Page</a>\n"
-
- "<script>\n"
- "$('form').submit(function(e){\n"
- "e.preventDefault();\n"
- "var form = $('#upload_form')[0];\n"
- "var data = new FormData(form);\n"
- " $.ajax({\n"
- "url: '/update',\n"
- "type: 'POST',\n"
- "data: data,\n"
- "contentType: false,\n"
- "processData:false,\n"
- "xhr: function() {\n"
- "var xhr = new window.XMLHttpRequest();\n"
- "xhr.upload.addEventListener('progress', function(evt) {\n"
- "if (evt.lengthComputable) {\n"
- "var per = evt.loaded / evt.total;\n"
- "$('#prg').html('progress: ' + Math.round(per*100) + '%');\n"
- "}\n"
- "}, false);\n"
- "return xhr;\n"
- "},\n"
- "success: function(d, s) {\n"
- "$('#prg').html('progress: success! redirecting...');\n"
- "setTimeout(function() {\n"
- "window.location.href = '/';\n"
- "}, 3000);\n"
- "},\n"
- "error: function(a, b, c) {\n"
- "$('#prg').html('progress: finished! redirecting...');\n"
- "setTimeout(function() {\n"
- "window.location.href = '/';\n"
- "}, 1000);\n"
- "}\n"
- "});\n"
- "});\n"
- "</script>\n"
- "</body></html>"
- );
-
- server->send(200, "text/html", uploadPage);
- }
-
- void SimpleUpdater::postResult(void) {
- server->sendHeader("Connection", "close");
- server->send(200, "text/plain", (Update.hasError()) ? "FAIL" : "OK");
- ESP.restart();
- }
-
- void SimpleUpdater::postUpload(void) {
- HTTPUpload& upload = server->upload();
- if (upload.status == UPLOAD_FILE_START) {
- Serial.printf("Update: %s\n", upload.filename.c_str());
- // start with max available size
- if (!Update.begin(UPDATE_SIZE_UNKNOWN)) {
- Update.printError(Serial);
- }
- } else if (upload.status == UPLOAD_FILE_WRITE) {
- // flashing firmware to ESP
- if (Update.write(upload.buf, upload.currentSize) != upload.currentSize) {
- Update.printError(Serial);
- }
- } else if (upload.status == UPLOAD_FILE_END) {
- // true to set the size to the current progress
- if (Update.end(true)) {
- Serial.printf("Update Success: %u\nRebooting...\n", upload.totalSize);
- } else {
- Update.printError(Serial);
- }
- }
- }
-
- #endif
-
- #if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
-
- void SimpleUpdater::setup(UPDATE_WEB_SERVER *_server) {
- if (_server == NULL) {
- return;
- }
-
- server = _server;
-
- #if defined(ARDUINO_ARCH_ESP8266)
-
- updateServer.setup(server);
-
- #elif defined(ARDUINO_ARCH_ESP32)
-
- server->on(uri.c_str(), HTTP_POST, [this]() {
- postResult();
- }, [this]() {
- postUpload();
- });
-
- server->on(uri.c_str(), HTTP_GET, [this]() {
- get();
- });
-
- #endif
- }
-
- SimpleUpdater::SimpleUpdater(String _uri) {
- uri = _uri;
- server = NULL;
- }
-
- #endif
|