Augustine Kizito / Mbed 2 deprecated Solar_Powered_Smart_Camera

Dependencies:   Adafruit_GFX Adafruit_ST7735 INA219 MODSERIAL MbedJSONValue mbed-rtos mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers LogManager.h Source File

LogManager.h

00001 #ifndef LOGMANAGER_H
00002 #define LOGMANAGER_H
00003 
00004 #include "LogRecord.h"
00005 
00006 // stores
00007 struct LogInstructions{
00008     int parameter;
00009     int duration;    
00010 } logInstructions;  
00011 
00012 
00013 const static char *LOGDATA_LOC = "/local/log.txt";
00014 const static char *LOGDATA1_LOC = "/local/log6.txt";
00015 const static char *LOGDATA2_LOC = "/local/log2.txt";
00016 const static char *LOGDATA3_LOC = "/local/log5.txt";
00017 const static char *LOGDATA4_LOC = "/local/log4.txt";
00018 
00019 
00020 class LogManager
00021 {
00022 public:
00023     LogManager();
00024     void createNewLog(int parameter, int duration);
00025     int retrieveLog(LogRecord records[]);
00026     void deleteLog(LogRecord &);
00027 private:
00028     void createLogStructure();
00029     void updateLog();
00030     bool logFileExists();
00031     bool logAvailable(int &);
00032     void readData(MbedJSONValue &, const char *location);
00033     void writeData(MbedJSONValue &, const char *location);
00034 
00035     char currLog[5];    // stores the log id of the current log
00036     char logLocation[20];   // location of the current log
00037     LogRecord *record;      // is used a buffer to store record data
00038     Ticker timer1; // create ticker object
00039 
00040 };
00041 
00042 LogManager::LogManager()
00043 {
00044     // constructor
00045     myled = 1;
00046 
00047     // it first checks if the log file exists
00048     // and if not, creates the JSON data structure
00049     if(!logFileExists()) {
00050 
00051         createLogStructure();
00052     }
00053 
00054 }
00055 
00056 void LogManager::createNewLog(int parameter, int duration)
00057 {
00058     // loops through the data structure to find an available
00059     // spot in the log structure
00060     
00061 
00062 
00063     int id = 0;
00064     if(!logAvailable(id)) {
00065         /// do some work
00066         // for example show display message
00067         return;
00068     }
00069     
00070     
00071     
00072     // determine the location of the log
00073     switch(id) {
00074         case 1:
00075             strcpy(logLocation,LOGDATA1_LOC);
00076             break;
00077         case 2:
00078             strcpy(logLocation,LOGDATA2_LOC);
00079             break;
00080         case 3:
00081             strcpy(logLocation,LOGDATA3_LOC);
00082             break;
00083         case 4:
00084             strcpy(logLocation,LOGDATA4_LOC);
00085             break;
00086         default:
00087             break;
00088     }
00089     
00090     
00091 
00092     // id will have the location of the empty log
00093     //create Log ID
00094     strcpy(currLog,"l");
00095     char num[2];
00096     sprintf(num,"%d",id);
00097     strcat(currLog,num);
00098 
00099 
00100     // intialises the log structure
00101     // e.g parameter, duration etc
00102 
00103     // create temporary JSON object
00104     MbedJSONValue temp;
00105     // populate JSONObject
00106     readData(temp,logLocation);
00107 
00108     // intialises the log structure
00109     // e.g parameter, duration etc
00110     
00111 
00112     record = new LogRecord(currLog);
00113     record->initialiseRecord(temp,parameter,duration);
00114     
00115     
00116     // write the data to file
00117     writeData(temp,logLocation);
00118     
00119 
00120     
00121     //RtosTimer timer()
00122 
00123     // set an interrupt to do this depending on the interval
00124     timer1.attach(callback(this,&LogManager::updateLog),10.0); // call ISR every 10.0 seconds
00125 
00126     // starts updating the log
00127     updateLog();
00128     
00129     
00130     
00131 }
00132 
00133 void LogManager::updateLog()
00134 {
00135 
00136     // update the record with necessary data
00137     //// REMEMBER TO WAIT BEFORE PERMISSION IS GRANTED
00138 
00139     // read the log from logstructure on file
00140     // this will be a JSON object
00141 
00142     // create temporary JSON object
00143     MbedJSONValue temp;
00144     // populate JSONObject
00145 
00146 
00147     readData(temp,logLocation);
00148     //  update the JSON object
00149     if (record->updateRecord(temp)) { // check if logging is unfinished
00150         // write the new JSON data to file
00151         writeData(temp,logLocation);
00152     } else {
00153         // Logging is complete
00154         // stop recurring interrupts here
00155         timer1.detach();
00156 
00157         // delete the Log Record
00158         delete record;
00159 
00160         // COMMIT SUICIDE
00161         delete this;
00162     }
00163 }
00164 
00165 
00166 bool LogManager::logFileExists()
00167 {
00168     // this function checks whether the file
00169     // that stores log data exists
00170 
00171     FILE *file;
00172     if (file = fopen(LOGDATA1_LOC, "r")) {
00173         fclose(file);
00174         return true;
00175     }
00176     return false;
00177 }
00178 
00179 bool LogManager::logAvailable( int &id)
00180 {
00181     // check if there is a space available for logging
00182     bool positionAvailable = false;
00183     // create location buffer
00184     char location[20];
00185 
00186     // loop through logs to check if there is a free position
00187     for( int i = 0; i < 4; i++ ) {
00188 
00189         int number = i+1;
00190 
00191         // determine location
00192         switch(number) {
00193             case 1:
00194                 strcpy(location,LOGDATA1_LOC);
00195                 break;
00196             case 2:
00197                 strcpy(location,LOGDATA2_LOC);
00198                 break;
00199             case 3:
00200                 strcpy(location,LOGDATA3_LOC);
00201                 break;
00202             case 4:
00203                 strcpy(location,LOGDATA4_LOC);
00204                 break;
00205             default:
00206                 break;
00207         }
00208 
00209         // create temporary JSON object
00210         MbedJSONValue temp;
00211         // populate JSON object
00212         readData(temp,location);
00213 
00214         //create Log ID
00215         char logID[] = "l";
00216         char num[2];
00217         sprintf(num,"%d",number);
00218         strcat(logID,num);
00219 
00220         // retrieve the status
00221         int status = temp[logID]["s"].get<int>();
00222 
00223 
00224         // check availability
00225         if(status == 0) {
00226             id = number;
00227             positionAvailable = true;
00228             break;
00229         }
00230 
00231     }
00232 
00233     return positionAvailable;
00234 
00235 }
00236 
00237 
00238 void LogManager::createLogStructure()
00239 {
00240     // this function creates the json data structure
00241     // and stores it on file
00242 
00243 
00244 
00245     // create location buffer
00246     char location[20];
00247 
00248     // create 4 empty logs in the JSON Object
00249     for( int i = 0; i < 4; i++) {
00250 
00251         int number = i+1;
00252 
00253         // create new temporary JSON object
00254         MbedJSONValue temp;
00255 
00256         // determine location
00257         switch(number) {
00258             case 1:
00259                 strcpy(location,LOGDATA1_LOC);
00260                 break;
00261             case 2:
00262                 strcpy(location,LOGDATA2_LOC);
00263                 break;
00264             case 3:
00265                 strcpy(location,LOGDATA3_LOC);
00266                 break;
00267             case 4:
00268                 strcpy(location,LOGDATA4_LOC);
00269                 break;
00270             default:
00271                 break;
00272         }
00273 
00274         //create Log ID
00275         char logID[] = "l";
00276         char num[2];
00277         sprintf(num,"%d",number);
00278         strcat(logID,num);
00279 
00280         temp[logID]["p"] = 0;
00281         temp[logID]["mx"] = 0;
00282         temp[logID]["mn"] = 0;
00283         temp[logID]["av"] = 0;
00284         temp[logID]["s"] = 0;
00285         temp[logID]["d"] = 0;
00286         temp[logID]["%"] = 0;
00287         temp[logID]["v"] = 0;
00288         temp[logID]["t"] = 0;
00289 
00290         // write the data to file
00291         writeData(temp,location);
00292     }
00293 }
00294 
00295 int LogManager::retrieveLog(LogRecord records[])
00296 {
00297     // look for the unempty records by checking their status
00298     int numberOfRecords = 0;
00299     
00300     // location of record in array
00301     int position = 0;
00302 
00303     // create location buffer
00304     char location[20];
00305 
00306     for ( int i = 0; i < 4; i++) {
00307         int number = i+1;
00308 
00309         // determine location
00310         switch(number) {
00311             case 1:
00312                 strcpy(location,LOGDATA1_LOC);
00313                 break;
00314             case 2:
00315                 strcpy(location,LOGDATA2_LOC);
00316                 break;
00317             case 3:
00318                 strcpy(location,LOGDATA3_LOC);
00319                 break;
00320             case 4:
00321                 strcpy(location,LOGDATA4_LOC);
00322                 break;
00323             default:
00324                 break;
00325         }
00326 
00327         // create temporary JSON Object
00328         MbedJSONValue temp;
00329         // populate JSON object
00330         readData(temp,location);
00331 
00332         //create Log ID
00333         char logID[] = "l";
00334         char num[2];
00335         sprintf(num,"%d",number);
00336         strcat(logID,num);
00337 
00338         // look for logs with data
00339         int status = temp[logID]["s"].get<int>();
00340         if(status != 0) {
00341             // logfile is not empty therefore
00342 
00343             // create new record
00344             LogRecord newRecord(logID);
00345             newRecord.initialiseRecord(temp,number);
00346 
00347             // store the records in an array
00348             records[position] = newRecord;
00349             position++;
00350 
00351             // increment the record counter
00352             numberOfRecords++;
00353         }
00354     }
00355 
00356     return numberOfRecords;
00357 }
00358 
00359 void LogManager::deleteLog(LogRecord &record)
00360 {
00361     // this function resets the log data
00362     // in the log structure to the default values
00363 
00364     // first determine the location
00365     // create location buffer
00366     char location[20];
00367 
00368     switch(record.getID()) {
00369         case 1:
00370             strcpy(location,LOGDATA1_LOC);
00371             break;
00372         case 2:
00373             strcpy(location,LOGDATA2_LOC);
00374             break;
00375         case 3:
00376             strcpy(location,LOGDATA3_LOC);
00377             break;
00378         case 4:
00379             strcpy(location,LOGDATA4_LOC);
00380             break;
00381         default:
00382             break;
00383     }
00384 
00385     // create temporary JSON Object
00386     MbedJSONValue temp;
00387 
00388     //create Log ID
00389     char logID[] = "l";
00390     char num[2];
00391     sprintf(num,"%d",record.getID());
00392     strcat(logID,num);
00393 
00394     // create empty log
00395     temp[logID]["p"] = 0;
00396     temp[logID]["mx"] = 0;
00397     temp[logID]["mn"] = 0;
00398     temp[logID]["av"] = 0;
00399     temp[logID]["s"] = 0;
00400     temp[logID]["d"] = 0;
00401     temp[logID]["%"] = 0;
00402     temp[logID]["v"] = 0;
00403     temp[logID]["t"] = 0;
00404     
00405     // write the data to file
00406     writeData(temp,location);
00407     
00408     //wait_ms(100);
00409 }
00410 
00411 void LogManager::readData(MbedJSONValue &temp, const char *location)
00412 {
00413 
00414     // buffer to store string read from file
00415     char read_string[100];
00416     // create the file pointer
00417     FILE *fp;
00418     // prepare file for reading
00419     fp = fopen(location, "r");
00420     // read the logs string into buffer
00421     fgets(read_string,100,fp);
00422     // convert string into JSON Data
00423     parse(temp,read_string);
00424     // close the file
00425     fclose(fp);
00426 }
00427 
00428 void LogManager::writeData(MbedJSONValue &temp, const char *location)
00429 {
00430     // create the file pointer
00431     FILE *fp;
00432     // prepare file for writing
00433     fp = fopen(location, "w");
00434     // convert json object to string
00435     std::string s;
00436     s = temp.serialize();
00437     // write string to file
00438     fprintf(fp,"%s",s.c_str());
00439     // close the file
00440     fclose(fp);
00441 }
00442 
00443 #endif