-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.py
More file actions
executable file
·65 lines (47 loc) · 1.5 KB
/
code.py
File metadata and controls
executable file
·65 lines (47 loc) · 1.5 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import asyncio
import gc
from microcontroller import watchdog
from watchdog import WatchDogTimeout
from thing_settings import thing_settings
from async_tasks import async_tasks
from wifi_client import wifi_client
from mqtt_client import mqtt_client
from mqtt_system_properties import mqtt_system_properties
from watchdog_manager import watchdog_manager
from reloader import reloader
# Collect Garbage
async def collect_gc():
gc.collect()
# Connect to wifi
async def connect_wifi():
await wifi_client.connect()
# Connect to mqtt
async def connect_mqtt():
await mqtt_client.connect()
# Keep main loop alive forever. Async tasks will control exit flow of the program
async def stay_awake():
while True:
# Two days
await asyncio.sleep(60 * 60 * 24 * 2)
try:
# Configure and load thing settings
thing_settings.configure()
# Start watchdog
watchdog_manager.start()
watchdog_manager.auto_feed()
# Start tasks
async_tasks.start()
# Create a GC collection task
async_tasks.every(20000, collect_gc)
# Connect wifi
asyncio.run(connect_wifi())
# Connect mqtt
asyncio.run(connect_mqtt())
# Start up system properties monitor
mqtt_system_properties.generate_properties()
mqtt_system_properties.start_monitor()
# Keep main execution running and defer to the async loops for the rest of the program
asyncio.run(stay_awake())
except WatchDogTimeout as e:
print("Watchdog timeout exception hit. Reloading.")
reloader.reload()