Skip to content

Commit

Permalink
Smoother update progression
Browse files Browse the repository at this point in the history
  • Loading branch information
noisymime committed Sep 24, 2024
1 parent 0abfbc9 commit fc6213f
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 12 deletions.
7 changes: 4 additions & 3 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ void setup()
initBLE();
initWiFi();

delay(1000);
delay(500);
Serial.println("Connection Type: " + String(config.getUChar("connection_type")));

if( (config.getUChar("connection_type") == CONNECTION_TYPE_TUNERSTUDIO) )
Expand Down Expand Up @@ -67,9 +67,10 @@ void setup()

//Updates the firmware AND data from remote URLs
server.on(UPDATE_REMOTE_URL, HTTP_POST, [](AsyncWebServerRequest *request) {
request->onDisconnect([](){
ESP.restart();
});
request->send(200, "text/html", saveRemoteFW_URLs(request));
delay(1000); //Wait 1 second to allow the page to be sent before restarting
ESP.restart();
});
//Scan the wifi networks and return them as JSON
server.on("/wifi", HTTP_GET, [](AsyncWebServerRequest *request) {
Expand Down
2 changes: 1 addition & 1 deletion src/static/static_js.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ String staticJS_updates()
{
String js = "";
js += "<script>";
js += "function i(t){return document.getElementById(t)}function r(t,e){return t=t.replace('v',''),e=e.replace('v',''),t.startsWith(e+'-')?-1:e.startsWith(t+'-')?1:t.localeCompare(e,void 0,{numeric:!0,sensitivity:'case',caseFirst:'upper'})}function o(t){return t.browser_download_url.split('/').pop()}async function getLatestGithubRelease(t){var e,a=await(await fetch('https://api.github.com/repos/speeduino/AirBear/releases/latest')).json(),n=a.tag_name;if(1==r(i('latest_release_txt').innerHTML=n,t)){i('update_btn').disabled=!1;for(const s of a.assets)s.name.includes('littlefs')?(e='http://speeduino.com/fw/AirBear/'+n+'/'+o(s),i('newData_url').value=e):(e='http://speeduino.com/fw/AirBear/'+n+'/'+o(s),i('newFW_url').value=e)}}async function scanWifi(){var t=i('ssid');for(const a of(await(await fetch('/wifi')).json()).networks){var e=document.createElement('option');e.value=a.ssid,e.text=a.ssid,t.add(e)}}function toggleData(){var t=i('newData_url');t.disabled=!t.disabled}function a(t,e){t.innerHTML=e}function updateProgress(){setTimeout(async()=>{let t;try{var e=await fetch('/updateStatus');t=await e.json()}catch(t){updateProgress()}t&&(a(i('updateStatus'),t.updateStatus),a(i('updateComplete'),t.updateProgress),a(i('updateSize'),t.updateSize),e=Math.floor(t.updateProgress/t.updateSize*100),a(i('updatePercent'),e),98<=e?window.location.href='/':updateProgress())},1500)}";
js += "function i(e){return document.getElementById(e)}function r(e,t){return e=e.replace('v',''),t=t.replace('v',''),e.startsWith(t+'-')?-1:t.startsWith(e+'-')?1:e.localeCompare(t,void 0,{numeric:!0,sensitivity:'case',caseFirst:'upper'})}function o(e){return e.browser_download_url.split('/').pop()}async function getLatestGithubRelease(e){var t,a=await(await fetch('https://api.github.com/repos/speeduino/AirBear/releases/latest')).json(),n=a.tag_name;if(1==r(i('latest_release_txt').innerHTML=n,e)){i('update_btn').disabled=!1;for(const s of a.assets)s.name.includes('littlefs')?(t='http://speeduino.com/fw/AirBear/'+n+'/'+o(s),i('newData_url').value=t):(t='http://speeduino.com/fw/AirBear/'+n+'/'+o(s),i('newFW_url').value=t)}}async function scanWifi(){var e=i('ssid');for(const a of(await(await fetch('/wifi')).json()).networks){var t=document.createElement('option');t.value=a.ssid,t.text=a.ssid,e.add(t)}}function toggleData(){var e=i('newData_url');e.disabled=!e.disabled}function a(e,t){e.innerHTML=t}function updateProgress(){setTimeout(async()=>{let e;try{var t=await fetch('/updateStatus');e=await t.json()}catch(e){updateProgress()}e&&(a(i('updateStatus'),e.updateStatus),a(i('updateComplete'),e.updateProgress),a(i('updateSize'),e.updateSize),t=Math.floor(e.updateProgress/e.updateSize*100),a(i('updatePercent'),t),98<=t?setTimeout(()=>{var e=window.location;e.href='/',e=e.href},7000):updateProgress())},1100)}";
js += "</script>";

return js;
Expand Down
11 changes: 9 additions & 2 deletions src/static/static_js.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,15 @@ function updateProgress()
const percentComplete = Math.floor((jsonData.updateProgress / jsonData.updateSize) * 100)
setInnerHTML(getElementByID('updatePercent'), percentComplete)
console.log(percentComplete)
if(percentComplete >= 98) { window.location.href = '/' }
if(percentComplete >= 98)
{
setTimeout(() => {
const wl = window.location
wl.href = '/'
wl=wl.href //Force a refresh of page without resending POST data
}, 7000)
}
else { updateProgress() }
}
}, 1500);
}, 1100);
}
12 changes: 6 additions & 6 deletions src/updater.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,12 @@ String updateInProgressPage()
updatePage += "<div class=\"content\">";

updatePage += "<div id=\"content-1\">";
updatePage += "Current Status: <span id=\"updateStatus\">Preparing to update</span><br/>";
updatePage += "Current Progress: <span id=\"updateComplete\"></span><br/>";
updatePage += "Update Size: <span id=\"updateSize\"></span><br/>";
updatePage += "Update Completion: <span id=\"updatePercent\"></span>%<div/>";
updatePage += "<strong>Current Status: </strong><span id=\"updateStatus\">Preparing to update</span><br/>";
updatePage += "<strong>Update Size: </strong><span id=\"updateSize\"></span><br/>";
updatePage += "<strong>Downloaded: </strong><span id=\"updateComplete\"></span><br/>";
updatePage += "<strong>Update Completion: </strong><span id=\"updatePercent\"></span>%<div/>";

updatePage += "</div></div></body></html>";
updatePage += "<br/></div></div></body></html>";

return updatePage;
}
Expand Down Expand Up @@ -193,7 +193,7 @@ void updateFromRemote()
case HTTP_UPDATE_OK:
Serial.println("HTTP_UPDATE_OK");
config.putString("newData_url", "");
delay(500);
delay(1300); //Need to wait at least 1 update cycle (1100ms) so that the frontend knows the update is complete
ESP.restart();
break;
}
Expand Down

0 comments on commit fc6213f

Please sign in to comment.