aboutsummaryrefslogtreecommitdiff
path: root/src/net/cbaines/suma/DataManager.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/net/cbaines/suma/DataManager.java')
-rw-r--r--src/net/cbaines/suma/DataManager.java72
1 files changed, 70 insertions, 2 deletions
diff --git a/src/net/cbaines/suma/DataManager.java b/src/net/cbaines/suma/DataManager.java
index 84731ff..634415d 100644
--- a/src/net/cbaines/suma/DataManager.java
+++ b/src/net/cbaines/suma/DataManager.java
@@ -24,6 +24,7 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.sql.SQLException;
+import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
@@ -550,18 +551,19 @@ public class DataManager {
if (bus == null) {
bus = new Bus(busID, route, dir);
bus.destination = destStop;
+ busDao.create(bus);
} else {
bus.destination = destStop;
bus.route = route;
bus.direction = dir;
+ busDao.update(bus);
}
} else {
bus = new Bus(null, route, dir);
+ busDao.create(bus);
}
- busDao.update(bus);
-
stop = new Stop(bus, busStop, calender.getTime(), now, live);
return stop;
@@ -636,6 +638,72 @@ public class DataManager {
return timetable;
}
+ public static Timetable getTimetable(Context context, Bus bus, BusStop startStop, int num) throws SQLException, ClientProtocolException, IOException,
+ JSONException {
+
+ if (helper == null)
+ helper = OpenHelperManager.getHelper(context, DatabaseHelper.class);
+ if (busRouteDao == null)
+ busRouteDao = helper.getBusRouteDao();
+ if (busStopDao == null)
+ busStopDao = helper.getBusStopDao();
+
+ Timetable timetable = new Timetable();
+
+ List<BusStop> busStops = new ArrayList<BusStop>(num);
+ busStops.add(startStop);
+
+ BusRoute route = bus.route;
+
+ for (int i = 0; i < num; i++) {
+ BusStop nextStop = route.moveInRoute(context, busStops.get(i), bus.direction, 1);
+
+ if (nextStop != null) {
+ busStops.add(nextStop);
+ } else {
+ Log.e(TAG, "nextStop is null");
+ }
+ }
+
+ for (BusStop busStop : busStops) {
+
+ String file = getFileFromServer(busStopUrl + busStop + ".json");
+
+ JSONObject data = new JSONObject(file);
+ JSONArray stopsArray = data.getJSONArray("stops");
+
+ HashSet<BusRoute> busRoutes = new HashSet<BusRoute>();
+ busRoutes.add(bus.route);
+
+ Log.i(TAG, "Number of entries " + data.length());
+
+ Log.i(TAG, "Stops: " + data.getJSONArray("stops"));
+
+ for (int stopNum = 0; stopNum < stopsArray.length(); stopNum++) {
+ JSONObject stopObj = stopsArray.getJSONObject(stopNum);
+
+ if (stopObj.getString("vehicle").equals(bus.id)) {
+
+ Stop stop = getStop(context, stopObj, busRoutes, busStop);
+
+ if (stop == null) {
+ Log.w(TAG, "Null stop, skiping");
+ continue;
+ }
+
+ Log.i(TAG, "Found stop for a unidentified " + stop.bus.toString() + " at " + stop.busStop.id + " at " + stop.arivalTime);
+
+ timetable.add(stop);
+
+ }
+ }
+ }
+
+ timetable.fetchTime = new Date(System.currentTimeMillis());
+
+ return timetable;
+ }
+
static PathOverlay getRoutePath(InputStream routeResource, int colour, ResourceProxy resProxy) {
PathOverlay data = null;