Skip to content
Snippets Groups Projects
Commit 8161e960 authored by Anton Sarukhanov's avatar Anton Sarukhanov
Browse files

Filtering routes, and associated cleanup.

parent 9231287e
No related branches found
No related tags found
No related merge requests found
...@@ -8,7 +8,7 @@ require_once('functions.php'); ...@@ -8,7 +8,7 @@ require_once('functions.php');
if (!isset($_GET['command'])) die(); if (!isset($_GET['command'])) die();
$agency = "rutgers"; $agency = $cfg['agency']['shortname'];
if ($_GET['command'] == "vehicleLocations") { if ($_GET['command'] == "vehicleLocations") {
// return a JSON array of vehicles and their locations. // return a JSON array of vehicles and their locations.
...@@ -16,7 +16,7 @@ if ($_GET['command'] == "vehicleLocations") { ...@@ -16,7 +16,7 @@ if ($_GET['command'] == "vehicleLocations") {
$db = dbc(); $db = dbc();
// MAX age of a bus location to return // MAX age of a bus location to return
$maxAge = 120; $maxAge = $cfg['agency']['staleBusTime'];
$time = 0; $time = 0;
if (isset($_GET['t'])) $time = $db->real_escape_string((int)$_GET['t']); if (isset($_GET['t'])) $time = $db->real_escape_string((int)$_GET['t']);
...@@ -27,6 +27,7 @@ if ($_GET['command'] == "vehicleLocations") { ...@@ -27,6 +27,7 @@ if ($_GET['command'] == "vehicleLocations") {
FROM `vehicleLocations` FROM `vehicleLocations`
LEFT JOIN `routes` ON `vehicleLocations`.`routeTag` = `routes`.`tag` LEFT JOIN `routes` ON `vehicleLocations`.`routeTag` = `routes`.`tag`
WHERE `time` > '{$time}' WHERE `time` > '{$time}'
AND `vehicleLocations`.`agency` = '{$agency}'
V_LOC; V_LOC;
$result = dbq($db, $sql); $result = dbq($db, $sql);
...@@ -39,6 +40,7 @@ V_LOC; ...@@ -39,6 +40,7 @@ V_LOC;
`predictions`.`vehicle`, `stops`.`title`, `predictions`.`epochTime` `predictions`.`vehicle`, `stops`.`title`, `predictions`.`epochTime`
FROM `predictions` FROM `predictions`
LEFT JOIN `stops` ON `predictions`.`stopTag` = `stops`.`tag` LEFT JOIN `stops` ON `predictions`.`stopTag` = `stops`.`tag`
WHERE `predictions`.`agency` = '{$agency}'
ORDER BY `epochTime` ASC ORDER BY `epochTime` ASC
V_LOC; V_LOC;
$result = dbq($db, $sql); $result = dbq($db, $sql);
...@@ -69,12 +71,12 @@ V_LOC; ...@@ -69,12 +71,12 @@ V_LOC;
$db = dbc(); $db = dbc();
$sql = "SELECT `tag`, `title` FROM `routes`"; $sql = "SELECT `tag`, `title` FROM `routes` WHERE `agency` = '{$agency}' AND `tag` NOT IN ('".implode($cfg['agency']['hideRoutes'], "','")."')";
$result = dbq($db, $sql); $result = dbq($db, $sql);
$routes = array(); $routes = array();
while($row = $result->fetch_assoc()) $routes[$row['tag']] = $row; while($row = $result->fetch_assoc()) $routes[$row['tag']] = $row;
$sql = "SELECT `tag`, `title`, `lat`, `lon`, `stopId` FROM `stops`"; $sql = "SELECT `tag`, `title`, `lat`, `lon`, `stopId` FROM `stops` WHERE `agency` = '{$agency}'";
$result = dbq($db, $sql); $result = dbq($db, $sql);
$stops = array(); $stops = array();
while($row = $result->fetch_assoc()) { while($row = $result->fetch_assoc()) {
...@@ -82,11 +84,11 @@ V_LOC; ...@@ -82,11 +84,11 @@ V_LOC;
$stops[$row['tag']]['routes'] = array(); $stops[$row['tag']]['routes'] = array();
} }
$sql = "SELECT `route`, `stop` FROM `route_stops`"; $sql = "SELECT `route`, `stop` FROM `route_stops` WHERE `agency` = '{$agency}' AND `route` NOT IN ('".implode($cfg['agency']['hideRoutes'], "','")."')";
$result = dbq($db, $sql); $result = dbq($db, $sql);
while($row = $result->fetch_assoc()) $stops[$row['stop']]['routes'][$row['route']] = array(); while($row = $result->fetch_assoc()) $stops[$row['stop']]['routes'][$row['route']] = array();
$sql = "SELECT `routes`.`tag`, `routes`.`title`, `stopTag`, `epochTime`, `vehicle` FROM `predictions` LEFT JOIN `routes` ON `predictions`.`routeTag` = `routes`.`tag` WHERE `routes`.`agency` = '{$agency}'"; $sql = "SELECT `routes`.`tag`, `routes`.`title`, `stopTag`, `epochTime`, `vehicle` FROM `predictions` LEFT JOIN `routes` ON `predictions`.`routeTag` = `routes`.`tag` WHERE `routes`.`agency` = '{$agency}' AND `tag` NOT IN ('".implode($cfg['agency']['hideRoutes'], "','")."')";
$result = dbq($db, $sql); $result = dbq($db, $sql);
while($row = $result->fetch_assoc()) $stops[$row['stopTag']]['routes'][$row['tag']][] = array("epochTime" => $row['epochTime'], "vehicle" => $row['vehicle']); while($row = $result->fetch_assoc()) $stops[$row['stopTag']]['routes'][$row['tag']][] = array("epochTime" => $row['epochTime'], "vehicle" => $row['vehicle']);
......
<?php <?php
/* /*
Pull down NextBus data Pull down NextBus data
API Doc: http://www.nextbus.com/xmlFeedDocs/NextBusXMLFeed.pdf API Doc: http://www.nextbus.com/xmlFeedDocs/NextBusXMLFeed.pdf
*/ */
$agency = "rutgers"; $agency = "rutgers";
...@@ -23,54 +23,54 @@ if (!isset($getRouteConfig_waitSeconds)) $getRouteConfig_waitSeconds = 1; ...@@ -23,54 +23,54 @@ if (!isset($getRouteConfig_waitSeconds)) $getRouteConfig_waitSeconds = 1;
$lastFetched = 0; $lastFetched = 0;
$result = dbq($db, "SELECT `time` FROM `lastChecked` WHERE `selector` = 'routeConfig' LIMIT 1"); $result = dbq($db, "SELECT `time` FROM `lastChecked` WHERE `selector` = 'routeConfig' LIMIT 1");
if ($result->num_rows == 1) { if ($result->num_rows == 1) {
$arr = $result->fetch_array(); $arr = $result->fetch_array();
$lastFetched = $arr[0]; $lastFetched = $arr[0];
} }
// ask for new data, if needed // ask for new data, if needed
if ($lastFetched + $getRouteConfig_waitSeconds < time()) { if ($lastFetched + $getRouteConfig_waitSeconds < time()) {
if (@$source = file_get_contents($url)) { if (@$source = file_get_contents($url)) {
if ($thisXML = simplexml_load_string($source)) { if ($thisXML = simplexml_load_string($source)) {
dbq($db, "LOCK TABLES `routes` WRITE, `stops` WRITE, `route_stops` WRITE, `lastChecked` WRITE"); dbq($db, "LOCK TABLES `routes` WRITE, `stops` WRITE, `route_stops` WRITE, `lastChecked` WRITE");
dbq($db, "DELETE FROM `routes` WHERE `agency` = '{$agency}'"); dbq($db, "DELETE FROM `routes` WHERE `agency` = '{$agency}'");
dbq($db, "DELETE FROM `stops` WHERE `agency` = '{$agency}'"); dbq($db, "DELETE FROM `stops` WHERE `agency` = '{$agency}'");
dbq($db, "DELETE FROM `route_stops` WHERE `agency` = '{$agency}'"); dbq($db, "DELETE FROM `route_stops` WHERE `agency` = '{$agency}'");
foreach ($thisXML->route as $route) { foreach ($thisXML->route as $route) {
foreach ($route as $key => $val) $route[$key] = $db->real_escape_string($val); foreach ($route as $key => $val) $route[$key] = $db->real_escape_string($val);
$q = "INSERT INTO `routes` "; $q = "INSERT INTO `routes` ";
$q .= "(`agency`, `tag`,`title`) "; $q .= "(`agency`, `tag`,`title`) ";
$q .= "VALUES ('{$agency}','{$route['tag']}','{$route['title']}')"; $q .= "VALUES ('{$agency}','{$route['tag']}','{$route['title']}')";
$q .= "ON DUPLICATE KEY UPDATE `tag`='{$route['tag']}', `title`='{$route['title']}'"; $q .= "ON DUPLICATE KEY UPDATE `tag`='{$route['tag']}', `title`='{$route['title']}'";
dbq($db, $q); dbq($db, $q);
foreach ($route->stop as $stop) { foreach ($route->stop as $stop) {
foreach ($stop as $key => $val) $stop[$key] = $db->real_escape_string($val); foreach ($stop as $key => $val) $stop[$key] = $db->real_escape_string($val);
$q = "INSERT INTO `stops` "; $q = "INSERT INTO `stops` ";
$q .= "(`agency`, `tag`,`title`,`lat`,`lon`,`stopId`) "; $q .= "(`agency`, `tag`,`title`,`lat`,`lon`,`stopId`) ";
$q .= "VALUES ('{$agency}','{$stop['tag']}','{$stop['title']}','{$stop['lat']}','{$stop['lon']}','{$stop['stopId']}')"; $q .= "VALUES ('{$agency}','{$stop['tag']}','{$stop['title']}','{$stop['lat']}','{$stop['lon']}','{$stop['stopId']}')";
$q .= "ON DUPLICATE KEY UPDATE `title`='{$stop['title']}', `lat`='{$stop['lat']}', `lon`='{$stop['lon']}', `stopId`='{$stop['stopId']}'"; $q .= "ON DUPLICATE KEY UPDATE `title`='{$stop['title']}', `lat`='{$stop['lat']}', `lon`='{$stop['lon']}', `stopId`='{$stop['stopId']}'";
dbq($db, $q); dbq($db, $q);
$q = "INSERT INTO `route_stops` "; $q = "INSERT INTO `route_stops` ";
$q .= "(`agency`, `route`, `stop`) "; $q .= "(`agency`, `route`, `stop`) ";
$q .= "VALUES ('{$agency}','{$route['tag']}', '{$stop['tag']}')"; $q .= "VALUES ('{$agency}','{$route['tag']}', '{$stop['tag']}')";
$q .= "ON DUPLICATE KEY UPDATE `stop` = '{$stop['tag']}'"; $q .= "ON DUPLICATE KEY UPDATE `stop` = '{$stop['tag']}'";
dbq($db, $q); dbq($db, $q);
} }
} }
dbq($db, "UNLOCK TABLES;"); dbq($db, "UNLOCK TABLES;");
// update lastChecked. // update lastChecked.
$tSec = time(); $tSec = time();
dbq($db, "INSERT INTO `lastChecked` (`selector`, `time`) VALUES ('routeConfig', '" . $tSec . "') ON DUPLICATE KEY UPDATE `time` = VALUES(`time`)"); dbq($db, "INSERT INTO `lastChecked` (`selector`, `time`) VALUES ('routeConfig', '" . $tSec . "') ON DUPLICATE KEY UPDATE `time` = VALUES(`time`)");
} else $warning = "Could not parse NextBus data as XML"; } else $warning = "Could not parse NextBus data as XML";
} else $warning = "Could not fetch NextBus data"; } else $warning = "Could not fetch NextBus data";
} }
$db->close(); $db->close();
?> ?>
\ No newline at end of file
...@@ -17,7 +17,7 @@ require_once('config.php'); ...@@ -17,7 +17,7 @@ require_once('config.php');
<link href="css/embed.css" rel="stylesheet" /> <link href="css/embed.css" rel="stylesheet" />
<?php endif; ?> <?php endif; ?>
<script> <script>
window.agency = <?php echo json_encode($cfg['agency']['name']); ?>; window.agency = <?php echo json_encode($cfg['agency']['shortname']); ?>;
window.busIconUrl = <?php echo json_encode($cfg['agency']['busIconUrl'], JSON_UNESCAPED_SLASHES); ?>; window.busIconUrl = <?php echo json_encode($cfg['agency']['busIconUrl'], JSON_UNESCAPED_SLASHES); ?>;
</script> </script>
<script src="js/fastclick.js"></script> <script src="js/fastclick.js"></script>
......
...@@ -185,7 +185,7 @@ function getVehicles(agency, gv_callback) { ...@@ -185,7 +185,7 @@ function getVehicles(agency, gv_callback) {
}); });
} }
function updateMapBusStops(routesIn,stops,maxPredictions) { function updateMapBusStops(routesIn,stops) {
if (window.map != undefined && stops != undefined) { if (window.map != undefined && stops != undefined) {
for (i in stops) { for (i in stops) {
if (stops[i]['lat'] != undefined && stops[i]['lon'] != undefined) { if (stops[i]['lat'] != undefined && stops[i]['lon'] != undefined) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment