Beispiel #1
0
 /**
  * Function: inner
  * @param int $screenid - The id of the screen you wish to load
  *
  * This function differs from the index function in that this function, that is
  * /screen/inner is called by the inner IFRAME.
  */
 public function inner($screenid)
 {
     //If no parameter, redirect somewhere else
     if (!isset($screenid)) {
         redirect('/');
     }
     // Load the screen model
     $this->load->model('screen_model');
     $screen = new Screen_model();
     //Load variables of screen model type
     $screen->load_model($screenid);
     $data['id'] = $screenid;
     // Check for sleep mode to determine the view.  If the screen is asleep,
     // just use the default three_col.  If the screen should be awake, set up
     // a $data variable and set the numcols to the number of columns and the
     // zoom level to the custom zoom level.
     if ($screen->is_asleep()) {
         $data['numcols'] = 3;
         $data['template'] = 'three_col';
     } else {
         $data['numcols'] = $screen->get_num_columns();
         $data['zoom'] = $screen->zoom;
     }
     // Call the screen_template view and pass the $data variable.  Each
     // element of the $data array will become a variable, i.e. $data['id'] will
     // become $id in the views
     $this->load->view('includes/screen_template', $data);
 }
Beispiel #2
0
 /**
  * Function: json
  * @param int $screen_id - The id of the screen whose updates you need to load
  *
  * This is one of the biggest and most complicated functions in the application.
  * It generates the JSON data update for every screen.  More specifically, it
  * loads a screen, ensures that it should be awake at this moment, and gets all
  * the blocks.  For each block it finds the agency-stop pairs and calls the
  * appropriate APIs to get the real-time transit data.  It assembles all this
  * information, including the custom block data and the CaBi status and prints
  * it all out as one JSON response.
  */
 public function json($screen_id)
 {
     // Load the Update model and the screen model
     $this->load->model('update_model');
     $update = new Update_model();
     $this->load->model('screen_model');
     $screen = new Screen_model();
     // Fill this variable with the screen values.
     $screendata = $this->screen_model->get_screen_values($screen_id, true);
     //Load variable of screen model type
     $screen->load_model($screen_id);
     // We will collect all the data to publish via JSON in the $update variable,
     // so set a few of its static properties based on the screendata variable.
     $update->screen_name = $screendata['settings'][0]->name;
     $update->screen_version = $screendata['settings'][0]->screen_version;
     $wmata_key = $screendata['settings'][0]->wmata_key;
     // Update the last_checkin value for this screen.  This allows us to ensure
     // that our screens are regularly calling for updates.
     $this->_update_timestamp($screen_id);
     // If the screen should be asleep right now, print that in JSON and do not
     // bother to load any real-time data.
     if ($screen->is_asleep()) {
         $update->sleep = true;
         print json_encode($update);
     } else {
         //Gather all the necessary data into the $update variable
         //and then output the variable as JSON
         // This helper contains the fuctions that call the various agency APIs
         $this->load->helper('transit_functions');
         // Obviously this screen should be awake.
         $update->sleep = false;
         $stopname = '';
         // For every block...  (remember that one block can contain more than one
         // stop or CaBi station!)
         foreach ($screendata['blocks'] as $block) {
             $stops = $block->stop;
             //Set up (or clear) variables to handle the various data
             $vehicles = array();
             unset($bike);
             $bikes = array();
             unset($override);
             // For each of the agency-stop pairs for this block...
             foreach ($stops as $stop) {
                 // ... get the arrival predictions for each agency.
                 // Collected the line exclusions for this block
                 $exclusions = array();
                 if (isset($stop['exclusions'])) {
                     $exclusions = explode(',', $stop['exclusions']);
                 }
                 // For this agency-stop pair, check to see what mode it is and then
                 // call the approriate API function.
                 switch ($this->_get_agency_type($stop['agency'])) {
                     case 'bus':
                         $newset = array();
                         // Get the bus prediction data back.  This get_bus_predictions
                         // function covers ART, WMATA, DC Circulator, Prince George's TheBus and Shuttle UM
                         $set = get_bus_predictions($stop['stop_id'], $wmata_key, $stop['agency']);
                         if (isset($set[0])) {
                             // Loop through the results.  If the bus line is not in the
                             // exclusions array, add it to a new set.  We will abandon the
                             // excluded lines.
                             foreach ($set as $b) {
                                 if (!in_array(strtoupper($b['route']), $exclusions)) {
                                     $newset[] = $b;
                                 }
                             }
                             $vehicles[] = $newset;
                         }
                         break;
                     case 'subway':
                         // Get predictions from WMATA for rail station with id $stop['stop_id').
                         $vehicles[] = get_rail_predictions($stop['stop_id'], $wmata_key);
                         break;
                     case 'cabi':
                         // For each bike station, get the status.  Notice that the data
                         // will be put into the $bikes array since each block may have
                         // multiple CaBi stations.
                         $bikes[] = get_cabi_status($stop['stop_id']);
                         break;
                     case 'custom':
                         // This is where the custom block data goes.  There is no clean up.
                         $override = $block->custom_body;
                         break;
                 }
             }
             // Combine the different agency predictions for this stop
             // into a single array and sort by time.  Make sure you have actual
             // predictions first!
             if (count($vehicles) > 0) {
                 if ($this->_get_agency_type($stop['agency']) == 'bus') {
                     // Combine multi-agency data for buses, then combine same routes
                     $stopdata = combine_agencies($vehicles);
                     $stopdata = $this->_combine_duplicates($stopdata);
                 } elseif ($this->_get_agency_type($stop['agency']) == 'subway') {
                     // Combine same routes data for subway
                     $stopdata = $vehicles[0];
                     $stopdata = $this->_combine_duplicates($stopdata);
                 } else {
                     $stopdata = $vehicles[0];
                 }
                 // If there is a limit to the number of arrival lines to list
                 // at any bus stop, remove the extra vehicles from the array
                 if (isset($block->limit) && isset($stopdata) && count($stopdata) > $block->limit && $block->limit > 0) {
                     array_splice($stopdata, $block->limit);
                 }
                 // Set the stop name to the API stop name that comes first.  WMATA and
                 // ART will have different descriptions for the same stop, but we will
                 // just use the first name instead.  You can override this with a
                 // custom stop name in the backend, of course.
                 if (isset($vehicles[0][0]['stop_name'])) {
                     $stopname = $vehicles[0][0]['stop_name'];
                 }
             }
             // If we're working with CaBi here
             if (isset($bike)) {
                 $stopdata = $bike;
                 $stopname = $bike['stop_name'];
             }
             // Set the stop's custom name
             if (strlen(trim($block->custom_name)) > 0) {
                 $stopname = $block->custom_name;
             }
             // If we're working with bikes, put all the relevant block data into
             // an array.  Otherwise, do the same for a bus or Metro block.
             if (isset($bikes) && count($bikes) > 0) {
                 $stopdata = array('id' => $block->id, 'name' => clean_destination($stopname), 'type' => $this->_get_agency_type($stop['agency']), 'column' => (int) $block->column, 'order' => (int) $block->position, 'stations' => $bikes);
             } else {
                 $stopdata = array('id' => $block->id, 'name' => clean_destination($stopname), 'type' => $this->_get_agency_type($stop['agency']), 'column' => (int) $block->column, 'order' => (int) $block->position, 'vehicles' => $stopdata);
             }
             // If this block is a custom block, put in the data here.
             if (isset($override)) {
                 $stopdata = array('id' => $block->id, 'name' => clean_destination($stopname), 'type' => $this->_get_agency_type($stop['agency']), 'column' => (int) $block->column, 'order' => (int) $block->position, 'custom_body' => $override);
             }
             // Add all the stop data for this block to the stops array in the $update
             // variable.
             $update->stops[] = $stopdata;
         }
         // Print out the entire $update variable encoded as JSON
         print json_encode($update);
     }
 }