package com.team6;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONObject;
import android.util.Log;
import com.google.android.maps.GeoPoint;
import com.team6.overlay.BusStopOverlayItem;
import com.team6.overlay.StopScheduleItem;
/**
* @author Ian Munro
*
*/
public class DbInterface {
private final static String mHostURL = "http://24.177.13.246:8086/uconn-map-project/";
private final static String TAG = "DbInterface";
public static ArrayList<StopScheduleItem> getSchedule(int stopid){
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("stopid",String.valueOf(stopid)));
String stopname = getStopName(stopid);
ArrayList<StopScheduleItem> result = new ArrayList<StopScheduleItem>();
try{
JSONArray jArray = new JSONArray(httpQuery(nameValuePairs,"getSchedule.php"));
for(int i=0;i<jArray.length();i++){
JSONObject json_data = jArray.getJSONObject(i);
String stoptime = timeFix(json_data.getString("stoptime"));
String busline = json_data.getString("linename");
int busid = json_data.getInt("busid");
result.add(new StopScheduleItem(stopid, stopname, stoptime, busline, busid));
}
}catch(Exception e){
Log.e(TAG, "Error parsing data " +e.toString());
}
return result;
}
private static String timeFix(String rawtime){
String time=rawtime.substring(0, 5);
int hour=Integer.valueOf(time.substring(0, 2));
if(hour>12){
time="" + (hour-12) + time.substring(2, 5);
}
if(hour>=12&&hour<24){
time = time + " PM";
}else{
time = time + " AM";
}
return time;
}
private static String timeUnFix(String ftime){
String time = ftime.substring(0, 5);
char timeofday = ftime.toCharArray()[6];
int hour = Integer.valueOf(time.substring(0, 2));
if(timeofday == 'P'){
time = "" + (hour + 12) + time.substring(2,5);
}
time = time + ":00";
return time;
}
public static GeoPoint getGeopoint(int stopid){
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("stopid",String.valueOf(stopid)));
GeoPoint gp = null;
try{
JSONArray jArray = new JSONArray(httpQuery(nameValuePairs,"getGeoPoint.php"));
for(int i=0;i<jArray.length();i++){
JSONObject json_data = jArray.getJSONObject(i);
int lat = json_data.getInt("lat");
int lon = json_data.getInt("lon");
gp = new GeoPoint(lat,lon);
}
}catch(Exception e){
Log.e(TAG, "Error parsing data " +e.toString());
}
return gp;
}
public static String getStopName(int stopid){
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("stopid",String.valueOf(stopid)));
String stopname = null;
try{
JSONArray jArray = new JSONArray(httpQuery(nameValuePairs,"getStopName.php"));
for(int i=0;i<jArray.length();i++){
JSONObject json_data = jArray.getJSONObject(i);
stopname = json_data.getString("stopname");
}
}catch(Exception e){
Log.e(TAG, "Error parsing data " +e.toString());
}
return stopname;
}
/**
* @deprecated
*/
public static ArrayList<BusStopOverlayItem> getBusRouteBy_busid(int busid){
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("busid",String.valueOf(busid)));
ArrayList<BusStopOverlayItem> result = new ArrayList<BusStopOverlayItem>();
try{
JSONArray jArray = new JSONArray(httpQuery(nameValuePairs,"getBusRouteBy_busid.php"));
for(int i=0;i<jArray.length();i++){
JSONObject json_data = jArray.getJSONObject(i);
int stopid = json_data.getInt("stopid");
String stopname = json_data.getString("stopname");
GeoPoint gp = new GeoPoint(json_data.getInt("lat"),json_data.getInt("lon"));
result.add(new BusStopOverlayItem(stopid, stopname, gp));
}
}catch(Exception e){
Log.e(TAG, "Error parsing data " +e.toString());
}
return result;
}
/**
* @deprecated
*/
public static ArrayList<BusStopOverlayItem> getBusRouteBy_lineid(int lineid){
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("lineid",String.valueOf(lineid)));
ArrayList<BusStopOverlayItem> result = new ArrayList<BusStopOverlayItem>();
try{
JSONArray jArray = new JSONArray(httpQuery(nameValuePairs,"getBusRouteBy_lineid.php"));
for(int i=0;i<jArray.length();i++){
JSONObject json_data = jArray.getJSONObject(i);
int stopid = json_data.getInt("stopid");
String stopname = json_data.getString("stopname");
GeoPoint gp = new GeoPoint(json_data.getInt("lat"),json_data.getInt("lon"));
result.add(new BusStopOverlayItem(stopid, stopname, gp));
}
}catch(Exception e){
Log.e(TAG, "Error parsing data " +e.toString());
}
return result;
}
public static ArrayList<BusStopOverlayItem> getStopsFrom(int stopid){
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("stopid",String.valueOf(stopid)));
ArrayList<BusStopOverlayItem> result = new ArrayList<BusStopOverlayItem>();
try{
JSONArray jArray = new JSONArray(httpQuery(nameValuePairs,"getStopsFrom.php"));
for(int i=0;i<jArray.length();i++){
JSONObject json_data = jArray.getJSONObject(i);
GeoPoint gp = new GeoPoint(json_data.getInt("lat"),json_data.getInt("lon"));
result.add(new BusStopOverlayItem(json_data.getInt("stopid"),json_data.getString("stopname"),gp));
}
}catch(Exception e){
Log.e(TAG, "Error parsing data " +e.toString());
}
return result;
}
private static String httpQuery(ArrayList<NameValuePair> namevaluepairs,String php){
InputStream is = null;
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(mHostURL +php);
httppost.setEntity(new UrlEncodedFormEntity(namevaluepairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
Log.e(TAG, "Error in http connection " +e.toString());
}
String result = null;
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while((line = reader.readLine()) != null){
sb.append(line + "\n");
}
is.close();
result = sb.toString();
}catch(Exception e){
Log.e(TAG, "Error in converting result " +e.toString());
}
return result;
}
public static ArrayList<BusStopOverlayItem> getStops() {
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
ArrayList<BusStopOverlayItem> stops = new ArrayList<BusStopOverlayItem>();
try{
JSONArray jArray = new JSONArray(httpQuery(nameValuePairs,"getStops.php"));
for(int i=0;i<jArray.length();i++){
JSONObject json_data = jArray.getJSONObject(i);
stops.add(new BusStopOverlayItem(json_data.getInt("stopid"),
json_data.getString("stopname").trim(),
new GeoPoint(json_data.getInt("lat"),
json_data.getInt("lon"))));
}
}catch(Exception e){
Log.e(TAG, "Error parsing data " +e.toString());
}
return stops;
}
public static BusStopOverlayItem getStopByGP(GeoPoint gp){
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("lat", String.valueOf(gp.getLatitudeE6())));
nameValuePairs.add(new BasicNameValuePair("lon", String.valueOf(gp.getLongitudeE6())));
BusStopOverlayItem result = null;
try{
JSONArray jArray = new JSONArray(httpQuery(nameValuePairs,"getStopByGP.php"));
for(int i=0;i<jArray.length();i++){
JSONObject json_data = jArray.getJSONObject(i);
int stopid = json_data.getInt("stopid");
String stopname = json_data.getString("stopname");
result = new BusStopOverlayItem(stopid,stopname,gp);
}
}catch(Exception e){
Log.e(TAG, "Error parsing data " +e.toString());
}
return result;
}
public static boolean shareRoute(BusStopOverlayItem origin, BusStopOverlayItem destination){
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("origin",Integer.toString(origin.getStopID())));
nameValuePairs.add(new BasicNameValuePair("destination",Integer.toString(destination.getStopID())));
boolean retVal = false;
try{
JSONArray jArray = new JSONArray(httpQuery(nameValuePairs,"shareRoute.php"));
for(int i=0;i<jArray.length();i++){
JSONObject json_data = jArray.getJSONObject(i);
retVal = json_data.getBoolean("bool");
}
}catch(Exception e){
Log.e(TAG, "Error parsing data " +e.toString());
}
return retVal;
}
public static boolean stopsAt(int busid, BusStopOverlayItem stop){
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("stopid",Integer.toString(stop.getStopID())));
nameValuePairs.add(new BasicNameValuePair("busid",Integer.toString(busid)));
boolean retVal = false;
try{
JSONArray jArray = new JSONArray(httpQuery(nameValuePairs,"stopsAt.php"));
for(int i=0;i<jArray.length();i++){
JSONObject json_data = jArray.getJSONObject(i);
retVal = json_data.getBoolean("bool");
}
}catch(Exception e){
Log.e(TAG, "Error parsing data " +e.toString());
}
return retVal;
}
public static ArrayList<BusStopOverlayItem> stopsNear(GeoPoint gp){
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("lat",Integer.toString(gp.getLatitudeE6())));
nameValuePairs.add(new BasicNameValuePair("lon",Integer.toString(gp.getLongitudeE6())));
ArrayList<BusStopOverlayItem> nearbyStops = new ArrayList<BusStopOverlayItem>();
try{
JSONArray jArray = new JSONArray(httpQuery(nameValuePairs,"stopsNear.php"));
for(int i=0;i<jArray.length();i++){
JSONObject json_data = jArray.getJSONObject(i);
nearbyStops.add(new BusStopOverlayItem(json_data.getInt("stopid"),
json_data.getString("stopname").trim(),
new GeoPoint(json_data.getInt("lat"),
json_data.getInt("lon"))));
}
}catch(Exception e){
Log.e(TAG, "Error parsing data " +e.toString());
}
return nearbyStops;
}
public static ArrayList<BusStopOverlayItem> stopsNear(BusStopOverlayItem b){
return stopsNear(b.getGeoPoint());
}
public static ArrayList<RouteTouple> getRoute(int lineid){
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("lineid",Integer.toString(lineid)));
ArrayList<RouteTouple> route = new ArrayList<RouteTouple>();
try{
JSONArray jArray = new JSONArray(httpQuery(nameValuePairs,"getRoute.php"));
for(int i=0;i<jArray.length();i++){
JSONObject json_data = jArray.getJSONObject(i);
route.add(new RouteTouple(new BusStopOverlayItem(json_data.getInt("stopid"),
json_data.getString("stopname").trim(),
new GeoPoint(json_data.getInt("lat"),
json_data.getInt("lon"))),
json_data.getInt("timetonext")));
}
}catch(Exception e){
Log.e(TAG, "Error parsing data " +e.toString());
}
return route;
}
public static ArrayList<StopScheduleItem> nextArrival(BusStopOverlayItem b){
return nextArrival(b,0);
}
/**
* @param offset The delay in minutes from now until desired arrival time.
*/
public static ArrayList<StopScheduleItem> nextArrival(BusStopOverlayItem b,int offset){
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("stopid",Integer.toString(b.getStopID())));
nameValuePairs.add(new BasicNameValuePair("offset",Integer.toString(offset)));
ArrayList<StopScheduleItem> arrivals = new ArrayList<StopScheduleItem>();
try{
JSONArray jArray = new JSONArray(httpQuery(nameValuePairs,"nextArrival.php"));
boolean blue = false, red = false, green = false, yellow = false;
for(int i=0;i<jArray.length();i++){
JSONObject json_data = jArray.getJSONObject(i);
switch(json_data.getInt("lineid")){
case 1:
if(!blue){
blue = true;
arrivals.add(new StopScheduleItem(b.getStopID(),
b.getStopName(),
timeFix(json_data.getString("stoptime")),
json_data.getString("linename"),
json_data.getInt("busid")));
}
break;
case 2:
if(!red){
red = true;
arrivals.add(new StopScheduleItem(b.getStopID(),
b.getStopName(),
timeFix(json_data.getString("stoptime")),
json_data.getString("linename"),
json_data.getInt("busid")));
}
break;
case 3:
if(!green){
green = true;
arrivals.add(new StopScheduleItem(b.getStopID(),
b.getStopName(),
timeFix(json_data.getString("stoptime")),
json_data.getString("linename"),
json_data.getInt("busid")));
}
break;
case 4:
if(!yellow){
yellow = true;
arrivals.add(new StopScheduleItem(b.getStopID(),
b.getStopName(),
timeFix(json_data.getString("stoptime")),
json_data.getString("linename"),
json_data.getInt("busid")));
}
break;
}
}
}catch(Exception e){
Log.e(TAG, "Error parsing data " +e.toString());
}
return arrivals;
}
public static StopScheduleItem lastStopBefore(int busid,int stopid,String time){
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("busid",Integer.toString(busid)));
nameValuePairs.add(new BasicNameValuePair("stopid",Integer.toString(stopid)));
nameValuePairs.add(new BasicNameValuePair("time",timeUnFix(time)));
StopScheduleItem retVal = null;
try{
JSONArray jArray = new JSONArray(httpQuery(nameValuePairs,"lastStopBefore.php"));
for(int i=0;i<jArray.length();i++){
JSONObject json_data = jArray.getJSONObject(i);
String stopname = json_data.getString("stopname");
String stoptime = timeFix(json_data.getString("stoptime"));
String linename = json_data.getString("linename");
retVal = new StopScheduleItem(stopid,stopname,stoptime,linename,busid);
}
}catch(Exception e){
Log.e(TAG, "Error parsing data " +e.toString());
}
return retVal;
}
}
|