|
| 1 | +import time |
1 | 2 |
|
| 3 | +print('main.py') |
| 4 | +time.sleep(1) |
2 | 5 |
|
3 |
| -import gc |
4 |
| -import sys |
5 |
| -import time |
6 | 6 |
|
7 |
| -from button_handler import Button |
8 |
| -from pins import Pins |
9 |
| -from reset import ResetDevice |
10 |
| -from rtc import get_rtc_value, update_rtc_dict |
11 |
| -from wifi import WiFi |
| 7 | +def main(): |
| 8 | + import gc |
| 9 | + gc.collect() |
| 10 | + |
| 11 | + import sys |
| 12 | + sys.modules.clear() |
12 | 13 |
|
13 |
| -print('main.py') |
| 14 | + from button_handler import Button |
| 15 | + from pins import Pins |
14 | 16 |
|
15 |
| -time.sleep(1) |
| 17 | + from wifi import WiFi |
16 | 18 |
|
17 |
| -sys.modules.clear() |
| 19 | + __version__ = 'v0.6.0' |
18 | 20 |
|
19 |
| -gc.collect() |
| 21 | + # Init device button IRQ: |
| 22 | + Pins.button_pin.irq(Button().irq_handler) |
20 | 23 |
|
| 24 | + wifi = WiFi() |
| 25 | + wifi.ensure_connection() |
| 26 | + print('wifi: %s' % wifi) |
21 | 27 |
|
22 |
| -__version__ = 'v0.5.1' |
| 28 | + _RTC_KEY_RUN = 'run' |
| 29 | + _RUN_WEB_SERVER = 'web-server' |
23 | 30 |
|
| 31 | + from rtc import get_rtc_value |
| 32 | + if get_rtc_value(_RTC_KEY_RUN) == _RUN_WEB_SERVER: |
| 33 | + print('start webserver') |
24 | 34 |
|
25 |
| -# Init device button IRQ: |
26 |
| -Pins.button_pin.irq(Button().irq_handler) |
| 35 | + from rtc import update_rtc_dict |
| 36 | + update_rtc_dict(data={_RTC_KEY_RUN: None}) # run OTA client on next boot |
27 | 37 |
|
28 |
| -wifi = WiFi() |
29 |
| -wifi.ensure_connection() |
30 |
| -print('wifi: %s' % wifi) |
| 38 | + del update_rtc_dict |
| 39 | + del get_rtc_value |
| 40 | + del sys.modules['rtc'] |
| 41 | + gc.collect() |
31 | 42 |
|
32 |
| -_RTC_KEY_RUN = 'run' |
33 |
| -_RUN_WEB_SERVER = 'web-server' |
| 43 | + from webswitch import WebServer # noqa isort:skip |
| 44 | + from watchdog import Watchdog # noqa isort:skip |
| 45 | + from power_timer import PowerTimer # noqa isort:skip |
34 | 46 |
|
| 47 | + power_timer = PowerTimer() |
| 48 | + power_timer.schedule_next_switch() |
35 | 49 |
|
36 |
| -if get_rtc_value(_RTC_KEY_RUN) == _RUN_WEB_SERVER: |
37 |
| - print('start webserver') |
38 |
| - update_rtc_dict(data={_RTC_KEY_RUN: None}) # run OTA client on next boot |
39 |
| - from webswitch import WebServer # noqa isort:skip |
40 |
| - from watchdog import Watchdog # noqa isort:skip |
41 |
| - from power_timer import PowerTimer # noqa isort:skip |
| 50 | + gc.collect() |
| 51 | + WebServer( |
| 52 | + power_timer=power_timer, |
| 53 | + watchdog=Watchdog( |
| 54 | + wifi=wifi, |
| 55 | + check_callback=power_timer.schedule_next_switch |
| 56 | + ), |
| 57 | + version=__version__ |
| 58 | + ).run() |
| 59 | + else: |
| 60 | + print('start OTA') |
| 61 | + Pins.power_led.off() |
| 62 | + from rtc import update_rtc_dict |
| 63 | + update_rtc_dict(data={_RTC_KEY_RUN: _RUN_WEB_SERVER}) # run web server on next boot |
| 64 | + from ota_client import OtaUpdate |
42 | 65 |
|
43 |
| - power_timer = PowerTimer() |
44 |
| - power_timer.schedule_next_switch() |
| 66 | + gc.collect() |
| 67 | + OtaUpdate().run() |
45 | 68 |
|
46 |
| - gc.collect() |
47 |
| - WebServer( |
48 |
| - power_timer=power_timer, |
49 |
| - watchdog=Watchdog( |
50 |
| - wifi=wifi, |
51 |
| - check_callback=power_timer.schedule_next_switch |
52 |
| - ), |
53 |
| - version=__version__ |
54 |
| - ).run() |
55 |
| -else: |
56 |
| - print('start OTA') |
57 |
| - Pins.power_led.off() |
58 |
| - update_rtc_dict(data={_RTC_KEY_RUN: _RUN_WEB_SERVER}) # run web server on next boot |
59 |
| - from ota_client import OtaUpdate |
| 69 | + from reset import ResetDevice |
| 70 | + ResetDevice(reason='unknown').reset() |
60 | 71 |
|
61 |
| - gc.collect() |
62 |
| - OtaUpdate().run() |
63 | 72 |
|
64 |
| -ResetDevice(reason='unknown').reset() |
| 73 | +main() |
0 commit comments