ota updates
This commit is contained in:
+64
-72
@@ -5,6 +5,7 @@
|
||||
#include <FileManager.h>
|
||||
#include <ChronoProfile.h>
|
||||
#include <GunProfile.h>
|
||||
#include <Update.h>
|
||||
#include "Display.h"
|
||||
|
||||
const char *ssid = "routeguy";
|
||||
@@ -73,6 +74,7 @@ void ChronoWebServer::_handleRoot() {
|
||||
.then(data => console.log(data))\
|
||||
.catch(error => console.error('Error:', error));\
|
||||
}\
|
||||
fetch('/version').then(r=>r.json()).then(v=>{document.getElementById('currentVersion').textContent=v.version}).catch(()=>{});\
|
||||
function doOta(){\
|
||||
const fileInput = document.getElementById('otaFile');\
|
||||
if (!fileInput.files.length) {\
|
||||
@@ -81,17 +83,21 @@ void ChronoWebServer::_handleRoot() {
|
||||
}\
|
||||
const formData = new FormData();\
|
||||
formData.append('otaFile', fileInput.files[0]);\
|
||||
fetch('/ota', {\
|
||||
method: 'POST',\
|
||||
body: formData\
|
||||
})\
|
||||
.then(response => response.text())\
|
||||
.then(data => {\
|
||||
console.log(data);\
|
||||
alert('OTA update in progress... Device will restart.');\
|
||||
window.location.href = '/';\
|
||||
})\
|
||||
.catch(error => console.error('Error:', error));\
|
||||
const xhr = new XMLHttpRequest();\
|
||||
xhr.open('POST', '/ota', true);\
|
||||
xhr.onload = function () {\
|
||||
if (xhr.status >= 200 && xhr.status < 300) {\
|
||||
const responseData = JSON.parse(xhr.responseText);\
|
||||
console.log('Success:', responseData);\
|
||||
window.location.href = '/';\
|
||||
} else {\
|
||||
console.error('Server error:', xhr.status);\
|
||||
}\
|
||||
};\
|
||||
xhr.onerror = function () {\
|
||||
console.error('Network request failed');\
|
||||
};\
|
||||
xhr.send(formData);\
|
||||
}\
|
||||
document.addEventListener('DOMContentLoaded', function() {\
|
||||
const rows = document.querySelectorAll('.clickable-row');\
|
||||
@@ -134,12 +140,14 @@ void ChronoWebServer::_handleRoot() {
|
||||
<button onclick='reset()'>Reset</button>\
|
||||
<button onclick='format()'>Format</button>\
|
||||
<button onclick='toggleTestMode()'>Toggle Test Mode</button>\
|
||||
<button onclick='doOta()'>OTA Update</button>\
|
||||
</div>\
|
||||
<div style='margin-top:20px; padding: 10px; border: 1px solid #999;'>\
|
||||
<h3>OTA Firmware Update</h3>\
|
||||
<input type='file' id='otaFile' accept='.bin,.bin'></input>\
|
||||
<p style='font-size: 12px; color: #666;'>Select a .bin firmware file to update over-the-air</p>\
|
||||
<p style='font-size: 12px; color: #666;'>Current Version: <strong id='currentVersion'>Unknown</strong></p>\
|
||||
<input type='file' id='otaFile' accept='.bin,.bin' style='margin-bottom: 10px;'></input>\
|
||||
<p style='font-size: 12px; color: #666;'>1. Select a .bin firmware file to update over-the-air</p>\
|
||||
<button onclick='doOta()' style='margin-top: 5px; padding: 8px 16px; background-color: #4CAF50; color: white; border: none; border-radius: 4px; cursor: pointer;'>OTA Upgrade</button>\
|
||||
<p style='font-size: 12px; color: #666;'>2. Click OTA Upgrade to install</p>\
|
||||
</div>\
|
||||
<div style='margin-top:100px; position:relative;'>\
|
||||
<h3>Gun Profiles</h3>\
|
||||
@@ -460,69 +468,46 @@ void ChronoWebServer::_handleTestMode(){
|
||||
testModeCallback();
|
||||
_server.send(200, "text/html", "ok");
|
||||
}
|
||||
void ChronoWebServer::_handleOta(){
|
||||
HTTPMethod method = _server.method();
|
||||
|
||||
if (method == HTTP_GET) {
|
||||
// Serve OTA update page
|
||||
String temp = "<html>\
|
||||
<head>\
|
||||
<title>OTA Firmware Update</title>\
|
||||
<style>\
|
||||
body { background-color: #cccccc; font-family: Arial, Helvetica, Sans-Serif; Color: #000088; }\
|
||||
</style>\
|
||||
</head>\
|
||||
<body>\
|
||||
<h1>OTA Firmware Update</h1>\
|
||||
<p>Select a .bin firmware file to update over-the-air.</p>\
|
||||
<form method='POST' action='/ota' enctype='multipart/form-data'>\
|
||||
<div>\
|
||||
<h3>Firmware File</h3>\
|
||||
<input type='file' name='otaFile' accept='.bin'></input>\
|
||||
</div>\
|
||||
<div style='margin-top: 20px;'>\
|
||||
<button type='submit'>Upload</button>\
|
||||
</div>\
|
||||
</form>\
|
||||
</body>\
|
||||
</html>";
|
||||
_server.send(200, "text/html", temp);
|
||||
void handleOtaUpdate() {
|
||||
size_t fsize = UPDATE_SIZE_UNKNOWN;
|
||||
if (_server.hasArg("size")) {
|
||||
fsize = _server.arg("size").toInt();
|
||||
}
|
||||
else if (method == HTTP_POST) {
|
||||
// Handle firmware upload
|
||||
HTTPUpload& upload = _server.upload();
|
||||
|
||||
if (upload.status == UPLOAD_FILE_START) {
|
||||
Serial.println("OTA: Starting update...");
|
||||
// Initialize update with the size of the uploaded file
|
||||
if (!Update.begin(upload.totalSize)) {
|
||||
Serial.println("OTA: Error: Not enough space");
|
||||
_server.send(500, "text/plain", "Not enough space");
|
||||
return;
|
||||
}
|
||||
HTTPUpload &upload = _server.upload();
|
||||
if (upload.status == UPLOAD_FILE_START) {
|
||||
|
||||
Serial.printf("Receiving Update: %s, Size: %d\n", upload.filename.c_str(), fsize);
|
||||
if (!Update.begin(fsize)) {
|
||||
//otaDone = 0;
|
||||
//Update.printError(Serial);
|
||||
}
|
||||
else if (upload.status == UPLOAD_FILE_WRITE) {
|
||||
// Write received firmware to flash
|
||||
if (Update.write(upload.buf, upload.currentSize) != upload.currentSize) {
|
||||
Serial.println("OTA: Error: Write failed");
|
||||
_server.send(500, "text/plain", "Write failed");
|
||||
return;
|
||||
}
|
||||
} else if (upload.status == UPLOAD_FILE_WRITE) {
|
||||
if (Update.write(upload.buf, upload.currentSize) != upload.currentSize) {
|
||||
// Update.printError(Serial);
|
||||
} else {
|
||||
// otaDone = 100 * Update.progress() / Update.size();
|
||||
}
|
||||
else if (upload.status == UPLOAD_FILE_END) {
|
||||
// End of upload
|
||||
if (Update.end(true)) {
|
||||
Serial.printf("OTA: Update complete: %u bytes\n", upload.totalSize);
|
||||
_server.send(200, "text/html", "<h1>OTA Update Successful!<br>Restarting...</h1>");
|
||||
delay(1000);
|
||||
ESP.restart();
|
||||
} else {
|
||||
Serial.println("OTA: Error: Update end failed");
|
||||
_server.send(500, "text/plain", "Update failed");
|
||||
}
|
||||
} else if (upload.status == UPLOAD_FILE_END) {
|
||||
if (Update.end(true)) {
|
||||
Serial.printf("Update Success: %u bytes\nRebooting...\n", upload.totalSize);
|
||||
} else {
|
||||
Serial.printf("%s\n", Update.errorString());
|
||||
// otaDone = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
void handleOtaUpdateEnd() {
|
||||
_server.sendHeader("Connection", "close");
|
||||
if (Update.hasError()) {
|
||||
_server.send(502, "text/plain", Update.errorString());
|
||||
} else {
|
||||
_server.sendHeader("Refresh", "10");
|
||||
_server.sendHeader("Location", "/");
|
||||
_server.send(307);
|
||||
delay(500);
|
||||
ESP.restart();
|
||||
}
|
||||
}
|
||||
void ChronoWebServer::init(){
|
||||
WiFi.mode(WIFI_STA);
|
||||
WiFi.begin(ssid, password);
|
||||
@@ -552,10 +537,17 @@ void ChronoWebServer::init(){
|
||||
_server.on("/reset", _handleReset);
|
||||
_server.on("/format", _handleFormat);
|
||||
_server.on("/testmode", _handleTestMode);
|
||||
_server.on("/ota", _handleOta);
|
||||
_server.on("/ota", HTTP_POST, [](){handleOtaUpdateEnd();}, [](){handleOtaUpdate();});
|
||||
_server.on("/version", HTTP_GET, _handleVersion);
|
||||
_server.on("/", _handleRoot);
|
||||
_server.begin();
|
||||
}
|
||||
|
||||
void ChronoWebServer::process(){
|
||||
_server.handleClient();
|
||||
}
|
||||
|
||||
void ChronoWebServer::_handleVersion() {
|
||||
String versionJson = String("{\"version\":\"") + CHRONO_VERSION + "\",\"major\":" + String(CHRONO_VERSION_MAJOR) + ",\"minor\":" + String(CHRONO_VERSION_MINOR) + ",\"patch\":" + String(CHRONO_VERSION_PATCH) + "}";
|
||||
_server.send(200, "application/json", versionJson);
|
||||
}
|
||||
Reference in New Issue
Block a user