/****************************************************************************/
/* Copyright (c) 2000 MBARI                                                 */
/* MBARI Proprietary Information. All rights reserved.                      */
/****************************************************************************/
/* Summary  :                                                               */
/* Filename : WorkSite.cc                                                   */
/* Author   :                                                               */
/* Project  :                                                               */
/* Version  : 1.0                                                           */
/* Created  : 02/07/2000                                                    */
/* Modified :                                                               */
/* Archived :                                                               */
/****************************************************************************/
/* Modification History:                                                    */
/*                                                                          */
/* 06/11/00 - Added sound speed and transponder frequency attributes        */
/*          - Eickstedt/MIT Sea Grant                                       */ 
/****************************************************************************/
#include <string.h>
#include <malloc.h>
#include "WorkSite.h"
#include "ourTypes.h"
#include "FloatAttribute.h"
#include "AngleAttribute.h"
#include "IntegerAttribute.h"
#include "FloatAttribute.h"
#include "Syslog.h"
#include "MathP.h"
#include "System.h"
#include "NavUtils.h"
#include "AttributeParser.h"
#include "Transponder.h"



WorkSite::WorkSite(const char *filename)
     : WorkSiteIF_SK()
{
  Boolean debug = True;
     
  dprintf("WorkSite::WorkSite -- Adding attributes");

  attributes = new Attributes("worksite");

  attributes->add(new AngleAttribute("latitude", "Site latitude", 
				     &_latitude));
     
  attributes->add(new AngleAttribute("longitude", "Site longitude", 
				     &_longitude));

  attributes->add(new IntegerAttribute("utmZone", "UTM zone", 
				       &_utmZone, MontereyUTM));
     
  attributes->add(new AngleAttribute("magneticVariation", 
				     "Magnetic variation at site", 
				     &_magneticVariation));

  attributes->add(new FloatAttribute("soundSpeed",
                                     "Speed of Sound (m/s)",
                                     &_soundSpeed));

  transponders = new TransponderList();
     
  loadWorkSite( filename );
     
  debug = False;

  dprintf("WorkSite::WorkSite() - latitude = %.2f deg", 
	  _latitude / Math::RadsPerDeg);
}


WorkSite::~WorkSite()
{
  Boolean debug = False;
  dprintf("WorkSite::~WorkSite() - latitude = %.2f deg", 
	  _latitude / Math::RadsPerDeg);
  delete attributes;
  delete transponders;
}


int WorkSite::loadWorkSite( const char *filename )
{
     Boolean debug = True;
     const char *fullFileName = System::configurationFile( filename );

     dprintf("WorkSite::WorkSite -- Loading parameters from file %s",
	     fullFileName );

     System::copyToLogDir(fullFileName);

     AttributeParser::reset();
     try {
//	  AttributeParser::parse( fullFileName, attributes, transponders,
//				  ParserDebugOn );
	  AttributeParser::parse( fullFileName, attributes, transponders );
     }
     catch(Exception e)
     {
	  Syslog::write("WorkSite::WorkSite -- AttributeParser threw: %s",
			e.msg );
	  return -1;
     }
     catch(...)
     {
	  Syslog::write("WorkSite::WorkSite -- AttributeParser threw an "
			"unknown exceptions");
	  return -1;
     }	  

     
     dprintf("WorkSite::WorkSite -- Finished loading parameters");

     if( debug )
	  transponders->dumpTransponderList();

     return 0;

}

double WorkSite::soundSpeed()
{
return _soundSpeed;
}


double WorkSite::latitude()
{
  return _latitude;
}


double WorkSite::longitude()
{
  return _longitude;
}


long WorkSite::utmZone()
{
  return _utmZone;
}


double WorkSite::magneticVariation()
{
  return _magneticVariation;
}


short WorkSite::nLblTransponders()
{
  return transponders->count();
}


long WorkSite::getTransponder(short transponderNo, 
			      double *easting, double *northing, 
			      double *depth, long *turnaroundTime, long *channel)
{

  Transponder *transponder;

  transponder = (Transponder *)transponders->findItem( transponderNo );
  if( transponder == NULL )
       return -1;

  *easting = transponder->easting();
  *northing = transponder->northing();
  *depth = transponder->depth();
  *turnaroundTime = transponder->turnAroundTime();
  *channel = transponder->channel();

  return 0;
}


// WorkSite::Transponder::Transponder()
//   : attributes("transponder")
// {
//   // Set attributes for transponders
//   attributes.add(new FloatAttribute("easting", "Easting", 
// 				    &easting));

//   attributes.add(new FloatAttribute("northing", "Northing", 
// 				    &northing));

//   attributes.add(new FloatAttribute("depth", "Depth", &depth));

//   attributes.add(new IntegerAttribute("turnaroundTime", 
// 				      "Turnaround time", 
// 				      &turnaroundTime));
// }



WorkSite::Error::Error(char *message)
  : Exception(message)
{
}


WorkSite::Error::~Error()
{
}


int WorkSite::spawnAuxTasks()
{
  return 0;
}

