/****************************************************************************/
/* Copyright (c) 2001 MBARI                                                 */
/* MBARI Proprietary Information. All rights reserved.                      */
/****************************************************************************/
/* Summary  :                                                               */
/* Filename : _ips4Test.cc                                                  */
/* Author   : Rich Henthorn                                                 */
/* Project  :                                                               */
/* Version  : 1.0                                                           */
/* Created  : 05 Jun 01                                                     */
/* Modified :                                                               */
/* Archived :                                                               */
/****************************************************************************/
/* Modification History:                                                    */
/****************************************************************************/

#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <signal.h>
#include <process.h>
#include <time.h>

#include "Ips4IF.h"
#include "Syslog.h"
#include "System.h"
#include "SerialParameters.h"

int main( int argc, char **argv ) 
{
  Ips4IF *ips4IF;

  pid_t _serverPid;

  const int sleepMillisecs = 2000;
  struct timespec sleepTime;

  sleepTime.tv_sec = 0;
  sleepTime.tv_nsec = sleepMillisecs * 1000000;

  try 
  { 
    SerialParameters serialParams(&argc, argv);
    char argString[100];
    sprintf(argString, "-serial %s", serialParams.string());
    if ((_serverPid = System::spawn("ips4Server", argString)) == -1)
      {
	Syslog::write("System::spawn(ips4Server) failed\n");
      }
  }
  catch ( ... )
  {
    Syslog::write("ips4Test -- "
		  "caught exception on spawning of Ips4 Server\n");
  }

  sleep(5);

  try 
  {
    ips4IF = new Ips4IF( "ips4", 1000 );
  }
  catch (...)
  {
    Syslog::write("ips4Test -- Caught exception on IF creation\n");
    exit( -1 );
  }

  Ips4IF::Vector cond = {0.4, 0.4, 0.4, 0.4, 0.4};
  Ips4IF::Vector temp = {14., 14., 14., 14., 14.};
  Ips4IF::Vector pres = {20., 20., 20., 20., 20.};
  double patm = 10.;
  double calc_ice_draft = -1.1;

  long ctdtime = time((long*)0);
  Boolean profile = False;
  while (True) 
  {
    if (!profile)
    {
      ctdtime+=15;
      profile = True;
      ips4IF->startProfile();
      printf("ips4Test: Start profiling\n");
    }

    if ((ctdtime > 0) && (ctdtime < time((long*)0)))
    {
      ctdtime = 0;
      ips4IF->stopProfile();
      printf("ips4Test: Start profiling\n");
    }

    if (ips4IF->get_ice_draft(patm, 5, cond, temp, pres, &calc_ice_draft) != 
	DeviceIF::Ok)
      fprintf(stderr, "ips4Test:: Invalid Ips data\n");

    printf("ips4Test: Calculated Ice Draft = %lf\n", calc_ice_draft);

    sleep( 2 );
  }
  ips4IF->stopProfile();

  delete ips4IF;
  kill(0, SIGTERM);
  return 0;
}
