#include "Gps.h"

Gps::Gps(const char *name, NavSensors *sensors, 
	 const NavigationIF::Position *position, 
	 double maxFixDepth, int maxBad)
  : NavSensor(name, sensors, maxBad)
{
  _gpsIF = 0;
  _position = position;
  _maxFixDepth = maxFixDepth;
}



TaskInterface *Gps::createTaskIF(int timeout)
{
  try {
    _gpsIF = new GpsIF(name(), timeout);
  }
  catch (...) {
    _gpsIF = 0;
  }

  return _gpsIF;
}


DeviceIF::Status Gps::readTaskIF(Boolean *valid, 
				 TimeIF::TimeSpec *sampleTime)
{
  if (_position->z > _maxFixDepth) {

    // We're underwater; can't get a fix.
    *valid = False;

    // But instrument might be okay; so just return last status
    return deviceStatus();
  }

  DeviceIF::Status status = _gpsIF->getFix(&fix);

  sampleTime->seconds = fix.sampleTime.seconds;
  sampleTime->nanoSeconds = fix.sampleTime.nanoSeconds;

  if (fix.quality == GpsIF::Invalid)
    *valid = False;
  else
    *valid = True;

  return status;
}




