/*
Dexter Travis
Bradley University
2001/2002 Senior Project Botdock
http://pioneer1.bradley.edu
http://cegt201.bradley.edu/projects/proj2002/botdock/
*/

//Java implementation of battery detection for Web Robot
//  http://cegt201.bradley.edu/projects/proj2002/botdock
//written by Dexter Travis
// begun on Jan 23 2002

import java.io.*;
import java.net.*;
import java.lang.*;
import java.util.*;


class BatteryDetector implements Runnable
{
	private static BatteryDetector batdetect = new BatteryDetector();
	public static boolean batterygood=true;
	public static boolean roboton = true;
	//constructor for this class
	BatteryDetector()
	{
		(new Thread(this, "battery detection")).start();
	}
	
	
	//
	public void run()
	{
		try
		{
			while(roboton==true)
			{
					Thread.sleep(60000);
				
					float batvolts = embeddedinterface.getbatvolts();
					
					if(batvolts<11.5)
					{
						ServiceClient.sendtoall("-ERR battery voltage too low");
						ServiceClient.sendtoall("-ERR your connection will be terminated, have a nice day :)");
						ServiceClient.KillAll();
						batterygood = false;			
						if(find.find())
						{
							Thread.sleep(600000);//once charger found then stay there 10 minutes
							break;
						}
						else if (find.count < 5)//could not find so try again
						{
							break;
						}
						else //if cannot find the station after 5 attempts then shutdown
						{
							log.log("could not find the dock after " +find.count+ "tries");
							botdock.terminate();
						}
						
						
					}
					else if(batvolts<13.5)
					{
						//check if charging
						//if charging continue to do so
						//if not charging allow users to connect.
					}
					else
					{
						//check if charging
						// if charging pull out of charger.
						//if not charging then send error and shutdown gracefully
					}
					
					log.log("battery voltage is " +batvolts);
					ServiceClient.sendtoall("Battery voltage is " +batvolts);
				
			}
		}
		catch(Exception e)
		{
			log.log("-E error in battery voltage detection " +e);
		}
	
	//end of method run	
	}
	
	public static void stop()
	{
		roboton=false;
	}
	
//end of class BatteryDetect
}