[v1.0] hotspot
This commit is contained in:
parent
b16550383b
commit
b050a67182
|
@ -15,14 +15,14 @@ except ImportError:
|
||||||
led_enabled = False
|
led_enabled = False
|
||||||
print("Error importing OrangePi")
|
print("Error importing OrangePi")
|
||||||
|
|
||||||
|
# blue: APP
|
||||||
|
# red: hotspot
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
|
||||||
def close_app():
|
def close_app():
|
||||||
if led_enabled:
|
if led_enabled:
|
||||||
orangepi.set_led_off('red')
|
orangepi.set_led_off('blue')
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# 获取当前Flask应用程序的进程ID
|
# 获取当前Flask应用程序的进程ID
|
||||||
flask_pid = os.getpid()
|
flask_pid = os.getpid()
|
||||||
|
@ -41,7 +41,7 @@ def start_hotspot():
|
||||||
try:
|
try:
|
||||||
subprocess.Popen('sudo systemctl start hotspot.service', shell=True)
|
subprocess.Popen('sudo systemctl start hotspot.service', shell=True)
|
||||||
if led_enabled:
|
if led_enabled:
|
||||||
orangepi.set_led_on('blue')
|
orangepi.set_led_on('red')
|
||||||
except subprocess.CalledProcessError as e:
|
except subprocess.CalledProcessError as e:
|
||||||
print(f"{datetime.datetime.now()}: Error starting create_ap service: {e}")
|
print(f"{datetime.datetime.now()}: Error starting create_ap service: {e}")
|
||||||
|
|
||||||
|
@ -50,6 +50,8 @@ def close_hotspot():
|
||||||
try:
|
try:
|
||||||
subprocess.Popen('sudo systemctl stop hotspot.service', shell=True)
|
subprocess.Popen('sudo systemctl stop hotspot.service', shell=True)
|
||||||
print(f"{datetime.datetime.now()}: Stopping create_ap service")
|
print(f"{datetime.datetime.now()}: Stopping create_ap service")
|
||||||
|
if led_enabled:
|
||||||
|
orangepi.set_led_off('red')
|
||||||
except subprocess.CalledProcessError as e:
|
except subprocess.CalledProcessError as e:
|
||||||
print(f"{datetime.datetime.now()}: Error stopping hotspot: {e}")
|
print(f"{datetime.datetime.now()}: Error stopping hotspot: {e}")
|
||||||
|
|
||||||
|
@ -125,56 +127,6 @@ def scan_wifi():
|
||||||
f.write(f"{wifi['ssid']},{wifi['signal']}\n")
|
f.write(f"{wifi['ssid']},{wifi['signal']}\n")
|
||||||
|
|
||||||
return wifi_list
|
return wifi_list
|
||||||
'''
|
|
||||||
|
|
||||||
def scan_wifi():
|
|
||||||
subprocess.run(['nmcli', 'dev', 'wifi', 'rescan'], check=True)
|
|
||||||
output = subprocess.check_output(['nmcli', 'dev', 'wifi'])
|
|
||||||
|
|
||||||
wifi_list = []
|
|
||||||
lines = output.decode().splitlines()[1:]
|
|
||||||
for idx, line in enumerate(lines):
|
|
||||||
print(f"{line}")
|
|
||||||
|
|
||||||
for i, column in enumerate(line):
|
|
||||||
print(f"{i}: {column}")
|
|
||||||
close_app()
|
|
||||||
# 分割字符串以空格为分隔符
|
|
||||||
columns = line.split()
|
|
||||||
|
|
||||||
# 提取第一列和第三列的值
|
|
||||||
mac_address = columns[0]
|
|
||||||
|
|
||||||
ssid = " ".join(columns[1:3])
|
|
||||||
if "Infra" in ssid:
|
|
||||||
ssid = ssid.replace(" Infra", "")
|
|
||||||
if ":" in ssid:
|
|
||||||
# "72:A6:CC:8C:89:6C Takway-AI"
|
|
||||||
# 去除字符串中的MAC地址
|
|
||||||
ssid = re.sub(r'\b\w{2}(:\w{2}){5}\b', '', ssid)
|
|
||||||
|
|
||||||
# 提取第六列的强度值
|
|
||||||
last_info = columns[4:8]
|
|
||||||
# 去除所有的空格
|
|
||||||
columns = line.replace(" ", "")
|
|
||||||
# 提取"Mbit/s"后的三位以内数字
|
|
||||||
strength = int(columns[columns.index("Mbit/s")+6:columns.index("Mbit/s")+8])
|
|
||||||
|
|
||||||
# print("MAC地址:", mac_address)
|
|
||||||
# print("SSID:", ssid)
|
|
||||||
# print("强度:", strength)
|
|
||||||
|
|
||||||
wifi_list.append({'ssid': ssid, 'signal': strength})
|
|
||||||
if len(wifi_list) == 15:
|
|
||||||
break
|
|
||||||
|
|
||||||
# save wifi_list to file
|
|
||||||
with open('wifi_list.txt', 'w') as f:
|
|
||||||
for wifi in wifi_list:
|
|
||||||
f.write(f"{wifi['ssid']},{wifi['signal']}\n")
|
|
||||||
|
|
||||||
return wifi_list
|
|
||||||
'''
|
|
||||||
|
|
||||||
# 连接 Wi-Fi
|
# 连接 Wi-Fi
|
||||||
def connect_wifi(ssid, password):
|
def connect_wifi(ssid, password):
|
||||||
|
|
Loading…
Reference in New Issue