  /*######################################################################################  
 ##########################################################################################
## ######################################################## ####################          ##
## ########  #######   ###      ## ########    ######   ### ####### ########  ###      ## ##
## ###    ## ###    #  ####    ### ###    ##  ###   ##  ### ###     ###    ## ####    ### ##
## ###   ### ###    ## #######  ## ###   ### ###     ## ### ###     ###   ### #######  ## ##
## #######   ######### ### ###  ## ########  ###     ## ### ####### #######   ### ###  ## ##
## ###   ### ###    ## ###  ##  ## ###   ### ###     ## ### ###     ###   ### ###  ##  ## ##
## ###    ## ###    ## ###      ## ###    ##  ###   ##  ### ###     ###    ## ###      ## ##
## ###    ## ###    ## ###      ## ########    ######   ### ####### ###    ## ###      ## ##
## #################################################### ### ############################# ##
############################################################################################
##### CURRENT / ramboTerm 2.2_ino / 04-04-2020                                            ##
##### ramboTerm: CLI Tool-set for fire alarm interfacing and communications.              ##
##### Matthew Stroble / matthewstroble@gmail.com / firealarmhackery.com                   ##
 ##                                                                                      ##
  ######################################################################################*/


#include <SPI.h>
#include <SD.h>
#include "Wire.h"
#include "EEPROM.h"

#include "rTerm.h"

// -- LOG Functions --
  void rTerm::dumpLog(char logPath[15]){
  
    char _buff[160];
    int cp=0;
       
    File logFile = SD.open(logPath, FILE_READ);
  
    if(!logFile){
      
      strcpy(_buff, "Unable to open file path: ");
      strcat(_buff, logPath);
  
      Serial.println(_buff);
      
      rTerm::smsTx(_buff); delay(200);
      
      for(int i=0; i<160; i++){
        _buff[i] = char('\0');
      }

      for(int i=0; i<15; i++){
        logPath[i] = char('\0');
      }

      return;
    
    }else{
  
      _buff[cp] = logPath[1]; cp++;
      _buff[cp] = logPath[2]; cp++;
      _buff[cp] = logPath[3]; cp++;
      _buff[cp] = char('('); cp++;
      
      for(int i=0; i<5; i++){
        if(isDigit(logPath[i+5])){
          _buff[cp] = logPath[i+5];
          cp++;
        }
      }

      _buff[cp] = char(')'); cp++;
      _buff[cp] = char(' '); cp++;
      _buff[cp] = char('\0');
                
      while(logFile.available()){
  
        char inChar = char(logFile.read());
  
        if(inChar == char(logFile.peek()) && char(logFile.peek()) == char(' ')){
  
          // Skip contiguous whitespace.
          
        }else if(inChar == char('\r') || inChar == char('\0')){
  
          // Skip
          
        }else if(inChar == char('\n')){
  
          _buff[cp] = char(' '); cp++;
          _buff[cp] = char('\0');
          
        }else{
  
          if(cp < 160){
            
            _buff[cp] = inChar; cp++;
            _buff[cp] = char('\0');
      
          }else{
  
            // -- The buffer is full --
              break;
            
          }
          
        }
        
      }

    }
  
    logFile.close(); delay(10);
  
    rTerm::smsTx(_buff); delay(10);
        
    for(int i=0; i<160; i++){ _buff[i] = char('\0'); } delay(10);
    cp=0;
    
  }

