/****************************************************************************/
/* Copyright (c) 2000 MBARI                                                 */
/* MBARI Proprietary Information. All rights reserved.                      */
/****************************************************************************/
/* Summary  :                                                               */
/* Filename : DropWeight.cc                                                 */
/* Author   : rsm                                                           */
/* Project  :                                                               */
/* Version  : 1.0                                                           */
/* Created  : 02/07/2000                                                    */
/* Modified :                                                               */
/* Archived :                                                               */
/****************************************************************************/
/* Modification History:                                                    */
/****************************************************************************/
#include <time.h>
#include "Gulper.h"
#define READ_TIMEOUT 3000           //Milliseconds
#define MAXRECORDBYTES 512
#include "PeriodicTask.h"
#include "SerialDevice.h"
#include "Syslog.h"

Boolean debug = False;

Gulper::Gulper(SerialDevice *device)
   : PeriodicTask("gulperDriver") 
{

   _device = device;
   _log = new GulperLog(this, DataLog::BinaryFormat);
   _output= new GulperOutput();
   _output->data.FireGulper = -1;
   _output->write();

   addPeriodicCallback(1000, (CallbackMethod)Gulper::readData);
}


Gulper::~Gulper()
{
   Syslog::write(" GulperDestructor Executing.");
   delete _log;
   delete _output;
}


void Gulper::initialize()
{
}

void Gulper::readData()
{
   _output->read();

   if (_output->data.FireGulper != -1) 
   {
      fireGulper(_output->data.FireGulper);
      //
      // Log which gulper was fired.
      _log->write();
      _output->data.FireGulper = -1;
   }
   //
   // Write the data out.
   //
   _output->write();
}

void Gulper::fireGulper(long gulperToFire)
{
     time_t theTime;
     char cmdStr[12];

     if (gulperToFire == NGULPERS) {
	int i;
	
	for (i = 0; i < 10; i++) {
		sprintf(cmdStr, "$%02X1Fff\n", i);
		theTime = time(NULL);
		Syslog::write("%d Gulper::fireGulper - cmd is %s\n", theTime, cmdStr);
		try { _device->write(cmdStr, 7); }
		catch( ... ) {
			Syslog::write("gulper::fireGulper - error");
		}
	}
     } else {
     	sprintf(cmdStr, "$%02X1Fff\n", gulperToFire);
     	theTime = time(NULL);
     	Syslog::write("%d Gulper::fireGulper - cmd is %s\n",theTime, cmdStr); 
     	try {_device->write(cmdStr, 7); sleep(1); _device->write(cmdStr, 7); }
     	catch( ... ) {
		Syslog::write("Gulper::fireGulper - error");
     	}
    }
}

