File manager

Dependencies:   SDFileSystem

Dependents:   RwSDCard_Xml_GPS

Revision:
1:1f1f2b99756b
Parent:
0:a27e0d3581d1
Child:
2:18e004a47f52
--- a/FileManager.cpp	Tue May 10 06:35:01 2016 +0000
+++ b/FileManager.cpp	Tue May 10 09:22:27 2016 +0000
@@ -1,14 +1,15 @@
+#include "mbed.h"
 #include "FileManager.h"
 #include "SDFileSystem.h"
 
 char m_GpsInterval[XMLTEXT_SIZE];
 char m_DataInterval[XMLTEXT_SIZE];
 Variable_Data_TypeDef m_varList[MAX_VAR];
-unsigned int m_amountVarList;
+unsigned int m_amountVarList = 0;
 
 static void removeSpaces(char* s , int size);
 static void getXmlText(char *str, char *ret);
-
+static void generateFileNameWithTime(time_t timestamp, char * file_name);
 
 static void removeSpaces(char* s , int size)
 {
@@ -47,6 +48,22 @@
     }
     removeSpaces(ret_addr, XMLTEXT_SIZE);
 }
+static void generateFileNameWithTime(time_t timestamp, char * file_name)
+{
+    char str[5];
+    struct tm * ptm;
+    
+    ptm = localtime ( &timestamp );  
+    sprintf(str,"%04d", ptm->tm_year-100 + 2000);
+    memcpy(&file_name[4], str, 4);
+    
+    sprintf(str,"%02d", ptm->tm_mon+1);
+    memcpy(&file_name[9], str, 2);
+    
+    sprintf(str,"%02d", ptm->tm_mday);
+    memcpy(&file_name[12], str, 2); 
+}
+
 void readSetupFile()
 {
     // now open file for reading
@@ -80,16 +97,73 @@
         fclose(fp);  // ensure you close the file after reading
     }
 }
-void logGPSData(char date[], char time[])
+void logGPSData(time_t timestamp ,char lat[], char longti[])
 {
-    FILE *fp  = fopen(GPS_LOG_FILE_NAME, "a");
+    char file_name[] = GPS_LOG_FILE_NAME;
+    
+    generateFileNameWithTime(timestamp,file_name);
+    
+    FILE *fp  = fopen(file_name, "a");
+
+    if (fp == NULL) {  // if it can't open the file then print error message
+        printf("Error! Unable to open file %s!\n",file_name);
+    } else {  // opened file so can write
+        printf("\r\n Writing to Gps Log File (%s)....",file_name);
+
+        fprintf(fp, "%d,%s,%s\n",timestamp,lat,longti);  // print formatted string to file (CSV)
+
+        printf("Done");
+        fclose(fp);  // ensure you close the file after writing
+    }
+}
+void logRMSData(time_t timestamp ,float * var, int size)
+{
+    char file_name[] = RTL_LOG_FILE_NAME;
+    
+    generateFileNameWithTime(timestamp,file_name);
+    if (!is_file_exist(file_name))
+    {
+        logRMSHeader(timestamp);
+    }   
+    FILE *fp  = fopen(file_name, "a");
 
     if (fp == NULL) {  // if it can't open the file then print error message
-        printf("Error! Unable to open file!\n");
+        printf("Error! Unable to open file %s!\n",file_name);
     } else {  // opened file so can write
-        printf("\r\n Writing to Gps Log File....");
+        printf("\r\n Writing to Log File (%s)....",file_name);
+
+        fprintf(fp, "%d",timestamp);  // print Timestamp
+
+        for(int i = 0; i < size; i++)
+        {
+            fprintf(fp, ",%f",var[i]);  // print variable list
+        }
+        fprintf(fp, "\n");  // print new line
 
-        fprintf(fp, "%s,%s\n",date,time);  // print formatted string to file (CSV)
+        printf("Done");
+        fclose(fp);  // ensure you close the file after writing
+    }
+}
+void logRMSHeader(time_t timestamp)
+{
+    char file_name[] = RTL_LOG_FILE_NAME;
+    
+    generateFileNameWithTime(timestamp,file_name);
+    
+
+    FILE *fp  = fopen(file_name, "a");
+
+    if (fp == NULL) {  // if it can't open the file then print error message
+        printf("Error! Unable to open file %s!\n",file_name);
+    } else {  // opened file so can write
+        printf("\r\n Writing to Log File (%s)....",file_name);
+
+        fprintf(fp, "%s",RMS_HEADER_TIME);  // print variable list
+        for(int i = 0; i < m_amountVarList; i++)
+        {
+            fprintf(fp, ",%s",m_varList[i].varName);  // print variable list
+        }
+        fprintf(fp, "\n");  // print new line
 
         printf("Done");
         fclose(fp);  // ensure you close the file after writing
@@ -99,11 +173,10 @@
 {
     FILE *fp = fopen(MINIRMS_LOG_FILE_NAME, "a");
 
-
     if (fp == NULL) {  // if it can't open the file then print error message
         printf("Error! Unable to open file!\n");
     } else {  // opened file so can write
-        fprintf(fp, "Start Mini-RMS System with Gps Interval = %f",gps_interval); // ensure data type matches
+        fprintf(fp, "\nStart Mini-RMS System with Gps Interval = %f",gps_interval); // ensure data type matches
         fclose(fp);  // ensure you close the file after writing
     }
 }
@@ -118,6 +191,17 @@
     }
     // if we can't open it, it doesn't exist and so we can't delete it
 }
+bool is_file_exist(char filename[])
+{
+    bool exist = false;
+    FILE *fp = fopen(filename, "r");  // try and open file
+    if (fp != NULL) {  // if it does open...
+        fclose(fp);    // close it
+        exist = true;
+    }
+    
+    return exist;
+}
 int GPSInterval()
 {
     //Return whether or not CRC is enabled