avm99963 | 99bb77c | 2020-01-27 03:15:08 +0100 | [diff] [blame] | 1 | <?php |
| 2 | class api { |
avm99963 | 583a461 | 2020-07-03 18:45:46 +0200 | [diff] [blame] | 3 | private static function writeJSON($array) { |
avm99963 | 99bb77c | 2020-01-27 03:15:08 +0100 | [diff] [blame] | 4 | echo json_encode($array); |
| 5 | } |
| 6 | |
avm99963 | 583a461 | 2020-07-03 18:45:46 +0200 | [diff] [blame] | 7 | private static function error($error = null) { |
avm99963 | 99bb77c | 2020-01-27 03:15:08 +0100 | [diff] [blame] | 8 | self::writeJSON(["status" => "error", "error" => $error]); |
| 9 | exit(); |
| 10 | } |
| 11 | |
avm99963 | 583a461 | 2020-07-03 18:45:46 +0200 | [diff] [blame] | 12 | private static function returnData($data) { |
avm99963 | 99bb77c | 2020-01-27 03:15:08 +0100 | [diff] [blame] | 13 | self::writeJSON(["status" => "ok", "data" => $data]); |
| 14 | exit(); |
| 15 | } |
| 16 | |
avm99963 | 583a461 | 2020-07-03 18:45:46 +0200 | [diff] [blame] | 17 | private static function transformRouteShortName($name) { |
avm99963 | 99bb77c | 2020-01-27 03:15:08 +0100 | [diff] [blame] | 18 | if ($name == "L9N") return "L9"; |
| 19 | if ($name == "L10N") return "L10"; |
| 20 | return $name; |
| 21 | } |
| 22 | |
| 23 | public static function handleRequest() { |
| 24 | global $_GET, $_POST, $conf; |
| 25 | |
| 26 | header("Content-Type: application/json"); |
| 27 | |
| 28 | if (!isset($_GET["action"])) self::error("actionNotProvided"); |
| 29 | |
| 30 | $gtfs = new gtfs(); |
| 31 | |
| 32 | switch ($_GET["action"]) { |
| 33 | case "routes": |
| 34 | self::returnData($gtfs->getRoutes()); |
| 35 | break; |
| 36 | |
| 37 | case "trips": |
| 38 | if (!isset($_GET["route"])) self::error("missingArguments"); |
| 39 | $route = $_GET["route"]; |
| 40 | |
| 41 | self::returnData($gtfs->getTrips($route)); |
| 42 | break; |
| 43 | |
| 44 | case "stations": |
| 45 | self::returnData($gtfs->getStations(true)); |
| 46 | |
| 47 | /*$estacionsFull = tmbApi::request("transit/linies/metro/".$linia."/estacions"); |
| 48 | if ($estacionsFull === false || !isset($estacionsFull["features"])) self::error("unexpected"); |
| 49 | |
| 50 | $estacions = []; |
| 51 | foreach ($estacionsFull["features"] as $estacio) { |
| 52 | if (!isset($estacio["properties"])) self::error("unexpected"); |
| 53 | $estacions[] = [ |
| 54 | "id" => $estacio["properties"]["CODI_ESTACIO"] ?? null, |
| 55 | "nom" => $estacio["properties"]["NOM_ESTACIO"] ?? "", |
| 56 | "color" => $estacio["properties"]["COLOR_LINIA"] ?? "000", |
| 57 | "ordre" => $estacio["properties"]["ORDRE_ESTACIO"] ?? 0 |
| 58 | ]; |
| 59 | } |
| 60 | |
| 61 | usort($estacions, function ($a, $b) { |
| 62 | return $a["ordre"] - $b["ordre"]; |
| 63 | });*/ |
| 64 | break; |
| 65 | |
| 66 | case "getTimes": |
| 67 | if (!isset($_GET["stop"])) self::error("missingArguments"); |
| 68 | |
| 69 | $stop = $gtfs->getStop($_GET["stop"]); |
| 70 | $times = $gtfs->getStopTimes($_GET["stop"]); |
| 71 | |
Adrià Vilanova Martínez | 98d0ade | 2021-10-18 01:10:33 +0200 | [diff] [blame^] | 72 | $todayTimestamp = (new DateTime("today"))->getTimestamp(); |
| 73 | $nowTimestamp = (new DateTime("now"))->getTimestamp(); |
| 74 | |
avm99963 | 99bb77c | 2020-01-27 03:15:08 +0100 | [diff] [blame] | 75 | $schedules = []; |
| 76 | $routes = []; |
| 77 | foreach ($times as $time) { |
| 78 | if ($time["trip_headsign"] == $stop["stop_name"]) continue; |
Adrià Vilanova Martínez | 98d0ade | 2021-10-18 01:10:33 +0200 | [diff] [blame^] | 79 | |
| 80 | if ($time["date"]) { |
| 81 | // In this case the train was specifically scheduled for this date, |
| 82 | // so we are given the exact date. |
| 83 | $date = new DateTime($time["date"]); |
| 84 | $arrivalTime = gtfs::time2seconds($time["arrival_time"], false); |
| 85 | $departureTime = gtfs::time2seconds($time["departure_time"], false); |
| 86 | while ($departureTime >= 24*60*60) { |
| 87 | $arrivalTime -= 24*60*60; |
| 88 | $departureTime -= 24*60*60; |
| 89 | $date->add(new DateInterval("P1D")); |
| 90 | } |
| 91 | |
| 92 | $dayTimestamp = $date->getTimestamp(); |
| 93 | } else { |
| 94 | // In this case the train was scheduled several days, so we'll check |
| 95 | // whether the train has already passed today (in which case it will |
| 96 | // pass tomorrow) or it hasn't (in which case it will pass today). |
| 97 | $arrivalTime = gtfs::time2seconds($time["arrival_time"], true); |
| 98 | $departureTime = gtfs::time2seconds($time["departure_time"], true); |
| 99 | if ($todayTimestamp + $departureTime >= $nowTimestamp) |
| 100 | $dayTimestamp = $todayTimestamp; |
| 101 | else |
| 102 | $dayTimestamp = $todayTimestamp->add(new DateInterval("P1D")); |
| 103 | } |
| 104 | |
| 105 | $schedule = [ |
avm99963 | 99bb77c | 2020-01-27 03:15:08 +0100 | [diff] [blame] | 106 | "destination" => $time["trip_headsign"], |
Adrià Vilanova Martínez | 98d0ade | 2021-10-18 01:10:33 +0200 | [diff] [blame^] | 107 | "arrivalTime" => $dayTimestamp + $arrivalTime, |
| 108 | "departureTime" => $dayTimestamp + $departureTime, |
avm99963 | 99bb77c | 2020-01-27 03:15:08 +0100 | [diff] [blame] | 109 | "route" => self::transformRouteShortName($time["route_short_name"]), |
| 110 | "color" => $time["route_color"], |
| 111 | "textColor" => $time["route_text_color"] |
| 112 | ]; |
Adrià Vilanova Martínez | 98d0ade | 2021-10-18 01:10:33 +0200 | [diff] [blame^] | 113 | if (isset($_GET["includeSqlRows"])) |
| 114 | $schedule["originalSqlRow"] = $time; |
| 115 | |
| 116 | $schedules[] = $schedule; |
avm99963 | 99bb77c | 2020-01-27 03:15:08 +0100 | [diff] [blame] | 117 | |
| 118 | if (!in_array($time["route_short_name"], $routes)) $routes[] = $time["route_short_name"]; |
| 119 | } |
| 120 | |
| 121 | $timeSinceMidnight = gtfs::timeSinceMidnight(); |
| 122 | |
| 123 | usort($schedules, function($a, $b) use ($timeSinceMidnight) { |
| 124 | return ($a["departureTime"] - $b["departureTime"]) % (24*60*60); |
| 125 | }); |
| 126 | |
| 127 | $data = [ |
| 128 | "schedules" => $schedules, |
| 129 | "numRoutes" => count($routes) |
| 130 | ]; |
| 131 | |
| 132 | self::returnData($data); |
| 133 | break; |
| 134 | |
| 135 | default: |
| 136 | self::error("actionNotImplemented"); |
| 137 | } |
| 138 | } |
| 139 | } |