/*
 *  Player Java Client 2 - Javaclient2Test.java
 *  Copyright (C) 2006 Radu Bogdan Rusu
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * $Id: Javaclient2Test.java,v 1.0 2006/03/01 11:48:25 veedee Exp $
 *
 */
import java.text.FieldPosition;
import java.text.NumberFormat;

import javaclient2.*;
import javaclient2.structures.*;
import javaclient2.structures.gps.*;

/**
 * Basic tests for a variety of interfaces. Just comment the ones that you don't
 * want to activate.
 * @author Radu Bogdan Rusu
 * @version
 * <ul>
 *      <li>v2.0 - Player 2.0 supported
 * </ul>
 */
public class Javaclient2Test {
	
	static NumberFormat fmt = NumberFormat.getInstance ();
	
	public static void main(String[] args) throws PlayerException {
		//System.setProperty ("PlayerClient.debug", "true");
		
		PlayerClient        robot = null;
		GPSInterface        gps  = null;
		
		try {
			robot = new PlayerClient                 ("localhost", 6665);
			gps    = robot.requestInterfaceGPS       (0, PlayerConstants.PLAYER_OPEN_MODE);
		} catch (PlayerException e) {
			System.err.println ("Javaclient test: > Error connecting to Player: ");
			System.err.println ("    [ " + e.toString() + " ]");
			System.exit (1);
		}
		
		int i = 0;
		
		//try { Thread.sleep (2000); } catch (Exception e) { e.printStackTrace(); }
		
		robot.runThreaded (-1, -1);
//		robot.setNotThreaded ();
		
		while (true)
		{
			//robot.requestData ();
//			robot.readAll ();
			
			// --[ Test RFID
			if (gps.isDataReady ()) {
        PlayerGpsData pgd = gps.getData(); 
        System.out.println(" GPS: LAT " + pgd.getLatitude() + " LON " + pgd.getLongitude()); 
      }
			// --]
			
			try { Thread.sleep (100); } catch (Exception e) { e.printStackTrace(); }
			i++;
			
			if (i == -1)	// modify -1 to a number if you wish to terminate
			        	// after X iterations
				break;
		}
		robot.close ();
	}
	
	static String byteArrayToHexString (byte in[]) {
		byte ch = 0x00;
		int i = 0;
		if (in == null || in.length <= 0)
			return null;

		String pseudo[] = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
				"A", "B", "C", "D", "E", "F" };

		StringBuffer out = new StringBuffer (in.length * 2);
		while (i < in.length) {
			ch = (byte) (in[i] & 0xF0); // Strip off high nibble
			ch = (byte) (ch >>> 4);
			// shift the bits down
			ch = (byte) (ch & 0x0F);
			// must do this is high order bit is on!

			out.append (pseudo[(int) ch]); // convert the nibble to a char
			ch = (byte) (in[i] & 0x0F);    // Strip off low nibble
			out.append (pseudo[(int) ch]); // convert the nibble to a char
			i++;
		}
		String rslt = new String (out);
		return rslt;
	}    
	
}
