[scan] wifi
This commit is contained in:
parent
fd1a26d0c3
commit
a13591b2be
|
@ -68,7 +68,7 @@ def edit_line_in_file(file_path, search_string, replacement_string):
|
||||||
if search_string in line:
|
if search_string in line:
|
||||||
file.write(replacement_string + '\n')
|
file.write(replacement_string + '\n')
|
||||||
edit = True
|
edit = True
|
||||||
logging.info(f"{datetime.datetime.now()}: The line has been edited. \noriginal line: {line}, \nnew line: {replacement_string}")
|
logging.info(f"The line has been edited. \noriginal line: {line}, \nnew line: {replacement_string}")
|
||||||
else:
|
else:
|
||||||
file.write(line)
|
file.write(line)
|
||||||
return edit
|
return edit
|
||||||
|
@ -89,7 +89,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)
|
||||||
logging.info(f"{datetime.datetime.now()}: Local IP address: {local_ip}")
|
logging.info(f"Local IP address: {local_ip}")
|
||||||
return local_ip
|
return local_ip
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.info(f"Error getting private IP: {e}")
|
logging.info(f"Error getting private IP: {e}")
|
||||||
|
@ -104,7 +104,7 @@ def close_app():
|
||||||
# 保存Wi-Fi IP地址
|
# 保存Wi-Fi IP地址
|
||||||
save_local_ip()
|
save_local_ip()
|
||||||
|
|
||||||
logging.info(f"{datetime.datetime.now()}: Exit Wi-Fi hotspot...")
|
logging.info(f"Exit Wi-Fi hotspot...")
|
||||||
|
|
||||||
# 获取当前Flask应用程序的进程ID
|
# 获取当前Flask应用程序的进程ID
|
||||||
flask_pid = os.getpid()
|
flask_pid = os.getpid()
|
||||||
|
@ -125,17 +125,17 @@ def start_hotspot():
|
||||||
if led_enabled:
|
if led_enabled:
|
||||||
orangepi.set_led_on('red')
|
orangepi.set_led_on('red')
|
||||||
except subprocess.CalledProcessError as e:
|
except subprocess.CalledProcessError as e:
|
||||||
logging.info(f"{datetime.datetime.now()}: Error starting create_ap service: {e}")
|
logging.info(f"Error starting create_ap service: {e}")
|
||||||
|
|
||||||
def close_hotspot():
|
def close_hotspot():
|
||||||
# 关闭热点
|
# 关闭热点
|
||||||
try:
|
try:
|
||||||
subprocess.Popen('sudo systemctl stop hotspot.service', shell=True)
|
subprocess.Popen('sudo systemctl stop hotspot.service', shell=True)
|
||||||
logging.info(f"{datetime.datetime.now()}: Stopping create_ap service")
|
logging.info(f"Stopping create_ap service")
|
||||||
if led_enabled:
|
if led_enabled:
|
||||||
orangepi.set_led_off('red')
|
orangepi.set_led_off('red')
|
||||||
except subprocess.CalledProcessError as e:
|
except subprocess.CalledProcessError as e:
|
||||||
logging.info(f"{datetime.datetime.now()}: Error stopping hotspot: {e}")
|
logging.info(f"Error stopping hotspot: {e}")
|
||||||
|
|
||||||
|
|
||||||
def init_networkmanager_file():
|
def init_networkmanager_file():
|
||||||
|
@ -152,9 +152,9 @@ def init_networkmanager_file():
|
||||||
sudo systemctl restart NetworkManager
|
sudo systemctl restart NetworkManager
|
||||||
'''
|
'''
|
||||||
# network_error_light_1()
|
# network_error_light_1()
|
||||||
logging.info(f"{datetime.datetime.now()}: Wi-Fi NetworkManager Error: restart NetworkManager.")
|
logging.info(f"Wi-Fi NetworkManager Error: restart NetworkManager.")
|
||||||
subprocess.Popen('sudo systemctl restart NetworkManager', shell=True)
|
subprocess.Popen('sudo systemctl restart NetworkManager', shell=True)
|
||||||
time.sleep(5) # 不得修改该时间,保留足够长时间给NetworkManager重启完成,并连接以往WiFi。
|
time.sleep(2) # 不得修改该时间,保留足够长时间给NetworkManager重启完成,并连接以往WiFi。
|
||||||
|
|
||||||
# 检测 Wi-Fi 连接状态
|
# 检测 Wi-Fi 连接状态
|
||||||
def check_wifi_connection():
|
def check_wifi_connection():
|
||||||
|
@ -188,10 +188,10 @@ def scan_wifi():
|
||||||
cmd = "nmcli dev wifi"
|
cmd = "nmcli dev wifi"
|
||||||
result = subprocess.run(cmd, shell=True, capture_output=True, text=True)
|
result = subprocess.run(cmd, shell=True, capture_output=True, text=True)
|
||||||
except subprocess.CalledProcessError as e:
|
except subprocess.CalledProcessError as e:
|
||||||
logging.info(f"{datetime.datetime.now()}: Error scanning Wi-Fi: {e}")
|
logging.info(f"Error scanning Wi-Fi: {e}")
|
||||||
network_error_light_1()
|
network_error_light_1()
|
||||||
return []
|
return []
|
||||||
logging.info(f"{datetime.datetime.now()}: Wi-Fi scan complete...")
|
logging.info(f"Wi-Fi scan complete...")
|
||||||
ssid_list = []
|
ssid_list = []
|
||||||
wifi_list = []
|
wifi_list = []
|
||||||
output = result.stdout.strip()
|
output = result.stdout.strip()
|
||||||
|
@ -245,7 +245,7 @@ def scan_wifi():
|
||||||
break
|
break
|
||||||
with open('scaned_wifi_list.json', 'w') as f:
|
with open('scaned_wifi_list.json', 'w') as f:
|
||||||
json.dump(wifi_list, f)
|
json.dump(wifi_list, f)
|
||||||
logging.info(f"{datetime.datetime.now()}: Wi-Fi scaned list: {wifi_list}")
|
logging.info(f"Wi-Fi scaned list: {wifi_list}")
|
||||||
|
|
||||||
|
|
||||||
return wifi_list
|
return wifi_list
|
||||||
|
@ -257,14 +257,14 @@ def connect_wifi(ssid, password):
|
||||||
output = subprocess.check_output(['nmcli', 'dev', 'wifi', 'connect', ssid, 'password', password, 'autoconnect', 'yes'])
|
output = subprocess.check_output(['nmcli', 'dev', 'wifi', 'connect', ssid, 'password', password, 'autoconnect', 'yes'])
|
||||||
output_str = output.decode('utf-8') # 将输出转换为字符串
|
output_str = output.decode('utf-8') # 将输出转换为字符串
|
||||||
if "successfully" in output_str:
|
if "successfully" in output_str:
|
||||||
logging.info(f"{datetime.datetime.now()}: Successfully connected to Wi-Fi: {ssid}")
|
logging.info(f"Successfully connected to Wi-Fi: {ssid}")
|
||||||
save_wifi(ssid, password) # 保存连接成功的Wi-Fi信息
|
save_wifi(ssid, password) # 保存连接成功的Wi-Fi信息
|
||||||
return True
|
return True
|
||||||
else:
|
else:
|
||||||
logging.info(f"{datetime.datetime.now()}: Error connecting to Wi-Fi: {output_str}")
|
logging.info(f"Error connecting to Wi-Fi: {output_str}")
|
||||||
return False
|
return False
|
||||||
except subprocess.CalledProcessError as e:
|
except subprocess.CalledProcessError as e:
|
||||||
logging.info(f"{datetime.datetime.now()}: Error connecting to Wi-Fi: {e}")
|
logging.info(f"Error connecting to Wi-Fi: {e}")
|
||||||
return False
|
return False
|
||||||
|
|
||||||
# 关闭 Wi-Fi
|
# 关闭 Wi-Fi
|
||||||
|
@ -273,11 +273,11 @@ def disconnect_wifi():
|
||||||
output = subprocess.check_output(['nmcli', 'dev', 'disconnect', 'iface', 'wlan0'])
|
output = subprocess.check_output(['nmcli', 'dev', 'disconnect', 'iface', 'wlan0'])
|
||||||
output_str = output.decode('utf-8') # 将输出转换为字符串
|
output_str = output.decode('utf-8') # 将输出转换为字符串
|
||||||
if "successfully disconnected" in output_str:
|
if "successfully disconnected" in output_str:
|
||||||
logging.info(f"{datetime.datetime.now()}: Wi-Fi disconnected successfully")
|
logging.info(f"Wi-Fi disconnected successfully")
|
||||||
else:
|
else:
|
||||||
logging.info(f"{datetime.datetime.now()}: Error disconnecting from Wi-Fi: {output_str}")
|
logging.info(f"Error disconnecting from Wi-Fi: {output_str}")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.info(f"{datetime.datetime.now()}: Error disconnecting from Wi-Fi: {e}")
|
logging.info(f"Error disconnecting from Wi-Fi: {e}")
|
||||||
|
|
||||||
def connect_saved_wifi(scaned_wifi_list):
|
def connect_saved_wifi(scaned_wifi_list):
|
||||||
wifi_list = load_saved_wifi()
|
wifi_list = load_saved_wifi()
|
||||||
|
@ -324,7 +324,7 @@ def submit():
|
||||||
orangepi.set_led_on('red')
|
orangepi.set_led_on('red')
|
||||||
ssid = request.form['ssid']
|
ssid = request.form['ssid']
|
||||||
password = request.form['password']
|
password = request.form['password']
|
||||||
logging.info(f"{datetime.datetime.now()}: Connecting to Wi-Fi: {ssid} with password {password}")
|
logging.info(f"Connecting to Wi-Fi: {ssid} with password {password}")
|
||||||
|
|
||||||
# 关闭热点
|
# 关闭热点
|
||||||
close_hotspot()
|
close_hotspot()
|
||||||
|
@ -339,7 +339,7 @@ def submit():
|
||||||
|
|
||||||
connected, wifi_ssid = check_wifi_connection()
|
connected, wifi_ssid = check_wifi_connection()
|
||||||
if not connected:
|
if not connected:
|
||||||
logging.info(f"{datetime.datetime.now()}: Wi-Fi连接失败。")
|
logging.info(f"Wi-Fi连接失败。")
|
||||||
wifi_list = scan_wifi()
|
wifi_list = scan_wifi()
|
||||||
start_hotspot()
|
start_hotspot()
|
||||||
else:
|
else:
|
||||||
|
@ -360,20 +360,20 @@ if __name__ == '__main__':
|
||||||
|
|
||||||
# 扫描Wi-Fi
|
# 扫描Wi-Fi
|
||||||
wifi_list = scan_wifi()
|
wifi_list = scan_wifi()
|
||||||
time.sleep(1) # 等待1秒,确保wifi scan完成
|
time.sleep(2) # 等待2秒,确保wifi scan完成
|
||||||
|
|
||||||
connected, wifi_ssid = check_wifi_connection()
|
connected, wifi_ssid = check_wifi_connection()
|
||||||
if connected:
|
if connected:
|
||||||
logging.info(f"{datetime.datetime.now()}: 系统已自动连接到 Wi-Fi 网络,退出程序")
|
logging.info(f"系统已自动连接到 Wi-Fi 网络,退出程序")
|
||||||
close_app()
|
close_app()
|
||||||
|
|
||||||
|
|
||||||
# 连接保存的Wi-Fi
|
# 连接保存的Wi-Fi
|
||||||
connected, wifi_ssid = connect_saved_wifi(wifi_list)
|
connected, wifi_ssid = connect_saved_wifi(wifi_list)
|
||||||
if connected:
|
if connected:
|
||||||
logging.info(f"{datetime.datetime.now()}: 系统已自动连接到 Wi-Fi 网络 {wifi_ssid},退出程序")
|
logging.info(f"系统已自动连接到 Wi-Fi 网络 {wifi_ssid},退出程序")
|
||||||
close_app()
|
close_app()
|
||||||
|
|
||||||
logging.info(f"{datetime.datetime.now()}: 未连接到 Wi-Fi 网络, 开始热点模式")
|
logging.info(f"未连接到 Wi-Fi 网络, 开始热点模式")
|
||||||
start_hotspot()
|
start_hotspot()
|
||||||
app.run(host='0.0.0.0', port=80)
|
app.run(host='0.0.0.0', port=80)
|
Loading…
Reference in New Issue