Compare commits
12 Commits
project/sh
...
project/ko
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
eca98c4469 | ||
|
|
c553384ce7 | ||
|
|
1bf5687505 | ||
|
|
0d9dcef994 | ||
|
|
d073243c67 | ||
|
|
0d364ddf61 | ||
|
|
05c94bda81 | ||
|
|
26230df612 | ||
|
|
ae10e212cb | ||
|
|
e2e68e8506 | ||
|
|
1375e6e4be | ||
|
|
154c5c3a78 |
@@ -7,5 +7,6 @@ CREATE TABLE processed_agent_data (
|
|||||||
z FLOAT,
|
z FLOAT,
|
||||||
latitude FLOAT,
|
latitude FLOAT,
|
||||||
longitude FLOAT,
|
longitude FLOAT,
|
||||||
timestamp TIMESTAMP
|
timestamp TIMESTAMP,
|
||||||
);
|
visible BOOLEAN
|
||||||
|
);
|
||||||
|
|||||||
@@ -8,12 +8,13 @@ from sqlalchemy import (
|
|||||||
Integer,
|
Integer,
|
||||||
String,
|
String,
|
||||||
Float,
|
Float,
|
||||||
|
Boolean,
|
||||||
DateTime,
|
DateTime,
|
||||||
)
|
)
|
||||||
from sqlalchemy.sql import select
|
from sqlalchemy.sql import select
|
||||||
|
|
||||||
from database import metadata, SessionLocal
|
from database import metadata, SessionLocal
|
||||||
from schemas import ProcessedAgentData, ProcessedAgentDataInDB
|
from schemas import ProcessedAgentData, ProcessedAgentDataInDB, WebSocketData
|
||||||
|
|
||||||
# FastAPI app setup
|
# FastAPI app setup
|
||||||
app = FastAPI()
|
app = FastAPI()
|
||||||
@@ -30,6 +31,7 @@ processed_agent_data = Table(
|
|||||||
Column("latitude", Float),
|
Column("latitude", Float),
|
||||||
Column("longitude", Float),
|
Column("longitude", Float),
|
||||||
Column("timestamp", DateTime),
|
Column("timestamp", DateTime),
|
||||||
|
Column("visible", Boolean),
|
||||||
)
|
)
|
||||||
|
|
||||||
# WebSocket subscriptions
|
# WebSocket subscriptions
|
||||||
@@ -46,7 +48,9 @@ async def websocket_endpoint(websocket: WebSocket, user_id: int):
|
|||||||
try:
|
try:
|
||||||
# send already available data
|
# send already available data
|
||||||
r = processed_agent_data.select().order_by(processed_agent_data.c.timestamp)
|
r = processed_agent_data.select().order_by(processed_agent_data.c.timestamp)
|
||||||
stored_data = SessionLocal().execute(r).fetchall()
|
session = SessionLocal()
|
||||||
|
stored_data = session.execute(r).fetchall()
|
||||||
|
session.close()
|
||||||
|
|
||||||
jsonable_data = [{c.name: getattr(i, c.name) for c in processed_agent_data.columns} for i in stored_data]
|
jsonable_data = [{c.name: getattr(i, c.name) for c in processed_agent_data.columns} for i in stored_data]
|
||||||
for i in jsonable_data:
|
for i in jsonable_data:
|
||||||
@@ -57,7 +61,24 @@ async def websocket_endpoint(websocket: WebSocket, user_id: int):
|
|||||||
|
|
||||||
# receive forever
|
# receive forever
|
||||||
while True:
|
while True:
|
||||||
await websocket.receive_text()
|
data = await websocket.receive_text()
|
||||||
|
try:
|
||||||
|
if (data):
|
||||||
|
ws_data = WebSocketData.model_validate(json.loads(data))
|
||||||
|
session = SessionLocal()
|
||||||
|
update_query = (
|
||||||
|
processed_agent_data.update()
|
||||||
|
.where(processed_agent_data.c.id == ws_data.id)
|
||||||
|
.values(visible=False)
|
||||||
|
).returning(processed_agent_data)
|
||||||
|
res = session.execute(update_query).fetchone()
|
||||||
|
if (not res):
|
||||||
|
session.rollback()
|
||||||
|
raise Exception("Error while websocket PUT")
|
||||||
|
session.commit()
|
||||||
|
finally:
|
||||||
|
session.close()
|
||||||
|
|
||||||
except WebSocketDisconnect:
|
except WebSocketDisconnect:
|
||||||
subscriptions.remove(websocket)
|
subscriptions.remove(websocket)
|
||||||
|
|
||||||
@@ -81,6 +102,7 @@ def ProcessedAgentData_to_td(data: List[ProcessedAgentData]):
|
|||||||
"latitude": item.agent_data.gps.latitude,
|
"latitude": item.agent_data.gps.latitude,
|
||||||
"longitude": item.agent_data.gps.longitude,
|
"longitude": item.agent_data.gps.longitude,
|
||||||
"timestamp": item.agent_data.timestamp,
|
"timestamp": item.agent_data.timestamp,
|
||||||
|
"visible": True,
|
||||||
}
|
}
|
||||||
for item in data
|
for item in data
|
||||||
]
|
]
|
||||||
@@ -178,8 +200,12 @@ def update_processed_agent_data(processed_agent_data_id: int, data: ProcessedAge
|
|||||||
session.commit()
|
session.commit()
|
||||||
|
|
||||||
updated_result = session.execute(query).fetchone()
|
updated_result = session.execute(query).fetchone()
|
||||||
|
|
||||||
return ProcessedAgentDataInDB(**updated_result._mapping)
|
return ProcessedAgentDataInDB(**updated_result._mapping)
|
||||||
|
|
||||||
|
except Exception as err:
|
||||||
|
session.rollback()
|
||||||
|
print(f"Database error: {err}")
|
||||||
|
raise HTTPException(status_code=500, detail="Internal Server Error")
|
||||||
|
|
||||||
finally:
|
finally:
|
||||||
session.close()
|
session.close()
|
||||||
@@ -210,7 +236,12 @@ def delete_processed_agent_data(processed_agent_data_id: int):
|
|||||||
session.commit()
|
session.commit()
|
||||||
|
|
||||||
return ProcessedAgentDataInDB(**result._mapping)
|
return ProcessedAgentDataInDB(**result._mapping)
|
||||||
|
|
||||||
|
except Exception as err:
|
||||||
|
session.rollback()
|
||||||
|
print(f"Database error: {err}")
|
||||||
|
raise HTTPException(status_code=500, detail="Internal Server Error")
|
||||||
|
|
||||||
finally:
|
finally:
|
||||||
session.close()
|
session.close()
|
||||||
|
|
||||||
|
|||||||
@@ -49,3 +49,7 @@ class AgentData(BaseModel):
|
|||||||
class ProcessedAgentData(BaseModel):
|
class ProcessedAgentData(BaseModel):
|
||||||
road_state: str
|
road_state: str
|
||||||
agent_data: AgentData
|
agent_data: AgentData
|
||||||
|
|
||||||
|
class WebSocketData(BaseModel):
|
||||||
|
id: int
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user