// -- DUMP ALL FCP EVENT LOG FILES -- 
  void rTerm::dumpLogsFCP(){

    if(rTerm::logIndexFCP > 1){

      for(int i=1; i<rTerm::logIndexFCP; i++){
  
        char logPath[18];
        char logName[5];
        strcpy(logPath, "/fcp/");
        itoa(i, logName, 10);
        strcat(logPath, logName);
        strcat(logPath, ".log");
        logPath[strlen(logPath)] = char('\0');
        
        File fcpLog = SD.open(logPath, FILE_READ);
      
        if(!fcpLog){
          Serial.print(F("Unable to read file path: ")); Serial.println(logPath); delay(30);
          return;
        }else{
  
          Serial.print(F("fcp(")); Serial.print(i); Serial.println(F("): "));
  
          while(fcpLog.available()){
            
            Serial.write(fcpLog.read());
          
          }
  
        }
  
        fcpLog.close(); delay(100);
        
      }
      
    }else{
  
      Serial.println(F("-- FCP Log empty --"));
      Serial.print(F("  logIndexFCP: ")); Serial.println(logIndexFCP);
      
    }
    
  }

  void rTerm::dumpLogsCID(){

    Serial.print(rTerm::logIndexCID); Serial.println(F(" total CID log files.")); delay(10);
    
    if(rTerm::logIndexCID > 0){
      
      char _buff[30];
      char logPath[15];
      char logName[5];
      int buffIndex;
        
        
        
      for(int i=1; i<=rTerm::logIndexCID; i++){
  
        itoa(i, logName, 10);
        strcpy(logPath, "/cid/");
        strcat(logPath, logName);
        strcat(logPath, ".log");
        logPath[strlen(logPath)] = char('\0');

        File cidLog = SD.open(logPath, FILE_READ);
      
        if(!cidLog){
          Serial.print(F("Unable to open file path: ")); Serial.println(logPath); delay(30);        
          //return;
        }else{
          
          strcpy(_buff, "cid(");
          strcat(_buff, logName);
          strcat(_buff, "): ");
          _buff[strlen(_buff)] = char('\0');
          
          buffIndex = strlen(_buff);
          
          while(cidLog.available()){
            
            _buff[buffIndex] = char(cidLog.read());
            buffIndex++;
                        
          }

          _buff[buffIndex] = char('\r'); buffIndex++;
          _buff[buffIndex] = char('\n'); buffIndex++;
          _buff[buffIndex] = char('\0');

        }
  
        cidLog.close();

        delay(10);

        Serial.print(_buff); delay(10);

        for(int i=0; i<30; i++){
          _buff[i] = char('\0');
        }

        buffIndex = 0;
  
        delay(100);
        
      }
      
    }else{
  
      Serial.println(F("-- No logs to dump --"));
      Serial.print(F("  logIndexCID: ")); Serial.println(logIndexCID);
      
    }
    
  }

  void rTerm::flushLogsFCP(){

    if(rTerm::logIndexFCP > 0){
  
      int xLog = 0;
      
      for(int i=1; i<rTerm::logIndexFCP; i++){
  
        char logPath[18]="/fcp/";
        itoa(i, logPath, 10);
        strcat(logPath, ".log");

        if(!SD.exists(logPath)){
          Serial.print(F(" -- ")); Serial.print(logPath); Serial.println(F(" not found. --")); delay(10);
        }else if(!SD.remove(logPath)){
          Serial.print(F(" -- Unable to remove file path: ")); Serial.println(logPath); delay(10);
        }else{
          SD.remove(logPath);
          Serial.print(logPath); Serial.println(F(" Removed successfully. --")); delay(10);
          xLog++;
        }
        
        delay(20);
        
      }
  
      Serial.print(xLog);
      Serial.println(F(" FCP Log Files Removed."));
      
      rTerm::logIndexFCP = 1;
      rTerm::procIndexFCP = 1;
  
      EEPROM.put(180, rTerm::logIndexFCP);
      EEPROM.put(183, rTerm::procIndexFCP);
      
    }
    
  }

void rTerm::flushLogsCID(){

  if(rTerm::logIndexCID > 0){

    int x = 0;
    
    for(int i=1; i<=rTerm::logIndexCID; i++){

      char logPath[15] = "/cid/";
      char logName[5];
      itoa(i, logName, 10);
      strcat(logPath, logName);
      strcat(logPath, ".log");
      
      logPath[strlen(logPath)] = char('\0');
      
      if(!SD.exists(logPath)){
        Serial.print(F(" -- ")); Serial.print(logPath); Serial.println(F(" not found. --")); delay(10);
      }else if(!SD.remove(logPath)){
        Serial.print(F(" -- Unable to remove file path: ")); Serial.println(logPath); delay(10);
      }else{
        Serial.print(logPath); Serial.println(F(" Removed successfully. --")); delay(10);
        x++;
      }
      
    }

    Serial.print(x); Serial.println(F(" Log Files Removed Successfully."));
    
    rTerm::logIndexCID = 1;
    rTerm::procIndexCID = 1;

    EEPROM.put(186, rTerm::logIndexCID); delay(10);
    EEPROM.put(189, rTerm::procIndexCID); delay(10);
    
  }else{
    Serial.println(F(" -- No CID log files found --"));
  }
  
}
