@@ -4,12 +4,12 @@ export function exportFlightLogToCSVs(flightLog) {
44 const flightLogDir = "flight-log-export"
55 let flightLogSections = [ "imu" , "baro" , "flightInfo" , "orientationInfo" , "filteredDataInfo" , "gnssInfo" , "flightStates" , "eventInfo" , "voltageInfo" ] ;
66
7- if ( ! fs . existsSync ( flightLogDir ) ) {
7+ if ( ! fs . existsSync ( flightLogDir ) ) {
88 fs . mkdirSync ( flightLogDir ) ;
99 }
1010
1111 for ( let flightLogSection of flightLogSections ) {
12- fs . writeFile ( `${ flightLogDir } /${ flightLogSection } .csv` , objectArrayToCSV ( flightLog [ flightLogSection ] ) , "utf8" , function ( err ) {
12+ fs . writeFile ( `${ flightLogDir } /${ flightLogSection } .csv` , objectArrayToCSV ( flightLogSection , flightLog [ flightLogSection ] ) , "utf8" , function ( err ) {
1313 if ( err ) {
1414 console . log ( "An error occurred while writing CSV object to file." ) ;
1515 return console . log ( err ) ;
@@ -29,20 +29,26 @@ export function exportJSON(flightLog) {
2929 } ) ;
3030}
3131
32- function objectArrayToCSV ( arr , separator = "," ) {
32+ function objectArrayToCSV ( section , arr , separator = "," ) {
3333 if ( ! Array . isArray ( arr ) ) {
3434 console . log ( "objectArrayToCSV first argument must be an array." ) ;
3535 return ;
3636 }
3737
38- let CSVColumnNames = getCSVColumnNames ( arr [ 0 ] ) ;
38+ if ( arr . length > 0 ) {
39+ console . log ( "Section " + section + " :" + arr [ 0 ] )
40+ let CSVColumnNames = getCSVColumnNames ( arr [ 0 ] ) ;
3941
40- let CSVHeader = CSVColumnNames . join ( separator ) ;
41- let CSVBody = arr . map ( obj =>
42- CSVColumnNames . map ( header => getObjectValue ( obj , header ) ) . join ( separator )
43- ) . join ( "\n" ) ;
42+ let CSVHeader = CSVColumnNames . join ( separator ) ;
43+ let CSVBody = arr . map ( obj =>
44+ CSVColumnNames . map ( header => getObjectValue ( obj , header ) ) . join ( separator )
45+ ) . join ( "\n" ) ;
4446
45- return CSVHeader + "\n" + CSVBody ;
47+ return CSVHeader + "\n" + CSVBody ;
48+ } else {
49+ console . log ( "Array length of " + section + " is 0!" ) ;
50+ return "No data recorded"
51+ }
4652}
4753
4854function getCSVColumnNames ( obj ) {
0 commit comments