-
Notifications
You must be signed in to change notification settings - Fork 0
/
sta_network.py
63 lines (51 loc) · 1.69 KB
/
sta_network.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
from micropython import const
import network
import time
import config
_TIMEOUT = const(30)
class STANetwork:
def __init__(self):
self._sta_if = network.WLAN(network.STA_IF)
self._expires_at = None
def __del__(self):
if self._sta_if.active():
self._sta_if.disconnect()
self._sta_if.active(False)
def safe_connect(self, timeout=None, progress=None):
if self._sta_if.isconnected():
# already connected
self._expires_at = None
elif self._expires_at is not None:
# connecting
pass
else:
# not connected
if not timeout:
timeout = _TIMEOUT
self._connect(timeout=timeout, progress=progress)
def _connect(self, timeout, progress=None):
if not self._sta_if.active():
self._sta_if.active(True)
self._sta_if.connect(config.ESSID, config.PASSWORD)
self._expires_at = time.time() + timeout
if progress and callable(progress):
while not self._sta_if.isconnected():
if self.is_timeout():
break
progress()
if self._sta_if.isconnected():
self._expires_at = None
def disconnect(self):
if self._sta_if.active():
self._sta_if.disconnect()
self._sta_if.active(False)
self._expires_at = None
def is_connected(self):
connected = self._sta_if.isconnected()
if connected:
self._expires_at = None
return connected
def is_timeout(self):
if self._expires_at is None:
return False
return self._expires_at < time.time()