Skip to content

Commit c348ae1

Browse files
committed
update: auto check network(cfun switch or power restart)
1 parent a7d43c5 commit c348ae1

4 files changed

Lines changed: 48 additions & 0 deletions

File tree

__init__.py

Whitespace-only changes.

code/__init__.py

Whitespace-only changes.

code/modules/net_manager.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
"""
2+
检查sim卡和网络状态,如有异常尝试CFUN切换或重启
3+
"""
4+
import net
5+
import utime
6+
import _thread
7+
import checkNet
8+
from misc import Power
9+
10+
11+
class NetManager(object):
12+
RECONNECT_LOCK = _thread.allocate_lock()
13+
14+
@classmethod
15+
def reconnect(cls, retry=3):
16+
with cls.RECONNECT_LOCK:
17+
for _ in range(retry):
18+
if cls.wait_connect():
19+
return True
20+
cls.__cfun_switch()
21+
else:
22+
Power.powerRestart()
23+
return False
24+
25+
@classmethod
26+
def wait_connect(cls, timeout=30):
27+
stage, state = checkNet.waitNetworkReady(timeout)
28+
# print('network status code: {}'.format((stage, state)))
29+
return stage == 3 and state == 1
30+
31+
@classmethod
32+
def __cfun_switch(cls):
33+
cls.disconnect()
34+
utime.sleep_ms(200)
35+
cls.connect()
36+
37+
@classmethod
38+
def connect(cls):
39+
return net.setModemFun(1, 0) == 0
40+
41+
@classmethod
42+
def disconnect(cls):
43+
return net.setModemFun(0, 0) == 0

code/modules/remote.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import _thread
1616
from usr.modules.logging import getLogger
1717
from usr.modules.common import Observable, CloudObserver
18+
from usr.modules.net_manager import NetManager
1819

1920

2021
log = getLogger(__name__)
@@ -147,6 +148,10 @@ def post_data(self, data, topic_id):
147148
"gps": []
148149
}
149150
"""
151+
if not NetManager.reconnect():
152+
log.error('Net Work not ready.')
153+
return
154+
150155
res = True
151156
if self.__cloud_conn():
152157
if not self.__cloud_post(data, topic_id):

0 commit comments

Comments
 (0)