/****************************************************************************/
/* Copyright (c) 2000 MBARI                                                 */
/* MBARI Proprietary Information. All rights reserved.                      */
/****************************************************************************/
/* Summary  :                                                               */
/* Filename : gpsTest.cc                                                    */
/* Author   :                                                               */
/* Project  :                                                               */
/* Version  : 1.0                                                           */
/* Created  : 02/07/2000                                                    */
/* Modified :                                                               */
/* Archived :                                                               */
/****************************************************************************/
/* Modification History:                                                    */
/****************************************************************************/
/*-----------------------------------------------------------------------*
  PROGRAM: gpsTest

  DESCRIPTION: Test program for GPS driver

  AUTHOR: John Rieffel

  $Id: gpsTest.cc,v 1.8 2003/01/28 22:21:54 dorado Exp $
  *-----------------------------------------------------------------------*/
#include <stdlib.h>
#include <conio.h>
#include <signal.h>
#include <process.h>
#include <errno.h>
#include <time.h>
#include "Syslog.h"
#include "GpsIF.h"
#include "GpsUtils.h"
#include "System.h"
#include "SerialParameters.h"
#include "Syslog.h"

int main( int argc, char **argv )
{
  Boolean debug = True;
  unsigned char utcTime;
  double lat;
  char NSHem;
  char EWHem;
  double longt;
  short qual;
  short numSat;
  float hdop;
  float antH;
  float geoH;
  short dgpsDA;
  short  dgpsRSID;
  long t;
  
  GpsIF *gps;
  pid_t _gpsServer;

  SerialParameters serialParams(&argc, argv);
  char argString[100];
  argString[0] = '\0';

  for (int ag = 1; ag < argc; ag++)
  {
    strcat(argString, " ");
    strcat(argString, argv[ag]);
  }

  //  sprintf(argString, "-r ashtech -serial %s", serialParams.string());

  if ((_gpsServer = System::spawn("gpsServer", argString)) == -1) {
    Syslog::write("System::spawn(gpsServer) failed\n");
  }
  
  try {
    gps = new GpsIF("gps");
  }
  catch (Exception e) {
    Syslog::write("gpsTest - caught Exception: %s", e.msg);
    exit(1);
  }
  catch (...) {
    Syslog::write("gpsTest - caught unknown exception");
    exit(1);
  }
  
  printf("Press any key to exit\n");

  GpsIF::Fix fix;
  char dateString[256];

  while (True) {

    if (gps->getFix(&fix) != DeviceIF::Ok)
      fprintf(stdout, "Error from gps->getFix()\n");

    else {

      time_t t = fix.sampleTime.seconds;
      struct tm *timeStruct = localtime(&t);
      strftime(dateString, sizeof(dateString), "%m/%d/%Y %H:%M:%S", 
	       timeStruct);

      printf("lat: %s %c, lon: %s %c (%s UTC, quality=%d)\n",
	     GpsUtils::latitude(fix.latitude),
	     GpsUtils::hemisphere(fix.nsHemisphere),
	     GpsUtils::longitude(fix.longitude),
	     GpsUtils::hemisphere(fix.ewHemisphere),
	     dateString, fix.quality);

      printf("nSat: %d, hdop: %.3f, altitude: %.1f, geoidH: %.1f\n",
	     fix.nSatellites, fix.hdop, fix.altitude, fix.geoidHeight);

      printf("ndgpsDataAge: %d s, dgpsStationId: %d\n\n",
	     fix.dgpsDataAge, fix.dgpsStationId);

    }

    sleep(1);
  }

  kill(0, SIGTERM);
  dprintf("_gpsServer=%d\n", _gpsServer);

  // dprintf("delete gps...");

  //  delete gps;

  dprintf("return...");
  return 0;
}
