From c228104c2963453ae6117d21e8f442264ee34a92 Mon Sep 17 00:00:00 2001 From: Nic Limper Date: Mon, 29 May 2023 23:39:07 +0200 Subject: [PATCH] update ota update --- ESP32_AP-Flasher/data/www/index.html | 15 +- ESP32_AP-Flasher/data/www/main.css | 16 +- ESP32_AP-Flasher/data/www/ota.js | 226 +++++++++++++++------------ ESP32_AP-Flasher/src/ota.cpp | 5 +- 4 files changed, 151 insertions(+), 111 deletions(-) diff --git a/ESP32_AP-Flasher/data/www/index.html b/ESP32_AP-Flasher/data/www/index.html index f4057a06..a3ebd384 100644 --- a/ESP32_AP-Flasher/data/www/index.html +++ b/ESP32_AP-Flasher/data/www/index.html @@ -122,18 +122,11 @@ Latency will be around 40 seconds.">

Update dashboard

- Updates are fetched directly from the Github repo.
-
- - +
+
+ +
diff --git a/ESP32_AP-Flasher/data/www/main.css b/ESP32_AP-Flasher/data/www/main.css index c28b2241..1c8da0e9 100644 --- a/ESP32_AP-Flasher/data/www/main.css +++ b/ESP32_AP-Flasher/data/www/main.css @@ -425,7 +425,21 @@ ul.messages li.new { #easyupdate{ margin-top: 10px; - font-size: 1.5em; +} + +#easyupdate button { + display: block; + margin: 15px 40px; + padding: 10px 20px; +} + +#easyupdate a { + cursor: pointer; + text-decoration: underline; +} + +#advanceddiv { + display: none; } #releasetable { diff --git a/ESP32_AP-Flasher/data/www/ota.js b/ESP32_AP-Flasher/data/www/ota.js index 4c4de77a..a9c95c69 100644 --- a/ESP32_AP-Flasher/data/www/ota.js +++ b/ESP32_AP-Flasher/data/www/ota.js @@ -36,18 +36,20 @@ export async function initUpdate() { }) .then(data => { if (data.env) { - print(`env: ${data.env}`); - print(`build date: ${formatEpoch(data.buildtime)}`); - print(`version: ${data.buildversion}`); - print(`filesystem: ${filesystemversion}`); - print(`sha: ${data.sha}`); - print(`psram size: ${data.psramsize}`); - print(`flash size: ${data.flashsize}`); + let matchtest=''; + if (data.buildversion != filesystemversion && filesystemversion != "custom" && data.buildversion != "custom") matchtest = " <- not matching!" + print(`env: ${data.env}`); + print(`build date: ${formatEpoch(data.buildtime)}`); + print(`esp32 version: ${data.buildversion}`); + print(`filesystem version: ${filesystemversion}` + matchtest); + print(`sha: ${data.sha}`); + print(`psram size: ${data.psramsize}`); + print(`flash size: ${data.flashsize}`); print("--------------------------","gray"); env = data.env; currentVer = data.buildversion; currentBuildtime = data.buildtime; - if (data.rollback) $("#rollbackOption").display = 'block'; + if (data.rollback) $("#rollbackOption").style.display = 'block'; } }) .catch(error => { @@ -75,24 +77,21 @@ export async function initUpdate() { }); const easyupdate = $('#easyupdate'); - if (releaseDetails.length === 0) { easyupdate.innerHTML = ("No releases found."); } else { const release = releaseDetails[0]; - if (release.tag_name == currentVer) { - easyupdate.innerHTML = `Version ${currentVer}. You are up to date`; - } else if (release.date < formatEpoch(currentBuildtime)) { - easyupdate.innerHTML = `Your version is newer than the latest release date. Are you the developer? :-)`; - } else { - easyupdate.innerHTML = `Update from version ${currentVer} to version ${release.tag_name} available.`; + if (release.tag_name) { + if (release.tag_name == currentVer) { + easyupdate.innerHTML = `Version ${currentVer}. You are up to date`; + } else if (release.date < formatEpoch(currentBuildtime)) { + easyupdate.innerHTML = `Your version is newer than the latest release date.
Are you the developer? :-)`; + } else { + easyupdate.innerHTML = `An update from version ${currentVer} to version ${release.tag_name} is available.`; + } } - - tableRow.innerHTML = tablerow; - table.appendChild(tableRow); - - return release; } + easyupdate.innerHTML += "
advanced options" const table = document.createElement('table'); const tableHeader = document.createElement('tr'); @@ -100,17 +99,19 @@ export async function initUpdate() { table.appendChild(tableHeader); releaseDetails.forEach(release => { - const tableRow = document.createElement('tr'); - let tablerow = `${release.tag_name}${release.date}${release.name}${release.author}`; - if (release.tag_name == currentVer) { - tablerow += "current version"; - } else if (release.date < formatEpoch(currentBuildtime)) { - tablerow += "older"; - } else { - tablerow += "newer"; + if (release && release.html_url) { + const tableRow = document.createElement('tr'); + let tablerow = `${release.tag_name}${release.date}${release.name}${release.author}`; + if (release.tag_name == currentVer) { + tablerow += "current version"; + } else if (release.date < formatEpoch(currentBuildtime)) { + tablerow += "older"; + } else { + tablerow += "newer"; + } + tableRow.innerHTML = tablerow; + table.appendChild(tableRow); } - tableRow.innerHTML = tablerow; - table.appendChild(tableRow); }); $('#releasetable').innerHTML = ""; @@ -123,77 +124,100 @@ export async function initUpdate() { } export function updateAll(fileUrl, tagname) { - otamodule.updateWebpage(fileUrl, tagname, false); - otamodule.updateESP(fileUrl) -} - -export function updateWebpage(fileUrl, tagname, showReload) { - if (running) return; - if (!confirm("Confirm updating the littleFS storage")) return; - - disableButtons(true); - running = true; - errors = 0; - const consoleDiv = document.getElementById('updateconsole'); - consoleDiv.scrollTop = consoleDiv.scrollHeight; - - print("Updating littleFS partition..."); - - fetch("/getexturl?url=" + fileUrl) - .then(response => response.json()) - .then(data => { - checkfiles(data.files); + updateWebpage(fileUrl, tagname, false) + .then(() => { + updateESP(fileUrl, false); }) .catch(error => { - print('Error fetching data:' + error, "red"); + console.error(error); }); - - const checkfiles = async (files) => { - for (const file of files) { - try { - const url = "/check_file?path=" + encodeURIComponent(file.path); - const response = await fetch(url); - if (response.ok) { - const data = await response.json(); - if (data.filesize == file.size && data.md5 == file.md5) { - print(`file ${file.path} is up to date`, "green"); - } else if (data.filesize == 0) { - await fetchAndPost(file.url, file.name, file.path); - } else { - await fetchAndPost(file.url, file.name, file.path); - } - } else { - print(`error checking file ${file.path}: ${response.status}`, "red"); - errors++; - } - } catch (error) { - console.error(`error checking file ${file.path}:` + error, "red"); - errors++; - } - } - writeVersion(tagname, "version.txt", "/www/version.txt") - running = false; - if (errors) { - print("------", "gray"); - print(`Finished updating with ${errors} errors.`, "red"); - } else { - print("------", "gray"); - print("Update succesful."); - } - disableButtons(false); - - if (showReload) { - const newLine = document.createElement('div'); - newLine.innerHTML = ""; - consoleDiv.appendChild(newLine); - consoleDiv.scrollTop = consoleDiv.scrollHeight; - } - }; } -export async function updateESP(fileUrl) { +export async function updateWebpage(fileUrl, tagname, showReload) { + return new Promise((resolve, reject) => { + (async function () { + try { + if (running) return; + if (showReload) { + if (!confirm("Confirm updating the filesystem")) return; + } else { + if (!confirm("Confirm updating the esp32 and filesystem")) return; + } + + disableButtons(true); + running = true; + errors = 0; + const consoleDiv = document.getElementById('updateconsole'); + consoleDiv.scrollTop = consoleDiv.scrollHeight; + + print("Updating littleFS partition..."); + fetch("/getexturl?url=" + fileUrl) + .then(response => response.json()) + .then(data => { + checkfiles(data.files); + }) + .catch(error => { + print('Error fetching data:' + error, "red"); + }); + + const checkfiles = async (files) => { + for (const file of files) { + try { + const url = "/check_file?path=" + encodeURIComponent(file.path); + const response = await fetch(url); + if (response.ok) { + const data = await response.json(); + if (data.filesize == file.size && data.md5 == file.md5) { + print(`file ${file.path} is up to date`, "green"); + } else if (data.filesize == 0) { + await fetchAndPost(file.url, file.name, file.path); + } else { + await fetchAndPost(file.url, file.name, file.path); + } + } else { + print(`error checking file ${file.path}: ${response.status}`, "red"); + errors++; + } + } catch (error) { + console.error(`error checking file ${file.path}:` + error, "red"); + errors++; + } + } + writeVersion(tagname, "version.txt", "/www/version.txt") + running = false; + if (errors) { + print("------", "gray"); + print(`Finished updating with ${errors} errors.`, "red"); + reject(error); + } else { + print("------", "gray"); + print("Update succesful."); + resolve(); + } + disableButtons(false); + + if (showReload) { + const newLine = document.createElement('div'); + newLine.innerHTML = ""; + consoleDiv.appendChild(newLine); + consoleDiv.scrollTop = consoleDiv.scrollHeight; + } + }; + } catch (error) { + print('Error: ' + error, "red"); + errors++; + reject(error); + } + })(); + }); +} + +export async function updateESP(fileUrl, showConfirm) { + print(running); if (running) return; - if (!confirm("Confirm updating the microcontroller")) return; + if (showConfirm) { + if (!confirm("Confirm updating the esp32")) return; + } disableButtons(true); running = true; @@ -261,7 +285,7 @@ $('#rollbackBtn').onclick = function () { fetch("/rollback", { method: "POST", - body: formData + body: '' }) running = false; @@ -276,7 +300,7 @@ export function print(line, color = "white") { const newLine = document.createElement('div'); newLine.style.color = color; if (line == "[reboot]") { - newLine.innerHTML = ""; + newLine.innerHTML = ""; } else { newLine.textContent = line; } @@ -287,6 +311,14 @@ export function print(line, color = "white") { } } +function reboot() { + print("Rebooting now... Reloading webpagina in 5 seconds...", "yellow"); + fetch("/reboot",{method: "POST"}); + setTimeout(() => { + location.reload(); + }, 5000); +} + function formatEpoch(epochTime) { const date = new Date(epochTime * 1000); // Convert seconds to milliseconds diff --git a/ESP32_AP-Flasher/src/ota.cpp b/ESP32_AP-Flasher/src/ota.cpp index ca1f5008..9444cf9c 100644 --- a/ESP32_AP-Flasher/src/ota.cpp +++ b/ESP32_AP-Flasher/src/ota.cpp @@ -231,9 +231,10 @@ void handleRollback(AsyncWebServerRequest* request) { if (rollbackSuccess) { request->send(200, "Rollback successful"); wsSerial("Rollback successful"); - wsSerial("Restarting system..."); + wsSerial("Reboot system now"); + wsSerial("[reboot]"); vTaskDelay(1000 / portTICK_PERIOD_MS); - ESP.restart(); + //ESP.restart(); } else { wsSerial("Rollback failed"); request->send(400, "Rollback failed");