/****************************************************************************/
/* Copyright (c) 2000 MBARI                                                 */
/* MBARI Proprietary Information. All rights reserved.                      */
/****************************************************************************/
/* Summary  :                                                               */
/* Filename : ctdTest.cc                                                    */
/* Author   :                                                               */
/* Project  :                                                               */
/* Version  : 1.0                                                           */
/* Created  : 02/07/2000                                                    */
/* Modified :                                                               */
/* Archived :                                                               */
/****************************************************************************/
/* Modification History:                                                    */
/* 09/08/2014 Renamed file from original Seabird files, and began           */
/* 			  refactoring so that this driver can support the seabird       */
/*            25plus system.  EJM                                           */
/****************************************************************************/
/*-----------------------------------------------------------------------*
  PROGRAM: ctdServer

  DESCRIPTION: Binary wrapper for ctd Server

  AUTHOR: Aaron Marsh

  $Id: seabird25pTest.cc,v 1.2 2014/10/02 23:54:33 emartin Exp $
  *-----------------------------------------------------------------------*/
#include <stdlib.h>
#include <conio.h>
#include <process.h>
#include <signal.h>

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

int main( int argc, char **argv )
{
     float cond, temp, depth, tfreq, cfreq;
     TimeIF::TimeSpec condSampleTime, tempSampleTime;
     float v1,v2,v3,v4,v5,v6, v7, v8;
     Seabird25pIF *ctd;
     pid_t _ctdServer;
     int i;
     char *ctdname = (char *)NULL;

     for (i = 1; i < argc; i++) {
	if (!strcmp(argv[i],"-n")) {
	    ctdname = strdup(argv[i+1]);
	}
     }
     if (!ctdname) {
	ctdname = strdup("ctdDriver");
     }
     
     try { 
       SerialParameters serialParams(&argc, argv);
       char argString[100];
       sprintf(argString, "-n %s -serial %s", ctdname, serialParams.string());
       if ((_ctdServer = System::spawn("seabird25pServer", argString)) == -1) {
	 Syslog::write("System::spawn(seabird25pServer) failed\n");
       }
     }
     catch ( ... )
       {
	 Syslog::write("seabird25pTest -- caught exception on spawning of "
		       "seabird25pServer\n");
       }
     
     try {
       ctd = new Seabird25pIF("ctd", ctdname);
     }
     catch (Exception e) 
       {
	 Syslog::write("seabird25pTest -- Caught exception on creation of CtdIF:%s\n",
		       e.msg);
	 exit( -1 );
       }
    free(ctdname); 

    DeviceIF::Status status;
     
     printf("Press any key to exit\n");
     
     while (True) {

       if (ctd->ready())
       {
         if(ctd->get_CTD_data(&cond, &temp, &depth, &tfreq, &cfreq, 
			      &condSampleTime, &tempSampleTime,
			      &v1,
			      &v2,
			      &v3,
			      &v4,
			      &v5,
			      &v6,
			      &v7,
			      &v8
			      ) == DeviceIF::Ok)
         {
	   printf("C: %lf, T: %lf, D: %lf, Tfreq: %lf, Dfreq: %lf, "
		  "Cts: %ld, Ctns: %ld, Tts: %ld, Ttns: %ld\n  ",
		  cond, temp, depth, tfreq, cfreq,
		  condSampleTime.seconds, condSampleTime.nanoSeconds,
		  tempSampleTime.seconds, tempSampleTime.nanoSeconds);

	   float salinity;
	   status = ctd->salinity(&salinity);
	   printf("Salinity = %.2f Psu\n", salinity);

	   printf("Voltages: %lf, %lf, \t%lf, %lf, \t%lf, %lf, %lf, %lf\n",v1,v2,v3,v4,v5,v6,v7,v8);

	 }
	 else 
         {
	   printf("ctd->get_CTD_data() returned error\n");
         }
       }
       sleep(1);
     }

     delete ctd;
     kill(0, SIGTERM);

     return 0;
}
