Browse Source

Upgrade form and handle code

fastled
Xose Pérez 7 years ago
parent
commit
a5f2cb6527
6 changed files with 195 additions and 65 deletions
  1. BIN
      code/espurna/data/index.html.gz
  2. +61
    -60
      code/espurna/static/index.html.gz.h
  3. +38
    -2
      code/espurna/web.ino
  4. +14
    -2
      code/html/custom.css
  5. +71
    -0
      code/html/custom.js
  6. +11
    -1
      code/html/index.html

BIN
code/espurna/data/index.html.gz View File


+ 61
- 60
code/espurna/static/index.html.gz.h
File diff suppressed because it is too large
View File


+ 38
- 2
code/espurna/web.ino View File

@ -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


+ 14
- 2
code/html/custom.css View File

@ -45,12 +45,15 @@
.button-update {
background: #1f8dd6;
}
.button-reset {
.button-reset,
.button-reconnect {
background: rgb(202, 60, 60);
}
.button-reconnect {
.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;
}


+ 71
- 0
code/html/custom.js View File

@ -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();


+ 11
- 1
code/html/index.html View File

@ -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">&nbsp;</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>


Loading…
Cancel
Save