实现显示屏。
This commit is contained in:
67
system.py
67
system.py
@@ -14,13 +14,16 @@ import shared_vars
|
||||
|
||||
from SingletonThreadPool import SingletonThreadPool
|
||||
|
||||
def connect_wifi(ssid,password):
|
||||
def connect_wifi(ssid, password):
|
||||
sta_if = network.WLAN(network.STA_IF)
|
||||
if not sta_if.isconnected():
|
||||
print("正在连接Wi-Fi...")
|
||||
sta_if.active(True)
|
||||
sta_if.connect(ssid, password)
|
||||
start_time = time.time()
|
||||
while not sta_if.isconnected():
|
||||
if time.time() - start_time > 10: # 超时10秒
|
||||
raise Exception("Wi-Fi连接超时")
|
||||
time.sleep(1)
|
||||
print("Wi-Fi连接成功", sta_if.ifconfig())
|
||||
return sta_if
|
||||
@@ -120,7 +123,7 @@ def handle_action(action, data):
|
||||
print(f"执行 {action} 方法")
|
||||
shared_vars.handle_task_id = generate_random_hex()
|
||||
#method(data)
|
||||
thread_pool = SingletonThreadPool()
|
||||
|
||||
|
||||
thread_pool.add_task(method,data)
|
||||
|
||||
@@ -270,6 +273,66 @@ def send_text(sock, message):
|
||||
print(f"发送失败:{e}")
|
||||
return False
|
||||
|
||||
def reset_watchdog():
|
||||
"""重置看门狗计时"""
|
||||
shared_vars.watchdog_last_reset = time.time()
|
||||
|
||||
def watchdog_thread():
|
||||
"""看门狗线程,超过 1 分钟未重置则重启设备"""
|
||||
while True:
|
||||
if time.time() - shared_vars.watchdog_last_reset > 60: # 超过 1 分钟
|
||||
print("看门狗超时,设备即将重启...")
|
||||
reset()
|
||||
time.sleep(1)
|
||||
|
||||
def connect_to_stored_wifi():
|
||||
"""
|
||||
尝试连接存储的 WiFi 列表,如果全部失败,则连接默认 WiFi
|
||||
"""
|
||||
# 获取存储的 WiFi 列表
|
||||
wifi_list = shared_vars.config_manager.get("wifi.list", [])
|
||||
print("尝试连接存储的 WiFi 列表:", wifi_list)
|
||||
|
||||
wlan = None
|
||||
for wifi in wifi_list:
|
||||
ssid = wifi.get("ssid")
|
||||
password = wifi.get("password")
|
||||
if not ssid or password is None: # 只检查 SSID 是否有效
|
||||
continue
|
||||
|
||||
print(f"尝试连接 WiFi: {ssid}")
|
||||
try:
|
||||
wlan = network.WLAN(network.STA_IF)
|
||||
wlan.active(False)
|
||||
wlan.active(True)
|
||||
wlan.connect(ssid, password)
|
||||
|
||||
# 设置超时时间为 10 秒
|
||||
start_time = time.time()
|
||||
while not wlan.isconnected():
|
||||
if time.time() - start_time > 10: # 超时 10 秒
|
||||
print(f"连接 WiFi {ssid} 超时,尝试下一个")
|
||||
break
|
||||
time.sleep(1)
|
||||
|
||||
if wlan.isconnected():
|
||||
print(f"成功连接到 WiFi: {ssid}")
|
||||
|
||||
# 将成功连接的 WiFi 移到列表最前面
|
||||
wifi_list.remove(wifi)
|
||||
wifi_list.insert(0, wifi)
|
||||
shared_vars.config_manager.set("wifi.list", wifi_list)
|
||||
reset_watchdog() # 重置看门狗
|
||||
return wlan
|
||||
except Exception as e:
|
||||
print(f"连接 WiFi {ssid} 失败: {e}")
|
||||
wlan.active(False)
|
||||
# 如果所有存储的 WiFi 都连接失败,则连接默认 WiFi
|
||||
print("所有存储的 WiFi 都连接失败,尝试连接默认 WiFi")
|
||||
wlan = connect_wifi(shared_vars.WIFI_SSID, shared_vars.WIFI_PASSWORD)
|
||||
reset_watchdog() # 重置看门狗
|
||||
return wlan
|
||||
|
||||
|
||||
def generate_random_hex(length = 32):
|
||||
# 初始化一个空的字节数组,用于存储随机字节
|
||||
|
||||
Reference in New Issue
Block a user