blob: ba3ec2f6475d9f4b3f8d1e81352e4fc8c5012619 [file] [log] [blame]
avm9996399bb77c2020-01-27 03:15:08 +01001<?php
2class api {
avm99963583a4612020-07-03 18:45:46 +02003 private static function writeJSON($array) {
avm9996399bb77c2020-01-27 03:15:08 +01004 echo json_encode($array);
5 }
6
avm99963583a4612020-07-03 18:45:46 +02007 private static function error($error = null) {
avm9996399bb77c2020-01-27 03:15:08 +01008 self::writeJSON(["status" => "error", "error" => $error]);
9 exit();
10 }
11
avm99963583a4612020-07-03 18:45:46 +020012 private static function returnData($data) {
avm9996399bb77c2020-01-27 03:15:08 +010013 self::writeJSON(["status" => "ok", "data" => $data]);
14 exit();
15 }
16
avm99963583a4612020-07-03 18:45:46 +020017 private static function transformRouteShortName($name) {
avm9996399bb77c2020-01-27 03:15:08 +010018 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ínez98d0ade2021-10-18 01:10:33 +020072 $todayTimestamp = (new DateTime("today"))->getTimestamp();
73 $nowTimestamp = (new DateTime("now"))->getTimestamp();
74
avm9996399bb77c2020-01-27 03:15:08 +010075 $schedules = [];
76 $routes = [];
77 foreach ($times as $time) {
78 if ($time["trip_headsign"] == $stop["stop_name"]) continue;
Adrià Vilanova Martínez98d0ade2021-10-18 01:10:33 +020079
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 = [
avm9996399bb77c2020-01-27 03:15:08 +0100106 "destination" => $time["trip_headsign"],
Adrià Vilanova Martínez98d0ade2021-10-18 01:10:33 +0200107 "arrivalTime" => $dayTimestamp + $arrivalTime,
108 "departureTime" => $dayTimestamp + $departureTime,
avm9996399bb77c2020-01-27 03:15:08 +0100109 "route" => self::transformRouteShortName($time["route_short_name"]),
110 "color" => $time["route_color"],
111 "textColor" => $time["route_text_color"]
112 ];
Adrià Vilanova Martínez98d0ade2021-10-18 01:10:33 +0200113 if (isset($_GET["includeSqlRows"]))
114 $schedule["originalSqlRow"] = $time;
115
116 $schedules[] = $schedule;
avm9996399bb77c2020-01-27 03:15:08 +0100117
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}