Files
AI-on-the-edge-device/sd-card/html/ota_page.html
2022-09-25 16:21:12 +02:00

246 lines
7.4 KiB
HTML

<!DOCTYPE html>
<html>
<head>
<link rel="icon" href="favicon.ico" type="image/x-icon">
<title>OTA Update</title>
<meta charset="utf-8">
<style>
h1 {font-size: 2em;}
h2 {font-size: 1.5em;}
h3 {font-size: 1.2em;}
p {font-size: 1em;}
input[type=number] {
width: 138px;
padding: 10px 5px;
display: inline-block;
border: 1px solid #ccc;
font-size: 16px;
}
.button {
padding: 10px 20px;
width: 211px;
font-size: 16px;
}
</style>
</head>
<body style="font-family: arial; padding: 0px 10px;">
<p>Check the <a href="https://github.com/jomjol/AI-on-the-edge-device/releases" target=_blank>Release Page</a> to see if there is an update available.</p>
<p>Normally, the overall update package (<i><span style="font-family:monospace">update__*.zip</span></i>) is your best choice!<br>
Alternatively you can use the old style <i><span style="font-family:monospace">firmware__*.bin</span></i> and
web interface (<i><span style="font-family:monospace">html__*.zip</span></i>). How ever it is strongly recommended to update firmware and
web interface at the same time!</p>
<hr>
<h2>Update</h2>
<table class="fixed" border="0">
<tr>
<p>
<label for="newfile">Select the file containig the update (
<i><span style="font-family:monospace">update__*.zip</span></i>,
<i><span style="font-family:monospace">firmware__*.bin</span></i>,
<i><span style="font-family:monospace">html__*.zip</span></i>,
<i><span style="font-family:monospace">*.tfl/tflite</span></i>)
</p>
</tr>
<tr>
<p>
<input id="newfile" type="file" onchange="setpath()" style="width:100%;">
</p>
</tr>
<tr>
<p>
<button class="button" id="doUpdate" type="button" onclick="upload()">Do upload and update<br>(incl. reboot - if needed)</button>
</p>
</tr>
<tr>
<p>
<h3><div id="status">Status: idle</div></h3>
</p>
</tr>
</table>
<h2>Reboot</h2>
<button class="button" id="reboot" type="button" onclick="doReboot()">Manual reboot</button>
<hr>
<script type="text/javascript" src="./gethost.js"></script>
<script language="JavaScript">
var basepath = "http://192.168.178.26";
function init(){
basepath = getbasepath();
// document.getElementById("reboot").disabled = true;
document.getElementById("doUpdate").disabled = true;
}
function doUpdate() {
if (confirm("Are you sure to update the firmware?")) {
var stringota = "/ota?file=firmware.bin";
document.getElementById("doUpdate").disabled = true;
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4) {
if (xhttp.status == 200) {
document.getElementById("reboot").disabled = false;
alert("Flash successfull - Reboot necessary to make changes active.");
/* keine Reaktion, damit sich das Dokument nicht ändert */
} else if (xhttp.status == 0) {
alert("Server closed the connection abruptly!");
UpdatePage();
} else {
alert(xhttp.status + " Error!\n" + xhttp.responseText);
UpdatePage();
}
}
};
xhttp.open("GET", stringota, true);
xhttp.send();
}
}
function doReboot() {
if (confirm("Are you sure you want to reboot the ESP32?")) {
var stringota = "/reboot";
window.location = stringota;
window.location.href = stringota;
window.location.assign(stringota);
window.location.replace(stringota);
}
}
function setpath() {
var nameneu = document.getElementById("newfile").value;
nameneu = nameneu.split(/[\\\/]/).pop();
document.getElementById("doUpdate").disabled = false;
document.getElementById("status").innerText = "Status: file selected";
}
function upload() {
document.getElementById("reboot").disabled = true;
document.getElementById("doUpdate").disabled = true;
var xhttp = new XMLHttpRequest();
var nameneu = document.getElementById("newfile").value;
filePath = nameneu.split(/[\\\/]/).pop();
var upload_path = "/upload/firmware/" + filePath;
var fileInput = document.getElementById("newfile").files;
/* first delete the old firmware */
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4) {
if (xhttp.status == 200) {
/* keine Reaktion, damit sich das Dokument nicht ändert */
} else if (xhttp.status == 0) {
alert("Server closed the connection abruptly!");
UpdatePage();
} else {
alert(xhttp.status + " Error!\n" + xhttp.responseText);
UpdatePage();
}
}
};
var _toDo = basepath + "/ota?delete=" + filePath;
xhttp.open("GET", _toDo, false);
xhttp.send();
/* ----------------------------- */
/* Max size of an individual file. Make sure this
* value is same as that set in server_file.c */
var MAX_FILE_SIZE = 8000*1024;
var MAX_FILE_SIZE_STR = "8MB";
if (fileInput.length == 0) {
alert("No file selected!");
} else if (filePath.length == 0) {
alert("File path on server is not set!");
} else if (filePath.indexOf(' ') >= 0) {
alert("File path on server cannot have spaces!");
} else if (filePath[filePath.length-1] == '/') {
alert("File name not specified after path!");
} else if (fileInput[0].size > MAX_FILE_SIZE) {
alert("File size must be less than " + MAX_FILE_SIZE_STR + "!");
} else {
document.getElementById("newfile").disabled = true;
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4) {
if (xhttp.status == 200) {
// alert("Upload successfull!")
// document.reload();
document.getElementById("reboot").disabled = false;
document.getElementById("doUpdate").disabled = false;
} else if (xhttp.status == 0) {
alert("Server closed the connection abruptly!");
UpdatePage();
} else {
alert(xhttp.status + " Error!\n" + xhttp.responseText);
UpdatePage();
}
}
};
var file = fileInput[0];
xhttp.open("POST", upload_path, false);
document.getElementById("status").innerText = "Status: uploading";
xhttp.send(file);
}
document.getElementById("status").innerText = "Status: processing on ESP32";
var xhttp = new XMLHttpRequest();
/* first delete the old firmware */
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4) {
if (xhttp.status == 200) {
if (xhttp.responseText.startsWith("reboot"))
{
doReboot();
}
else
{
alert("Processing done!\n\n" + xhttp.responseText);
}
} else if (xhttp.status == 0) {
alert("Server closed the connection abruptly!");
UpdatePage();
} else {
alert(xhttp.status + " Error!\n" + xhttp.responseText);
UpdatePage();
}
}
};
var _toDo = basepath + "/ota?task=update&file=" + filePath;
xhttp.open("GET", _toDo, false);
xhttp.send();
/* ----------------------------- */
document.getElementById("reboot").disabled = false;
document.getElementById("doUpdate").disabled = true;
document.getElementById("newfile").disabled = false;
}
init();
</script>
</body>
</html>