Skip to content

Commit

Permalink
More http cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
md8n committed Jan 24, 2025
1 parent ca336a8 commit e6d3322
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 69 deletions.
10 changes: 6 additions & 4 deletions www/js/SPIFFSdlg.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//import - get_icon_svg, conErr, stdErrMsg, displayBlock, displayNone, id, setValue, setHTML, closeModal, setactiveModal, showModal, alertdlg, confirmdlg, inputdlg, SendFileHttp, SendGetHttp, translate_text_item

let SPIFFS_currentpath = "/";
let SPIFFS_currentfile = "";
let SPIFFS_upload_ongoing = false;

Expand Down Expand Up @@ -93,6 +94,7 @@ function processSPIFFS_Createdir(answer) {
}

function SPIFFSDownload(url) {
console.info(`Preparing to download ${url}`);
const anchor = document.createElement("a");
anchor.href = url;
anchor.download = url;
Expand Down Expand Up @@ -204,7 +206,7 @@ const buildSPIFFSTotalBar = (jsonresponse) => {
};

const upDirAndRelist = (previouspath) => {
SPIFFS_currentpath(previouspath);
SPIFFS_currentpath = previouspath;
SPIFFSSendCommand("list", "all");
};

Expand All @@ -231,9 +233,9 @@ function SPIFFSdispatchfilestatus(jsonresponse) {
const filesize = jsonresponse.files[i].size;
const pathname = jsonresponse.path;
const filename = jsonresponse.files[i].name;
let filecontent = `<td style='vertical-align:middle; color:#5BC0DE'>${get_icon_svg("file")}</td>`;
// filecontent += "<td width='100%' style='vertical-align:middle'><a href=\"" + pathname + filename + "\" target=_blank download><button class=\"btn btn-link no_overflow\">" + filename + "</button></a></td>"
filecontent += `<td width='100%' style='vertical-align:middle'>${filename}</td>`;
let filecontent = `<td style='vertical-align:middle; color:#5BC0DE'>${get_icon_svg("file")}</td>`;
// filecontent += "<td width='100%' style='vertical-align:middle'><a href=\"" + pathname + filename + "\" target=_blank download><button class=\"btn btn-link no_overflow\">" + filename + "</button></a></td>"
filecontent += `<td width='100%' style='vertical-align:middle'>${filename}</td>`;
filecontent += `<td nowrap style='vertical-align:middle; text-align:right'>${filesize}</td>`;
filecontent += SPIFFSbutton(`${bIdF}download_${i}`, "btn-default", "download");
filecontent += SPIFFSbutton(`${bIdF}delete_${i}`, "btn-danger", "trash");
Expand Down
86 changes: 39 additions & 47 deletions www/js/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,35 @@ var xmlhttpupload;

var max_cmd = 100;

/** Clear all current commands, used by socket.js */
const clear_cmd_list = () => {
http_cmd_list.length = 0;
processing_cmd = false;
};

const cleanFunc = (command, funcName, defFn) => {
return typeof command !== "undefined" && typeof command[funcName] === "function"
? command[funcName]
: http_cmd_list.length > 0 && typeof http_cmd_list[0][funcName] === "function"
? http_cmd_list[0][funcName]
: defFn;
/** Do the final steps to 'complete' a command */
const complete_cmd = () => {
http_cmd_list.shift();
processing_cmd = false;
// This should already be false, but we'll make sure
http_communication_locked = false;
process_cmd();
}

/** Add a cmd to the http_cmd_list and trigger processing */
const add_cmd = (command) => {
http_cmd_list.push(command);
process_cmd();
}

/** A default success function */
const http_resultfn = (response_text) => {
console.info(`Success: ${response_text}`);
}

const http_handleSuccess = (command, response_text) => {
http_communication_locked = false;

const resultfn = cleanFunc(command, "resultfn", http_resultfn);

resultfn(response_text);

http_cmd_list.shift();
processing_cmd = false;
process_cmd();
command.resultfn(response_text);
complete_cmd();
}

const authErrorFound = (error_code, response_text) => {
Expand All @@ -50,18 +51,13 @@ const http_errorfn = (error_code, response_text) => {
}

function http_handleError(command, error_code, response_text) {
http_communication_locked = false;

if (authErrorFound(error_code, response_text)) {
// For now with an auth_error, we continue with regular error handling
} else {
const errorfn = cleanFunc(command, "errorfn", http_errorfn);
errorfn(error_code, response_text);
command.errorfn(error_code, response_text);
}

http_cmd_list.shift();
processing_cmd = false;
process_cmd();
complete_cmd();
}

/** A default progress event handler function */
Expand Down Expand Up @@ -92,9 +88,7 @@ function process_cmd() {
// Note: NOT an actual http command, just something else to be done
const fn = command.fn;
fn();
http_cmd_list.shift();
processing_cmd = false;
process_cmd();
complete_cmd();
} else {
ProcessHttpCommand(command);
}
Expand Down Expand Up @@ -155,7 +149,7 @@ const checkForMaxListSize = (desc) => {
http_errorfn(999, translate_text_item("Server not responding"));
console.error(`${desc} could not be added to the http_cmd_list. Maximum pending commands length has been exceeded.`);

console.info("Will attempt to continue processes commands");
console.info("Will attempt to continue processing commands");
process_cmd();

return false;
Expand All @@ -164,13 +158,12 @@ const checkForMaxListSize = (desc) => {
/** Add some arbitrary command to the http_cmd_list.
* Note: This is assumed to NOT be an actual HTTP command
*/
const AddCmd = (fn, id) => {
const SendCmdCmd = (fn, id) => {
if (!checkForMaxListSize("An arbitrary function")) {
return;
}

http_cmd_list.push(buildCmdCmd(fn, id));
process_cmd();
add_cmd(buildCmdCmd(fn, id));
}

function GetIdentificationStatus() {
Expand Down Expand Up @@ -226,42 +219,41 @@ function SendGetHttp(cmd, result_fn, error_fn, id, max_id) {
}
}

http_cmd_list.push(buildGetCmd(cmd, cmd_id, result_fn, error_fn));
process_cmd();
add_cmd(buildGetCmd(cmd, cmd_id, result_fn, error_fn));
}

function SendFileHttp(cmd, postdata, result_fn, error_fn, progress_fn) {
if (!checkForMaxListSize(`The command '${cmd}'`)) {
return;
}

if (http_cmd_list.length !== 0) {
if (http_cmd_list.length) {
// TODO: figure out what, if anything this did
// biome-ignore lint/suspicious/noGlobalAssign: <explanation>
process = false;
}
http_cmd_list.push(buildPostFileCmd(cmd, postdata, result_fn, error_fn, progress_fn));
process_cmd();
add_cmd(buildPostFileCmd(cmd, postdata, result_fn, error_fn, progress_fn));
}


const ProcessHttpCommand = (command) => {
if (http_communication_locked) {
const cmdType = command.type;

if (!["GET", "POST"].includes(cmdType)) {
// This is a safety check - if we're here then there's been a programmer error
return;
}

if (http_communication_locked && cmdType !== "CMD") {
http_errorfn(503, translate_text_item("Communication locked!"));
console.warn("locked");
return;
}

const req = { method: command.type };
if (req.method === "POST") {
// Note: Only used for uploading files
req.body = command.postdata;
}

var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4) {
if (xmlhttp.status == 200) {
http_communication_locked = false;
if (xmlhttp.readyState == XMLHttpRequest.DONE) {
if (xmlhttp.status === 0 || (xmlhttp.status >= 200 && xmlhttp.status < 400)) {
//console.log("*** " + command.cmd + " done");
http_handleSuccess(command, xmlhttp.responseText);
} else {
Expand All @@ -272,14 +264,14 @@ const ProcessHttpCommand = (command) => {
}
}
}
if (command.type === "POST") {
if (cmdType === "POST") {
xmlhttp.upload.addEventListener("progress", command.progressfn, false);
}

http_communication_locked = true;
//console.log("GET:" + cmd);
xmlhttp.open(command.type, command.cmd, true);
if (command.type === "GET") {
xmlhttp.open(cmdType, command.cmd, true);
if (cmdType === "GET") {
xmlhttp.send();
} else {
// Note: Only used for uploading files
Expand Down
16 changes: 8 additions & 8 deletions www/js/initUI.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ const initUI = () => {
// Start up connect dialog, don't try and get the FW data
connectdlg(false);

AddCmd(display_boot_progress);
SendCmdCmd(display_boot_progress);
//check FW
update_UI_firmware_target();
//set title using hostname
Expand All @@ -143,7 +143,7 @@ const initUI = () => {
/** InitUI step2 - get settings from ESP3D and start processing them */
function initUI_2() {
console.log("Init UI - Step 2 - Get Settings");
AddCmd(display_boot_progress);
SendCmdCmd(display_boot_progress);
//query settings but do not update list in case wizard is showed
refreshSettings(true);
initUI_3();
Expand All @@ -152,7 +152,7 @@ function initUI_2() {
/** InitUI step3 - Initialise the control and GRBL panels, get the preferences */
function initUI_3() {
console.log("Init UI - Step 3 - Initialise the control and GRBL panels, get the preferences");
AddCmd(display_boot_progress);
SendCmdCmd(display_boot_progress);
//init panels
init_controls_panel();
init_grbl_panel();
Expand All @@ -163,23 +163,23 @@ function initUI_3() {
/** InitUI step4 - Initialise the command and files panels, determine if the setup wizard needs to be run */
function initUI_4() {
console.log("Init UI - Step 4 - Initialise the command and files panels, determine if the setup wizard needs to be run");
AddCmd(display_boot_progress);
SendCmdCmd(display_boot_progress);
init_command_panel();
init_files_panel(false);
//check if we need setup
if (target_firmware === "???") {
console.log("Launch Setup");
AddCmd(display_boot_progress);
SendCmdCmd(display_boot_progress);
closeModal("Connection successful");
setupdlg();
} else {
//wizard is done UI can be updated
setup_is_done = true;
do_not_build_settings = false;
AddCmd(display_boot_progress);
SendCmdCmd(display_boot_progress);
build_HTML_setting_list(current_setting_filter);
AddCmd(closeModal);
AddCmd(show_main_UI);
SendCmdCmd(closeModal);
SendCmdCmd(show_main_UI);
}
}

Expand Down
24 changes: 14 additions & 10 deletions www/js/tablet.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,9 @@ const move = (params = {}) => {
const sendMove = (cmd) => {
tabletClick();

let distance = cmd.includes('Z') ? Number(getText('disZ')) || 0 : Number(getText('disM')) || 0;
let distance = cmd.includes('Z')
? Number(getText('disZ')) || 0
: Number(getText('disM')) || 0;

const fn = {
G28: () => sendCommand('G28'),
Expand Down Expand Up @@ -467,24 +469,26 @@ var oldCannotClick = null

function scaleUnits(target) {
//Scale the units to move when jogging down or up by 25.4 to keep them reasonable
let disMElement = id(target);
let currentValue = Number(disMElement.innerText);
let distanceElement = id(target);
let currentValue = Number(distanceElement.innerText);

if (!Number.isNaN(currentValue)) {
disMElement.innerText = modal.units == 'G20' ? currentValue / 25.4 : currentValue * 25.4;
distanceElement.innerText = modal.units == 'G20' ? currentValue / 25.4 : currentValue * 25.4;
} else {
console.error('Invalid number in disM element');
}
}

function tabletUpdateModal() {
const newUnits = modal.units === 'G21' ? 'mm' : 'Inch'
if (getValue('units') !== newUnits) {
setText('units', newUnits)
setJogSelector(modal.units)
scaleUnits("disM")
scaleUnits("disZ")
const newUnits = modal.units === "G21" ? "mm" : "Inch";
if (getValue("tablettab_toggle_units") === newUnits) {
return;
}

setText("tablettab_toggle_units", newUnits);
setJogSelector(modal.units);
scaleUnits("disM");
scaleUnits("disZ");
}

function tabletGrblState(grbl, response) {
Expand Down

0 comments on commit e6d3322

Please sign in to comment.