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 | |
| 72 | $schedules = []; |
| 73 | $routes = []; |
| 74 | foreach ($times as $time) { |
| 75 | if ($time["trip_headsign"] == $stop["stop_name"]) continue; |
| 76 | $schedules[] = [ |
| 77 | "destination" => $time["trip_headsign"], |
| 78 | "arrivalTime" => gtfs::time2seconds($time["arrival_time"]), |
| 79 | "departureTime" => gtfs::time2seconds($time["departure_time"]), |
| 80 | "route" => self::transformRouteShortName($time["route_short_name"]), |
| 81 | "color" => $time["route_color"], |
| 82 | "textColor" => $time["route_text_color"] |
| 83 | ]; |
| 84 | |
| 85 | if (!in_array($time["route_short_name"], $routes)) $routes[] = $time["route_short_name"]; |
| 86 | } |
| 87 | |
| 88 | $timeSinceMidnight = gtfs::timeSinceMidnight(); |
| 89 | |
| 90 | usort($schedules, function($a, $b) use ($timeSinceMidnight) { |
| 91 | return ($a["departureTime"] - $b["departureTime"]) % (24*60*60); |
| 92 | }); |
| 93 | |
| 94 | $data = [ |
| 95 | "schedules" => $schedules, |
| 96 | "numRoutes" => count($routes) |
| 97 | ]; |
| 98 | |
| 99 | self::returnData($data); |
| 100 | break; |
| 101 | |
| 102 | default: |
| 103 | self::error("actionNotImplemented"); |
| 104 | } |
| 105 | } |
| 106 | } |