<html>
<xmp>
/**
 * Title: iSIM
 * Copyright: Copyright (c) 2002
 * Company:   University of Oregon Computer Science Dept.
 *
 * @author Jason Prideaux
 * @version 1.0
 *
 */

import gps_monitor.gps.*;
import gps_monitor.gui.*;
import gps_monitor.util.*;


/* ====================================================================== */
/** This class is for running the GpsMonitor iPAQ Application.
 *
 */
public class GpsProxy extends Thread implements iPAQProxy{

	/** The iPAQ application owner that wants the data */
  	private GpsMonitorUI parent;

	/** The gps object that gives us data. */
  	private GpsGenerator gps;

	/** Port to use for connecting to the simulator. */
	private int port;

	/** Port to use in connecting to a serial gps device. */
	private String serial_port;



	/* ====================================================================== */
  	/** This simple Constructor opens merely instantiates this proxy object
  	 *  and gets the owner of this proxy that wants incoming data.
  	 *
  	 *  @param  parent  The parent to pass data up to.
  	 *
  	 */
  	public GpsProxy(GpsMonitorUI parent, int port, String serial_port){

		this.parent = parent;
		this.port = port;
		this.serial_port = serial_port;

  	} //Constructor




	/* ====================================================================== */
  	/** This method will open a connection to receive data.  This method is used
  	 *  for openning a connection to get real data or simulated data.  However,
  	 *  there may be more than 2 choices for the proxy, necessitating the creation
  	 *  of multiple methods.
  	 *
  	 *  @param  isim   True if connecting to simulator.
  	 *
  	 */
	public void connect(boolean isim){

  		try{

			if(isim){
  				gps = new NmeaSocketGps(parent, port);
			}else{
				gps = new NmeaSerialGps(serial_port, 9600, parent);
			}

		}catch (Exception e){}

		this.start();

	} //method:



	/* ====================================================================== */
  	/** This method is used to close a connection, thus the iPAQ application
  	 *  will no longer receive any data through this proxy.
  	 *
  	 */
  	public void closeConnection(){

		this.stop();

	} //method: closeConnection




	/* ====================================================================== */
  	/** This method is not used by GPS, since this GPS application only receives
  	 *  data, it does not send any.
  	 *
  	 *  @param  data   Data to send either to simulator or some real source.
  	 *
  	 */
	public void sendData(Object data){}




	/* ====================================================================== */
  	/** This loop continues polling for data until it is stopped.
  	 *
  	 */
  	public void run(){

  		while (true){

			try{
				Thread.sleep(5000);

			}catch(Exception e){System.out.println(e);}

			//check for new gps values
			pollGps();

		} //while

  	} //method: run



	/* ====================================================================== */
  	/** This method updates the GUI with the latest values available from the
  	 *  GPS or simulator.
  	 *
  	 */
  	private void pollGps(){

		try{
			//Relay the data received to the UI//Relay the data received to the UI

			//I really only care about coordinates and heading, maybe speed for later
			//update our gps position on the display chart in the UI
			LatOrLongitude lat = gps.getLatitude();
			LatOrLongitude lon = gps.getLongitude();
			Position chart_pos = new Position();
			chart_pos.lat = ((int)lat.getDegrees())*60 + lat.getMinutes();
			chart_pos.lng = (((int)lon.getDegrees())*-60) + lon.getMinutes()+60;

			//Point p = chartdata.transPos(chart_pos);
			//parent.updatePos(p,chartdata.transPos(calcPred(chart_pos)));

			//now set the values in the UI
			parent.setLatitude(lat.toStringDDMM());
			parent.setLongitude(lon.toStringDDMM());
			parent.setHeading(gps.getTrueHeading());


		}catch (NullPointerException e){}

  	} //method: pollGps



} //class: GpsProxy
</xmp>
</html>
