/****************************************************************************/
/* Copyright (c) 2000 MBARI                                                 */
/* MBARI Proprietary Information. All rights reserved.                      */
/****************************************************************************/
/* Summary  :                                                               */
/* Filename : _dropWeightTest.cc                                            */
/* Author   : rsm                                                           */
/* Project  :                                                               */
/* Version  : 1.0                                                           */
/* Created  : 11 Feb 2004                                                   */
/* Modified :                                                               */
/* Archived :                                                               */
/****************************************************************************/
/* Modification History:                                                    */
/****************************************************************************/
/*-----------------------------------------------------------------------*

  $Id: _dropWeightTest.cc,v 1.2 2004/03/04 00:09:25 rob Exp $
  *-----------------------------------------------------------------------*/

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

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

int main( int argc, char **argv ) 
{
  DropWeightIF *dropWeightIF;
  pid_t _serverPid;
  int externServerPid = 0;
  FILE *ppipe;
  char buf[512];
  buf[0] = '\0';
  int rc = 0;
  ppipe = popen("slay -pQ dropWeightServer","r");
  rc = fscanf( ppipe, "%s", buf );
  pclose(ppipe);
  buf[511]='\0';
  //printf("The popen returned string %s, of length %d.\n", buf, strlen(buf));
  if( rc != -1 ) 
  {
     Syslog::write("dropWeightServer, pid = %s, exists.\n", buf);
     externServerPid = atoi(buf);
     try 
     {
	dropWeightIF = new DropWeightIF( "DropWeight", 5 );
     }
     catch (Exception e)
     {
	Syslog::write("dropWeightTest -- Caught exception on IF creation\n");
	Syslog::write(" %s", e.msg);
	delete dropWeightIF;
	exit( -1 );
     }
  }
  else
  {
     Syslog::write("dropWeightServer isn't executing.  Start it.");

     try 
     { 
	SerialParameters serialParams(&argc, argv);
	char argString[100];
	sprintf(argString, "-v -serial %s", serialParams.string());
	if ((_serverPid = System::spawn("dropWeightServer", argString)) == -1)
	{
	   Syslog::write("System::spawn(dropWeightServer) failed\n");
	}
     }
     catch ( ... )
     {
	Syslog::write("dropWeightTest -- "
		      "caught exception on spawning of DropWeight Server\n");
     }
     printf(" Spawned the server/driver!\n");
     sleep(1);
     try 
     {
	dropWeightIF = new DropWeightIF( "DropWeight", 5 );
     }
     catch (Exception e)
     {
	Syslog::write("dropWeightTest -- Caught exception on IF creation\n");
	Syslog::write(" %s", e.msg);
	//kill(0, SIGKILL);
	//kill(0, SIGTERM);
	kill(0, SIGINT);       //Without this the server and driver remain.
	delete dropWeightIF;
	exit( -1 );
     }
  }
  //
  // Enter test loop.
  //
  while( True )
  {
     char buf[512];
     char *helpStr = " Type: \n"
	             "    Rel  - to release the weight.\n"
	             "    Res  - to reset the watchdog.\n"
	             "    RRC  - to reset the relay and the counters.\n"
	             "    RRP  - to reset the relay and the PIC.\n"
	             "    Read - to read the PIC output.\n"
	             "    Q or E - to exit this program; leave the "
	                  "server/driver running.\n"
	             "    QK or EK - to exit this program and kill the "
	                  "server/driver.\n";
     printf( "%s", helpStr );
     printf( "dropWeightTest>");
     gets(buf);
     if     ( !strncmp( buf, "Rel", 3) ) dropWeightIF->release();
     else if( !strncmp( buf, "Res", 3) ) dropWeightIF->resetWatchdog();
     else if( !strncmp( buf, "RRC", 3) ) dropWeightIF->resetRelayAndCounters();
     else if( !strncmp( buf, "RRP", 3) ) dropWeightIF->resetRelayAndPIC();
     else if( !strncmp( buf, "QK", 2) || !strncmp( buf, "EK", 2) ) 
     {
	system("slay -Q dropWeightServer");
	system("slay -Q dropWeight");
	break;
     }
     else if( !strncmp( buf, "Q", 1) || !strncmp( buf, "E", 1) ) break;
     else if( !strncmp( buf, "Read", 3) ) 
     {	
	printf("Press any key to exit\n");
	while ( !kbhit() )
	{
	   printf(" Wdt = %d Bwt = %d ACommsDN = %d DW_out = %d\n",
		  dropWeightIF->watchdogTimeout(),
		  dropWeightIF->burnwireTimeout(),
		  dropWeightIF->ACommsDN(),
		  dropWeightIF->DW_out());
	   //
	   // Clear the screen:
	   //
	   // printf("\033[2J");
	   sleep( 1 );
	}  //while ( !kbhit() )
     }  //else if( !strncmp( buf, "Read", 3) ) 
     else printf(" Command not recognized.  Please try again.\n");
  }  //while( true )
  printf("\n Exiting dropWeightTest.\n");
  delete dropWeightIF;
  return 0;
}
