/****************************************************************************/
/* Copyright (c) 2015 MBARI                                                 */
/* MBARI Proprietary Information. All rights reserved.                      */
/****************************************************************************/
/* Summary  : Power Buoy Data Logger log file writer class                  */
/* Filename : CSVWriter.hpp                                                 */
/* Author   : Henthorn                                                      */
/* Project  : Power Buoy                                                    */
/* Version  : 1.0                                                           */
/* Created  : 04/21/2015                                                    */
/* Modified :                                                               */
/* Archived :                                                               */
/****************************************************************************/
/* Modification History:                                                    */
/****************************************************************************/
/*                                                                          */
/* This class is meant to be used with data source objects to write source  */
/* data to a log file in a particular format. Need a different format?      */
/* Derive a new class from CSVWriter and provide the updated functions.     */
/*                                                                          */
/****************************************************************************/
#ifndef _CSVWRITER_
#define _CSVWRITER_

// CSVWriters know about data readers
#include "Crossbow.hpp"
#include "CanBus.hpp"

class CSVWriter {

public:

   // CSVWriter constructor opens a log file for writing.
   // Default to files spanning 60 minutes.
   //
   CSVWriter(const char *dirname, const char *filename,
             unsigned char new_file_interval = 60);

   virtual ~CSVWriter();

   virtual bool file_open() { return (_file != NULL); }

   virtual int update_xbow_record(Crossbow::XB_Record *xb_rec);
   virtual int update_power_record(CanBus::PC_Record *pc_rec);
   virtual int update_battery_record(CanBus::BC_Record *bc_rec);
   virtual int update_spring_record(CanBus::SC_Record *sc_rec);
   virtual int update_trefoil_record(CanBus::TF_Record *tf_rec);
   virtual int update_rudder_record(CanBus::RC_Record *rc_rec);
   virtual int open_log();
  virtual void close_log() { if (file_open()) fclose(_file); }

protected:

   virtual int write_to_log(const char* buf);
   virtual int write_header();
   virtual int write_record(char s);  // Write record with source id
   virtual int write_xb();            // Write the Crossbow record
   virtual int write_pc();            // Write the Power Controller record
   virtual int write_bc();            // Write the Battery Controller record
   virtual int write_sc();            // Write the Spring Controller record
   virtual int write_rc();            // Write the Rudder Controller record
   virtual int write_tf();            // Write the Trefoil Controller record

   char* _dirname;
   char* _filename;
   char  _pathname[200];           // Full pathname of log file

   FILE* _file;
   long  _file_open_time;  // Keep track of when the log file was opened
   unsigned char _file_interval;

   Crossbow::XB_Record _xb;
   CanBus::PC_Record   _pc;
   CanBus::BC_Record   _bc;
   CanBus::SC_Record   _sc;
   CanBus::TF_Record   _tf;
   CanBus::RC_Record   _rc;

   char _verbose;
};

#endif

