update led
This commit is contained in:
parent
d73eec7673
commit
1d453dc6c1
|
@ -12,18 +12,31 @@ import time
|
|||
import datetime
|
||||
from flask import Flask, render_template, request, redirect, url_for, make_response
|
||||
|
||||
try:
|
||||
from takway.board import OrangePi
|
||||
led_enabled = True
|
||||
orangepi = OrangePi()
|
||||
except ImportError:
|
||||
led_enabled = False
|
||||
print("Error importing OrangePi")
|
||||
|
||||
|
||||
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
# 检测 Wi-Fi 连接状态
|
||||
def check_wifi_connection():
|
||||
cmd = "nmcli dev status"
|
||||
result = subprocess.run(cmd, shell=True, capture_output=True, text=True)
|
||||
time.sleep(1)
|
||||
output = result.stdout.strip()
|
||||
lines = output.split("\n")[1:] # Skip the header line
|
||||
for line in lines:
|
||||
columns = line.split()
|
||||
print(columns)
|
||||
if len(columns) >= 4 and columns[2] == "connected":
|
||||
orangepi.set_led_off('blue')
|
||||
return True
|
||||
return False
|
||||
|
||||
|
@ -39,6 +52,7 @@ def restart_hotspot():
|
|||
subprocess.run(['sudo', 'nmcli', 'radio', 'wifi', 'off'], check=True)
|
||||
subprocess.run(['sudo', 'nmcli', 'radio', 'wifi', 'on'], check=True)
|
||||
subprocess.Popen('sudo nohup create_ap wlan0 eth0 Takway-Toys --no-virt > create_ap.log 2>&1 &', shell=True)
|
||||
orangepi.set_led_on('blue')
|
||||
except subprocess.CalledProcessError as e:
|
||||
print(f"{datetime.datetime.now()} Error stopping create_ap hotspot service: {e}")
|
||||
|
||||
|
@ -134,6 +148,7 @@ def index():
|
|||
# 提交 Wi-Fi 信息
|
||||
@app.route('/submit', methods=['POST'])
|
||||
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}")
|
||||
|
@ -142,6 +157,7 @@ def submit():
|
|||
time.sleep(5)
|
||||
# 检查是否成功连接Wi-Fi
|
||||
if check_wifi_connection() and connected:
|
||||
orangepi.set_led_off('red')
|
||||
os._exit(0) # 成功连接后退出程序
|
||||
else:
|
||||
print("Wi-Fi连接失败。")
|
||||
|
@ -152,6 +168,7 @@ def submit():
|
|||
if __name__ == '__main__':
|
||||
debug_mode = False # 设置为 True 以跳过 Wi-Fi 连接状态检测
|
||||
|
||||
orangepi.set_led_on('blue')
|
||||
if not debug_mode:
|
||||
time.sleep(5) # 等待 Wi-Fi 连接
|
||||
if check_wifi_connection():
|
||||
|
|
Loading…
Reference in New Issue