Spaces:
Sleeping
Sleeping
LapStore commited on
Commit ·
95608ed
1
Parent(s): 31a8c53
debug mqtt
Browse files- Utils.py +2 -1
- app.py +3 -3
- mqtt_manager.py +16 -4
- state.py +18 -1
- traffic_utils.py +13 -4
Utils.py
CHANGED
|
@@ -4,4 +4,5 @@ from pydantic import BaseModel
|
|
| 4 |
class CoordinatesPayload(BaseModel):
|
| 5 |
coords: Union[str, List[List[float]]]
|
| 6 |
state: str
|
| 7 |
-
|
|
|
|
|
|
| 4 |
class CoordinatesPayload(BaseModel):
|
| 5 |
coords: Union[str, List[List[float]]]
|
| 6 |
state: str
|
| 7 |
+
duration: int
|
| 8 |
+
delay : int
|
app.py
CHANGED
|
@@ -41,17 +41,17 @@ def main():
|
|
| 41 |
def receive_coordinates(payload: CoordinatesPayload,background_tasks: BackgroundTasks):
|
| 42 |
coords= payload.coords
|
| 43 |
state_= payload.state
|
| 44 |
-
|
|
|
|
| 45 |
|
| 46 |
if isinstance(coords, str):
|
| 47 |
coords = ast.literal_eval(coords)
|
| 48 |
|
| 49 |
traffic_lights = get_traffic_in_container(coords)
|
| 50 |
tl_ids = check_signals(traffic_lights)
|
| 51 |
-
print("ok")
|
| 52 |
if tl_ids:
|
| 53 |
for tl_id in tl_ids:
|
| 54 |
-
background_tasks.add_task(open_signal, tl_id, state_,
|
| 55 |
|
| 56 |
return {"found_signals": tl_ids}
|
| 57 |
else:
|
|
|
|
| 41 |
def receive_coordinates(payload: CoordinatesPayload,background_tasks: BackgroundTasks):
|
| 42 |
coords= payload.coords
|
| 43 |
state_= payload.state
|
| 44 |
+
duration= payload.duration
|
| 45 |
+
delay= payload.delay
|
| 46 |
|
| 47 |
if isinstance(coords, str):
|
| 48 |
coords = ast.literal_eval(coords)
|
| 49 |
|
| 50 |
traffic_lights = get_traffic_in_container(coords)
|
| 51 |
tl_ids = check_signals(traffic_lights)
|
|
|
|
| 52 |
if tl_ids:
|
| 53 |
for tl_id in tl_ids:
|
| 54 |
+
background_tasks.add_task(open_signal, tl_id, state_, duration,delay) #"EMR" # "ACC"
|
| 55 |
|
| 56 |
return {"found_signals": tl_ids}
|
| 57 |
else:
|
mqtt_manager.py
CHANGED
|
@@ -26,15 +26,22 @@ def topic_rep(tl_id):
|
|
| 26 |
def get_State(tl_id):
|
| 27 |
return state.singlas_state.get(tl_id, "FREE")
|
| 28 |
|
| 29 |
-
|
|
|
|
| 30 |
# ------------------ Listener ------------------ #
|
| 31 |
def mqtt_listener():
|
| 32 |
def on_message(client, userdata, message):
|
| 33 |
msg = message.payload.decode()
|
| 34 |
topic = message.topic
|
|
|
|
| 35 |
print(f"📥 Reply Received on {topic}: {msg}")
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
client = mqtt.Client()
|
| 40 |
client.connect(mqtt_broker, mqtt_port)
|
|
@@ -58,12 +65,17 @@ def mqtt_publisher_loop(tl_id):
|
|
| 58 |
last_state = None
|
| 59 |
while True:
|
| 60 |
current_state = get_State(tl_id)
|
|
|
|
| 61 |
if current_state != last_state:
|
| 62 |
topic = topic_msg(tl_id)
|
| 63 |
pub_client.publish(topic, current_state)
|
| 64 |
print(f"📡 Published {current_state} to {topic}")
|
| 65 |
last_state = current_state
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
|
| 69 |
# ------------------ Startup ------------------ #
|
|
|
|
| 26 |
def get_State(tl_id):
|
| 27 |
return state.singlas_state.get(tl_id, "FREE")
|
| 28 |
|
| 29 |
+
def checked_ql(ql):
|
| 30 |
+
return True
|
| 31 |
# ------------------ Listener ------------------ #
|
| 32 |
def mqtt_listener():
|
| 33 |
def on_message(client, userdata, message):
|
| 34 |
msg = message.payload.decode()
|
| 35 |
topic = message.topic
|
| 36 |
+
tl_id = topic_rep.split('/')[1]
|
| 37 |
print(f"📥 Reply Received on {topic}: {msg}")
|
| 38 |
+
if (msg == 'AVBL'):
|
| 39 |
+
client.publish(topic_msg(tl_id), 'DONE'+str(msg))
|
| 40 |
+
|
| 41 |
+
if (msg.startswith("QL")):
|
| 42 |
+
ql = msg.split()[1]
|
| 43 |
+
if checked_ql(ql):
|
| 44 |
+
client.publish(topic_msg(tl_id), 'ACC'+str(msg))
|
| 45 |
|
| 46 |
client = mqtt.Client()
|
| 47 |
client.connect(mqtt_broker, mqtt_port)
|
|
|
|
| 65 |
last_state = None
|
| 66 |
while True:
|
| 67 |
current_state = get_State(tl_id)
|
| 68 |
+
'''
|
| 69 |
if current_state != last_state:
|
| 70 |
topic = topic_msg(tl_id)
|
| 71 |
pub_client.publish(topic, current_state)
|
| 72 |
print(f"📡 Published {current_state} to {topic}")
|
| 73 |
last_state = current_state
|
| 74 |
+
|
| 75 |
+
'''
|
| 76 |
+
if current_state != 'FREE':
|
| 77 |
+
topic = topic_msg(tl_id)
|
| 78 |
+
time.sleep(.01)
|
| 79 |
|
| 80 |
|
| 81 |
# ------------------ Startup ------------------ #
|
state.py
CHANGED
|
@@ -5,4 +5,21 @@ status_chk = "CHK"
|
|
| 5 |
|
| 6 |
singlas_state = {
|
| 7 |
"1698478721" :status_free,
|
| 8 |
-
"6082411793":status_free }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
singlas_state = {
|
| 7 |
"1698478721" :status_free,
|
| 8 |
+
"6082411793":status_free }
|
| 9 |
+
|
| 10 |
+
request = {
|
| 11 |
+
"1698478721":{
|
| 12 |
+
'State' : status_free,
|
| 13 |
+
'time' : None,
|
| 14 |
+
'Duration': None,
|
| 15 |
+
'accepted' : False
|
| 16 |
+
},
|
| 17 |
+
"6082411793":{
|
| 18 |
+
'State' : status_free,
|
| 19 |
+
'time' : None,
|
| 20 |
+
'Duration': None,
|
| 21 |
+
'accepted' : False
|
| 22 |
+
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
}
|
traffic_utils.py
CHANGED
|
@@ -30,11 +30,20 @@ def get_rectangle_container(coordinates):
|
|
| 30 |
min_lat, max_lat = min(lats), max(lats)
|
| 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_,
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
state.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
|
| 40 |
|
|
|
|
| 30 |
min_lat, max_lat = min(lats), max(lats)
|
| 31 |
min_lon, max_lon = min(lons), max(lons)
|
| 32 |
return min_lat, max_lat, min_lon, max_lon
|
| 33 |
+
import asyncio
|
| 34 |
|
| 35 |
+
async def open_signal(tl_id,state_,duration,delay):
|
| 36 |
+
check_time = 10
|
| 37 |
+
asyncio.sleep(delay)
|
| 38 |
+
state.request[tl_id]['State'] = state.status_chk
|
| 39 |
+
asyncio.sleep(check_time)
|
| 40 |
+
if (state.request[tl_id]['accepted']== True):
|
| 41 |
+
state.request[tl_id]['State'] = state_
|
| 42 |
+
state.request[tl_id]['time'] = time.time()
|
| 43 |
+
state.request[tl_id]['duration'] = duration
|
| 44 |
+
return "Request Accepted"
|
| 45 |
+
else:
|
| 46 |
+
return "Request Refused , No Internet Connection or Very High Queue Length"
|
| 47 |
|
| 48 |
|
| 49 |
|