/****************************************************************************/
/* Copyright (c) 2001 MBARI                                                 */
/* MBARI Proprietary Information. All rights reserved.                      */
/****************************************************************************/
/* Summary  :                                                               */
/* 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 "BuoyLauncherIF.h"
#include "Syslog.h"
#include "System.h"
#include "SerialParameters.h"

void printStat(BuoyLauncherIF::LC_status stat)
{
  printf("\t**** Launcher Status Record from %s",
	 asctime(localtime(&stat.timeSec)));
  printf("LC Error: %d\t\tLast Launch OK: %d\tBuoy Responsive: %d\n",
	 stat.launcherError, stat.lastLaunchOK, stat.buoyResponsive);
  printf("Next Buoy Ready: %d\tCurrent Buoy#: %d\tTotal # Buoys: %d\n",
	 stat.nextBuoyReady, stat.currentBuoyNumber, stat.numTotalBuoys);
  printf("Remain Buoys: %d\tBuoys Launched: %d\n",
	 stat.numBuoysRemain, stat.numBuoysLaunched);
  printf("ARGOS Loaded: %ld\tARGOS Left: %ld\n",
	 stat.argosBytes, stat.dataMemory);
  printf("Archive Loaded: %ld\tArchive Left: %ld\n",
	 stat.archiveBytes, stat.picoBytes);
}

int main( int argc, char **argv ) 
{
  BuoyLauncherIF *buoyLauncherIF;

  pid_t _serverPid;
  pid_t workSiteServer;

  const int sleepMillisecs = 2000;
  struct timespec sleepTime;

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

  if ((workSiteServer = System::spawn("workSite",  "")) == -1) 
    throw Exception("System::spawn(workSite Server) failed");

  printf(" buoyLauncherTest: Started WorkSite Server.\n");

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

  try 
  {
    buoyLauncherIF = new BuoyLauncherIF( "BLS", 1000 );
  }
  catch (...)
  {
    Syslog::write("_buoyLauncherTest -- Caught exception on IF creation\n");
    exit( -1 );
  }

  sleep(30);

  BuoyLauncherIF::LC_status stat;
  printStat(stat);
  do
  {
    sleep(15);
    buoyLauncherIF->getLauncherStatus(&stat);
    printStat(stat);
  } while (0);
//  } while (!stat.buoyResponsive);

  // Data transfer sequence
  //
/*
  char *xferfile = getenv("AUVDATAXFER");
  if (!xferfile)
  {
    xferfile = "/usr/include/iostream.h";
  }
  BuoyLauncherIF::Filename filename;
  strcpy(filename, xferfile);
  buoyLauncherIF->loadData(filename);
  sleep(40);
*/

  buoyLauncherIF->getLauncherStatus(&stat);
  printStat(stat);
  sleep(15);

  // Launch sequence
  //
  if (getenv("AUVLAUNCH"))
  {
//    if (stat.buoyResponsive && stat.nextBuoyReady)
    if (1)
    {
      Syslog::write("Launching buoy...");
      buoyLauncherIF->launch();
      sleep(20);
      while (!buoyLauncherIF->launched)
      {
	Syslog::write("Launch not yet confirmed...");
	sleep(5);
      }
      Syslog::write("Launch confirmed!");
    }
    else
      Syslog::write("Wanted to Launch but LC/Buoy not ready. Sorry.");
  }
  else
    Syslog::write("No Launch");

  sleep(180); // idle for awhile

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