[wifi] fix big bugs
This commit is contained in:
parent
9ac857e9ac
commit
c0e8bdd6af
|
@ -54,6 +54,7 @@ def network_error_light_2():
|
|||
# close_app()
|
||||
|
||||
def edit_line_in_file(file_path, search_string, replacement_string):
|
||||
edit = False
|
||||
try:
|
||||
# 读取文件内容
|
||||
with open(file_path, 'r') as file:
|
||||
|
@ -66,15 +67,17 @@ def edit_line_in_file(file_path, search_string, replacement_string):
|
|||
for line in lines:
|
||||
if search_string in line:
|
||||
file.write(replacement_string + '\n')
|
||||
edit = True
|
||||
else:
|
||||
file.write(line)
|
||||
|
||||
logging.info(f"{datetime.datetime.now()}: The line has been edited.")
|
||||
return edit
|
||||
except FileNotFoundError:
|
||||
logging.info(f"The file {file_path} does not exist.")
|
||||
return edit
|
||||
except IOError as e:
|
||||
logging.info(f"An error occurred: {e}")
|
||||
|
||||
return edit
|
||||
|
||||
# 获取私有IP地址
|
||||
def save_local_ip():
|
||||
|
@ -133,11 +136,28 @@ def close_hotspot():
|
|||
logging.info(f"{datetime.datetime.now()}: Error stopping hotspot: {e}")
|
||||
|
||||
|
||||
def init_networkmanager_file():
|
||||
file_path = '/etc/NetworkManager/NetworkManager.conf'
|
||||
search_string = 'unmanaged-devices=interface-name:'
|
||||
replacement_string = '' # 如果不需要替换为其他内容,可以设置为空字符串
|
||||
if edit_line_in_file(file_path, search_string, replacement_string):
|
||||
'''
|
||||
# 从 /etc/NetworkManager/NetworkManager.conf 文件中移除或注释掉 [keyfile] 部分的 unmanaged-devices 条目,因为它可能阻止了 NetworkManager 管理 wlan0 接口。编辑文件如下:
|
||||
```
|
||||
[keyfile]
|
||||
#unmanaged-devices=interface-name:ap0;interface-name:wlan0
|
||||
```
|
||||
sudo systemctl restart NetworkManager
|
||||
'''
|
||||
# network_error_light_1()
|
||||
logging.info(f"{datetime.datetime.now()}: Wi-Fi NetworkManager Error: restart NetworkManager.")
|
||||
subprocess.Popen('sudo systemctl restart NetworkManager', shell=True)
|
||||
time.sleep(3)
|
||||
|
||||
# 检测 Wi-Fi 连接状态
|
||||
def check_wifi_connection():
|
||||
cmd = "nmcli dev status"
|
||||
result = subprocess.run(cmd, shell=True, capture_output=True, text=True)
|
||||
time.sleep(2)
|
||||
output = result.stdout.strip()
|
||||
lines = output.split("\n")[1:] # Skip the header line
|
||||
for line in lines:
|
||||
|
@ -154,26 +174,6 @@ def check_wifi_connection():
|
|||
if led_enabled:
|
||||
orangepi.set_led_off('blue')
|
||||
return True, wifi_ssid
|
||||
elif len(columns) >= 4 and columns[0] == "wlan0" and columns[2] == "unmanaged":
|
||||
# 使用函数
|
||||
file_path = '/etc/NetworkManager/NetworkManager.conf'
|
||||
search_string = 'unmanaged-devices=interface-name:'
|
||||
replacement_string = '' # 如果不需要替换为其他内容,可以设置为空字符串
|
||||
edit_line_in_file(file_path, search_string, replacement_string)
|
||||
'''
|
||||
# 从 /etc/NetworkManager/NetworkManager.conf 文件中移除或注释掉 [keyfile] 部分的 unmanaged-devices 条目,因为它可能阻止了 NetworkManager 管理 wlan0 接口。编辑文件如下:
|
||||
```
|
||||
[keyfile]
|
||||
#unmanaged-devices=interface-name:ap0;interface-name:wlan0
|
||||
```
|
||||
sudo systemctl restart NetworkManager
|
||||
'''
|
||||
network_error_light_1()
|
||||
logging.info(f"{datetime.datetime.now()}: Wi-Fi NetworkManager Error: restart NetworkManager.")
|
||||
subprocess.Popen('sudo systemctl restart NetworkManager', shell=True)
|
||||
# subprocess.run(['sudo', 'systemctl', 'restart', 'NetworkManager'], check=True)
|
||||
time.sleep(2)
|
||||
return check_wifi_connection()
|
||||
return False, None
|
||||
|
||||
def scan_wifi():
|
||||
|
@ -243,7 +243,7 @@ def scan_wifi():
|
|||
def connect_wifi(ssid, password):
|
||||
# 连接到用户选择的 Wi-Fi 网络
|
||||
try:
|
||||
output = subprocess.check_output(['nmcli', 'dev', 'wifi', 'connect', ssid, 'password', password])
|
||||
output = subprocess.check_output(['nmcli', 'dev', 'wifi', 'connect', ssid, 'password', password, 'autoconnect', 'yes'])
|
||||
output_str = output.decode('utf-8') # 将输出转换为字符串
|
||||
if "successfully" in output_str:
|
||||
logging.info(f"{datetime.datetime.now()}: Successfully connected to Wi-Fi: {ssid}")
|
||||
|
@ -260,7 +260,6 @@ def connect_wifi(ssid, password):
|
|||
def disconnect_wifi():
|
||||
try:
|
||||
output = subprocess.check_output(['nmcli', 'dev', 'disconnect', 'iface', 'wlan0'])
|
||||
time.sleep(0.5)
|
||||
output_str = output.decode('utf-8') # 将输出转换为字符串
|
||||
if "successfully disconnected" in output_str:
|
||||
logging.info(f"{datetime.datetime.now()}: Wi-Fi disconnected successfully")
|
||||
|
@ -275,7 +274,7 @@ def connect_saved_wifi(scaned_wifi_list):
|
|||
return False, None
|
||||
for wifi in wifi_list:
|
||||
if wifi['ssid'] in [item['ssid'] for item in scaned_wifi_list]:
|
||||
connect_wifi(wifi['ssid'], wifi['password'])
|
||||
if connect_wifi(wifi['ssid'], wifi['password']):
|
||||
return True, wifi['ssid']
|
||||
return check_wifi_connection()
|
||||
|
||||
|
@ -342,19 +341,12 @@ if __name__ == '__main__':
|
|||
debug_mode = False # 设置为 True 以跳过 Wi-Fi 连接状态检测
|
||||
if debug_mode:
|
||||
disconnect_wifi()
|
||||
# wifi_list = scan_wifi()
|
||||
# logging.info(wifi_list)
|
||||
|
||||
# start_hotspot()
|
||||
|
||||
# app.run(host='0.0.0.0', port=80)
|
||||
|
||||
# if connect_wifi("Innoxsz-Public", "innox2023"):
|
||||
# close_app()
|
||||
|
||||
if led_enabled:
|
||||
orangepi.set_led_on('blue')
|
||||
|
||||
init_networkmanager_file() # 初始化 NetworkManager 配置文件
|
||||
|
||||
connected, wifi_ssid = check_wifi_connection()
|
||||
if connected:
|
||||
logging.info(f"{datetime.datetime.now()}: 系统已自动连接到 Wi-Fi 网络,退出程序")
|
||||
|
|
Loading…
Reference in New Issue