Skip to content

Commit

Permalink
Implement vehicle tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
Benyamin Ginzburg committed Aug 12, 2024
1 parent f0e0c4b commit dab3ccf
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 9 deletions.
Empty file removed Pipfile
Empty file.
2 changes: 1 addition & 1 deletion israel_transport_api/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Env(BaseSettings):
SCHED_HOURS: int
SCHED_MINS: int

DB_BATCH_SIZE: int = 300_000
WS_UPDATE_INTERVAL: int = 5


env = Env()
12 changes: 6 additions & 6 deletions israel_transport_api/siri/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
from fastapi import APIRouter, Path, Query, Request, WebSocket, WebSocketDisconnect

from israel_transport_api.siri.client import get_incoming_routes, get_vehicle_location
from israel_transport_api.siri.models import IncomingRoutesResponse, VehicleLocationResponse
from israel_transport_api.siri.models import IncomingRoutesResponse
from israel_transport_api.config import env


WS_UPDATE_INTERVAL = 3

siri_router = APIRouter(prefix='/siri', tags=['Siri'])


Expand All @@ -28,10 +27,11 @@ async def track_vehicle(
):
await ws.accept()
try:
previous_resp = None
while True:
resp = await get_vehicle_location(vehicle_plate_number, stop_code)
resp and await ws.send_json(resp.dict())
await sleep(WS_UPDATE_INTERVAL)
resp and previous_resp != resp and await ws.send_json(resp.model_dump())
previous_resp = resp
await sleep(env.WS_UPDATE_INTERVAL)
except WebSocketDisconnect:
pass

17 changes: 16 additions & 1 deletion pdm.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ authors = [
{name = "Benyamin Ginzburg", email = "[email protected]"},
]
dependencies = [
"fastapi==0.111.1",
"fastapi[websockets]==0.111.1",
"pydantic==2.8.2",
"pydantic-settings==2.4.0",
"uvicorn==0.30.4",
Expand Down

0 comments on commit dab3ccf

Please sign in to comment.