Spaces:
Running
Running
Upload app.py
Browse files- src/app.py +20 -13
src/app.py
CHANGED
|
@@ -209,18 +209,23 @@ async def get_vehicle_view(vehicle_id: str):
|
|
| 209 |
next_stop_id = bus.get('next_stop_id')
|
| 210 |
predicted_time = bus.get('predicted_time')
|
| 211 |
|
| 212 |
-
# 3. Handshake with Database
|
| 213 |
-
# We get the destination name and the specific scheduled arrival time
|
| 214 |
destination = "Not in Schedule"
|
|
|
|
|
|
|
| 215 |
delay_mins = 0
|
| 216 |
|
|
|
|
| 217 |
if next_stop_id:
|
| 218 |
query = """
|
| 219 |
SELECT
|
| 220 |
t.trip_headsign,
|
| 221 |
-
st.arrival_time as scheduled_time
|
|
|
|
|
|
|
| 222 |
FROM trips t
|
| 223 |
JOIN stop_times st ON CAST(t.trip_id AS VARCHAR) = CAST(st.trip_id AS VARCHAR)
|
|
|
|
| 224 |
WHERE CAST(t.trip_id AS VARCHAR) = ?
|
| 225 |
AND CAST(st.stop_id AS VARCHAR) = ?
|
| 226 |
LIMIT 1
|
|
@@ -228,32 +233,33 @@ async def get_vehicle_view(vehicle_id: str):
|
|
| 228 |
row = db.execute(query, [trip_id, str(next_stop_id)]).fetchone()
|
| 229 |
|
| 230 |
if row:
|
| 231 |
-
destination = row
|
| 232 |
-
|
| 233 |
|
| 234 |
-
# DELAY = SCHEDULED - PREDICTED (negative = late, positive = early)
|
| 235 |
if predicted_time:
|
| 236 |
service_day_ts = get_service_day_start_ts()
|
| 237 |
-
# Handle GTFS times >= 24 hours (next day)
|
| 238 |
h, m, s = map(int, scheduled_hms.split(':'))
|
| 239 |
extra_days = h // 24
|
| 240 |
plan_ts = service_day_ts + (extra_days * 86400) + hms_to_seconds(scheduled_hms)
|
| 241 |
delay_mins = round((plan_ts - predicted_time) / 60)
|
| 242 |
else:
|
| 243 |
-
#
|
| 244 |
query = """
|
| 245 |
-
SELECT trip_headsign
|
| 246 |
-
FROM trips
|
| 247 |
-
|
|
|
|
| 248 |
LIMIT 1
|
| 249 |
"""
|
| 250 |
row = db.execute(query, [trip_id]).fetchone()
|
| 251 |
if row:
|
| 252 |
-
destination = row
|
|
|
|
| 253 |
|
| 254 |
return {
|
| 255 |
"vehicle_number": vehicle_id,
|
| 256 |
"route_id": bus['route'],
|
|
|
|
| 257 |
"name": destination,
|
| 258 |
"location": {
|
| 259 |
"lat": bus['lat'],
|
|
@@ -261,7 +267,8 @@ async def get_vehicle_view(vehicle_id: str):
|
|
| 261 |
},
|
| 262 |
"delay_mins": delay_mins,
|
| 263 |
"fullness": translate_occupancy(bus['occupancy']),
|
| 264 |
-
"trip_id": trip_id
|
|
|
|
| 265 |
}
|
| 266 |
|
| 267 |
@app.get("/api/stop/{stop_code}")
|
|
|
|
| 209 |
next_stop_id = bus.get('next_stop_id')
|
| 210 |
predicted_time = bus.get('predicted_time')
|
| 211 |
|
| 212 |
+
# 3. Handshake with Database
|
|
|
|
| 213 |
destination = "Not in Schedule"
|
| 214 |
+
shape_id = None # New field
|
| 215 |
+
route_color = "FF0000" # New field
|
| 216 |
delay_mins = 0
|
| 217 |
|
| 218 |
+
# Updated query to pull shape_id and route_color
|
| 219 |
if next_stop_id:
|
| 220 |
query = """
|
| 221 |
SELECT
|
| 222 |
t.trip_headsign,
|
| 223 |
+
st.arrival_time as scheduled_time,
|
| 224 |
+
t.shape_id,
|
| 225 |
+
r.route_color
|
| 226 |
FROM trips t
|
| 227 |
JOIN stop_times st ON CAST(t.trip_id AS VARCHAR) = CAST(st.trip_id AS VARCHAR)
|
| 228 |
+
JOIN routes r ON t.route_id = r.route_id
|
| 229 |
WHERE CAST(t.trip_id AS VARCHAR) = ?
|
| 230 |
AND CAST(st.stop_id AS VARCHAR) = ?
|
| 231 |
LIMIT 1
|
|
|
|
| 233 |
row = db.execute(query, [trip_id, str(next_stop_id)]).fetchone()
|
| 234 |
|
| 235 |
if row:
|
| 236 |
+
destination, scheduled_hms, shape_id, r_color = row
|
| 237 |
+
route_color = r_color if r_color else "FF0000"
|
| 238 |
|
|
|
|
| 239 |
if predicted_time:
|
| 240 |
service_day_ts = get_service_day_start_ts()
|
|
|
|
| 241 |
h, m, s = map(int, scheduled_hms.split(':'))
|
| 242 |
extra_days = h // 24
|
| 243 |
plan_ts = service_day_ts + (extra_days * 86400) + hms_to_seconds(scheduled_hms)
|
| 244 |
delay_mins = round((plan_ts - predicted_time) / 60)
|
| 245 |
else:
|
| 246 |
+
# Fallback query if no next_stop_id
|
| 247 |
query = """
|
| 248 |
+
SELECT t.trip_headsign, t.shape_id, r.route_color
|
| 249 |
+
FROM trips t
|
| 250 |
+
JOIN routes r ON t.route_id = r.route_id
|
| 251 |
+
WHERE CAST(t.trip_id AS VARCHAR) = ?
|
| 252 |
LIMIT 1
|
| 253 |
"""
|
| 254 |
row = db.execute(query, [trip_id]).fetchone()
|
| 255 |
if row:
|
| 256 |
+
destination, shape_id, r_color = row
|
| 257 |
+
route_color = r_color if r_color else "FF0000"
|
| 258 |
|
| 259 |
return {
|
| 260 |
"vehicle_number": vehicle_id,
|
| 261 |
"route_id": bus['route'],
|
| 262 |
+
"route_color": route_color, # Frontend now gets the color
|
| 263 |
"name": destination,
|
| 264 |
"location": {
|
| 265 |
"lat": bus['lat'],
|
|
|
|
| 267 |
},
|
| 268 |
"delay_mins": delay_mins,
|
| 269 |
"fullness": translate_occupancy(bus['occupancy']),
|
| 270 |
+
"trip_id": trip_id,
|
| 271 |
+
"shape_id": shape_id # Frontend now gets the ID to fetch lines
|
| 272 |
}
|
| 273 |
|
| 274 |
@app.get("/api/stop/{stop_code}")
|