LapStore commited on
Commit
971a3e7
·
1 Parent(s): b2d90c8

diving into adding connection to database ,simple test

Browse files
Files changed (1) hide show
  1. app.py +27 -10
app.py CHANGED
@@ -1,9 +1,11 @@
1
- from fastapi import FastAPI, HTTPException
2
  from typing import List, Union
 
3
  import requests
4
  import ast
5
  import pymysql
6
- import asyncio
 
7
 
8
  # -------------------------
9
  # App Initialization
@@ -53,16 +55,13 @@ def get_traffic_in_container(coordinates):
53
  data = response.json()
54
  return [(el['lat'], el['lon']) for el in data['elements']]
55
 
 
56
  singlas_state = {}
57
- import time
58
 
59
  def open_signal(tl_id,state,time_):
60
  singlas_state[tl_id] = state
61
- start=time.time()
62
  time.sleep(time_)
63
- end=time.time()
64
  singlas_state.pop(tl_id)
65
- print(f"action for {tl_id} --> {state} is Done ,time {end-start}")
66
 
67
 
68
  # -------------------------
@@ -79,7 +78,28 @@ def get_State(tl_id):
79
  return singlas_state[tl_id]
80
  else:
81
  return "FREE"
 
 
82
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83
  @app.get("/test-db")
84
  def test_db():
85
  try:
@@ -92,14 +112,12 @@ def test_db():
92
  except Exception as e:
93
  return {"status": "error", "detail": str(e)}
94
 
95
- from pydantic import BaseModel
96
 
97
  class CoordinatesPayload(BaseModel):
98
  coords: Union[str, List[List[float]]]
99
  state: str
100
  time: int
101
 
102
- from fastapi import BackgroundTasks
103
 
104
  @app.post("/send-coordinates")
105
  def receive_coordinates(payload: CoordinatesPayload,background_tasks: BackgroundTasks):
@@ -115,8 +133,7 @@ def receive_coordinates(payload: CoordinatesPayload,background_tasks: Background
115
 
116
  if tl_ids:
117
  for tl_id in tl_ids:
118
- background_tasks.add_task(open_signal, tl_id, state, time)
119
- #open_signal(tl_id,state,time) #"EMR" # "ACC"
120
 
121
  return {"found_signals": tl_ids}
122
  else:
 
1
+ from fastapi import FastAPI, HTTPException,BackgroundTasks
2
  from typing import List, Union
3
+ from pydantic import BaseModel
4
  import requests
5
  import ast
6
  import pymysql
7
+ import time
8
+
9
 
10
  # -------------------------
11
  # App Initialization
 
55
  data = response.json()
56
  return [(el['lat'], el['lon']) for el in data['elements']]
57
 
58
+
59
  singlas_state = {}
 
60
 
61
  def open_signal(tl_id,state,time_):
62
  singlas_state[tl_id] = state
 
63
  time.sleep(time_)
 
64
  singlas_state.pop(tl_id)
 
65
 
66
 
67
  # -------------------------
 
78
  return singlas_state[tl_id]
79
  else:
80
  return "FREE"
81
+
82
+ from fastapi import FastAPI, WebSocket, WebSocketDisconnect
83
 
84
+ singlas_state = {
85
+ "123": "EMR",
86
+ "456": "ACC",
87
+ # ...
88
+ }
89
+
90
+ @app.websocket("/ws/state")
91
+ async def websocket_get_state(websocket: WebSocket):
92
+ await websocket.accept()
93
+ try:
94
+ while True:
95
+ tl_id = await websocket.receive_text()
96
+ state = singlas_state.get(tl_id, "FREE")
97
+ await websocket.send_text(state)
98
+ except WebSocketDisconnect:
99
+ print("Client disconnected")
100
+
101
+
102
+
103
  @app.get("/test-db")
104
  def test_db():
105
  try:
 
112
  except Exception as e:
113
  return {"status": "error", "detail": str(e)}
114
 
 
115
 
116
  class CoordinatesPayload(BaseModel):
117
  coords: Union[str, List[List[float]]]
118
  state: str
119
  time: int
120
 
 
121
 
122
  @app.post("/send-coordinates")
123
  def receive_coordinates(payload: CoordinatesPayload,background_tasks: BackgroundTasks):
 
133
 
134
  if tl_ids:
135
  for tl_id in tl_ids:
136
+ background_tasks.add_task(open_signal, tl_id, state, time) #"EMR" # "ACC"
 
137
 
138
  return {"found_signals": tl_ids}
139
  else: