2 Commits
Author SHA1 Message Date
linyimin f0df9ad915 fix: 重连时间设置更小 2026-07-31 14:09:11 +08:00
linyimin 3fcd38f417 fix: 4g通讯 2026-07-31 13:59:48 +08:00
3 changed files with 48 additions and 14 deletions
+7 -6
View File
@@ -76,10 +76,11 @@ class ATClient:
""" """
expect_b = expect.encode() if isinstance(expect, str) else expect expect_b = expect.encode() if isinstance(expect, str) else expect
with self._cmd_lock: with self._cmd_lock:
# 初始化等待 with self._q_lock:
self._waiting = True # 初始化等待
self._expect = expect_b self._waiting = True
self._resp = b"" self._expect = expect_b
self._resp = b""
# 发送 # 发送
if cmd: if cmd:
@@ -300,8 +301,8 @@ class ATClient:
if len(self._rx) > 512 * 1024: if len(self._rx) > 512 * 1024:
self._rx = self._rx[-256 * 1024:] self._rx = self._rx[-256 * 1024:]
else: else:
if len(self._rx) > 16384: if len(self._rx) > 32768:
self._rx = self._rx[-4096:] self._rx = self._rx[-16384:]
+1 -1
View File
@@ -24,7 +24,7 @@ TRIANGLE_DETECT_SCALE = 0.4
# SERVER_IP = "stcp.shelingxingqiu.com" # SERVER_IP = "stcp.shelingxingqiu.com"
SERVER_IP = "www.shelingxingqiu.com" SERVER_IP = "www.shelingxingqiu.com"
SERVER_PORT = 50005 SERVER_PORT = 50005
HEARTBEAT_INTERVAL = 15 # 心跳间隔(秒) HEARTBEAT_INTERVAL = 5 # 心跳间隔(秒)
# WiFi 质量评估(开机先尝试 WiFi;质量差且 4G 可用则切到 4G,本次上电直至关机锁定 4G) # WiFi 质量评估(开机先尝试 WiFi;质量差且 4G 可用则切到 4G,本次上电直至关机锁定 4G)
WIFI_QUALITY_RTT_SAMPLES = 3 # 到业务服务器 TCP 建连耗时采样次数,取中位数 WIFI_QUALITY_RTT_SAMPLES = 3 # 到业务服务器 TCP 建连耗时采样次数,取中位数
+40 -7
View File
@@ -721,7 +721,7 @@ class NetworkManager:
if self._network_type == "wifi": if self._network_type == "wifi":
return self._check_wifi_connection() return self._check_wifi_connection()
elif self._network_type == "4g": elif self._network_type == "4g":
return True # 4G连接状态由AT命令维护 return self._check_4g_connection()
return False return False
# 自动选择网络 # 自动选择网络
@@ -738,6 +738,37 @@ class NetworkManager:
return self._connect_tcp_via_4g() return self._connect_tcp_via_4g()
return False return False
def _check_4g_connection(self):
"""检查4G TCP连接是否仍然有效(通过查询PDP地址验证网络附着状态)"""
try:
atc = hardware_manager.at_client
if atc is None:
return False
if not self._uart4g_lock.acquire(timeout=3000):
# 获取锁超时说明有其他操作在进行,视为连接仍有效
return True
try:
r = atc.send("AT+CGPADDR=1", "OK", 3000)
m = re.search(r'\+CGPADDR:\s*1,"([^"]+)"', r)
ip = m.group(1) if m else ""
if ip and ip != "0.0.0.0":
return True
# 无IP或IP无效,尝试重新激活PDP
self.logger.warning("[4G-TCP] PDP地址无效,尝试重新激活")
atc.send("AT+MIPCALL=1,1", "OK", 15000)
r2 = atc.send("AT+CGPADDR=1", "OK", 3000)
m2 = re.search(r'\+CGPADDR:\s*1,"([^"]+)"', r2)
ip2 = m2.group(1) if m2 else ""
if ip2 and ip2 != "0.0.0.0":
return True
self.logger.error("[4G-TCP] 重新激活PDP仍无有效IP,连接已断开")
return False
finally:
self._uart4g_lock.release()
except Exception as e:
self.logger.warning(f"[4G-TCP] 连接检查异常: {e}")
return True # 异常时不误判断线
def _wrap_wifi_tls(self, plain_sock, hostname): def _wrap_wifi_tls(self, plain_sock, hostname):
""" """
在已建立的 TCP socket 上做 TLSWiFi 走主机 ssl 库;4G 仍用模组 AT+SSL)。 在已建立的 TCP socket 上做 TLSWiFi 走主机 ssl 库;4G 仍用模组 AT+SSL)。
@@ -1085,6 +1116,8 @@ class NetworkManager:
total += n total += n
hardware_manager.uart4g.write(b"\x1A") hardware_manager.uart4g.write(b"\x1A")
with hardware_manager.at_client._q_lock:
hardware_manager.at_client._rx = b""
r = hardware_manager.at_client.send("", "OK", 8000) r = hardware_manager.at_client.send("", "OK", 8000)
if ("SEND OK" in r) or ("OK" in r) or ("+MIPSEND" in r): if ("SEND OK" in r) or ("OK" in r) or ("+MIPSEND" in r):
return True return True
@@ -1799,7 +1832,7 @@ class NetworkManager:
continue continue
if not self.connect_server(): if not self.connect_server():
time.sleep_ms(5000) time.sleep_ms(1000)
continue continue
# 发送登录包 # 发送登录包
@@ -1820,7 +1853,7 @@ class NetworkManager:
self.disconnect_server() self.disconnect_server()
except: except:
pass pass
time.sleep_ms(2000) time.sleep_ms(500)
continue continue
self.logger.info("➡️ 登录包已发送,等待确认...") self.logger.info("➡️ 登录包已发送,等待确认...")
@@ -2309,8 +2342,8 @@ class NetworkManager:
pass pass
break break
else: else:
# 不立即断开,让下一轮心跳再试;同时缩短一点等待,提升恢复速度 # 不立即断开,让下一轮心跳再试
time.sleep_ms(200) time.sleep_ms(50)
continue continue
else: else:
send_hartbeat_fail_count = 0 send_hartbeat_fail_count = 0
@@ -2340,8 +2373,8 @@ class NetworkManager:
self._send_event.clear() self._send_event.clear()
self._tcp_connected = False self._tcp_connected = False
self.logger.error("连接异常,2秒后重连...") self.logger.error("连接异常,50ms后重连...")
time.sleep_ms(200) time.sleep_ms(50)
except Exception as e: except Exception as e:
# TCP主循环的顶层异常捕获,防止线程静默退出 # TCP主循环的顶层异常捕获,防止线程静默退出