firmware streaming

This commit is contained in:
Empire Phoenix 2024-08-11 19:17:12 +02:00
parent 65e5d0d753
commit 66e276f15e
3 changed files with 22 additions and 41 deletions

View File

@ -20,9 +20,10 @@
<h2>Battery Firmeware (bq34z100 may be R2)</h2> <h2>Battery Firmeware (bq34z100 may be R2)</h2>
<form id="upload_form" method="post"> <form id="upload_form" method="post">
<input type="file" name="battery_flash_file" id="battery_flash_file"><br> <input type="file" name="battery_flash_file" id="battery_flash_file"><br>
<progress id="progressBar" value="0" max="100" style="width:300px;"></progress> <progress id="battery_flash_progressBar" value="0" max="100" style="width:300px;"></progress>
<input type="button" name="battery_flash_button" id="battery_flash_button"><br> <input type="button" name="battery_flash_button" id="battery_flash_button"><br>
<h3 id="status"></h3> <h3 id="battery_flash_status"></h3>
<p id="battery_flash_loaded_n_total"></p>
<div style="height: 100px; display: block; overflow-y: auto;" id = "battery_flash_message"></div> <div style="height: 100px; display: block; overflow-y: auto;" id = "battery_flash_message"></div>
</form> </form>

View File

@ -2,58 +2,37 @@ let battery_flash_button = document.getElementById("battery_flash_button") as HT
let battery_flash_file = document.getElementById("battery_flash_file") as HTMLInputElement; let battery_flash_file = document.getElementById("battery_flash_file") as HTMLInputElement;
let battery_flash_message = document.getElementById("battery_flash_message") as HTMLElement; let battery_flash_message = document.getElementById("battery_flash_message") as HTMLElement;
let battery_flash_progressBar = document.getElementById("battery_flash_progressBar") as HTMLProgressElement; let battery_flash_progressBar = document.getElementById("battery_flash_progressBar") as HTMLProgressElement;
let battery_flash_loaded_n_total = document.getElementById("battery_flash_loaded_n_total") as HTMLElement;
let battery_flash_status = document.getElementById("battery_flash_status") as HTMLElement;
var ajax = new XMLHttpRequest(); var ajax = new XMLHttpRequest();
ajax.upload.addEventListener("progress", event => { ajax.upload.addEventListener("progress", event => {
loaded_n_total.innerHTML = "Uploaded " + event.loaded + " bytes of " + event.total; battery_flash_loaded_n_total.innerHTML = "Uploaded " + event.loaded + " bytes of " + event.total;
var percent = (event.loaded / event.total) * 100; var percent = (event.loaded / event.total) * 100;
progressBar.value = Math.round(percent); battery_flash_progressBar.value = Math.round(percent);
status.innerHTML = Math.round(percent) + "%"; battery_flash_status.innerHTML = Math.round(percent) + "%";
answer.innerHTML = "in progress"; battery_flash_message.innerHTML = "in progress";
}, false); }, false);
ajax.addEventListener("load", () => { ajax.addEventListener("load", () => {
status.innerHTML = ajax.responseText; battery_flash_status.innerHTML = ajax.responseText;
answer.innerHTML = "finished"; battery_flash_message.innerHTML = "finished";
progressBar.value = 0; battery_flash_progressBar.value = 0;
}, false); }, false);
ajax.addEventListener("error", () => { ajax.addEventListener("error", () => {
status.innerHTML = ajax.responseText; battery_flash_status.innerHTML = ajax.responseText;
answer.innerHTML = "failed"; battery_flash_message.innerHTML = "failed";
}, false); }, false);
ajax.addEventListener("abort", () => { ajax.addEventListener("abort", () => {
status.innerHTML = ajax.responseText; battery_flash_status.innerHTML = ajax.responseText;
answer.innerHTML = "aborted"; battery_flash_message.innerHTML = "aborted";
}, false); }, false);
ajax.open("POST", "/ota");
ajax.send(file);
}
export async function streamToText(stream: ReadableStream<Uint8Array>): Promise<string> {
let result = '';
const reader = stream.pipeThrough(new TextDecoderStream()).getReader();
while (true) {
const { done, value } = await reader.read();
if (done) {
break;
}
result += value;
battery_flash_message.innerHTML = result;
battery_flash_message.scrollTop = battery_flash_message.scrollHeight;
}
return result;
}
battery_flash_button.onclick = async function (){ battery_flash_button.onclick = async function (){
fetch("/flashbattery", { ajax.open("POST", "/flashbattery");
method :"POST", ajax.send(battery_flash_file.files[0]);
body: battery_flash_file.files[0],
headers: {
"Content-Type": ""
},
}).then((response) => response.body)
.then((body) => {
streamToText(body)
});
}; };

View File

@ -327,7 +327,8 @@ let fromWrapper = (() => {
updateJson() updateJson()
fetch("/set_config", { fetch("/set_config", {
method :"POST", method :"POST",
body: json.value body: json.value,
}) })
.then(response => response.text()) .then(response => response.text())
.then(text => submit_status.innerText = text) .then(text => submit_status.innerText = text)