feat: conn wifi

This commit is contained in:
2026-06-08 16:46:56 +08:00
parent 9dd6fef6f8
commit 1ea8c64a40

View File

@@ -627,6 +627,38 @@ class NetworkManager:
2, 2,
) )
def _cmd600_conn_wifi(self, data_obj):
hardware_manager.start_idle_timer()
inner_data = data_obj.get("data", {}) if isinstance(data_obj, dict) else {}
self.logger.info(f"[conn wifi] cmd600 , data: {inner_data}")
ssid = inner_data.get("ssid")
password = inner_data.get("password")
try:
w = network.wifi.Wifi()
e = w.connect(ssid, password, wait=True, timeout=15)
err.check_raise(e, "connect wifi failed")
if self.logger:
self.logger.info(f"[ota] Connect success, got ip{w.get_ip()}")
self.safe_enqueue(
{
"cmd": 600,
"result": "success",
"wifi": w.get_ip(),
},
2,
)
except Exception as e:
self.logger.error(f"cmd600 失败: {e}")
self.safe_enqueue(
{
"cmd": 600,
"result": "conn fail",
"reason": str(e),
},
2,
)
self._switch_to_4g_due_to_poor_wifi()
def safe_enqueue(self, data_dict, msg_type=2, high=False): def safe_enqueue(self, data_dict, msg_type=2, high=False):
"""线程安全地将消息加入队列(公共方法)""" """线程安全地将消息加入队列(公共方法)"""
self._enqueue((msg_type, data_dict), high) self._enqueue((msg_type, data_dict), high)
@@ -2162,6 +2194,12 @@ class NetworkManager:
self.logger.info("[New Ota] cmd300 在后台线程执行OTA") self.logger.info("[New Ota] cmd300 在后台线程执行OTA")
import _thread import _thread
_thread.start_new_thread(self._cmd300_ota, (data_obj,)) _thread.start_new_thread(self._cmd300_ota, (data_obj,))
elif inner_cmd == 600:
self.logger.info("[conn wifi] cmd600 在后台线程执行连接wifi: {data_obj}")
import _thread
_thread.start_new_thread(self._cmd600_conn_wifi, (data_obj,))
elif inner_cmd == 601:
pass
else: # data的结构不是 dict else: # data的结构不是 dict
self.logger.info(f"[NET] body={body}, {time.time()}") self.logger.info(f"[NET] body={body}, {time.time()}")
else: else:
@@ -2262,7 +2300,7 @@ class NetworkManager:
self._tcp_connected = False self._tcp_connected = False
self.logger.error("连接异常2秒后重连...") self.logger.error("连接异常2秒后重连...")
time.sleep_ms(2000) time.sleep_ms(200)
except Exception as e: except Exception as e:
# TCP主循环的顶层异常捕获防止线程静默退出 # TCP主循环的顶层异常捕获防止线程静默退出
@@ -2270,7 +2308,7 @@ class NetworkManager:
import traceback import traceback
self.logger.error(traceback.format_exc()) self.logger.error(traceback.format_exc())
self._tcp_connected = False self._tcp_connected = False
time.sleep_ms(5000) # 等待5秒后重试连接 time.sleep_ms(500) # 等待5秒后重试连接
# 创建全局单例实例 # 创建全局单例实例