Spaces:
Sleeping
Sleeping
LapStore commited on
Commit ·
3eeeacd
1
Parent(s): 854e34f
debug
Browse files- __pycache__/Utils.cpython-311.pyc +0 -0
- __pycache__/app.cpython-311.pyc +0 -0
- __pycache__/database_manager.cpython-311.pyc +0 -0
- __pycache__/mqtt_manager.cpython-311.pyc +0 -0
- __pycache__/state.cpython-311.pyc +0 -0
- __pycache__/traffic_utils.cpython-311.pyc +0 -0
- app.py +17 -10
- mqtt_manager.py +6 -9
- state.py +7 -0
- traffic_utils.py +7 -12
__pycache__/Utils.cpython-311.pyc
ADDED
|
Binary file (787 Bytes). View file
|
|
|
__pycache__/app.cpython-311.pyc
CHANGED
|
Binary files a/__pycache__/app.cpython-311.pyc and b/__pycache__/app.cpython-311.pyc differ
|
|
|
__pycache__/database_manager.cpython-311.pyc
ADDED
|
Binary file (1.4 kB). View file
|
|
|
__pycache__/mqtt_manager.cpython-311.pyc
ADDED
|
Binary file (3.27 kB). View file
|
|
|
__pycache__/state.cpython-311.pyc
ADDED
|
Binary file (374 Bytes). View file
|
|
|
__pycache__/traffic_utils.cpython-311.pyc
ADDED
|
Binary file (3.72 kB). View file
|
|
|
app.py
CHANGED
|
@@ -8,28 +8,40 @@ from mqtt_manager import *
|
|
| 8 |
from traffic_utils import *
|
| 9 |
from Utils import *
|
| 10 |
from threading import Thread
|
|
|
|
|
|
|
| 11 |
|
| 12 |
# -------------------------
|
| 13 |
# App Initialization
|
| 14 |
# -------------------------
|
| 15 |
-
app = FastAPI()
|
| 16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
|
| 19 |
#--------------------------
|
| 20 |
# Routes
|
| 21 |
# -------------------------
|
| 22 |
@app.get("/")
|
| 23 |
-
def
|
| 24 |
return {"message": "Hello from FastAPI on Hugging Face Spaces!"}
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
-
|
| 29 |
@app.post("/send-coordinates")
|
| 30 |
def receive_coordinates(payload: CoordinatesPayload,background_tasks: BackgroundTasks):
|
| 31 |
coords= payload.coords
|
| 32 |
-
|
| 33 |
time_= payload.time
|
| 34 |
|
| 35 |
if isinstance(coords, str):
|
|
@@ -40,12 +52,7 @@ def receive_coordinates(payload: CoordinatesPayload,background_tasks: Background
|
|
| 40 |
print("ok")
|
| 41 |
if tl_ids:
|
| 42 |
for tl_id in tl_ids:
|
| 43 |
-
|
| 44 |
-
print(singlas_state[tl_id] )
|
| 45 |
-
Thread(target=open_signal, args=(tl_id, state, time_), daemon=True).start()
|
| 46 |
-
end =time.time()
|
| 47 |
-
print(end-start)
|
| 48 |
-
print(singlas_state[tl_id] )
|
| 49 |
|
| 50 |
return {"found_signals": tl_ids}
|
| 51 |
else:
|
|
|
|
| 8 |
from traffic_utils import *
|
| 9 |
from Utils import *
|
| 10 |
from threading import Thread
|
| 11 |
+
import state
|
| 12 |
+
from contextlib import asynccontextmanager
|
| 13 |
|
| 14 |
# -------------------------
|
| 15 |
# App Initialization
|
| 16 |
# -------------------------
|
|
|
|
| 17 |
|
| 18 |
+
@asynccontextmanager
|
| 19 |
+
async def lifespan(app: FastAPI):
|
| 20 |
+
# Starting code
|
| 21 |
+
intialize_mqtt_brokers()
|
| 22 |
+
start_mqtt_publishers()
|
| 23 |
+
|
| 24 |
+
yield
|
| 25 |
+
|
| 26 |
+
# At End
|
| 27 |
+
print("🔻 Shutting down...")
|
| 28 |
+
|
| 29 |
+
app = FastAPI(lifespan=lifespan)
|
| 30 |
|
| 31 |
|
| 32 |
#--------------------------
|
| 33 |
# Routes
|
| 34 |
# -------------------------
|
| 35 |
@app.get("/")
|
| 36 |
+
def main():
|
| 37 |
return {"message": "Hello from FastAPI on Hugging Face Spaces!"}
|
| 38 |
|
| 39 |
|
| 40 |
|
|
|
|
| 41 |
@app.post("/send-coordinates")
|
| 42 |
def receive_coordinates(payload: CoordinatesPayload,background_tasks: BackgroundTasks):
|
| 43 |
coords= payload.coords
|
| 44 |
+
state_= payload.state
|
| 45 |
time_= payload.time
|
| 46 |
|
| 47 |
if isinstance(coords, str):
|
|
|
|
| 52 |
print("ok")
|
| 53 |
if tl_ids:
|
| 54 |
for tl_id in tl_ids:
|
| 55 |
+
background_tasks.add_task(open_signal, tl_id, state_, time_) #"EMR" # "ACC"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
return {"found_signals": tl_ids}
|
| 58 |
else:
|
mqtt_manager.py
CHANGED
|
@@ -4,8 +4,7 @@ import time
|
|
| 4 |
import threading
|
| 5 |
from dotenv import load_dotenv
|
| 6 |
import os
|
| 7 |
-
|
| 8 |
-
|
| 9 |
import paho.mqtt.client as mqtt
|
| 10 |
import time
|
| 11 |
|
|
@@ -26,24 +25,22 @@ client = mqtt.Client()
|
|
| 26 |
|
| 27 |
def intialize_mqtt_brokers():
|
| 28 |
client.connect(mqtt_broker, mqtt_port)
|
| 29 |
-
for tl_id in singlas_state.keys():
|
| 30 |
client.subscribe(topic_rep(tl_id))
|
| 31 |
|
| 32 |
def start_mqtt_publishers():
|
| 33 |
-
for tl_id in singlas_state.keys():
|
| 34 |
threading.Thread(target=mqtt_publisher_loop, args=(tl_id,), daemon=True).start()
|
| 35 |
|
| 36 |
|
| 37 |
def get_State(tl_id):
|
| 38 |
-
if tl_id in singlas_state:
|
| 39 |
-
return singlas_state[tl_id]
|
| 40 |
else:
|
| 41 |
return "FREE"
|
| 42 |
|
| 43 |
|
| 44 |
def mqtt_publisher_loop(tl_id):
|
| 45 |
-
client = mqtt.Client()
|
| 46 |
-
client.connect(mqtt_broker, mqtt_port)
|
| 47 |
client.loop_start()
|
| 48 |
|
| 49 |
last_state = None
|
|
@@ -56,7 +53,7 @@ def mqtt_publisher_loop(tl_id):
|
|
| 56 |
last_state = current_state
|
| 57 |
time.sleep(1)
|
| 58 |
|
| 59 |
-
|
| 60 |
|
| 61 |
|
| 62 |
|
|
|
|
| 4 |
import threading
|
| 5 |
from dotenv import load_dotenv
|
| 6 |
import os
|
| 7 |
+
import state
|
|
|
|
| 8 |
import paho.mqtt.client as mqtt
|
| 9 |
import time
|
| 10 |
|
|
|
|
| 25 |
|
| 26 |
def intialize_mqtt_brokers():
|
| 27 |
client.connect(mqtt_broker, mqtt_port)
|
| 28 |
+
for tl_id in state.singlas_state.keys():
|
| 29 |
client.subscribe(topic_rep(tl_id))
|
| 30 |
|
| 31 |
def start_mqtt_publishers():
|
| 32 |
+
for tl_id in state.singlas_state.keys():
|
| 33 |
threading.Thread(target=mqtt_publisher_loop, args=(tl_id,), daemon=True).start()
|
| 34 |
|
| 35 |
|
| 36 |
def get_State(tl_id):
|
| 37 |
+
if tl_id in state.singlas_state:
|
| 38 |
+
return state.singlas_state[tl_id]
|
| 39 |
else:
|
| 40 |
return "FREE"
|
| 41 |
|
| 42 |
|
| 43 |
def mqtt_publisher_loop(tl_id):
|
|
|
|
|
|
|
| 44 |
client.loop_start()
|
| 45 |
|
| 46 |
last_state = None
|
|
|
|
| 53 |
last_state = current_state
|
| 54 |
time.sleep(1)
|
| 55 |
|
| 56 |
+
|
| 57 |
|
| 58 |
|
| 59 |
|
state.py
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
status_free = "FREE"
|
| 2 |
+
status_emr = "EMR"
|
| 3 |
+
status_acc = "ACC"
|
| 4 |
+
|
| 5 |
+
singlas_state = {
|
| 6 |
+
"1698478721" :status_free,
|
| 7 |
+
"6082411793":status_free }
|
traffic_utils.py
CHANGED
|
@@ -2,19 +2,15 @@ import requests
|
|
| 2 |
from database_manager import *
|
| 3 |
from dotenv import load_dotenv
|
| 4 |
import os
|
| 5 |
-
|
| 6 |
load_dotenv(dotenv_path="keys.env")
|
| 7 |
|
| 8 |
overpass_url = os.getenv("URL_Traffic")
|
| 9 |
|
| 10 |
import time
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
status_acc = "ACC"
|
| 15 |
-
singlas_state = {
|
| 16 |
-
"1698478721" :status_free,
|
| 17 |
-
"6082411793":status_free}
|
| 18 |
|
| 19 |
def get_traffic_in_container(coordinates):
|
| 20 |
(min_lat, max_lat, min_lon, max_lon) = get_rectangle_container(coordinates)
|
|
@@ -35,12 +31,11 @@ def get_rectangle_container(coordinates):
|
|
| 35 |
min_lon, max_lon = min(lons), max(lons)
|
| 36 |
return min_lat, max_lat, min_lon, max_lon
|
| 37 |
|
| 38 |
-
def open_signal(tl_id,
|
| 39 |
-
singlas_state[tl_id] =
|
| 40 |
-
print(singlas_state[tl_id] )
|
| 41 |
time.sleep(time_)
|
| 42 |
-
singlas_state[tl_id] = status_free
|
| 43 |
-
|
| 44 |
|
| 45 |
|
| 46 |
def check_signals(coords):
|
|
|
|
| 2 |
from database_manager import *
|
| 3 |
from dotenv import load_dotenv
|
| 4 |
import os
|
| 5 |
+
import state
|
| 6 |
load_dotenv(dotenv_path="keys.env")
|
| 7 |
|
| 8 |
overpass_url = os.getenv("URL_Traffic")
|
| 9 |
|
| 10 |
import time
|
| 11 |
|
| 12 |
+
|
| 13 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
def get_traffic_in_container(coordinates):
|
| 16 |
(min_lat, max_lat, min_lon, max_lon) = get_rectangle_container(coordinates)
|
|
|
|
| 31 |
min_lon, max_lon = min(lons), max(lons)
|
| 32 |
return min_lat, max_lat, min_lon, max_lon
|
| 33 |
|
| 34 |
+
def open_signal(tl_id,state_,time_):
|
| 35 |
+
state.singlas_state[tl_id] = state_
|
|
|
|
| 36 |
time.sleep(time_)
|
| 37 |
+
state.singlas_state[tl_id] = state.status_free
|
| 38 |
+
|
| 39 |
|
| 40 |
|
| 41 |
def check_signals(coords):
|