/****************************************************************************/
/* Copyright (c) 2012 MBARI                                                 */
/* MBARI Proprietary Information. All rights reserved.                      */
/****************************************************************************/
/* Summary  :                                                               */
/* Filename : ModtronixLog.cc                                               */
/* Author   :                                                               */
/* Project  :                                                               */
/* Version  : 1.0                                                           */
/* Created  : 12/07/2014                                                    */
/* Modified :                                                               */
/* Archived :                                                               */
/****************************************************************************/
/* Modification History:                                                    */
/****************************************************************************/
#include "DataLogWriter.h"
#include "ModtronixLog.h"
#include "Modtronix.h"
#include "ModtronixOutput.h"
#include "ShortData.h"
#include "TimeP.h"
#include "Syslog.h"

ModtronixLog::ModtronixLog(Modtronix *m,
           DataLog::FileFormat fileFormat,
           const char* name)
   : _mod(m),
     DataLogWriter(name, fileFormat, AutoTimeStamp)
{
  init();
}


void ModtronixLog::init(void)
{
  char dname[100];
  sprintf(dname, "%s.data", name());
  setMnemonic(dname);

  // Indicates that the modtronix board initialized and ready
  sprintf(dname, "%s.ready", name());
  addField((_mod_ready = new IntegerData(dname)));
  sprintf(dname, "%s.comms", name());
  addField((_mod_comms = new IntegerData(dname)));

  // Switch status
  //
  sprintf(dname, "%s.camera_power", name());
  addField((_mod_camera_power = new IntegerData(dname)));

  sprintf(dname, "%s.light_level", name());
  addField((_mod_light_level = new IntegerData(dname)));
  sprintf(dname, "%s.light_power_1", name());
  addField((_mod_light_power_1 = new IntegerData(dname)));
  sprintf(dname, "%s.light_power_2", name());
  addField((_mod_light_power_2 = new IntegerData(dname)));
  sprintf(dname, "%s.light_power_3", name());
  addField((_mod_light_power_3 = new IntegerData(dname)));
  sprintf(dname, "%s.light_power_4", name());
  addField((_mod_light_power_4 = new IntegerData(dname)));

  sprintf(dname, "%s.recorder_power", name());
  addField((_mod_recorder_power = new IntegerData(dname)));
  sprintf(dname, "%s.recorder_ready", name());
  addField((_mod_recorder_ready = new IntegerData(dname)));

  sprintf(dname, "%s.ctd_power", name());
  addField((_mod_ctd_power = new IntegerData(dname)));

  // Env data and alarms
  //
  sprintf(dname, "%s.h2o_humidity", name());
  addField((_mod_humidity = new FloatData(dname)));
  sprintf(dname, "%s.temp", name());
  addField((_mod_temp = new FloatData(dname)));
  sprintf(dname, "%s.h2o_fwd", name());
  addField((_mod_h2o_fwd = new FloatData(dname)));
  sprintf(dname, "%s.h2o_aft", name());
  addField((_mod_h2o_aft = new FloatData(dname)));
  sprintf(dname, "%s.h2o_alarm", name());
  addField((_mod_h2o_alarm = new IntegerData(dname)));

  // Video analog data
  //
  sprintf(dname, "%s.video_mean", name());
  addField((_mod_video_mean = new FloatData(dname)));
  sprintf(dname, "%s.video_max", name());
  addField((_mod_video_max = new FloatData(dname)));

  // Motor stuff
  //
  sprintf(dname, "%s.motor_position", name());
  addField((_ez_motor_position = new IntegerData(dname)));
  sprintf(dname, "%s.motor_action", name());
  addField((_ez_motor_action = new IntegerData(dname)));
}


ModtronixLog::~ModtronixLog()
{
}


void ModtronixLog::setFields()
{
   Boolean debug = False;


   if( _mod )
   {
      ModtronixOutput *o = _mod->_output;

      _mod_camera_power->setValue(o->data.camera_power);

      _mod_light_power_1->setValue(o->data.light_power[0]);
      _mod_light_power_2->setValue(o->data.light_power[1]);
      _mod_light_power_3->setValue(o->data.light_power[2]);
      _mod_light_power_4->setValue(o->data.light_power[3]);
      _mod_light_level->setValue(o->data.light_level);

      _mod_recorder_power->setValue(o->data.recorder_power);
      _mod_recorder_ready->setValue(o->data.recorder_ready);

      _mod_ctd_power->setValue(o->data.ctd_power);

      _mod_humidity->setValue(o->data.humidity);
      _mod_temp->setValue(o->data.temp);
      _mod_h2o_fwd->setValue(o->data.h2o_fwd);
      _mod_h2o_aft->setValue(o->data.h2o_aft);

      _mod_video_mean->setValue(o->data.video_mean);
      _mod_video_max->setValue(o->data.video_max);

      _ez_motor_position->setValue(o->data.motor_position);
      _ez_motor_action->setValue(o->data.motor_action);
   }
}
