wifi config while no 4g and wifi

This commit is contained in:
gcw_4spBpAfv
2026-04-07 17:29:24 +08:00
parent bdc3254ed2
commit ead2060ab3
7 changed files with 588 additions and 5 deletions

View File

@@ -410,6 +410,57 @@ class NetworkManager:
except Exception:
return False
def get_4g_phone_number(self):
"""
读取 SIM 本机号码AT+CNUM
典型响应:+CNUM: "","+861442093407954",145
部分运营商/卡未在卡内写入号码时可能为空。
Returns:
str: 号码(含国家码,如 +86138...),失败或未写入时返回 None
"""
try:
atc = hardware_manager.at_client
if atc is None:
return None
with self.get_uart_lock():
resp = atc.send("AT+CNUM", "OK", 3000)
if not resp:
return None
# 可能多行 +CNUM取第一个非空号码
for m in re.finditer(r'\+CNUM:\s*"[^"]*"\s*,\s*"([^"]*)"', resp):
num = (m.group(1) or "").strip()
if num:
return num
return None
except Exception:
return None
def get_4g_mccid(self):
"""
读取 MCCIDAT+MCCID模组侧命令常用于 SIM/卡标识类信息)。
典型响应行:+MCCID: <值> 或 +MCCID: \"...\"
Returns:
str: 解析到的字符串;失败时返回 None
"""
try:
atc = hardware_manager.at_client
if atc is None:
return None
with self.get_uart_lock():
resp = atc.send("AT+MCCID", "OK", 3000)
if not resp or "ERROR" in resp.upper():
return None
m = re.search(r"\+MCCID:\s*(.+)", resp, re.IGNORECASE)
if not m:
return None
val = (m.group(1) or "").strip()
# 去掉行尾 OK 之前可能粘在一起的杂质:只取第一行有效内容
val = val.split("\r")[0].split("\n")[0].strip()
val = val.strip('"').strip()
return val if val else None
except Exception:
return None
def _apply_session_force_4g(self):
"""锁定本次会话为 4G直到关机期间不再回切 WiFi"""
self._session_force_4g = True
@@ -1477,6 +1528,14 @@ class NetworkManager:
except:
ip = "error_getting_ip"
self.safe_enqueue({"result": "current_ip", "ip": ip}, 2)
elif inner_cmd == 44: # 读 4G 本机号码AT+CNUM
cnum = self.get_4g_phone_number()
self.logger.info(f"4G 本机号码: {cnum}")
self.safe_enqueue({"result": "cnum", "number": cnum if cnum is not None else ""}, 2)
elif inner_cmd == 45: # 读 MCCIDAT+MCCID
mccid = self.get_4g_mccid()
self.logger.info(f"4G MCCID: {mccid}")
self.safe_enqueue({"result": "mccid", "mccid": mccid if mccid is not None else ""}, 2)
# elif inner_cmd == 7:
# from ota_manager import ota_manager
# if ota_manager.update_thread_started: