Skip to content

Commit 0016d81

Browse files
committed
Ask user to specify export folder for flight log charts and CSVs
1 parent e2ecf57 commit 0016d81

File tree

2 files changed

+55
-24
lines changed

2 files changed

+55
-24
lines changed

src/modules/flightlog.js

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,71 @@
11
import fs from "fs";
2+
import path from "path";
3+
import { dialog } from "electron";
24

35
export function exportFlightLogToCSVs(flightLog) {
4-
const flightLogDir = "flight-log-export"
56
let flightLogSections = ["imu", "baro", "flightInfo", "orientationInfo", "filteredDataInfo", "gnssInfo", "flightStates", "eventInfo", "voltageInfo"];
67

7-
if (!fs.existsSync(flightLogDir)) {
8-
fs.mkdirSync(flightLogDir);
8+
let paths = dialog.showOpenDialogSync({ properties: ["openDirectory"] });
9+
10+
if (!paths) {
11+
console.log("No directory selected for export.");
12+
// TODO: after snackbar is merged print error message to user
13+
return;
14+
}
15+
16+
const exportFolderPath = path.join(paths[0]);
17+
18+
if (!fs.existsSync(exportFolderPath)) {
19+
fs.mkdirSync(exportFolderPath);
920
}
1021

1122
for (let flightLogSection of flightLogSections) {
12-
fs.writeFile(`${flightLogDir}/${flightLogSection}.csv`, objectArrayToCSV(flightLogSection, flightLog[flightLogSection]), "utf8", function (err) {
23+
fs.writeFile(`${exportFolderPath}/${flightLogSection}.csv`, objectArrayToCSV(flightLogSection, flightLog[flightLogSection]), "utf8", function (err) {
1324
if (err) {
1425
console.log("An error occurred while writing CSV object to file.");
26+
// TODO: after snackbar is merged print error message to user
1527
return console.log(err);
1628
}
17-
console.log("CSV file has been saved.");
1829
});
1930
}
2031
}
2132

33+
export function exportFlightLogChartsToHTML(flightLogChartsHTML) {
34+
let paths = dialog.showOpenDialogSync({ properties: ["openDirectory"] });
35+
36+
if (!paths) {
37+
console.log("No directory selected for export.");
38+
return;
39+
// TODO: after snackbar is merged print error message to user
40+
}
41+
42+
const exportFolderPath = path.join(paths[0]);
43+
44+
if (!fs.existsSync(exportFolderPath)) {
45+
fs.mkdirSync(exportFolderPath);
46+
}
47+
48+
const flightLogHtmlDocument = `
49+
<!DOCTYPE html>
50+
<html>
51+
<head>
52+
<script src="https://cdn.plot.ly/plotly-2.18.2.min.js"></script>
53+
</head>
54+
<body>
55+
${flightLogChartsHTML}
56+
</body>
57+
</html>
58+
`;
59+
60+
fs.writeFile(`${exportFolderPath}/plots.html`, flightLogHtmlDocument, 'utf8', function (err) {
61+
if (err) {
62+
console.log("An error occurred while writing HTML Object to File.");
63+
return console.log(err);
64+
// TODO: after snackbar is merged print error message to user
65+
}
66+
});
67+
}
68+
2269
export function exportJSON(flightLog) {
2370
fs.writeFile("flightlog.json", JSON.stringify(flightLog), "utf8", function (err) {
2471
if (err) {

src/modules/ipc.js

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { ipcMain, dialog } from "electron";
22
import fs from "fs";
33
import { parseFlightLog } from "./logparser.js"
4-
import { exportFlightLogToCSVs } from "./flightlog.js"
4+
import { exportFlightLogToCSVs, exportFlightLogChartsToHTML } from "./flightlog.js"
55
import { connect, disconnect, command, cliCommand, getList } from "./serial.js";
66

77
export function subscribeListeners() {
@@ -74,24 +74,8 @@ export function subscribeListeners() {
7474
event.sender.send("EXPORT_FLIGHTLOG_CSVS");
7575
});
7676

77-
ipcMain.on("EXPORT_FLIGHTLOG_HTML", (event, flightLogHtmlStr) => {
78-
79-
flightLogHtmlStr = `
80-
<!DOCTYPE html>
81-
<html>
82-
<head>
83-
<script src="https://cdn.plot.ly/plotly-2.18.2.min.js"></script>
84-
</head>
85-
<body>
86-
` + flightLogHtmlStr + "</body></html>"
87-
fs.writeFile("plots.html", flightLogHtmlStr, 'utf8', function (err) {
88-
if (err) {
89-
console.log("An error occurred while writing CSV Object to File.");
90-
return console.log(err);
91-
}
92-
console.log("HTML file has been saved.");
93-
});
94-
77+
ipcMain.on("EXPORT_FLIGHTLOG_HTML", (event, flightLogChartsHTML) => {
78+
exportFlightLogChartsToHTML(flightLogChartsHTML);
9579
event.sender.send("EXPORT_FLIGHTLOG_HTML");
9680
});
9781

0 commit comments

Comments
 (0)