Skip to content

Commit

Permalink
Server send event testing
Browse files Browse the repository at this point in the history
  • Loading branch information
pkendall64 committed Nov 9, 2021
1 parent 09c99cd commit 2205f42
Show file tree
Hide file tree
Showing 10 changed files with 168 additions and 140 deletions.
141 changes: 80 additions & 61 deletions html/scan.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,83 +173,102 @@ function autocomplete(inp, arr) {

//=========================================================

var uploading = {};

function uploadFile(type_suffix) {
var file = _("firmware_file_" + type_suffix).files[0];
var formdata = new FormData();
formdata.append("type", type_suffix);
formdata.append("upload", file, file.name);
uploading = {"type": type_suffix, "size": file.size};

var ajax = new XMLHttpRequest();
ajax.upload.addEventListener("progress", progressHandler(type_suffix), false);
ajax.addEventListener("load", completeHandler(type_suffix), false);
ajax.addEventListener("load", (e)=>{
console.log("complete");
}, false);
ajax.addEventListener("error", errorHandler(type_suffix), false);
ajax.addEventListener("abort", abortHandler(type_suffix), false);
ajax.open("POST", "/update");
ajax.send(formdata);
}

function progressHandler(type_suffix) {
return function (event) {
//_("loaded_n_total").innerHTML = "Uploaded " + event.loaded + " bytes of " + event.total;
var percent = Math.round((event.loaded / event.total) * 100);
_("progressBar_" + type_suffix).value = percent;
_("status_" + type_suffix).innerHTML = percent + "% uploaded... please wait";
function uploaded(event) {
var type_suffix = uploading.type;
_("status_" + type_suffix).innerHTML = "";
_("progressBar_" + type_suffix).value = 0;
var data = JSON.parse(event.data);
if (data.status === 'ok') {
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("POST", "/finishupdate", true);
xmlhttp.send();
cuteAlert({
type: 'success',
title: "Update Succeeded",
message: data.msg
});
} else if (data.status === 'cancelled') {
cuteAlert({
type: 'success',
title: "Update Cancelled",
message: data.msg
});
} else if (data.status === 'mismatch') {
cuteAlert({
type: 'question',
title: "Targets Mismatch",
message: data.msg,
confirmText: "Flash anyway",
cancelText: "Cancel"
}).then((e)=>{
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function () {
if (this.readyState == 4) {
_("status_" + type_suffix).innerHTML = "";
_("progressBar_" + type_suffix).value = 0;
if (this.status == 200) {
var data = JSON.parse(this.responseText);
cuteAlert({
type: "info",
title: "Force Update",
message: data.msg
});
}
else {
cuteAlert({
type: "error",
title: "Force Update",
message: "An error occurred trying to force the update"
});
}
}
};
xmlhttp.open("POST", "/forceupdate", true);
var data = new FormData();
data.append("action", e);
xmlhttp.send(data);
});
} else {
cuteAlert({
type: 'error',
title: "Update Failed",
message: data.msg
});
}
}

function completeHandler(type_suffix) {
return function(event) {
_("status_" + type_suffix).innerHTML = "";
_("progressBar_" + type_suffix).value = 0;
var data = JSON.parse(event.target.responseText);
if (data.status === 'ok') {
cuteAlert({
type: 'success',
title: "Update Succeeded",
message: data.msg
});
} else if (data.status === 'mismatch') {
cuteAlert({
type: 'question',
title: "Targets Mismatch",
message: data.msg,
confirmText: "Flash anyway",
cancelText: "Cancel"
}).then((e)=>{
xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function () {
if (this.readyState == 4) {
_("status_" + type_suffix).innerHTML = "";
_("progressBar_" + type_suffix).value = 0;
if (this.status == 200) {
var data = JSON.parse(this.responseText);
cuteAlert({
type: "info",
title: "Force Update",
message: data.msg
});
}
else {
cuteAlert({
type: "error",
title: "Force Update",
message: "An error occurred trying to force the update"
});
}
}
};
xmlhttp.open("POST", "/forceupdate", true);
var data = new FormData();
data.append("action", e);
xmlhttp.send(data);
});
} else {
cuteAlert({
type: 'error',
title: "Update Failed",
message: data.msg
});
}
if (!!window.EventSource) {
var source = new EventSource('/events');
function progress(mode) { return (e) => {
var loaded = e.data;
var total = uploading.size;
var percent = Math.round((loaded / total) * 100);
_("progressBar_" + uploading.type).value = percent;
_("status_" + uploading.type).innerHTML = percent + "% (" + loaded + "/" + total + ") " + mode + "... please wait";
};
}
source.addEventListener('upload', progress("uploaded"), false);
source.addEventListener('flash', progress("flashed"), false);
source.addEventListener('complete', uploaded, false);
}

function errorHandler(type_suffix) {
Expand Down
4 changes: 3 additions & 1 deletion lib/STM32UPDATE/stk500.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ void serial_empty_rx(void)
Serial.read();
}

const __FlashStringHelper *stk500_write_file(const char *filename)
const __FlashStringHelper *stk500_write_file(const char *filename, std::function<void(size_t)> progress)
{
uint8_t sync_iter;
if (!SPIFFS.exists(filename)) {
Expand Down Expand Up @@ -84,6 +84,7 @@ const __FlashStringHelper *stk500_write_file(const char *filename)
fp.close();
return F("write failed.");
}
progress(fp.position());
}
fp.close();

Expand Down Expand Up @@ -158,6 +159,7 @@ int wait_data_timeout(int const count, uint32_t timeout)
uint32_t const start = millis();
while ((millis() - start) < timeout) {
ESP.wdtFeed();
delay(1);
if (count <= Serial.available()) {
return 0;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/STM32UPDATE/stk500.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
#define STK_READ_FUSE_EXT 0x77 // 'w'
#define STK_READ_OSCCAL_EXT 0x78 // 'x'

const __FlashStringHelper *stk500_write_file(const char *filename);
const __FlashStringHelper *stk500_write_file(const char *filename, std::function<void(size_t)> progress);
int prog_mode_exit(void);

#endif
41 changes: 10 additions & 31 deletions lib/STM32UPDATE/stm32Updater.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ void stm32_restart()
cmd_go(FLASH_START);
}

const __FlashStringHelper *esp8266_spiffs_write_file(const char *filename, uint32_t begin_addr)
const __FlashStringHelper *esp8266_spiffs_write_file(const char *filename, uint32_t begin_addr, std::function<void(size_t)> progress)
{
if (!SPIFFS.exists(filename))
{
Expand Down Expand Up @@ -389,58 +389,37 @@ const __FlashStringHelper *esp8266_spiffs_write_file(const char *filename, uint3
DBGLN("erase Success!");

DBGLN("begin to write file.");
uint32_t junks = (filesize + (BLOCK_SIZE - 1)) / BLOCK_SIZE;
uint32_t junk = 0;
uint8_t proc;
while (fp.position() < filesize)
{
while (fp.position() < filesize) {
uint8_t nread = BLOCK_SIZE;
if ((filesize - fp.position()) < BLOCK_SIZE)
{
if ((filesize - fp.position()) < BLOCK_SIZE) {
nread = filesize - fp.position();
}
nread = fp.readBytes((char *)memory_buffer, nread);

//DBGLN("write bytes: %d, file position: %d", nread, fp.position());
proc = (++junk * 100) / junks;
if ((junk % 40) == 0 || junk >= junks)
DBGLN("Write %u%%", proc);

uint8_t result = cmd_write_memory(begin_addr + fp.position() - nread, nread);
if (result != 1)
{
if (result != 1) {
fp.close();
return F("[ERROR] file write failed.");
}
progress(fp.position());
}
DBGLN("file write succeeded.");

DBGLN("begin to verify file.");
fp.seek(0, SeekSet);
junk = 0;
while (fp.position() < filesize)
{
while (fp.position() < filesize) {
uint8_t nread = BLOCK_SIZE;
if ((filesize - fp.position()) < BLOCK_SIZE)
{
if ((filesize - fp.position()) < BLOCK_SIZE) {
nread = filesize - fp.position();
}
nread = fp.readBytes((char *)file_buffer, nread);

//DBGLN("read bytes: %d, file position: %d", nread, fp.position());
proc = (++junk * 100) / junks;
if ((junk % 40) == 0 || junk >= junks)
DBGLN("Verify %u%%", proc);

uint8_t result = cmd_read_memory(begin_addr + fp.position() - nread, nread);
if (result != 1)
{
if (result != 1) {
fp.close();
DBGLN("[ERROR] read memory failed: %d", fp.position());
return F("[ERROR] read memory failed");
}
result = memcmp(file_buffer, memory_buffer, nread);
if (result != 0)
{
if (result != 0) {
fp.close();
return F("[ERROR] verify failed.");
}
Expand Down
2 changes: 1 addition & 1 deletion lib/STM32UPDATE/stm32Updater.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ void debug_log();

void stm32_restart();

const __FlashStringHelper *esp8266_spiffs_write_file(const char *filename, uint32_t const begin_addr);
const __FlashStringHelper *esp8266_spiffs_write_file(const char *filename, uint32_t const begin_addr, std::function<void(size_t)> progress);
Loading

0 comments on commit 2205f42

Please sign in to comment.