Spaces:
Sleeping
Sleeping
LapStore
commited on
Commit
·
618e662
1
Parent(s):
6424caa
debug mqtt
Browse files- mqtt_manager.py +4 -2
- state.py +16 -0
- traffic_utils.py +13 -8
mqtt_manager.py
CHANGED
|
@@ -36,12 +36,14 @@ def mqtt_listener():
|
|
| 36 |
tl_id = topic.split('/')[1]
|
| 37 |
print(f"📥 Reply Received on {topic}: {msg}")
|
| 38 |
if (msg == 'AVBL'):
|
| 39 |
-
|
|
|
|
|
|
|
| 40 |
|
| 41 |
if (msg.startswith("QL")):
|
| 42 |
ql = msg.split()[1]
|
| 43 |
if checked_ql(ql):
|
| 44 |
-
client.publish(topic_msg(tl_id), 'ACC'
|
| 45 |
|
| 46 |
client = mqtt.Client()
|
| 47 |
client.connect(mqtt_broker, mqtt_port)
|
|
|
|
| 36 |
tl_id = topic.split('/')[1]
|
| 37 |
print(f"📥 Reply Received on {topic}: {msg}")
|
| 38 |
if (msg == 'AVBL'):
|
| 39 |
+
state.request[tl_id]['accepted'] = True
|
| 40 |
+
next_request = 'DONE' if state.request[tl_id]['State'] == state.status_acc else 'DONE QL?' # ask for queue length if to open road in emergency
|
| 41 |
+
client.publish(topic_msg(tl_id), next_request)
|
| 42 |
|
| 43 |
if (msg.startswith("QL")):
|
| 44 |
ql = msg.split()[1]
|
| 45 |
if checked_ql(ql):
|
| 46 |
+
client.publish(topic_msg(tl_id), 'ACC')
|
| 47 |
|
| 48 |
client = mqtt.Client()
|
| 49 |
client.connect(mqtt_broker, mqtt_port)
|
state.py
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
|
|
|
|
|
| 1 |
status_free = "FREE"
|
| 2 |
status_emr = "EMR"
|
| 3 |
status_acc = "ACC"
|
|
@@ -23,3 +25,17 @@ request = {
|
|
| 23 |
}
|
| 24 |
|
| 25 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import time
|
| 2 |
+
|
| 3 |
status_free = "FREE"
|
| 4 |
status_emr = "EMR"
|
| 5 |
status_acc = "ACC"
|
|
|
|
| 25 |
}
|
| 26 |
|
| 27 |
}
|
| 28 |
+
|
| 29 |
+
def set_request(tl_id,state,duration):
|
| 30 |
+
request[tl_id]['State'] = state
|
| 31 |
+
request[tl_id]['Duration'] = duration
|
| 32 |
+
request[tl_id]['time'] = time.time()
|
| 33 |
+
|
| 34 |
+
def delete_last_request(tl_id):
|
| 35 |
+
request[tl_id]['State'] = status_free
|
| 36 |
+
request[tl_id]['Duration'] = None
|
| 37 |
+
request[tl_id]['time'] = None
|
| 38 |
+
|
| 39 |
+
#optional later store it in database
|
| 40 |
+
|
| 41 |
+
|
traffic_utils.py
CHANGED
|
@@ -42,23 +42,28 @@ async def wait_check_conection(tl_id,check_time):
|
|
| 42 |
await asyncio.sleep(0.2)
|
| 43 |
|
| 44 |
async def open_signal(tl_id,state_,duration,delay):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
check_time = 4
|
| 46 |
-
|
| 47 |
await asyncio.sleep(delay)
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
|
|
|
|
|
|
| 51 |
await wait_check_conection(tl_id,check_time)
|
| 52 |
-
print(state.request[tl_id]['State'])
|
| 53 |
|
| 54 |
|
| 55 |
if (state.request[tl_id]['accepted']== True):
|
| 56 |
-
state.
|
| 57 |
-
state.
|
| 58 |
-
|
| 59 |
return "Request Accepted"
|
| 60 |
else:
|
| 61 |
-
state.
|
| 62 |
return "Request Refused , No Internet Connection or Very High Queue Length"
|
| 63 |
|
| 64 |
|
|
|
|
| 42 |
await asyncio.sleep(0.2)
|
| 43 |
|
| 44 |
async def open_signal(tl_id,state_,duration,delay):
|
| 45 |
+
if (state.request[tl_id]['State'] != state.status_free): #exist old request
|
| 46 |
+
request_type = "Accident to stop road " if (state.request[tl_id]['State'] == state.status_acc) else "Exist Emergency request"
|
| 47 |
+
return "Request Refused , Exist old request : \n"+ request_type
|
| 48 |
+
|
| 49 |
check_time = 4
|
| 50 |
+
|
| 51 |
await asyncio.sleep(delay)
|
| 52 |
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
state.singlas_state[tl_id] = state.status_chk
|
| 57 |
await wait_check_conection(tl_id,check_time)
|
|
|
|
| 58 |
|
| 59 |
|
| 60 |
if (state.request[tl_id]['accepted']== True):
|
| 61 |
+
state.singlas_state[tl_id] = state_
|
| 62 |
+
state.set_request(tl_id,state_,duration)
|
| 63 |
+
|
| 64 |
return "Request Accepted"
|
| 65 |
else:
|
| 66 |
+
state.delete_last_request(tl_id)
|
| 67 |
return "Request Refused , No Internet Connection or Very High Queue Length"
|
| 68 |
|
| 69 |
|