public static function get_coordinates($routeName) { $age = self::get_vehicle_age(); $coordinates = Array(); // if cache is fresh, return cached values if ($age && $age <= NEXTBUS_VEHICLE_CACHE_TIMEOUT) { self::$vehicleCache = self::read_vehicle_cache(); if (array_key_exists($routeName, self::$vehicleCache)) { foreach (self::$vehicleCache[$routeName] as $coords) { $coords['secsSinceReport'] += $age; $coordinates[] = $coords; } } return $coordinates; } $age = ($age === FALSE) ? 0 : $age; // get all vehicle locations self::set_command('vehicleLocations'); self::set_other_params(Array('t' => 0)); $xml = self::query(); if (!$xml) { if (array_key_exists($routeName, self::$vehicleCache)) { foreach (self::$vehicleCache[$routeName] as $coords) { $coords['secsSinceReport'] += $age; $coordinates[] = $coords; } } } else { // since not all vehicles always show up, // update each secsSinceReport tag // to be consistent with the new file modification time // and allow them to be overwritten if found foreach (self::$vehicleCache as $route => $data) { for ($i = 0; $i < count($data); $i++) { self::$vehicleCache[$route][$i]['secsSinceReport'] += $age; } } // there's only one lastTime tag foreach ($xml->getElementsByTagName('lastTime') as $lastTime) { // drop microseconds $reportAge = time() - (int) substr($lastTime->getAttribute('time'), 0, 10); } $results = Array(); foreach ($xml->getElementsByTagName('vehicle') as $vehicle) { $routeTag = $vehicle->getAttribute('routeTag'); if (!array_key_exists($routeTag, $results)) $results[$routeTag] = Array(); $results[$routeTag][] = Array( 'lat' => $vehicle->getAttribute('lat'), 'lon' => $vehicle->getAttribute('lon'), 'secsSinceReport' => $reportAge + (int) $vehicle->getAttribute('secsSinceReport'), 'heading' => $vehicle->getAttribute('heading'), ); } foreach ($results as $route => $data) { self::$vehicleCache[$route] = $data; } self::write_vehicle_cache(); if (self::$vehicleCache[$routeName]) $coordinates = self::$vehicleCache[$routeName]; } return $coordinates; }
if (!$result = db::$connection->query($sql)) { d_error("sql failed: {$db->errno} {$db->error} in $sql"); } else { while ($row = $result->fetch_assoc()) { $route_id = $row['route_id']; // skip rows whose start times are more than 1.5 loops ago if ($time - $row['start_time'] > 1.5 * ShuttleSchedule::get_interval($route_id)) continue; $stop_id = $row['stop_id']; $next_seconds = -1; $source = 'null'; if ($route_preds = NextBusReader::get_predictions($route_id)) { $stop_preds = $route_preds[$stop_id]; if ($stop_preds) { $source = 'nextbus'; $next_seconds = $stop_preds[0]; $next_time = $time + $next_seconds; } } else { $stop_times = ShuttleSchedule::get_next_scheduled_loop($route_id, $row['start_time']); foreach ($stop_times as $stop_time) { if ($stop_time['nextBusId'] == $stop_id) { $source = 'schedule'; $next_time = $stop_time['nextScheduled']; $next_seconds = $next_time - $time; $stopname = $stop_time['title']; break;
function list_stop_times($route, $time, $gps_active, $singleStopId=NULL) { $stops = Array(); $schedStops = ShuttleSchedule::get_next_scheduled_loop($route, $time); $nbStopsInfo = NextBusReader::get_route_info($route); $interval = ShuttleSchedule::get_interval($route); $end_time = ShuttleSchedule::get_last_run($route, $time) + $interval; // this array will contain the stops to be highlighted $upcoming_stops = Array(); $previous_time = 0; if ($gps_active) { $nbPredictions = NextBusReader::get_predictions($route, $time); } if (!$nbPredictions) { // this condition might occur because we have a delay in // deciding the gps is offline after the bus has actually // gone out of service $gps_active = FALSE; } // this gives an array of seconds until next arrival // at each stop foreach ($schedStops as $index => $stop) { $stopData = Array(); $stopId = $stop['nextBusId']; $stopData['id'] = $stopId; $stopData['title'] = $stop['title']; $stopData['lat'] = $nbStopsInfo[$stopId]['lat']; $stopData['lon'] = $nbStopsInfo[$stopId]['lon']; $stopData['next'] = $stop['nextScheduled']; if ($gps_active && $stopData['next'] !== 0) { $stopData['predictions'] = Array(); if (!$nbPredictions[$stopId]) { error_log("no predictions for $stopId on route $route. predictions:" . serialize($nbPredictions)); continue; } sort($nbPredictions[$stopId]); $firstPrediction = array_shift($nbPredictions[$stopId]); $stopData['next'] = $time + $firstPrediction; foreach ($nbPredictions[$stopId] as $prediction) { if ($time + (int) $prediction > $end_time) { break; } $stopData['predictions'][] = $prediction - $firstPrediction; } } // short circuit if a single stop is requested if ($stopId == $singleStopId) { return $stopData; } if ($stopData['next'] < $previous_time && $stopData['next'] - $time < $interval) { $stopData['upcoming'] = TRUE; } $stops[] = $stopData; $previous_time = $stopData['next']; } if ($stops[0]['next'] < $stops[count($stops) - 1]['next'] && $stops[0]['next'] - $time < $interval) { $stops[0]['upcoming'] = TRUE; } return $stops; }
<? $docRoot = getenv("DOCUMENT_ROOT"); require_once $docRoot . "/mobi-config/mobi_lib_constants.php"; require_once(LIBDIR . "NextBusReader.php"); NextBusReader::init(); $routes = NextBusReader::get_route_list(); print_r($routes); $route = reset($routes); $route = 'saferidebostonw'; print_r(NextBusReader::get_route_info($route)); print_r(NextBusReader::get_last_refreshed($route)); //NextBusReader::get_route_info($route); //print_r(NextBusReader::get_coordinates($route)); //print_r(NextBusReader::get_predictions($route)); ?>
foreach ($stopTimes as $index => $stopTimeInfo) { $stopInfo = $stops[$stopTimeInfo['id']]; foreach ($stopInfo as $property => $value) { if ($property == 'title') continue; $stopTimes[$index][$property] = $value; } } } } $data['stops'] = $stopTimes; if ($gpsActive) { $data['gpsActive'] = TRUE; $data['vehicleLocations'] = NextBusReader::get_coordinates($route); } $data['now'] = $time; } else { $data = Array('error' => "no route parameter"); } break; case 'subscribe': case 'unsubscribe': require_once $APIROOT . '/push/apns_lib.php'; $data = Array('error' => "could not perform $command"); if ($sub = APNSSubscriber::create()) { $route = $_REQUEST['route'];
$route = $_REQUEST['route']; if (!in_array($route, ShuttleSchedule::get_active_routes())) { $routeName = ucwords(str_replace('_', ' ', $_REQUEST['route'])); $not_found_text = '<p>The route ' . $routeName . ' is not currently in service. Please update your bookmarks accordingly. For more information see the <a href="help.php">help page</a>.</p>'; $page->prepare_error_page('Shuttle Schedule', 'shuttle', $not_found_text); } else { $now = time(); $routeName = ShuttleSchedule::get_title($route); $interval = ShuttleSchedule::get_interval($route); $loop_time = $interval / 60; $summary = ShuttleSchedule::get_summary($route); if ($page->branch != 'Basic') { // format: 9:05AM -> 9:05<span class="ampm">AM</span> $summary = preg_replace('/(\\d)(AM|PM)/', '$1<span class="ampm">$2</span>', $summary); } $gps_active = ShuttleSchedule::is_running($route) && NextBusReader::gps_active($route); $stops = list_stop_times($route, $now, $gps_active); $upcoming_stops = array(); foreach ($stops as $index => $stop) { if ($stop['upcoming']) { $upcoming_stops[] = $index; } } // determine size of route map to display on each device switch ($page->branch) { case 'Webkit': $size = 270; break; case 'Touch': $size = 200; break;
<?php require_once "../config/mobi_web_constants.php"; require_once PAGE_HEADER; require_once LIBDIR . "ShuttleSchedule.php"; require_once LIBDIR . "NextBusReader.php"; NextBusReader::init(); $now = time(); $routes = ShuttleSchedule::get_active_routes(); $day_routes = array(); $night_routes = array(); foreach ($routes as $route) { if (ShuttleSchedule::is_safe_ride($route)) { $night_routes[] = $route; } else { $day_routes[] = $route; } } require "{$page->branch}/index.html"; $page->output();