Skip to content

Commit

Permalink
- Made improvements according to: iRail/iRail#210.
Browse files Browse the repository at this point in the history
- Made sure queries for a vehicle on a certain date were available.
- Cleaned up some code.
  • Loading branch information
StanCallewaert committed Jul 25, 2016
1 parent cb3e4f6 commit b4a9f9d
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 35 deletions.
70 changes: 37 additions & 33 deletions api/data/NMBS/vehicleinformation.php
Expand Up @@ -11,7 +11,7 @@
include_once 'data/NMBS/stations.php';
include_once '../includes/simple_html_dom.php';
include_once '../includes/getUA.php';
use MongoDB\Collection;
include_once 'occupancy/OccupancyOperations.php';

class vehicleinformation
{
Expand Down Expand Up @@ -39,14 +39,15 @@ public static function fillDataRoot($dataroot, $request)
$html = str_get_html($serverData);
}


$dataroot->vehicle = self::getVehicleData($html, $request->getVehicleId(), $lang);
if ($request->getAlerts() && self::getAlerts($html)) {
$dataroot->alert = self::getAlerts($html);
}

$vehicleOccupancy = self::getOccupancy(substr(strrchr($request->getVehicleId(), "."), 1));


$vehicleOccupancy = OccupancyOperations::getOccupancy(substr(strrchr($request->getVehicleId(), "."), 1), DateTime::createFromFormat('dmy', $date)->format('Ymd'));
$dataroot->stop = [];

$dataroot->stop = self::getData($html, $lang, $request->getFast(), iterator_to_array($vehicleOccupancy), $date);
}

Expand Down Expand Up @@ -85,18 +86,6 @@ private static function getServerData($id, $date, $lang)
return $result;
}

/**
* @param $vehicle
*/
private static function getOccupancy($vehicle)
{
$m = new MongoDB\Driver\Manager("mongodb://localhost:27017");
$occupancy = new MongoDB\Collection($m, 'spitsgids', 'occupancy');

// If we ever start using a date as parmater the parameter should be put here as date
return $occupancy->find(array('vehicle' => $vehicle, 'date' => date('Ymd')));
}

/**
* @param $html
* @param $lang
Expand All @@ -106,6 +95,15 @@ private static function getOccupancy($vehicle)
*/
private static function getData($html, $lang, $fast, $occupancyArr, $date)
{
$now = new DateTime();
$requestedDate = DateTime::createFromFormat('dmy', $date);
$daysBetweenNowAndRequest = $now->diff($requestedDate);
$occupancyDate = true;

if ($daysBetweenNowAndRequest->d > 1 && $daysBetweenNowAndRequest->invert == 0) {
$occupancyDate = false;
}

try {
$stops = [];
$nodes = $html->getElementById('tq_trainroute_content_table_alteAnsicht')
Expand Down Expand Up @@ -243,14 +241,30 @@ private static function getData($html, $lang, $fast, $occupancyArr, $date)
$stops[$j]->delay = $departureDelay;
$stops[$j]->canceled = $departureCanceled;

// Add occupancy
foreach ($occupancyArr as $stopOccupancy) {
if($station->{'@id'} == $stopOccupancy["from"][0]) {
$URI = self::getURLForOccupancy($stopOccupancy["occupancy"]);
// Check if it is in less than 2 days
if($occupancyDate) {
// Add occupancy
$occupancyOfStationFound = false;
$k = 0;

while ($k < count($occupancyArr) && !$occupancyOfStationFound) {
if ($station->{'@id'} == $occupancyArr[$k]["from"]) {
$URI = OccupancyOperations::NumberToURI($occupancyArr[$k]["occupancy"]);

$stops[$j]->occupancy->{'@id'} = $URI;
$stops[$j]->occupancy->name = basename($URI);
break;
$stops[$j]->occupancy->{'@id'} = $URI;
$stops[$j]->occupancy->name = basename($URI);

$occupancyOfStationFound = true;
}

$k++;
}

if(is_null($stops[$j]->occupancy->{'@id'})) {
$unknown = OccupancyOperations::getUnknown();

$stops[$j]->occupancy->{'@id'} = $unknown;
$stops[$j]->occupancy->name = basename($unknown);
}
}

Expand Down Expand Up @@ -431,14 +445,4 @@ private static function getServerDataByUrl($url)

return $result;
}

private static function getURLForOccupancy($occupancy) {
if($occupancy < 1/3) {
return 'https://api.irail.be/terms/low';
} else if ($occupancy > 2/3) {
return 'https://api.irail.be/terms/high';
} else {
return 'https://api.irail.be/terms/medium';
}
}
};
21 changes: 19 additions & 2 deletions api/occupancy/OccupancyOperations.php
Expand Up @@ -7,18 +7,35 @@
* @author Stan Callewaert
*/

use Dotenv\Dotenv;
use MongoDB\Collection;

class OccupancyOperations
{
const UNKNOWN = 'https://api.irail.be/terms/unknown';
const LOW = 'https://api.irail.be/terms/low';
const MEDIUM = 'https://api.irail.be/terms/medium';
const HIGH = 'https://api.irail.be/terms/high';

public static function getOccupancy($vehicle, $date)
{
$dotenv = new Dotenv(dirname(dirname(__DIR__)));
$dotenv->load();
$mongodb_url = getenv('MONGODB_URL');
$mongodb_db = getenv('MONGODB_DB');

$m = new MongoDB\Driver\Manager($mongodb_url);
$occupancy = new MongoDB\Collection($m, $mongodb_db, 'occupancy');

// If we ever start using a date as parameter the parameter should be put here as date
return $occupancy->find(array('vehicle' => $vehicle, 'date' => $date));
}

public static function NumberToURI($occupancy)
{
if ($occupancy < 1/3) {
if ($occupancy < 2/3) {
return self::LOW;
} elseif ($occupancy <= 2/3) {
} elseif ($occupancy <= 4/3) {
return self::MEDIUM;
} else {
return self::HIGH;
Expand Down

0 comments on commit b4a9f9d

Please sign in to comment.