diff --git a/app/utils/map.util.js b/app/utils/map.util.js index fa38383..022f298 100644 --- a/app/utils/map.util.js +++ b/app/utils/map.util.js @@ -1,6 +1,7 @@ const csv = require("fast-csv"); const fs = require("fs"); const path = require("path"); +const uuid = require("uuid"); const map_conf = require("../config/map.conf"); const logger = require("./logger.util"); @@ -24,7 +25,7 @@ async function addMap(filename, meta, throttle, motor) { const throttle_values = Object.values(throttle); const current_values = Object.values(motor["0"]); const brake_values = Object.values(motor["1"]); - + for (let i = 0; i <= map_conf.steps; i++) { map[i] = [current_values[i], brake_values[i], throttle_values[i]]; } @@ -45,6 +46,10 @@ async function updateMap(filename, meta, throttle, motor) { try { await csv.writeToString(map).then(data => total += data); fs.writeFileSync(`app/storage/${filename}`, total); + fs.renameSync(`app/storage/${filename}`, `app/storage/map_${uuid.v4()}.csv`) + if (fs.existsSync("app/storage/selected")) { + fs.unlinkSync("app/storage/selected"); + } } catch (error) { throw new Error(error); } @@ -151,14 +156,14 @@ async function getAvailableMaps() { 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)