wifi_hotpot/wifi_manager.py

365 lines
12 KiB
Python
Raw Normal View History

2024-05-20 23:07:29 +08:00
import subprocess
import re
import os
2024-05-25 00:24:51 +08:00
import psutil
import signal
2024-05-20 23:07:29 +08:00
import time
2024-05-21 01:42:24 +08:00
import datetime
2024-05-27 21:20:54 +08:00
import json
2024-05-20 23:07:29 +08:00
from flask import Flask, render_template, request, redirect, url_for, make_response
2024-06-09 02:48:38 +08:00
import socket
2024-05-20 23:07:29 +08:00
2024-06-13 15:11:25 +08:00
import logging
logging.basicConfig(
filename='app.log', # 日志文件的名称
filemode='a', # 'a'代表追加模式,如果文件存在则追加内容
level=logging.DEBUG, # 日志级别DEBUG是最低的级别会记录所有级别的日志
format='%(asctime)s - %(levelname)s - %(message)s' # 日志的格式
)
2024-05-24 23:16:37 +08:00
try:
from takway.board import OrangePi
led_enabled = True
orangepi = OrangePi()
except ImportError:
led_enabled = False
2024-06-13 15:11:25 +08:00
logging.info("Error importing OrangePi")
2024-05-24 23:16:37 +08:00
2024-05-25 23:24:02 +08:00
# blue: APP
# red: hotspot
2024-05-25 00:24:51 +08:00
2024-06-13 13:39:38 +08:00
def network_error_light_1():
2024-05-27 21:57:07 +08:00
error_time = datetime.datetime.now()
while led_enabled:
orangepi.set_led_off('blue')
orangepi.set_led_on('red')
2024-06-13 13:39:38 +08:00
time.sleep(0.25)
2024-05-27 21:57:07 +08:00
orangepi.set_led_on('blue')
orangepi.set_led_off('red')
2024-06-13 13:39:38 +08:00
time.sleep(0.25)
if error_time + datetime.timedelta(seconds=2) < datetime.datetime.now():
break
# close_app()
def network_error_light_2():
error_time = datetime.datetime.now()
while led_enabled:
2024-06-13 15:11:25 +08:00
orangepi.set_led_on('green')
2024-06-13 13:39:38 +08:00
time.sleep(0.25)
2024-06-13 15:11:25 +08:00
orangepi.set_led_off('green')
2024-06-13 13:39:38 +08:00
time.sleep(0.25)
if error_time + datetime.timedelta(seconds=2) < datetime.datetime.now():
2024-05-27 21:57:07 +08:00
break
2024-05-31 12:50:04 +08:00
# close_app()
2024-05-27 21:57:07 +08:00
2024-05-31 12:17:53 +08:00
def edit_line_in_file(file_path, search_string, replacement_string):
2024-06-13 15:31:40 +08:00
edit = False
2024-05-31 12:17:53 +08:00
try:
# 读取文件内容
with open(file_path, 'r') as file:
lines = file.readlines()
# 替换包含特定内容的行
with open(file_path, 'r+') as file:
file.seek(0) # 移动到文件开头
file.truncate() # 清空文件内容
for line in lines:
if search_string in line:
file.write(replacement_string + '\n')
2024-06-13 15:31:40 +08:00
edit = True
2024-05-31 12:17:53 +08:00
else:
file.write(line)
2024-06-13 15:11:25 +08:00
logging.info(f"{datetime.datetime.now()}: The line has been edited.")
2024-06-13 15:31:40 +08:00
return edit
2024-05-31 12:17:53 +08:00
except FileNotFoundError:
2024-06-13 15:11:25 +08:00
logging.info(f"The file {file_path} does not exist.")
2024-06-13 15:31:40 +08:00
return edit
2024-05-31 12:17:53 +08:00
except IOError as e:
2024-06-13 15:11:25 +08:00
logging.info(f"An error occurred: {e}")
2024-06-13 15:31:40 +08:00
return edit
2024-06-09 02:48:38 +08:00
# 获取私有IP地址
def save_local_ip():
try:
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("8.8.8.8", 80))
local_ip = s.getsockname()[0]
s.close()
# save local_ip to file
with open('local_ip.txt', 'w') as f:
f.write(local_ip)
2024-06-13 15:11:25 +08:00
logging.info(f"{datetime.datetime.now()}: Local IP address: {local_ip}")
2024-06-09 02:48:38 +08:00
return local_ip
except Exception as e:
2024-06-13 15:11:25 +08:00
logging.info(f"Error getting private IP: {e}")
2024-06-09 02:48:38 +08:00
return None
2024-05-25 21:59:11 +08:00
app = Flask(__name__)
2024-05-24 23:16:37 +08:00
2024-05-25 00:02:37 +08:00
def close_app():
2024-05-25 00:33:37 +08:00
if led_enabled:
2024-05-25 23:24:02 +08:00
orangepi.set_led_off('blue')
2024-05-27 21:20:54 +08:00
2024-06-09 02:48:38 +08:00
# 保存Wi-Fi IP地址
save_local_ip()
2024-05-25 00:02:37 +08:00
# 获取当前Flask应用程序的进程ID
flask_pid = os.getpid()
2024-05-27 21:20:54 +08:00
2024-05-25 00:02:37 +08:00
# 获取所有名为'python3'的进程ID
python_pids = [p.info['pid'] for p in psutil.process_iter(attrs=['pid', 'name']) if p.info['name'] == 'python3']
2024-05-27 21:20:54 +08:00
2024-05-25 00:02:37 +08:00
# 关闭Flask应用程序进程
os.kill(flask_pid, signal.SIGTERM)
2024-05-27 21:20:54 +08:00
2024-05-25 00:02:37 +08:00
# 关闭Python进程
for pid in python_pids:
os.kill(pid, signal.SIGTERM)
2024-05-24 23:16:37 +08:00
2024-05-25 21:59:11 +08:00
def start_hotspot():
try:
subprocess.Popen('sudo systemctl start hotspot.service', shell=True)
if led_enabled:
2024-05-25 23:24:02 +08:00
orangepi.set_led_on('red')
2024-05-25 21:59:11 +08:00
except subprocess.CalledProcessError as e:
2024-06-13 15:11:25 +08:00
logging.info(f"{datetime.datetime.now()}: Error starting create_ap service: {e}")
2024-05-24 23:16:37 +08:00
2024-05-25 21:59:11 +08:00
def close_hotspot():
# 关闭热点
try:
subprocess.Popen('sudo systemctl stop hotspot.service', shell=True)
2024-06-13 15:11:25 +08:00
logging.info(f"{datetime.datetime.now()}: Stopping create_ap service")
2024-05-25 23:24:02 +08:00
if led_enabled:
orangepi.set_led_off('red')
2024-05-25 21:59:11 +08:00
except subprocess.CalledProcessError as e:
2024-06-13 15:11:25 +08:00
logging.info(f"{datetime.datetime.now()}: Error stopping hotspot: {e}")
2024-05-27 21:20:54 +08:00
2024-06-13 15:31:40 +08:00
def init_networkmanager_file():
file_path = '/etc/NetworkManager/NetworkManager.conf'
search_string = 'unmanaged-devices=interface-name:'
replacement_string = '' # 如果不需要替换为其他内容,可以设置为空字符串
if edit_line_in_file(file_path, search_string, replacement_string):
'''
# 从 /etc/NetworkManager/NetworkManager.conf 文件中移除或注释掉 [keyfile] 部分的 unmanaged-devices 条目,因为它可能阻止了 NetworkManager 管理 wlan0 接口。编辑文件如下:
```
[keyfile]
#unmanaged-devices=interface-name:ap0;interface-name:wlan0
```
sudo systemctl restart NetworkManager
'''
# network_error_light_1()
logging.info(f"{datetime.datetime.now()}: Wi-Fi NetworkManager Error: restart NetworkManager.")
subprocess.Popen('sudo systemctl restart NetworkManager', shell=True)
time.sleep(3)
2024-05-20 23:07:29 +08:00
# 检测 Wi-Fi 连接状态
def check_wifi_connection():
2024-05-21 01:42:24 +08:00
cmd = "nmcli dev status"
result = subprocess.run(cmd, shell=True, capture_output=True, text=True)
output = result.stdout.strip()
lines = output.split("\n")[1:] # Skip the header line
for line in lines:
columns = line.split()
2024-06-13 15:11:25 +08:00
logging.info(columns)
2024-05-27 21:57:07 +08:00
'''
['wlan0', 'wifi', 'disconnected', '--']
['p2p-dev-wlan0', 'wifi-p2p', 'disconnected', '--']
['eth0', 'ethernet', 'unavailable', '--']
['lo', 'loopback', 'unmanaged', '--']
'''
2024-05-21 01:42:24 +08:00
if len(columns) >= 4 and columns[2] == "connected":
2024-05-25 21:59:11 +08:00
wifi_ssid = columns[3]
2024-05-25 00:33:37 +08:00
if led_enabled:
orangepi.set_led_off('blue')
2024-05-25 21:59:11 +08:00
return True, wifi_ssid
2024-05-27 21:20:54 +08:00
return False, None
2024-05-20 23:07:29 +08:00
2024-05-25 21:59:11 +08:00
def scan_wifi():
2024-05-27 21:50:20 +08:00
try:
subprocess.run(['nmcli', 'dev', 'wifi', 'rescan'], check=True)
cmd = "nmcli dev wifi"
result = subprocess.run(cmd, shell=True, capture_output=True, text=True)
except subprocess.CalledProcessError as e:
2024-06-13 15:11:25 +08:00
logging.info(f"{datetime.datetime.now()}: Error scanning Wi-Fi: {e}")
2024-06-13 13:39:38 +08:00
network_error_light_2()
2024-06-13 15:11:25 +08:00
return []
logging.info(f"{datetime.datetime.now()}: Wi-Fi scan complete...")
2024-05-25 22:44:42 +08:00
ssid_list = []
2024-05-25 21:59:11 +08:00
wifi_list = []
output = result.stdout.strip()
lines = output.split("\n")[1:] # Skip the header line
for line in lines:
columns = line.split()
2024-05-25 22:35:35 +08:00
'''
2024-06-13 15:11:25 +08:00
logging.info(columns)
2024-05-25 21:59:11 +08:00
for i, column in enumerate(columns):
2024-06-13 15:11:25 +08:00
logging.info(f"{i}: {column}")
2024-05-25 22:35:35 +08:00
'''
# ['94:14:57:15:13:50', 'Meeting', 'MG', 'Infra', '1', '130', 'Mbit/s', '100', '****', 'WPA1', 'WPA2']
2024-05-25 22:32:53 +08:00
# 提取MAC地址
mac_address = columns[0]
# 提取Wi-Fi名称
2024-05-25 22:44:42 +08:00
wifi_name = []
2024-05-25 22:32:53 +08:00
for i in range(1, len(columns)):
if columns[i] == 'Infra':
2024-05-25 22:44:42 +08:00
_wifi_name = ' '.join(columns[1:i])
2024-05-25 22:45:21 +08:00
wifi_name.append(_wifi_name)
2024-05-25 22:32:53 +08:00
columns = columns[i+1:]
break
2024-05-25 22:44:42 +08:00
ssid = wifi_name[0]
2024-05-25 23:41:36 +08:00
if ':' in ssid:
# 94:14:57:15:13:50 Meeting
# 去除mac地址
ssid = ssid.split(' ')[1:]
ssid = ''.join(ssid)
2024-05-27 21:20:54 +08:00
2024-05-25 22:44:42 +08:00
if ssid in ssid_list:
continue
ssid_list.append(ssid)
2024-05-27 21:20:54 +08:00
2024-05-25 22:32:53 +08:00
# 提取强度
strength = None
for i in range(len(columns)):
if columns[i] == 'Mbit/s':
strength = columns[i+1]
break
2024-06-13 15:11:25 +08:00
# logging.info("MAC地址:", mac_address)
# logging.info("Wi-Fi名称:", ssid)
# logging.info("强度:", strength)
2024-05-27 21:20:54 +08:00
2024-05-25 22:59:48 +08:00
wifi_list.append({'ssid': ssid, 'signal': strength, 'mac': mac_address})
2024-06-09 15:06:31 +08:00
with open('scaned_wifi_list.json', 'w') as f:
json.dump(wifi_list, f)
2024-05-25 21:59:11 +08:00
if len(wifi_list) == 15:
break
2024-05-27 21:20:54 +08:00
2024-05-25 21:59:11 +08:00
return wifi_list
2024-05-20 23:07:29 +08:00
# 连接 Wi-Fi
def connect_wifi(ssid, password):
# 连接到用户选择的 Wi-Fi 网络
try:
2024-06-13 15:31:40 +08:00
output = subprocess.check_output(['nmcli', 'dev', 'wifi', 'connect', ssid, 'password', password, 'autoconnect', 'yes'])
2024-05-25 21:59:11 +08:00
output_str = output.decode('utf-8') # 将输出转换为字符串
if "successfully" in output_str:
2024-06-13 15:11:25 +08:00
logging.info(f"{datetime.datetime.now()}: Successfully connected to Wi-Fi: {ssid}")
2024-05-27 21:20:54 +08:00
save_wifi(ssid, password) # 保存连接成功的Wi-Fi信息
2024-05-25 21:59:11 +08:00
return True
else:
2024-06-13 15:11:25 +08:00
logging.info(f"{datetime.datetime.now()}: Error connecting to Wi-Fi: {output_str}")
2024-05-25 21:59:11 +08:00
return False
2024-05-20 23:07:29 +08:00
except subprocess.CalledProcessError as e:
2024-06-13 15:11:25 +08:00
logging.info(f"{datetime.datetime.now()}: Error connecting to Wi-Fi: {e}")
2024-05-20 23:07:29 +08:00
return False
2024-05-25 21:59:11 +08:00
# 关闭 Wi-Fi
def disconnect_wifi():
try:
output = subprocess.check_output(['nmcli', 'dev', 'disconnect', 'iface', 'wlan0'])
output_str = output.decode('utf-8') # 将输出转换为字符串
if "successfully disconnected" in output_str:
2024-06-13 15:11:25 +08:00
logging.info(f"{datetime.datetime.now()}: Wi-Fi disconnected successfully")
2024-05-25 22:13:04 +08:00
else:
2024-06-13 15:11:25 +08:00
logging.info(f"{datetime.datetime.now()}: Error disconnecting from Wi-Fi: {output_str}")
2024-05-25 22:21:14 +08:00
except Exception as e:
2024-06-13 15:11:25 +08:00
logging.info(f"{datetime.datetime.now()}: Error disconnecting from Wi-Fi: {e}")
2024-05-25 21:59:11 +08:00
2024-05-27 21:50:20 +08:00
def connect_saved_wifi(scaned_wifi_list):
wifi_list = load_saved_wifi()
2024-06-09 02:48:38 +08:00
if wifi_list == []:
2024-05-27 21:50:20 +08:00
return False, None
for wifi in wifi_list:
if wifi['ssid'] in [item['ssid'] for item in scaned_wifi_list]:
2024-06-13 15:31:40 +08:00
if connect_wifi(wifi['ssid'], wifi['password']):
return True, wifi['ssid']
2024-06-11 21:38:07 +08:00
return check_wifi_connection()
2024-05-27 21:50:20 +08:00
def load_saved_wifi():
try:
with open('wifi_list.json', 'r') as f:
wifi_list = json.load(f)
return wifi_list
2024-06-07 15:34:12 +08:00
except:
2024-05-27 21:50:20 +08:00
return []
def save_wifi(ssid, password):
wifi_list = load_saved_wifi()
2024-05-27 23:22:57 +08:00
if ssid not in [item['ssid'] for item in wifi_list]:
wifi_list.append({'ssid': ssid, 'password': password})
2024-05-27 21:50:20 +08:00
with open('wifi_list.json', 'w') as f:
json.dump(wifi_list, f)
2024-05-25 21:59:11 +08:00
2024-05-20 23:07:29 +08:00
# 主页
@app.route('/')
def index():
global wifi_list
2024-05-27 21:20:54 +08:00
2024-05-20 23:07:29 +08:00
response = make_response(render_template('index.html', wifi_list=wifi_list))
response.headers.set('Content-Type', 'text/html')
response.headers.set('Apple-Web-App-Capable', 'yes')
response.headers.set('Apple-Mobile-Web-App-Status-Bar-Style', 'black-translucent')
return response
2024-05-27 21:20:54 +08:00
2024-05-20 23:07:29 +08:00
# 提交 Wi-Fi 信息
@app.route('/submit', methods=['POST'])
def submit():
2024-05-25 00:33:37 +08:00
if led_enabled:
orangepi.set_led_on('red')
2024-05-20 23:07:29 +08:00
ssid = request.form['ssid']
password = request.form['password']
2024-06-13 15:11:25 +08:00
logging.info(f"{datetime.datetime.now()}: Connecting to Wi-Fi: {ssid} with password {password}")
2024-05-27 21:20:54 +08:00
2024-05-25 22:57:24 +08:00
# 关闭热点
close_hotspot()
2024-05-27 21:20:54 +08:00
2024-06-13 14:03:32 +08:00
time.sleep(0.5)
2024-06-09 03:14:23 +08:00
scan_wifi()
2024-06-13 14:03:32 +08:00
time.sleep(0.5)
2024-06-09 03:14:23 +08:00
2024-05-25 22:57:24 +08:00
# 连接到用户选择的 Wi-Fi 网络
if connect_wifi(ssid, password):
2024-05-25 00:02:37 +08:00
close_app()
2024-05-27 21:20:54 +08:00
connected, wifi_ssid = check_wifi_connection()
if not connected:
2024-06-13 15:11:25 +08:00
logging.info(f"{datetime.datetime.now()}: Wi-Fi连接失败。")
2024-05-25 22:57:24 +08:00
wifi_list = scan_wifi()
start_hotspot()
2024-05-27 21:20:54 +08:00
else:
2024-06-09 15:06:31 +08:00
save_wifi(ssid, password) # 保存连接成功的Wi-Fi信息
2024-05-27 21:20:54 +08:00
2024-05-20 23:07:29 +08:00
return redirect(url_for('index'))
if __name__ == '__main__':
2024-05-25 23:04:33 +08:00
debug_mode = False # 设置为 True 以跳过 Wi-Fi 连接状态检测
2024-05-25 21:59:11 +08:00
if debug_mode:
disconnect_wifi()
2024-05-25 22:10:04 +08:00
2024-05-25 00:33:37 +08:00
if led_enabled:
orangepi.set_led_on('blue')
2024-05-27 21:20:54 +08:00
2024-06-13 15:31:40 +08:00
init_networkmanager_file() # 初始化 NetworkManager 配置文件
2024-05-27 21:20:54 +08:00
connected, wifi_ssid = check_wifi_connection()
if connected:
2024-06-13 15:11:25 +08:00
logging.info(f"{datetime.datetime.now()}: 系统已自动连接到 Wi-Fi 网络,退出程序")
2024-05-25 21:59:11 +08:00
close_app()
2024-05-27 21:50:20 +08:00
2024-06-13 15:11:25 +08:00
# 扫描Wi-Fi
2024-05-27 21:50:20 +08:00
wifi_list = scan_wifi()
2024-06-13 15:11:25 +08:00
# 连接保存的Wi-Fi
2024-05-27 21:50:20 +08:00
connected, wifi_ssid = connect_saved_wifi(wifi_list)
if connected:
2024-06-13 15:11:25 +08:00
logging.info(f"{datetime.datetime.now()}: 系统已自动连接到 Wi-Fi 网络 {wifi_ssid},退出程序")
2024-05-27 21:50:20 +08:00
close_app()
2024-06-13 15:11:25 +08:00
logging.info(f"{datetime.datetime.now()}: 未连接到 Wi-Fi 网络, 开始热点模式")
2024-05-27 21:50:20 +08:00
start_hotspot()
app.run(host='0.0.0.0', port=80)