[wifi] update

This commit is contained in:
IrvingGao 2024-06-09 15:06:31 +08:00
parent 9454163063
commit 9949c7f58c
1 changed files with 8 additions and 4 deletions

View File

@ -49,7 +49,7 @@ def edit_line_in_file(file_path, search_string, replacement_string):
else: else:
file.write(line) file.write(line)
print("The line has been edited.") print(f"{datetime.datetime.now()}: The line has been edited.")
except FileNotFoundError: except FileNotFoundError:
print(f"The file {file_path} does not exist.") print(f"The file {file_path} does not exist.")
except IOError as e: except IOError as e:
@ -66,6 +66,7 @@ def save_local_ip():
# save local_ip to file # save local_ip to file
with open('local_ip.txt', 'w') as f: with open('local_ip.txt', 'w') as f:
f.write(local_ip) f.write(local_ip)
print(f"{datetime.datetime.now()}: Local IP address: {local_ip}")
return local_ip return local_ip
except Exception as e: except Exception as e:
print(f"Error getting private IP: {e}") print(f"Error getting private IP: {e}")
@ -116,7 +117,7 @@ def close_hotspot():
def check_wifi_connection(): def check_wifi_connection():
cmd = "nmcli dev status" cmd = "nmcli dev status"
result = subprocess.run(cmd, shell=True, capture_output=True, text=True) result = subprocess.run(cmd, shell=True, capture_output=True, text=True)
time.sleep(1) time.sleep(2)
output = result.stdout.strip() output = result.stdout.strip()
lines = output.split("\n")[1:] # Skip the header line lines = output.split("\n")[1:] # Skip the header line
for line in lines: for line in lines:
@ -150,7 +151,7 @@ def check_wifi_connection():
network_error_light() network_error_light()
subprocess.Popen('sudo systemctl restart NetworkManager', shell=True) subprocess.Popen('sudo systemctl restart NetworkManager', shell=True)
# subprocess.run(['sudo', 'systemctl', 'restart', 'NetworkManager'], check=True) # subprocess.run(['sudo', 'systemctl', 'restart', 'NetworkManager'], check=True)
time.sleep(5) time.sleep(2)
return False, None return False, None
def scan_wifi(): def scan_wifi():
@ -161,6 +162,7 @@ def scan_wifi():
except subprocess.CalledProcessError as e: except subprocess.CalledProcessError as e:
print(f"{datetime.datetime.now()}: Error scanning Wi-Fi: {e}") print(f"{datetime.datetime.now()}: Error scanning Wi-Fi: {e}")
network_error_light() network_error_light()
print(f"{datetime.datetime.now()}: Wi-Fi scan complete...")
ssid_list = [] ssid_list = []
wifi_list = [] wifi_list = []
output = result.stdout.strip() output = result.stdout.strip()
@ -207,6 +209,8 @@ def scan_wifi():
# print("强度:", strength) # print("强度:", strength)
wifi_list.append({'ssid': ssid, 'signal': strength, 'mac': mac_address}) wifi_list.append({'ssid': ssid, 'signal': strength, 'mac': mac_address})
with open('scaned_wifi_list.json', 'w') as f:
json.dump(wifi_list, f)
if len(wifi_list) == 15: if len(wifi_list) == 15:
break break
@ -304,7 +308,7 @@ def submit():
wifi_list = scan_wifi() wifi_list = scan_wifi()
start_hotspot() start_hotspot()
else: else:
save_wifi(wifi_ssid, password) # 保存连接成功的Wi-Fi信息 save_wifi(ssid, password) # 保存连接成功的Wi-Fi信息
return redirect(url_for('index')) return redirect(url_for('index'))