Skip to content

Commit

Permalink
feat: reimplement websockets? :O
Browse files Browse the repository at this point in the history
  • Loading branch information
albertkun committed Nov 21, 2023
1 parent 58850f8 commit c120f79
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion fastapi/app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

from datetime import timedelta, date, datetime

from fastapi import FastAPI, Request, Response, Depends, HTTPException, status, Query
from fastapi import FastAPI, Request, Response, Depends, HTTPException, status, Query, Websocket
from fastapi import Path as FastAPIPath
# from fastapi import FastAPI, Request, Response, Depends, HTTPException, status
from fastapi.encoders import jsonable_encoder
Expand Down Expand Up @@ -399,6 +399,29 @@ async def get_list_of_field_values(agency_id: AgencyIdEnum, field: VehiclePositi
return data


@app.websocket("/ws/{agency_id}/vehicle_positions")
async def websocket_endpoint(websocket: WebSocket, agency_id: str, async_db: AsyncSession = Depends(get_async_db)):
await websocket.accept()
while True:
data = await crud.get_all_data_async(async_db, models.VehiclePositions, agency_id)
if data is not None:
await websocket.send_json(data)
await asyncio.sleep(5)

@app.websocket("/ws/{agency_id}/vehicle_positions/{field}/{ids}")
async def websocket_vehicle_positions_by_ids(websocket: WebSocket, agency_id: AgencyIdEnum, field: VehiclePositionsFieldsEnum, ids: str, async_db: AsyncSession = Depends(get_async_db)):
await websocket.accept()
model = models.VehiclePositions
ids = ids.split(',')
while True:
data = {}
for id in ids:
result = await crud.get_data_async(async_db, model, agency_id.value, field.value, id)
if result is not None:
data[id] = result
if data:
await websocket.send_json(data)
await asyncio.sleep(5)


##### todo: Needs to be tested
Expand Down

0 comments on commit c120f79

Please sign in to comment.