From 05a8baed4ff0827fe80acb93e86766b8e8d985a6 Mon Sep 17 00:00:00 2001 From: Kwstas Drakontidis Date: Sat, 31 May 2025 10:10:18 +0200 Subject: [PATCH] Bug fixes --- app/utils/map.util.js | 39 +++++--- app/views/error.ejs | 5 +- src/javascript/add.js | 144 ++++++++++++++-------------- src/javascript/edit.js | 201 ++++++++++++++++++++-------------------- src/javascript/error.js | 57 +++++++++--- src/javascript/index.js | 4 +- 6 files changed, 246 insertions(+), 204 deletions(-) diff --git a/app/utils/map.util.js b/app/utils/map.util.js index 0e18140..fa38383 100644 --- a/app/utils/map.util.js +++ b/app/utils/map.util.js @@ -1,7 +1,8 @@ const csv = require("fast-csv"); const fs = require("fs"); const path = require("path"); -const { steps } = require("../config/map.conf"); +const map_conf = require("../config/map.conf"); +const logger = require("./logger.util"); async function addMap(filename, meta, throttle, motor) { var total = ""; @@ -24,7 +25,7 @@ async function addMap(filename, meta, throttle, motor) { const current_values = Object.values(motor["0"]); const brake_values = Object.values(motor["1"]); - for (let i = 0; i <= steps; i++) { + for (let i = 0; i <= map_conf.steps; i++) { map[i] = [current_values[i], brake_values[i], throttle_values[i]]; } } @@ -146,25 +147,33 @@ async function parseMapJson(filename, mode) { async function getAvailableMaps() { // path in app_conf - var files = fs.readdirSync("app/storage"); var available_maps = {}; - for (const file of files) { - if (path.extname(file) == ".csv") { - available_maps[file] = {}; - - try { - let meta = await parseMap(file, 1); - - if (meta && meta.length) { - for (const m of meta) { - available_maps[file][m.slice(0, m.indexOf(":")).toLowerCase()] = m.slice(m.indexOf(":") + 1) + try { + var files = fs.readdirSync("app/storage"); + + for (const file of files) { + if (path.extname(file) == ".csv") { + available_maps[file] = {}; + + try { + let meta = await parseMap(file, 1); + + if (meta && meta.length) { + for (const m of meta) { + available_maps[file][m.slice(0, m.indexOf(":")).toLowerCase()] = m.slice(m.indexOf(":") + 1) + } } + } catch (error) { + throw new Error(error); } - } catch (error) { - throw new Error(error); } } + } catch (error) { + if (error.code === "ENOENT") { + logger.info("No storage directory found. Creating directory."); + fs.mkdirSync("app/storage"); + } } return available_maps; diff --git a/app/views/error.ejs b/app/views/error.ejs index 69df11b..d3b27e2 100644 --- a/app/views/error.ejs +++ b/app/views/error.ejs @@ -21,7 +21,10 @@
- + + +
diff --git a/src/javascript/add.js b/src/javascript/add.js index 3464d91..d2d7071 100644 --- a/src/javascript/add.js +++ b/src/javascript/add.js @@ -9,6 +9,77 @@ const map_description = document.getElementById("map_description"); var changes = 0; +function setEventListeners(element, cb) { + element.onmouseenter = async function () { + this.style.backgroundColor = "blue"; + }; + element.onmouseleave = async function () { + this.style.backgroundColor = ""; + }; + element.onmousedown = async function () { + this.style.backgroundColor = "red"; + }; + element.ondblclick = async function () { + cb(); + this.style.backgroundColor = "blue"; + }; +} + +setEventListeners(commit, async () => { + if (changes) { + let req_body = {}; + + req_body["meta"] = {}; + req_body["throttle"] = {}; + req_body["motor"] = { 0: {}, 1: {} }; + + if (map_name.style.backgroundColor == "blue") { + req_body["meta"]["name"] = map_name.value; + } + if (map_id.style.backgroundColor == "blue") { + req_body["meta"]["id"] = map_id.value; + } + if (map_description.style.backgroundColor == "blue") { + req_body["meta"]["description"] = map_description.value; + } + + for (let i = 1; i < throttle_table.rows[1].cells.length; i++) { + let e = throttle_table.rows[1].cells[i].firstChild; + req_body["throttle"][i - 1] = (e.value ? e.value : e.placeholder); + } + + for (let i = 1; i < motor_table.rows[1].cells.length; i++) { + let e = motor_table.rows[1].cells[i].firstChild; + req_body["motor"][0][i - 1] = (e.value ? e.value : e.placeholder); + } + + for (let i = 1; i < motor_table.rows[2].cells.length; i++) { + let e = motor_table.rows[2].cells[i].firstChild; + req_body["motor"][1][i - 1] = (e.value ? e.value : e.placeholder); + } + + try { + const response = await fetch("/app/add", { + method: "POST", + body: JSON.stringify(req_body), + headers: { + "Content-Type": "application/json", + } + }); + if (!response.ok) { + throw new Error(`Response status: ${response.status}`); + } else { + window.location.href = "/app"; + } + } catch (error) { + alert(`Add unsuccessful. ${error.message}`); + console.error(error.message); + } + } else { + alert("Nothing to commit."); + } +}); + (function () { let data = []; @@ -305,76 +376,3 @@ var changes = 0; } } })(); - - - -function setEventListeners(element, cb) { - element.onmouseenter = async function () { - this.style.backgroundColor = "blue"; - }; - element.onmouseleave = async function () { - this.style.backgroundColor = ""; - }; - element.onmousedown = async function () { - this.style.backgroundColor = "red"; - }; - element.ondblclick = async function () { - cb(); - this.style.backgroundColor = "blue"; - }; -} - -setEventListeners(commit, async () => { - if (changes) { - let req_body = {}; - - req_body["meta"] = {}; - req_body["throttle"] = {}; - req_body["motor"] = { 0: {}, 1: {} }; - - if (map_name.style.backgroundColor == "blue") { - req_body["meta"]["name"] = map_name.value; - } - if (map_id.style.backgroundColor == "blue") { - req_body["meta"]["id"] = map_id.value; - } - if (map_description.style.backgroundColor == "blue") { - req_body["meta"]["description"] = map_description.value; - } - - for (let i = 1; i < throttle_table.rows[1].cells.length; i++) { - let e = throttle_table.rows[1].cells[i].firstChild; - req_body["throttle"][i - 1] = (e.value ? e.value : e.placeholder); - } - - for (let i = 1; i < motor_table.rows[1].cells.length; i++) { - let e = motor_table.rows[1].cells[i].firstChild; - req_body["motor"][0][i - 1] = (e.value ? e.value : e.placeholder); - } - - for (let i = 1; i < motor_table.rows[2].cells.length; i++) { - let e = motor_table.rows[2].cells[i].firstChild; - req_body["motor"][1][i - 1] = (e.value ? e.value : e.placeholder); - } - - try { - const response = await fetch("/app/add", { - method: "POST", - body: JSON.stringify(req_body), - headers: { - "Content-Type": "application/json", - } - }); - if (!response.ok) { - throw new Error(`Response status: ${response.status}`); - } else { - window.location.href = "/app"; - } - } catch (error) { - alert(`Add unsuccessful. ${error.message}`); - console.error(error.message); - } - } else { - alert("Nothing to commit."); - } -}); \ No newline at end of file diff --git a/src/javascript/edit.js b/src/javascript/edit.js index f433062..0436906 100644 --- a/src/javascript/edit.js +++ b/src/javascript/edit.js @@ -11,7 +11,107 @@ const map_description = document.getElementById("map_description"); var changes = 0; +function setEventListeners(element, cb) { + element.onmouseenter = async function () { + this.style.backgroundColor = "blue"; + }; + element.onmouseleave = async function () { + this.style.backgroundColor = ""; + }; + element.onmousedown = async function () { + this.style.backgroundColor = "red"; + }; + element.ondblclick = async function () { + cb(); + this.style.backgroundColor = "blue"; + }; +} + +setEventListeners(remove, async () => { + const map_filename = (new URLSearchParams(window.location.search)).get("element"); + + try { + const response = await fetch(`/app/remove?element=${map_filename}`, { + method: "DELETE" + }); + if (!response.ok) { + throw new Error(`Response status: ${response.status}`); + } else { + window.location.href = "/app"; + } + } catch (error) { + alert(`Remove unsuccessful. ${error.message}`); + console.error(error.message); + } +}); + +setEventListeners(commit, async () => { + if (changes) { + const map_filename = (new URLSearchParams(window.location.search)).get("element"); + let req_body = {}; + + req_body["meta"] = {}; + req_body["throttle"] = {}; + req_body["motor"] = { 0: {}, 1: {} }; + + if (map_name.style.backgroundColor == "blue") { + req_body["meta"]["name"] = map_name.value; + } + if (map_id.style.backgroundColor == "blue") { + req_body["meta"]["id"] = map_id.value; + } + if (map_description.style.backgroundColor == "blue") { + req_body["meta"]["description"] = map_description.value; + } + + for (let i = 1; i < throttle_table.rows[1].cells.length; i++) { + if (throttle_table.rows[1].cells[i].style.backgroundColor == "blue") { + req_body["throttle"][i - 1] = throttle_table.rows[1].cells[i].firstChild.value; + } + } + + for (let i = 1; i < motor_table.rows[1].cells.length; i++) { + if (motor_table.rows[1].cells[i].style.backgroundColor == "blue") { + req_body["motor"][0][i - 1] = motor_table.rows[1].cells[i].firstChild.value; + } + } + + for (let i = 1; i < motor_table.rows[2].cells.length; i++) { + if (motor_table.rows[2].cells[i].style.backgroundColor == "blue") { + req_body["motor"][1][i - 1] = motor_table.rows[2].cells[i].firstChild.value; + } + } + + try { + const response = await fetch(`/app/edit?element=${map_filename}`, { + method: "POST", + body: JSON.stringify(req_body), + headers: { + "Content-Type": "application/json", + } + }); + if (!response.ok) { + throw new Error(`Response status: ${response.status}`); + } else { + window.location.href = "/app"; + } + } catch (error) { + alert(`Edit unsuccessful. ${error.message}`); + console.error(error.message); + } + } else { + alert("Nothing to commit."); + } +}); + +setEventListeners(download, () => { + const map_filename = (new URLSearchParams(window.location.search)).get("element"); + + window.location.href = `/app/download?element=${map_filename}`; +}); + var parsed_map = JSON.parse(map); + (function () { let data = []; @@ -323,104 +423,3 @@ var parsed_map = JSON.parse(map); } } })(); - - - -function setEventListeners(element, cb) { - element.onmouseenter = async function () { - this.style.backgroundColor = "blue"; - }; - element.onmouseleave = async function () { - this.style.backgroundColor = ""; - }; - element.onmousedown = async function () { - this.style.backgroundColor = "red"; - }; - element.ondblclick = async function () { - cb(); - this.style.backgroundColor = "blue"; - }; -} - -setEventListeners(remove, async () => { - const map_filename = (new URLSearchParams(window.location.search)).get("element"); - - try { - const response = await fetch(`/app/remove?element=${map_filename}`, { - method: "DELETE" - }); - if (!response.ok) { - throw new Error(`Response status: ${response.status}`); - } else { - window.location.href = "/app"; - } - } catch (error) { - alert(`Remove unsuccessful. ${error.message}`); - console.error(error.message); - } -}); - -setEventListeners(commit, async () => { - if (changes) { - const map_filename = (new URLSearchParams(window.location.search)).get("element"); - let req_body = {}; - - req_body["meta"] = {}; - req_body["throttle"] = {}; - req_body["motor"] = { 0: {}, 1: {} }; - - if (map_name.style.backgroundColor == "blue") { - req_body["meta"]["name"] = map_name.value; - } - if (map_id.style.backgroundColor == "blue") { - req_body["meta"]["id"] = map_id.value; - } - if (map_description.style.backgroundColor == "blue") { - req_body["meta"]["description"] = map_description.value; - } - - for (let i = 1; i < throttle_table.rows[1].cells.length; i++) { - if (throttle_table.rows[1].cells[i].style.backgroundColor == "blue") { - req_body["throttle"][i - 1] = throttle_table.rows[1].cells[i].firstChild.value; - } - } - - for (let i = 1; i < motor_table.rows[1].cells.length; i++) { - if (motor_table.rows[1].cells[i].style.backgroundColor == "blue") { - req_body["motor"][0][i - 1] = motor_table.rows[1].cells[i].firstChild.value; - } - } - - for (let i = 1; i < motor_table.rows[2].cells.length; i++) { - if (motor_table.rows[2].cells[i].style.backgroundColor == "blue") { - req_body["motor"][1][i - 1] = motor_table.rows[2].cells[i].firstChild.value; - } - } - - try { - const response = await fetch(`/app/edit?element=${map_filename}`, { - method: "POST", - body: JSON.stringify(req_body), - headers: { - "Content-Type": "application/json", - } - }); - if (!response.ok) { - throw new Error(`Response status: ${response.status}`); - } else { - window.location.href = "/app"; - } - } catch (error) { - alert(`Edit unsuccessful. ${error.message}`); - console.error(error.message); - } - } else { - alert("Nothing to commit."); - } -}); - -setEventListeners(download, () => { - const map_filename = (new URLSearchParams(window.location.search)).get("element"); - - window.location.href = `/app/download?element=${map_filename}`; -}); \ No newline at end of file diff --git a/src/javascript/error.js b/src/javascript/error.js index 02345f4..95b39a1 100644 --- a/src/javascript/error.js +++ b/src/javascript/error.js @@ -1,15 +1,48 @@ +const upload = document.getElementById("upload"); const logout = document.getElementById("logout"); -logout.onmouseenter = async function () { - this.style.backgroundColor = "blue"; -}; -logout.onmouseleave = async function () { - this.style.backgroundColor = ""; -}; -logout.onmousedown = async function () { - this.style.backgroundColor = "red"; -}; -logout.ondblclick = async function () { - this.style.backgroundColor = "blue"; +function setEventListeners(element, cb) { + element.onmouseenter = async function () { + this.style.backgroundColor = "blue"; + }; + element.onmouseleave = async function () { + this.style.backgroundColor = ""; + }; + element.onmousedown = async function () { + this.style.backgroundColor = "red"; + }; + element.ondblclick = async function () { + this.style.backgroundColor = "blue"; + cb(); + }; +} + +setEventListeners(add, () => { + window.location.href = "/app/add"; +}); + +setEventListeners(upload, async () => { + const input = document.getElementById("file_input"); + const upload = (file) => { + const formData = new FormData(); + formData.append('file', file); + + fetch('/app/upload', { + method: 'POST', + body: formData + }).then((response) => { + if (!response.ok) { + alert(`Upload unsuccessful. ${response.status}`); + } else { + location.reload(); + } + }); + } + const onSelectFile = () => upload(input.files[0]); + input.addEventListener('change', onSelectFile, false); + input.click(); +}); + +setEventListeners(logout, () => { window.location.href = `${window.location.protocol}//logout@${window.location.host}/`; -}; \ No newline at end of file +}); \ No newline at end of file diff --git a/src/javascript/index.js b/src/javascript/index.js index 1739cd9..1c90c6b 100644 --- a/src/javascript/index.js +++ b/src/javascript/index.js @@ -3,8 +3,6 @@ const upload = document.getElementById("upload"); const logout = document.getElementById("logout"); const add = document.getElementById("add"); -const sql_rows = sql_table.rows; - function setEventListeners(element, cb) { element.onmouseenter = async function () { this.style.backgroundColor = "blue"; @@ -51,6 +49,8 @@ setEventListeners(logout, () => { window.location.href = `${window.location.protocol}//logout@${window.location.host}/`; }); +const sql_rows = sql_table.rows; + for (let i = 1; i < sql_rows.length; i++) { setEventListeners(sql_rows[i], () => { window.location.href = `/app/edit?element=${sql_rows[i].cells[0].innerText}`;