v1.2.2
This commit is contained in:
57
network.py
57
network.py
@@ -232,47 +232,49 @@ class NetworkManager:
|
||||
Returns:
|
||||
(ip, error): IP地址和错误信息(成功时error为None)
|
||||
"""
|
||||
conf_path = "/etc/wpa_supplicant.conf"
|
||||
ssid_file = "/boot/wifi.ssid"
|
||||
pass_file = "/boot/wifi.pass"
|
||||
|
||||
# 配置文件路径定义
|
||||
conf_path = "/etc/wpa_supplicant.conf" # wpa_supplicant配置文件路径
|
||||
ssid_file = "/boot/wifi.ssid" # 用于保存SSID的文件路径
|
||||
pass_file = "/boot/wifi.pass" # 用于保存密码的文件路径
|
||||
|
||||
try:
|
||||
# 生成 wpa_supplicant 配置
|
||||
net_conf = os.popen(f'wpa_passphrase "{ssid}" "{password}"').read()
|
||||
if "network={" not in net_conf:
|
||||
net_conf = os.popen(f'wpa_passphrase "{ssid}" "{password}"').read() # 调用系统命令生成配置
|
||||
if "network={" not in net_conf: # 检查配置是否生成成功
|
||||
return None, "Failed to generate wpa config"
|
||||
|
||||
# 写入运行时配置
|
||||
with open(conf_path, "w") as f:
|
||||
f.write("ctrl_interface=/var/run/wpa_supplicant\n")
|
||||
f.write("update_config=1\n\n")
|
||||
f.write(net_conf)
|
||||
with open(conf_path, "w") as f: # 打开配置文件准备写入
|
||||
f.write("ctrl_interface=/var/run/wpa_supplicant\n") # 设置控制接口路径
|
||||
f.write("update_config=1\n\n") # 允许更新配置
|
||||
f.write(net_conf) # 写入网络配置
|
||||
|
||||
# 持久化保存 SSID/PASS
|
||||
with open(ssid_file, "w") as f:
|
||||
f.write(ssid.strip())
|
||||
with open(pass_file, "w") as f:
|
||||
f.write(password.strip())
|
||||
with open(ssid_file, "w") as f: # 打开SSID文件准备写入
|
||||
f.write(ssid.strip()) # 写入SSID(去除首尾空格)
|
||||
with open(pass_file, "w") as f: # 打开密码文件准备写入
|
||||
f.write(password.strip()) # 写入密码(去除首尾空格)
|
||||
|
||||
# 重启 Wi-Fi 服务
|
||||
os.system("/etc/init.d/S30wifi restart")
|
||||
os.system("/etc/init.d/S30wifi restart") # 执行WiFi服务重启命令
|
||||
|
||||
# 等待获取 IP
|
||||
import time as std_time
|
||||
for _ in range(20):
|
||||
ip = os.popen("ifconfig wlan0 2>/dev/null | grep 'inet ' | awk '{print $2}'").read().strip()
|
||||
if ip:
|
||||
self._wifi_connected = True
|
||||
self._wifi_ip = ip
|
||||
self.logger.info(f"[WIFI] 已连接,IP: {ip}")
|
||||
import time as std_time # 导入time模块并重命名为std_time
|
||||
for _ in range(50): # 最多等待50秒
|
||||
ip = os.popen("ifconfig wlan0 2>/dev/null | grep 'inet ' | awk '{print $2}'").read().strip() # 获取wlan0的IP地址
|
||||
if ip: # 如果获取到IP地址
|
||||
self._wifi_connected = True # 设置WiFi连接状态为已连接
|
||||
self._wifi_ip = ip # 保存IP地址
|
||||
self.logger.info(f"[WIFI] 已连接,IP: {ip}") # 记录连接成功日志
|
||||
return ip, None
|
||||
std_time.sleep(1)
|
||||
std_time.sleep(1) # 每次循环等待1秒
|
||||
|
||||
return None, "Timeout: No IP obtained"
|
||||
return None, "Timeout: No IP obtained" # 超时未获取到IP
|
||||
|
||||
except Exception as e:
|
||||
self.logger.error(f"[WIFI] 连接失败: {e}")
|
||||
return None, f"Exception: {str(e)}"
|
||||
except Exception as e: # 捕获所有异常
|
||||
self.logger.error(f"[WIFI] 连接失败: {e}") # 记录错误日志
|
||||
return None, f"Exception: {str(e)}" # 返回异常信息
|
||||
|
||||
def is_server_reachable(self, host, port=80, timeout=5):
|
||||
"""检查目标主机端口是否可达(用于网络检测)"""
|
||||
@@ -308,6 +310,9 @@ class NetworkManager:
|
||||
if self.is_server_reachable(self._server_ip, self._server_port, timeout=3):
|
||||
self._network_type = "wifi"
|
||||
self.logger.info(f"[NET] 选择WiFi网络,IP: {self._wifi_ip}")
|
||||
import os
|
||||
os.environ["TZ"] = "Asia/Shanghai"
|
||||
os.system("ntpdate pool.ntp.org")
|
||||
return "wifi"
|
||||
else:
|
||||
self.logger.warning("[NET] WiFi已连接但无法访问服务器,尝试4G")
|
||||
|
||||
Reference in New Issue
Block a user