-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
43 lines (35 loc) · 1.1 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import asyncio
import time
from mqtt import mqtt_sub, mqtt_broker
from send_data import HandlingJsonFile
async def main():
broker_address = "192.168.0.71"
broker_port = 1883
# Initialize MQTT broker and client
broker = mqtt_broker()
broker.broker_start()
sub = mqtt_sub(broker_address, broker_port)
sub.start()
# Initialize HandlingJsonFile
handler = HandlingJsonFile()
# Start send_beacon_data in the background
send_data_task = asyncio.create_task(handler.send_beacon_data())
try:
# Keep the main event loop running
while True:
await asyncio.sleep(1)
except KeyboardInterrupt:
print("Exiting...")
finally:
# Stop MQTT client loop and terminate broker
sub.client.loop_stop()
broker.terminate()
# Ensure send_beacon_data is properly stopped (optional)
send_data_task.cancel()
try:
await send_data_task
except asyncio.CancelledError:
print("send_beacon_data task cancelled.")
# Run the main coroutine
if __name__ == "__main__":
asyncio.run(main())