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.java636
1 files changed, 435 insertions, 201 deletions
diff --git a/src/net/cbaines/suma/DataManager.java b/src/net/cbaines/suma/DataManager.java
index d4d0968..c6a3a36 100644
--- a/src/net/cbaines/suma/DataManager.java
+++ b/src/net/cbaines/suma/DataManager.java
@@ -24,11 +24,15 @@ 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;
import java.util.HashMap;
+import java.util.HashSet;
import java.util.Iterator;
+import java.util.List;
+import java.util.Set;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
@@ -62,12 +66,12 @@ import com.j256.ormlite.table.TableUtils;
public class DataManager {
- final static String TAG = "DataManager";
+ private final static String TAG = "DataManager";
- final static String busStopUrl = "http://data.southampton.ac.uk/bus-stop/";
+ private final static String busStopUrl = "http://data.southampton.ac.uk/bus-stop/";
private static DatabaseHelper helper;
- private static Dao<BusRoute, Integer> busRoutes;
+ private static Dao<BusRoute, Integer> busRouteDao;
private static Dao<Bus, Integer> busDao;
private static Dao<BusStop, String> busStopDao;
@@ -77,7 +81,7 @@ public class DataManager {
TableUtils.clearTable(helper.getConnectionSource(), Building.class);
- Log.i(TAG, "Loading buildings from csv");
+ Log.i(TAG, "Begining loading buildings from csv");
HashMap<String, GeoPoint> buildingPoints = new HashMap<String, GeoPoint>();
@@ -92,32 +96,17 @@ public class DataManager {
// Log.i(TAG, "Data: " + strLine);
String[] dataBits = strLine.split(",");
GeoPoint point = Util.csLatLongToGeoPoint(dataBits[2], dataBits[1]);
- // Log.i(TAG, "Creating building with id " + dataBits[0] + " and " + point.getLatitudeE6() + " " + point.getLongitudeE6());
+ // Log.i(TAG, "Creating building with id " + dataBits[0] + " and " + point.getLatitudeE6() + " " +
+ // point.getLongitudeE6());
buildingPoints.put(dataBits[0], point);
}
bufferedReader.close();
} catch (IOException e) {
- // TODO Auto-generated catch block
e.printStackTrace();
}
- Log.i(TAG, "Number of building points " + buildingPoints.size());
-
- /*
- * inputStream = context.getResources().openRawResource(R.raw.buildings_shapes); bufferedReader = new BufferedReader(new
- * InputStreamReader(inputStream));
- *
- * try { String def = bufferedReader.readLine(); // Log.i(TAG, "Reading the definition " + def);
- *
- * while ((strLine = bufferedReader.readLine()) != null) { // Log.i(TAG, "Data: " + strLine); String[] dataBits = strLine.split(","); Polygon poly =
- * Util.csPolygonToPolygon(strLine.split("\"")[1]); // Log.i(TAG, "Creating building with id " + dataBits[0] + " and " + poly);
- * buildingPolys.put(dataBits[0], poly); }
- *
- * bufferedReader.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }
- *
- * Log.i(TAG, "Number of polys points " + buildingPolys.size());
- */
+ // Log.i(TAG, "Number of building points " + buildingPoints.size());
inputStream = context.getAssets().open("building_estates.csv");
bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
@@ -139,16 +128,19 @@ public class DataManager {
continue;
}
- Building bdg = new Building(dataBits[1], buildingPoints.get(dataBits[1]), dataBits[3].equals("R"), quoteBits[0]);
+ Building bdg = new Building(dataBits[1], buildingPoints.get(dataBits[1]), dataBits[3].equals("R"),
+ quoteBits[0]);
/*
* Polygon poly = buildingPolys.get(dataBits[1]);
*
- * if (poly != null) { bdg.outline = poly; // Log.i(TAG, "Adding building " + key + " " + bdg.point.getLatitudeE6() + " " +
- * bdg.point.getLongitudeE6() + " " + poly); } else { // Log.i(TAG, "Adding building " + key + " " + bdg.point.getLatitudeE6() + " " +
+ * if (poly != null) { bdg.outline = poly; // Log.i(TAG, "Adding building " + key + " " +
+ * bdg.point.getLatitudeE6() + " " + bdg.point.getLongitudeE6() + " " + poly); } else { //
+ * Log.i(TAG, "Adding building " + key + " " + bdg.point.getLatitudeE6() + " " +
* bdg.point.getLongitudeE6()); }
*/
- // Log.i(TAG, "Creating building " + bdg.id + " " + bdg.name + " " + bdg.point + " " + bdg.residential + " " + bdg.outline);
+ // Log.i(TAG, "Creating building " + bdg.id + " " + bdg.name + " " + bdg.point + " " +
+ // bdg.residential + " " + bdg.outline);
buildingDao.create(bdg);
@@ -161,16 +153,19 @@ public class DataManager {
continue;
}
- Building bdg = new Building(dataBits[1], buildingPoints.get(dataBits[1]), dataBits[3].equals("R"), dataBits[0]);
+ Building bdg = new Building(dataBits[1], buildingPoints.get(dataBits[1]), dataBits[3].equals("R"),
+ dataBits[0]);
/*
* Polygon poly = buildingPolys.get(dataBits[1]);
*
- * if (poly != null) { bdg.outline = poly; // Log.i(TAG, "Adding building " + key + " " + bdg.point.getLatitudeE6() + " " +
- * bdg.point.getLongitudeE6() + " " + poly); } else { // Log.i(TAG, "Adding building " + key + " " + bdg.point.getLatitudeE6() + " " +
+ * if (poly != null) { bdg.outline = poly; // Log.i(TAG, "Adding building " + key + " " +
+ * bdg.point.getLatitudeE6() + " " + bdg.point.getLongitudeE6() + " " + poly); } else { //
+ * Log.i(TAG, "Adding building " + key + " " + bdg.point.getLatitudeE6() + " " +
* bdg.point.getLongitudeE6()); }
*/
- // Log.i(TAG, "Creating building " + bdg.id + " " + bdg.name + " " + bdg.point + " " + bdg.residential + " " + bdg.outline);
+ // Log.i(TAG, "Creating building " + bdg.id + " " + bdg.name + " " + bdg.point + " " +
+ // bdg.residential + " " + bdg.outline);
buildingDao.create(bdg);
@@ -188,8 +183,9 @@ public class DataManager {
*
* Building bdg = new Building(key, buildingPoints.get(key), false); Polygon poly = buildingPolys.get(key);
*
- * if (poly != null) { bdg.outline = poly; // Log.i(TAG, "Adding building " + key + " " + bdg.point.getLatitudeE6() + " " + bdg.point.getLongitudeE6() +
- * " " + poly); } else { // Log.i(TAG, "Adding building " + key + " " + bdg.point.getLatitudeE6() + " " + bdg.point.getLongitudeE6()); }
+ * if (poly != null) { bdg.outline = poly; // Log.i(TAG, "Adding building " + key + " " +
+ * bdg.point.getLatitudeE6() + " " + bdg.point.getLongitudeE6() + " " + poly); } else { // Log.i(TAG,
+ * "Adding building " + key + " " + bdg.point.getLatitudeE6() + " " + bdg.point.getLongitudeE6()); }
*
* buildingDao.create(bdg); }
*/
@@ -225,19 +221,21 @@ public class DataManager {
String[] quBitsLng = dataBits[4].substring(1, dataBits[4].length() - 1).split(" ");
// Log.i(TAG, "Whole " + dataBits[3] + " First bit " + quBitsLat[0] + " last bit " + quBitsLat[1]);
- double lat = Double.valueOf(quBitsLat[0]) + Double.valueOf(quBitsLat[1].substring(0, quBitsLat[1].length() - 1)) / 60d; // TODO Much hackage
+ double lat = Double.valueOf(quBitsLat[0])
+ + Double.valueOf(quBitsLat[1].substring(0, quBitsLat[1].length() - 1)) / 60d;
// Log.i(TAG, "Whole " + dataBits[4] + " First bit " + quBitsLng[0] + " last bit " + quBitsLng[1]);
- double lng = Double.valueOf(quBitsLng[0]) + Double.valueOf(quBitsLng[1].substring(0, quBitsLng[1].length() - 1)) / 60d; // TODO Much hackage
+ double lng = Double.valueOf(quBitsLng[0])
+ + Double.valueOf(quBitsLng[1].substring(0, quBitsLng[1].length() - 1)) / 60d;
GeoPoint point = new GeoPoint((int) (lat * 1e6), (int) (lng * -1e6));
// Log.i(TAG, "Lat " + point.getLatitudeE6() + " lng " + point.getLongitudeE6());
- busStopDao.create(new BusStop(dataBits[0].replace("\"", ""), dataBits[1].replace("\"", ""), dataBits[2].replace("\"", ""), point));
+ busStopDao.create(new BusStop(dataBits[0].replace("\"", ""), dataBits[1].replace("\"", ""), dataBits[2]
+ .replace("\"", ""), point));
}
bufferedReader.close();
} catch (IOException e) {
- // TODO Auto-generated catch block
Log.e(TAG, "Line: " + strLine);
e.printStackTrace();
}
@@ -256,11 +254,28 @@ public class DataManager {
String[] dataBits = strLine.split(",");
BusRoute route;
- if (dataBits.length > 3) {
- route = new BusRoute(Integer.parseInt(dataBits[0]), dataBits[1], dataBits[2].replace("\"", ""), dataBits[3], dataBits[4]);
+
+ boolean uniLink;
+ int id = Integer.parseInt(dataBits[0]);
+ if (id == 326 || id == 468 || id == 327 || id == 329 || id == 354) {
+ uniLink = true;
} else {
- route = new BusRoute(Integer.parseInt(dataBits[0]), dataBits[1], dataBits[2].replace("\"", ""));
+ uniLink = false;
}
+
+ route = new BusRoute(id, dataBits[1], dataBits[2].replace("\"", ""), uniLink);
+
+ if (id == 326) { // U1
+ route.forwardDirection = "A";
+ route.reverseDirection = "C";
+ } else if (id == 329) { // U2
+ route.forwardDirection = "B";
+ route.reverseDirection = "C";
+ } else if (id == 327) { // U6
+ route.forwardDirection = "H";
+ route.reverseDirection = "C";
+ }
+
// Log.i(TAG, "Loaded route " + route.id + " " + route.code + " " + route.label);
busRouteDao.create(route);
@@ -268,7 +283,6 @@ public class DataManager {
bufferedReader.close();
} catch (IOException e) {
- // TODO Auto-generated catch block
Log.e(TAG, "Line: " + strLine);
e.printStackTrace();
}
@@ -286,16 +300,16 @@ public class DataManager {
// Log.i(TAG, "Data: " + strLine);
String[] dataBits = strLine.split(",");
- BusStop stop = busStopDao.queryForId(dataBits[2]);
- if (stop != null) {
+ BusStop busStop = busStopDao.queryForId(dataBits[2]);
+ if (busStop != null) {
// Log.i(TAG, "Found stop " + stop.id);
} else {
Log.w(TAG, "No stop found for " + dataBits[2]);
continue;
}
- BusRoute route = busRouteDao.queryForId(Integer.parseInt(dataBits[0]));
- if (route != null) {
+ BusRoute busRoute = busRouteDao.queryForId(Integer.parseInt(dataBits[0]));
+ if (busRoute != null) {
// Log.i(TAG, "Found route " + route.id);
} else {
Log.w(TAG, "No route found for " + dataBits[0]);
@@ -303,70 +317,64 @@ public class DataManager {
}
int sequence = Integer.parseInt(dataBits[1]);
- Log.i(TAG, "Creating RouteStop " + stop.id + " " + route.code + " " + sequence);
-
- routeStopsDao.create(new RouteStops(stop, route, sequence));
-
- if (route.id == 326) { // U1
- stop.routes = (byte) (stop.routes | 1);
- } else if (route.id == 468) { // U1N
- stop.routes = (byte) (stop.routes | (1 << 1));
- } else if (route.id == 329) { // U2
- stop.routes = (byte) (stop.routes | (1 << 2));
- } else if (route.id == 327) { // U6
- stop.routes = (byte) (stop.routes | (1 << 3));
- } else if (route.id == 354) { // U9
- stop.routes = (byte) (stop.routes | (1 << 4));
+ Log.i(TAG, "Creating RouteStop " + busStop.id + " " + busRoute.code + " " + sequence);
+
+ routeStopsDao.create(new RouteStops(busStop, busRoute, sequence));
+
+ if (busRoute.id == 326) { // U1
+ busStop.routes = (byte) (busStop.routes | 1);
+ busStop.uniLink = true;
+ } else if (busRoute.id == 468) { // U1N
+ busStop.routes = (byte) (busStop.routes | (1 << 1));
+ busStop.uniLink = true;
+ } else if (busRoute.id == 329) { // U2
+ busStop.routes = (byte) (busStop.routes | (1 << 2));
+ busStop.uniLink = true;
+ } else if (busRoute.id == 327) { // U6
+ busStop.routes = (byte) (busStop.routes | (1 << 3));
+ busStop.uniLink = true;
+ } else if (busRoute.id == 354) { // U9
+ busStop.routes = (byte) (busStop.routes | (1 << 4));
+ busStop.uniLink = true;
+ } else {
+ busStop.routes = 0;
}
- Log.v(TAG, "Stop routes " + stop.routes);
- busStopDao.update(stop);
+ // Log.v(TAG, "Stop routes " + busStop.routes);
+ busStopDao.update(busStop);
}
bufferedReader.close();
} catch (IOException e) {
- // TODO Auto-generated catch block
Log.e(TAG, "Line: " + strLine);
e.printStackTrace();
}
- long sizeBeforeRemoval = busStopDao.countOf();
-
- // Removing busstops not used by unilink busses
- for (Iterator<BusStop> busStopIter = busStopDao.iterator(); busStopIter.hasNext();) {
- BusStop stop = busStopIter.next();
- // Log.i(TAG, "Looking at stop " + stop.id);
-
- /*
- * QueryBuilder<RouteStops, Integer> routeStopsQueryBuilder = routeStopsDao.queryBuilder(); routeStopsQueryBuilder.where().eq(columnName, value)
- *
- * DeleteBuilder<BusStop, String> deleteBuilder = busStopDao.deleteBuilder(); // only delete the rows where password is null
- * deleteBuilder.where().in(RouteStops.STOP_ID_FIELD_NAME, objects) accountDao.delete(deleteBuilder.prepare());
- */
-
- QueryBuilder<RouteStops, Integer> routeStopsQueryBuilder = routeStopsDao.queryBuilder();
- routeStopsQueryBuilder.setCountOf(true);
- routeStopsQueryBuilder.where().eq(RouteStops.STOP_ID_FIELD_NAME, stop);
-
- PreparedQuery<RouteStops> routeStopsPreparedQuery = routeStopsQueryBuilder.prepare();
- long num = routeStopsDao.countOf(routeStopsPreparedQuery);
- // long num = routeStopsDao.query(routeStopsPreparedQuery).size();
- // Log.i(TAG, "Number is " + num);
- if (num == 0) {
- // Log.i(TAG, "Removing " + stop.id);
- stop.uniLink = false;
- if (onlyUniLink) {
- busStopIter.remove();
- }
- } else {
- stop.uniLink = true;
- }
- }
-
- long sizeAfterRemoval = busStopDao.countOf();
-
- Log.i(TAG, "Removed " + (sizeBeforeRemoval - sizeAfterRemoval) + " stops (from " + sizeBeforeRemoval + ") now have " + sizeAfterRemoval);
+ /*
+ * for (Iterator<BusStop> busStopIter = busStopDao.iterator(); busStopIter.hasNext();) { BusStop stop =
+ * busStopIter.next(); // Log.i(TAG, "Looking at stop " + stop.id);
+ *
+ *
+ * QueryBuilder<RouteStops, Integer> routeStopsQueryBuilder = routeStopsDao.queryBuilder();
+ * routeStopsQueryBuilder.where().eq(columnName, value)
+ *
+ * DeleteBuilder<BusStop, String> deleteBuilder = busStopDao.deleteBuilder(); // only delete the rows where
+ * password is null deleteBuilder.where().in(RouteStops.STOP_ID_FIELD_NAME, objects)
+ * accountDao.delete(deleteBuilder.prepare());
+ *
+ *
+ * QueryBuilder<RouteStops, Integer> routeStopsQueryBuilder = routeStopsDao.queryBuilder();
+ * routeStopsQueryBuilder.setCountOf(true); routeStopsQueryBuilder.where().eq(RouteStops.STOP_ID_FIELD_NAME,
+ * stop);
+ *
+ * PreparedQuery<RouteStops> routeStopsPreparedQuery = routeStopsQueryBuilder.prepare(); List<RouteStops>
+ * routeStops = routeStopsDao.query(routeStopsPreparedQuery); // long num =
+ * routeStopsDao.query(routeStopsPreparedQuery).size(); // Log.i(TAG, "Number is " + num);
+ *
+ * stop.uniLink = false; for (RouteStops routeStop : routeStops) { if (routeStop.busRoute.uniLink) {
+ * stop.uniLink = true; } } busStopDao.update(stop); }
+ */
Log.i(TAG, "Finished loading bus data");
}
@@ -415,144 +423,180 @@ public class DataManager {
Log.i(TAG, "Loaded sites from csv");
}
- private static Stop getStop(Context context, JSONObject stopObj, BusStop busStop) throws SQLException {
+ private static Stop getStop(Context context, JSONObject stopObj, Set<BusRoute> routes, BusStop busStop)
+ throws SQLException, JSONException {
if (helper == null)
helper = OpenHelperManager.getHelper(context, DatabaseHelper.class);
- if (busRoutes == null)
- busRoutes = helper.getBusRouteDao();
+ if (busRouteDao == null)
+ busRouteDao = helper.getBusRouteDao();
+ Dao<RouteStops, Integer> routeStopsDao = null;
+ if (routeStopsDao == null)
+ routeStopsDao = helper.getRouteStopsDao();
if (busDao == null)
busDao = helper.getBusDao();
if (busStopDao == null)
busStopDao = helper.getBusStopDao();
- try {
- String time = stopObj.getString("time");
+ String time = stopObj.getString("time");
- GregorianCalendar calender = new GregorianCalendar();
- if (!time.equals("Due")) {
+ GregorianCalendar calender = new GregorianCalendar();
+ boolean live = true;
+ if (!time.equals("Due")) {
- Log.v(TAG, "Time: " + time + " current time " + calender.getTime());
+ // Log.v(TAG, "Time: " + time + " current time " + calender.getTime());
- if (time.contains(":")) {
- String[] minAndHour = time.split(":");
- calender.set(Calendar.HOUR_OF_DAY, Integer.parseInt(minAndHour[0]));
- calender.set(Calendar.MINUTE, Integer.parseInt(minAndHour[1]));
- } else {
- // Log.i(TAG, "Parsing " + time.substring(0, time.length() - 1) + " for min");
- calender.add(Calendar.MINUTE, Integer.parseInt(time.substring(0, time.length() - 1)));
- }
-
- Log.v(TAG, "Date: " + calender.getTime());
+ if (time.contains(":")) {
+ String[] minAndHour = time.split(":");
+ calender.set(Calendar.HOUR_OF_DAY, Integer.parseInt(minAndHour[0]));
+ calender.set(Calendar.MINUTE, Integer.parseInt(minAndHour[1]));
+ live = false;
+ } else {
+ // Log.i(TAG, "Parsing " + time.substring(0, time.length() - 1) + " for min");
+ calender.add(Calendar.MINUTE, Integer.parseInt(time.substring(0, time.length() - 1)));
}
- String name = stopObj.getString("name");
+ // Log.v(TAG, "Date: " + calender.getTime());
+ }
- BusRoute route;
- String dir = null;
+ String name = stopObj.getString("name");
- if (name.equals("U1N")) {
- route = busRoutes.queryForId(468);
- } else if (name.startsWith("U9")) {
- route = busRoutes.queryForId(354);
- } else {
- if (name.startsWith("U1")) {
- route = busRoutes.queryForId(326);
- } else if (name.startsWith("U2")) {
- route = busRoutes.queryForId(329);
- } else if (name.startsWith("U6")) {
- route = busRoutes.queryForId(327);
- } else {
- Log.e(TAG, "Error detecting route " + name);
- return null;
- }
+ BusRoute route = null;
+ String dir = "";
- if (route.forwardDirection.equals(name.substring(2))) {
- dir = route.forwardDirection;
- } else if (route.reverseDirection.equals(name.substring(2))) {
- dir = route.reverseDirection;
+ for (BusRoute tempRoute : routes) {
+ if (name.contains("U")) {
+ if (name.equals("U1N")) {
+ if (tempRoute.code.equals(name)) {
+ route = tempRoute;
+ dir = null;
+ }
} else {
- Log.e(TAG, "Error detecting direction for " + name);
- return null;
+ if (tempRoute == null) {
+ Log.e(TAG, "tempRoute == null");
+ }
+ if (tempRoute.code == null) {
+ Log.e(TAG, "tempRoute.code == null");
+ }
+ if (tempRoute.code.equals(name.substring(0, 2))) {
+ route = tempRoute;
+ if (route.forwardDirection.equals(name.substring(2))) {
+ dir = route.forwardDirection;
+ } else if (route.reverseDirection.equals(name.substring(2))) {
+ dir = route.reverseDirection;
+ } else {
+ Log.e(TAG, "Error detecting direction for " + name);
+ dir = null;
+ return null;
+ }
+ }
}
- }
-
- String destString = stopObj.getString("dest");
- BusStop destStop;
-
- if (destString.equals("Central Station")) {
- destStop = busStopDao.queryForId("SNA19709");
- } else if (destString.equals("Civic Centre")) {
- destStop = busStopDao.queryForId("SN120527");
- } else if (destString.equals("City DG4")) {
- destStop = busStopDao.queryForId("HAA13579");
- } else if (destString.equals("Central Station")) {
- destStop = busStopDao.queryForId("SN120520");
- } else if (destString.equals("Airport")) {
- destStop = busStopDao.queryForId("HA030184");
- } else if (destString.equals("City, Town Quay")) {
- destStop = busStopDao.queryForId("SNA13766");
- } else if (destString.equals("Dock Gate 4")) {
- destStop = busStopDao.queryForId("MG1031");
- } else if (destString.equals("Eastleigh")) {
- destStop = busStopDao.queryForId("HA030212");
- } else if (destString.equals("Crematorium")) {
- destStop = busStopDao.queryForId("SN121009");
- } else if (destString.equals("General Hosp")) {
- destStop = busStopDao.queryForId("SNA19482");
} else {
- Log.e(TAG, "Unknown end dest " + destString + " for route " + route.code);
- return null;
+ if (tempRoute.code.equals(name)) {
+ route = tempRoute;
+ dir = null;
+ }
}
+ }
- Date now = new Date(System.currentTimeMillis());
+ if (route == null) {
+ Log.e(TAG, "Route not found (route == null) " + name);
+ return null;
+ }
- String busID = null;
- Stop stop;
- Bus bus;
- if (stopObj.has("vehicle")) {
- busID = stopObj.getString("vehicle");
+ if (dir != null && dir.equals("")) {
+ Log.e(TAG, "Error detecting direction for " + name);
+ return null;
+ }
- QueryBuilder<Bus, Integer> busQueryBuilder = busDao.queryBuilder();
- busQueryBuilder.where().eq(Bus.ID_FIELD_NAME, busID);
- PreparedQuery<Bus> busPreparedQuery = busQueryBuilder.prepare();
+ String destString = stopObj.getString("dest");
+ BusStop destStop = null;
+
+ if (destString.equals("Central Station")) {
+ destStop = busStopDao.queryForId("SN120520");
+ } else if (destString.equals("Civic Centre")) {
+ destStop = busStopDao.queryForId("SN120527");
+ } else if (destString.equals("City DG4")) {
+ destStop = busStopDao.queryForId("HAA13579");
+ } else if (destString.equals("Airport")) {
+ destStop = busStopDao.queryForId("HA030184");
+ } else if (destString.equals("City, Town Quay")) {
+ destStop = busStopDao.queryForId("SNA13766");
+ } else if (destString.equals("City Centre")) {
+ destStop = busStopDao.queryForId("SNA13766");
+ } else if (destString.equals("Dock Gate 4")) {
+ destStop = busStopDao.queryForId("MG1031");
+ } else if (destString.equals("Eastleigh")) {
+ destStop = busStopDao.queryForId("HA030212");
+ } else if (destString.equals("Crematorium")) {
+ destStop = busStopDao.queryForId("SN121009");
+ } else if (destString.equals("General Hosp")) {
+ destStop = busStopDao.queryForId("SNA19482");
+ } else if (destString.equals("Wessex Lane")) {
+ destStop = busStopDao.queryForId("SNA19780");
+ } else {
+ Log.e(TAG, "Unknown end dest " + destString + " for route " + route.code);
+ }
- bus = busDao.queryForFirst(busPreparedQuery);
+ if (destStop != null) {
- if (bus == null) {
- bus = new Bus(busID, route, dir);
- bus.destination = destStop;
- } else {
- bus.destination = destStop;
- bus.route = route;
- bus.direction = dir;
- }
+ QueryBuilder<RouteStops, Integer> routeStopsQueryBuilder = routeStopsDao.queryBuilder();
+ routeStopsQueryBuilder.where().eq(RouteStops.ROUTE_ID_FIELD_NAME, route.id).and()
+ .eq(RouteStops.STOP_ID_FIELD_NAME, destStop.id);
+ PreparedQuery<RouteStops> routeStopsPreparedQuery = routeStopsQueryBuilder.prepare();
+ List<RouteStops> routeStops = routeStopsDao.query(routeStopsPreparedQuery);
+ if (routeStops.size() > 0) {
+ Log.i(TAG, "Found " + routeStops.size() + " stops matching the destStop " + destStop + " on route "
+ + route.code);
} else {
- bus = new Bus(null, route, dir);
+ Log.w(TAG, "Found " + routeStops.size() + " stops matching the destStop " + destStop + " on route "
+ + route.code);
}
+ }
- busDao.update(bus);
+ Date now = new Date(System.currentTimeMillis());
- stop = new Stop(bus, busStop, calender.getTime(), now);
+ String busID = null;
+ Stop stop;
+ Bus bus;
+ if (stopObj.has("vehicle")) {
+ busID = stopObj.getString("vehicle");
- return stop;
+ QueryBuilder<Bus, Integer> busQueryBuilder = busDao.queryBuilder();
+ busQueryBuilder.where().eq(Bus.ID_FIELD_NAME, busID);
+ PreparedQuery<Bus> busPreparedQuery = busQueryBuilder.prepare();
- } catch (Exception e) {
- // TODO Auto-generated catch block
- Log.e(TAG, "Error parsing stop " + stopObj, e);
- return null;
+ bus = busDao.queryForFirst(busPreparedQuery);
+
+ 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);
}
+ stop = new Stop(bus, busStop, calender.getTime(), now, live);
+
+ return stop;
}
- public static Timetable getTimetable(Context context, String busStop, boolean keepUniLink, boolean keepNonUniLink) throws SQLException,
- ClientProtocolException, IOException, JSONException {
+ public static Timetable getTimetable(Context context, String busStop, boolean keepUniLink, boolean keepNonUniLink)
+ throws SQLException, ClientProtocolException, IOException, JSONException {
if (helper == null)
helper = OpenHelperManager.getHelper(context, DatabaseHelper.class);
- if (busRoutes == null)
- busRoutes = helper.getBusRouteDao();
+ if (busRouteDao == null)
+ busRouteDao = helper.getBusRouteDao();
if (busStopDao == null)
busStopDao = helper.getBusStopDao();
@@ -561,8 +605,24 @@ public class DataManager {
String file = getFileFromServer(busStopUrl + busStop + ".json");
JSONObject data = new JSONObject(file);
-
JSONArray stopsArray = data.getJSONArray("stops");
+ JSONObject routesObject = data.getJSONObject("routes");
+
+ HashSet<BusRoute> busRoutes = new HashSet<BusRoute>();
+ for (Iterator<String> keyIter = routesObject.keys(); keyIter.hasNext();) {
+ String key = keyIter.next();
+
+ Log.v(TAG, "Route Key: " + key);
+
+ BusRoute route = busRouteDao.queryForId(Integer.parseInt(key.substring(key.length() - 3, key.length())));
+
+ if (route != null) {
+ busRoutes.add(route);
+ } else {
+ throw new RuntimeException("Route not found " + key.substring(key.length() - 3, key.length()) + " "
+ + key);
+ }
+ }
Log.i(TAG, "Number of entries " + data.length());
@@ -572,10 +632,12 @@ public class DataManager {
JSONObject stopObj = stopsArray.getJSONObject(stopNum);
if (!keepNonUniLink && !stopObj.getString("name").startsWith("U")) {
+ Log.v(TAG, "Skipping non uni-link stop " + stopObj.getString("name"));
continue;
}
-
+
if (!keepUniLink && stopObj.getString("name").startsWith("U")) {
+ Log.v(TAG, "Skipping uni-link stop " + stopObj.getString("name"));
continue;
}
@@ -584,14 +646,15 @@ public class DataManager {
Log.e(TAG, "BusStopObj == null");
}
- Stop stop = getStop(context, stopObj, busStopObj);
+ Stop stop = getStop(context, stopObj, busRoutes, busStopObj);
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);
+ Log.v(TAG, "Found stop for a unidentified " + stop.bus.toString() + " at " + stop.busStop.id + " at "
+ + stop.arivalTime);
timetable.add(stop);
}
@@ -600,6 +663,115 @@ 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.id + ".json");
+
+ JSONObject data = new JSONObject(file);
+ JSONArray stopsArray = data.getJSONArray("stops");
+
+ HashSet<BusRoute> busRoutes = new HashSet<BusRoute>();
+ busRoutes.add(bus.route);
+
+ Log.v(TAG, "Number of entries " + data.length());
+
+ Log.v(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.v(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;
+ }
+
+ public static Stop getStop(Context context, Bus bus, BusStop busStop) 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();
+
+ String file = getFileFromServer(busStopUrl + busStop.id + ".json");
+
+ JSONObject data = new JSONObject(file);
+ JSONArray stopsArray = data.getJSONArray("stops");
+
+ HashSet<BusRoute> busRoutes = new HashSet<BusRoute>();
+ busRouteDao.refresh(bus.route);
+ busRoutes.add(bus.route);
+
+ Stop stop = null;
+
+ // Log.v(TAG, "Number of entries " + data.length());
+
+ // Log.v(TAG, "Stops: " + data.getJSONArray("stops"));
+
+ for (int stopNum = 0; stopNum < stopsArray.length(); stopNum++) {
+ JSONObject stopObj = stopsArray.getJSONObject(stopNum);
+
+ // Log.v(TAG, "stopObj: " + stopObj);
+ if (stopObj.has("vehicle") && stopObj.getString("vehicle").equals(bus.id)) {
+
+ stop = getStop(context, stopObj, busRoutes, busStop);
+ break;
+
+ // Log.v(TAG, "Found stop for a unidentified " + stop.bus.toString() + " at " + stop.busStop.id + " at "
+ // + stop.arivalTime);
+ }
+ }
+
+ return stop;
+ }
+
static PathOverlay getRoutePath(InputStream routeResource, int colour, ResourceProxy resProxy) {
PathOverlay data = null;
@@ -632,7 +804,7 @@ public class DataManager {
StringBuilder builder = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(request);
- Log.i("Util.getFileFromServer", "Request used: " + request);
+ Log.v("Util.getFileFromServer", "Request used: " + request);
HttpResponse response = client.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
@@ -651,4 +823,66 @@ public class DataManager {
return builder.toString();
}
+
+ public static void routeMovementTest(Context context) throws SQLException {
+
+ Dao<RouteStops, Integer> routeStopsDao = null;
+
+ if (helper == null)
+ helper = OpenHelperManager.getHelper(context, DatabaseHelper.class);
+ if (routeStopsDao == null)
+ routeStopsDao = helper.getRouteStopsDao();
+ if (busRouteDao == null)
+ busRouteDao = helper.getBusRouteDao();
+ if (busStopDao == null)
+ busStopDao = helper.getBusStopDao();
+
+ for (BusRoute busRoute : busRouteDao) {
+ if (!busRoute.code.startsWith("U") || busRoute.code.equals("U9")) {
+ continue;
+ }
+ List<RouteStops> routeStops = routeStopsDao.queryForEq(RouteStops.ROUTE_ID_FIELD_NAME, busRoute.id);
+
+ ArrayList<String> directions = new ArrayList<String>();
+ if (busRoute.forwardDirection != null) {
+ directions.add(busRoute.forwardDirection);
+ }
+ if (busRoute.reverseDirection != null) {
+ directions.add(busRoute.reverseDirection);
+ }
+ if (directions.size() == 0) {
+ directions.add(null);
+ }
+
+ BusStop startBusStop = null;
+
+ for (int moveAmount = 1; moveAmount >= -1; moveAmount = moveAmount - 2) {
+ for (String direction : directions) {
+ for (int busStop = 0; busStop < routeStops.size() && busStop >= 0; busStop = busStop + moveAmount) {
+ // if (routeStops.get(busStop).stop.equals(startBusStop))
+ // continue;
+ startBusStop = routeStops.get(busStop).busStop;
+ busStopDao.refresh(startBusStop);
+
+ BusStop predictedNextStop = busRoute.moveInRoute(context, startBusStop, direction, moveAmount);
+
+ BusStop nextStop;
+ if (busStop == routeStops.size() - 1) {
+ nextStop = routeStops.get(0).busStop;
+ } else {
+ nextStop = routeStops.get(busStop + 1).busStop;
+ }
+ busStopDao.refresh(nextStop);
+
+ if (!nextStop.equals(predictedNextStop) && !startBusStop.equals(nextStop)) {
+ Log.e(TAG, "predicted: " + predictedNextStop + " next: " + nextStop);
+ Log.e(TAG, startBusStop.id + " " + nextStop.id);
+ return;
+ }
+ }
+ }
+ }
+ }
+
+ }
}