update exit
This commit is contained in:
parent
1d453dc6c1
commit
8dd05a406f
|
@ -104,7 +104,7 @@ def scan_wifi():
|
|||
|
||||
return wifi_list
|
||||
except subprocess.CalledProcessError as e:
|
||||
print(f"Error scanning for Wi-Fi networks: {e}")
|
||||
print(f"{datetime.datetime.now()}: Error scanning for Wi-Fi networks: {e}")
|
||||
return []
|
||||
|
||||
|
||||
|
@ -113,9 +113,9 @@ def connect_wifi(ssid, password):
|
|||
# 关闭热点
|
||||
try:
|
||||
subprocess.run(['sudo', 'create_ap', '--stop', 'wlan0'], check=True)
|
||||
print(f"{datetime.datetime.now()} Stopping create_ap service")
|
||||
print(f"{datetime.datetime.now()}: Stopping create_ap service")
|
||||
except subprocess.CalledProcessError as e:
|
||||
print(f"{datetime.datetime.now()} Error stopping hotspot: {e}")
|
||||
print(f"{datetime.datetime.now()}: Error stopping hotspot: {e}")
|
||||
|
||||
time.sleep(1)
|
||||
|
||||
|
@ -127,10 +127,10 @@ def connect_wifi(ssid, password):
|
|||
# 连接到用户选择的 Wi-Fi 网络
|
||||
try:
|
||||
subprocess.run(['nmcli', 'dev', 'wifi', 'connect', ssid, 'password', password], check=True)
|
||||
print(f"{datetime.datetime.now()} Successfully connected to Wi-Fi: {ssid}")
|
||||
print(f"{datetime.datetime.now()}: Successfully connected to Wi-Fi: {ssid}")
|
||||
return True
|
||||
except subprocess.CalledProcessError as e:
|
||||
print(f"{datetime.datetime.now()} Error connecting to Wi-Fi: {e}")
|
||||
print(f"{datetime.datetime.now()}: Error connecting to Wi-Fi: {e}")
|
||||
return False
|
||||
|
||||
# 主页
|
||||
|
@ -151,16 +151,32 @@ def submit():
|
|||
orangepi.set_led_on('red')
|
||||
ssid = request.form['ssid']
|
||||
password = request.form['password']
|
||||
print(f"{datetime.datetime.now()} Connecting to Wi-Fi: {ssid} with password {password}")
|
||||
print(f"{datetime.datetime.now()}: Connecting to Wi-Fi: {ssid} with password {password}")
|
||||
connected = connect_wifi(ssid, password)
|
||||
|
||||
time.sleep(5)
|
||||
# 检查是否成功连接Wi-Fi
|
||||
if check_wifi_connection() and connected:
|
||||
orangepi.set_led_off('red')
|
||||
os._exit(0) # 成功连接后退出程序
|
||||
# connected successfully, close flask
|
||||
print(f"{datetime.datetime.now()}: Wi-Fi连接成功,关闭应用程序。")
|
||||
# 关闭flask app,关闭python相关的所有进程,并退出程序
|
||||
flask_pid = os.getpid()
|
||||
python_pids = [p.info['pid'] for p in psutil.process_iter(attrs=['pid', 'name']) if p.info['name'] == 'python3']
|
||||
|
||||
# 关闭Flask应用程序进程
|
||||
os.kill(flask_pid, signal.SIGTERM)
|
||||
|
||||
# 关闭Python进程
|
||||
for pid in python_pids:
|
||||
os.kill(pid, signal.SIGTERM)
|
||||
|
||||
exit(0)
|
||||
|
||||
|
||||
else:
|
||||
print("Wi-Fi连接失败。")
|
||||
print(f"{datetime.datetime.now()}: Wi-Fi连接失败。")
|
||||
restart_hotspot()
|
||||
|
||||
return redirect(url_for('index'))
|
||||
|
||||
|
@ -172,12 +188,12 @@ if __name__ == '__main__':
|
|||
if not debug_mode:
|
||||
time.sleep(5) # 等待 Wi-Fi 连接
|
||||
if check_wifi_connection():
|
||||
print(f"{datetime.datetime.now()} 已连接到 Wi-Fi 网络,退出程序")
|
||||
print(f"{datetime.datetime.now()}: 已连接到 Wi-Fi 网络,退出程序")
|
||||
exit(0)
|
||||
else:
|
||||
print(f"{datetime.datetime.now()} 未连接到 Wi-Fi 网络")
|
||||
print(f"{datetime.datetime.now()}: 未连接到 Wi-Fi 网络")
|
||||
|
||||
print(f"{datetime.datetime.now()} Starting Flask server")
|
||||
print(f"{datetime.datetime.now()}: Starting Flask server")
|
||||
print("----------------------------")
|
||||
wifi_list = scan_wifi()
|
||||
print("----------------------------")
|
||||
|
|
Loading…
Reference in New Issue