Skip to content

Commit 7851644

Browse files
committed
Change flight log export filenames
1 parent 0016d81 commit 7851644

File tree

4 files changed

+39
-9
lines changed

4 files changed

+39
-9
lines changed

src/modules/flightlog.js

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import fs from "fs";
22
import path from "path";
33
import { dialog } from "electron";
4+
import { formatDateTime } from "@/utils/date.js";
5+
import { flightLogFilename } from "./ipc.js";
46

57
export function exportFlightLogToCSVs(flightLog) {
68
let flightLogSections = ["imu", "baro", "flightInfo", "orientationInfo", "filteredDataInfo", "gnssInfo", "flightStates", "eventInfo", "voltageInfo"];
@@ -13,11 +15,10 @@ export function exportFlightLogToCSVs(flightLog) {
1315
return;
1416
}
1517

16-
const exportFolderPath = path.join(paths[0]);
18+
const userFolderPath = path.join(paths[0]);
19+
const exportFolderPath = `${userFolderPath}/${flightLogFilename}_export_${formatDateTime(new Date())}`;
1720

18-
if (!fs.existsSync(exportFolderPath)) {
19-
fs.mkdirSync(exportFolderPath);
20-
}
21+
fs.mkdirSync(`${exportFolderPath}`);
2122

2223
for (let flightLogSection of flightLogSections) {
2324
fs.writeFile(`${exportFolderPath}/${flightLogSection}.csv`, objectArrayToCSV(flightLogSection, flightLog[flightLogSection]), "utf8", function (err) {
@@ -41,10 +42,6 @@ export function exportFlightLogChartsToHTML(flightLogChartsHTML) {
4142

4243
const exportFolderPath = path.join(paths[0]);
4344

44-
if (!fs.existsSync(exportFolderPath)) {
45-
fs.mkdirSync(exportFolderPath);
46-
}
47-
4845
const flightLogHtmlDocument = `
4946
<!DOCTYPE html>
5047
<html>
@@ -57,7 +54,7 @@ export function exportFlightLogChartsToHTML(flightLogChartsHTML) {
5754
</html>
5855
`;
5956

60-
fs.writeFile(`${exportFolderPath}/plots.html`, flightLogHtmlDocument, 'utf8', function (err) {
57+
fs.writeFile(`${exportFolderPath}/${flightLogFilename}_plots_${formatDateTime(new Date())}.html`, flightLogHtmlDocument, 'utf8', function (err) {
6158
if (err) {
6259
console.log("An error occurred while writing HTML Object to File.");
6360
return console.log(err);

src/modules/ipc.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ import fs from "fs";
33
import { parseFlightLog } from "./logparser.js"
44
import { exportFlightLogToCSVs, exportFlightLogChartsToHTML } from "./flightlog.js"
55
import { connect, disconnect, command, cliCommand, getList } from "./serial.js";
6+
import { getFilename } from "../utils/file.js";
7+
8+
export let flightLogFilename = "";
69

710
export function subscribeListeners() {
811
ipcMain.on("BOARD:CONFIG", async (event, key) => {
@@ -59,6 +62,8 @@ export function subscribeListeners() {
5962
return
6063
}
6164

65+
flightLogFilename = getFilename(file.slice(0, -4)); // Remove the .cfl extension
66+
6267
let data = fs.readFileSync(file, { encoding: "binary" });
6368
if (!data || !data.length) {
6469
event.sender.send("LOAD_FLIGHTLOG", { error: "File is empty" });

src/utils/date.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
export function formatDateTime(date) {
2+
if (!(date instanceof Date)) {
3+
console.error("Invalid date object provided.");
4+
return "";
5+
}
6+
7+
const options = { year: "numeric", month: "2-digit", day: "2-digit" };
8+
const formattedDate = date
9+
.toLocaleDateString("en-US", options)
10+
.replace(/\//g, "");
11+
12+
const formattedTime = date.toLocaleTimeString("en-UK").replace(/:/g, "");
13+
14+
return `${formattedDate}_${formattedTime}`;
15+
}

src/utils/file.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export function getFilename(filePath) {
2+
// Use a regex to split by either / or \ (to handle both Unix and Windows paths)
3+
// Then pop the last element
4+
const parts = filePath.split(/[\\/]/);
5+
const filename = parts.pop();
6+
7+
// Handle cases where the path might end with a slash, resulting in an empty string
8+
if (filename === '' && parts.length > 0) {
9+
return parts.pop(); // Get the name of the directory itself
10+
}
11+
12+
return filename;
13+
}

0 commit comments

Comments
 (0)