-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
58 lines (50 loc) · 1.42 KB
/
utils.py
File metadata and controls
58 lines (50 loc) · 1.42 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
import machine
import ntptime
import utime
import urandom
import config
if config.WDT_ENABLE:
print('using watchdog')
wdt_class = machine.WDT
else:
# fake watchdog
class WDT:
def feed(self):
pass
wdt_class = WDT
watchdog = wdt_class() # global use instance
def sleep_s(interval: int):
watchdog.feed()
for _ in range(interval):
utime.sleep(1)
watchdog.feed()
def randInt(min: int, max: int):
return int(round(urandom.getrandbits(8) / 255 * (max - min) + min))
def retry_on_error(func):
def looped_call(*args, **kwargs):
n = 1
while True:
if n > 1:
print(f'Attempt #{n}')
try:
return func(*args, **kwargs)
except Exception as e:
if n > 50:
raise e
print(f'Function {str(func)} failed {n} times')
sleep_s(n)
n += 1
return looped_call
@retry_on_error
def sync_time():
'''
Syncronize local time with NTP server
'''
ntptime.host = f'{randInt(0, 3)}.ru.pool.ntp.org'
print(f'begin clock synchronization using {ntptime.host}')
t = ntptime.time()
tz_sec = config.TIMEZONE * 60 * 60
tm = utime.localtime(t + tz_sec)
tm = tm[0:3] + (0,) + tm[3:6] + (0,)
machine.RTC().datetime(tm)
print('clock is synchronized')