mirror of
https://github.com/xoseperez/espurna.git
synced 2026-03-11 10:47:10 +01:00
Upgrade form and handle code
This commit is contained in:
Binary file not shown.
File diff suppressed because one or more lines are too long
@@ -172,6 +172,9 @@ void _wsParse(uint32_t client_id, uint8_t * payload, size_t length) {
|
||||
String key = config[i]["name"];
|
||||
String value = config[i]["value"];
|
||||
|
||||
// Skip firmware filename
|
||||
if (key.equals("filename")) continue;
|
||||
|
||||
#if ENABLE_POW
|
||||
|
||||
if (key == "powExpectedPower") {
|
||||
@@ -815,9 +818,41 @@ void _onHome(AsyncWebServerRequest *request) {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
void _onUpgrade(AsyncWebServerRequest *request) {
|
||||
AsyncWebServerResponse *response = request->beginResponse(200, "text/plain", Update.hasError() ? "FAIL" : "OK");
|
||||
response->addHeader("Connection", "close");
|
||||
request->send(response);
|
||||
deferred.once_ms(100, []() {
|
||||
ESP.restart();
|
||||
});
|
||||
}
|
||||
|
||||
void _onUpgradeData(AsyncWebServerRequest *request, String filename, size_t index, uint8_t *data, size_t len, bool final) {
|
||||
if (!index) {
|
||||
DEBUG_MSG_P(PSTR("[UPGRADE] Start: %s\n"), filename.c_str());
|
||||
Update.runAsync(true);
|
||||
if (!Update.begin((ESP.getFreeSketchSpace() - 0x1000) & 0xFFFFF000)) {
|
||||
Update.printError(Serial);
|
||||
}
|
||||
}
|
||||
if (!Update.hasError()) {
|
||||
if (Update.write(data, len) != len) {
|
||||
Update.printError(Serial);
|
||||
}
|
||||
}
|
||||
if (final) {
|
||||
if (Update.end(true)){
|
||||
DEBUG_MSG_P(PSTR("[UPGRADE] Success: %u bytes\n"), index + len);
|
||||
} else {
|
||||
Update.printError(Serial);
|
||||
}
|
||||
} else {
|
||||
DEBUG_MSG_P(PSTR("[UPGRADE] Progress: %u bytes\r"), index + len);
|
||||
}
|
||||
}
|
||||
|
||||
void webSetup() {
|
||||
|
||||
// Create server
|
||||
@@ -843,7 +878,8 @@ void webSetup() {
|
||||
_server->on("/config", HTTP_GET, _onGetConfig);
|
||||
_server->on("/auth", HTTP_GET, _onAuth);
|
||||
_server->on("/apis", HTTP_GET, _onAPIs);
|
||||
_server->on("/rpc", HTTP_GET, _onRPC);
|
||||
// _server->on("/rpc", HTTP_GET, _onRPC);
|
||||
_server->on("/upgrade", HTTP_POST, _onUpgrade, _onUpgradeData);
|
||||
|
||||
// Serve static files
|
||||
#if not EMBEDDED_WEB
|
||||
|
||||
@@ -45,12 +45,15 @@
|
||||
.button-update {
|
||||
background: #1f8dd6;
|
||||
}
|
||||
.button-reset {
|
||||
background: rgb(202, 60, 60);
|
||||
}
|
||||
.button-reset,
|
||||
.button-reconnect {
|
||||
background: rgb(202, 60, 60);
|
||||
}
|
||||
.button-upgrade {
|
||||
background: rgb(202, 60, 60);
|
||||
margin-left: 5px;
|
||||
}
|
||||
.button-upgrade-browse,
|
||||
.button-apikey {
|
||||
background: rgb(0, 202, 0);
|
||||
margin-left: 5px;
|
||||
@@ -101,6 +104,15 @@ div.hint {
|
||||
.template {
|
||||
display: none;
|
||||
}
|
||||
input[name=upgrade] {
|
||||
display: none;
|
||||
}
|
||||
#upgrade-progress {
|
||||
display: none;
|
||||
width: 100%;
|
||||
height: 20px;
|
||||
margin-top: 10px;
|
||||
}
|
||||
.pure-form .center {
|
||||
margin: .5em 0 .2em;
|
||||
}
|
||||
|
||||
@@ -41,6 +41,7 @@ function doUpdate() {
|
||||
var form = $("#formSave");
|
||||
if (validateForm(form)) {
|
||||
var data = form.serializeArray();
|
||||
delete(data['filename']);
|
||||
websock.send(JSON.stringify({'config': data}));
|
||||
$(".powExpected").val(0);
|
||||
$("input[name='powExpectedReset']")
|
||||
@@ -50,6 +51,66 @@ function doUpdate() {
|
||||
return false;
|
||||
}
|
||||
|
||||
function doUpgrade() {
|
||||
|
||||
var contents = $("input[name='upgrade']")[0].files[0];
|
||||
if (typeof contents == 'undefined') {
|
||||
alert("First you have to select a file from your computer.");
|
||||
return false;
|
||||
}
|
||||
var filename = $("input[name='upgrade']").val().split('\\').pop();
|
||||
|
||||
var data = new FormData();
|
||||
data.append('upgrade', contents, filename);
|
||||
|
||||
$.ajax({
|
||||
|
||||
// Your server script to process the upload
|
||||
url: 'http://' + host + ':' + port + '/upgrade',
|
||||
type: 'POST',
|
||||
|
||||
// Form data
|
||||
data: data,
|
||||
|
||||
// Tell jQuery not to process data or worry about content-type
|
||||
// You *must* include these options!
|
||||
cache: false,
|
||||
contentType: false,
|
||||
processData: false,
|
||||
|
||||
success: function(data, text) {
|
||||
$("#upgrade-progress").hide();
|
||||
if (data == 'OK') {
|
||||
alert("Firmware image uploaded, board rebooting. This page will be refreshed in 3 seconds.");
|
||||
setTimeout(function() {
|
||||
window.location = "/";
|
||||
}, 3000);
|
||||
} else {
|
||||
alert("There was an error trying to upload the new image, please try again.");
|
||||
}
|
||||
},
|
||||
|
||||
// Custom XMLHttpRequest
|
||||
xhr: function() {
|
||||
$("#upgrade-progress").show();
|
||||
var myXhr = $.ajaxSettings.xhr();
|
||||
if (myXhr.upload) {
|
||||
// For handling the progress of the upload
|
||||
myXhr.upload.addEventListener('progress', function(e) {
|
||||
if (e.lengthComputable) {
|
||||
$('progress').attr({ value: e.loaded, max: e.total });
|
||||
}
|
||||
} , false);
|
||||
}
|
||||
return myXhr;
|
||||
},
|
||||
|
||||
});
|
||||
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
function doUpdatePassword() {
|
||||
var form = $("#formPassword");
|
||||
if (validateForm(form)) {
|
||||
@@ -448,6 +509,16 @@ function init() {
|
||||
$(".button-settings-restore").on('click', restoreSettings);
|
||||
$('#uploader').on('change', onFileUpload);
|
||||
$(".button-apikey").on('click', doGenerateAPIKey);
|
||||
$(".button-upgrade").on('click', doUpgrade);
|
||||
$(".button-upgrade-browse").on('click', function() {
|
||||
$("input[name='upgrade']")[0].click();
|
||||
return false;
|
||||
});
|
||||
$("input[name='upgrade']").change(function (){
|
||||
var fileName = $(this).val();
|
||||
$("input[name='filename']").val(fileName.replace(/^.*[\\\/]/, ''));
|
||||
});
|
||||
$('progress').attr({ value: 0, max: 100 });
|
||||
$(".pure-menu-link").on('click', showPanel);
|
||||
$(".button-add-network").on('click', function() {
|
||||
$("div.more", addNetwork()).toggle();
|
||||
|
||||
@@ -242,7 +242,7 @@
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<form id="formSave" class="pure-form" action="/" method="post">
|
||||
<form id="formSave" class="pure-form" action="/" method="post" enctype="multipart/form-data">
|
||||
|
||||
<input class="pure-u-1 pure-u-sm-3-4" type="hidden" name="webMode" value="0" />
|
||||
|
||||
@@ -385,6 +385,16 @@
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="pure-g">
|
||||
<label class="pure-u-1 pure-u-md-1-4">Upgrade</label>
|
||||
<input class="pure-u-1-2 pure-u-md-1-2" name="filename" type="text" readonly />
|
||||
<div class=" pure-u-1-8 pure-u-md-1-8"><button class="pure-button button-upgrade-browse pure-u-23-24">Browse</button></div>
|
||||
<div class=" pure-u-1-8 pure-u-md-1-8"><button class="pure-button button-upgrade pure-u-23-24">Upgrade</button></div>
|
||||
<div class="pure-u-0 pure-u-md-1-4"> </div>
|
||||
<div class="pure-u-1 pure-u-md-3-4"><progress id="upgrade-progress"></progress></div>
|
||||
<input name="upgrade" type="file" tabindex="15" />
|
||||
</div>
|
||||
|
||||
</fieldset>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user