138 lines
3.8 KiB
Python
138 lines
3.8 KiB
Python
import os
|
||
import re
|
||
import subprocess
|
||
import time
|
||
from subprocess import Popen, PIPE
|
||
from ping3 import ping
|
||
|
||
from AcrossPlatform.get_platform import GLOBAL_DIR
|
||
from LogRecord.log_recorder import GLOBAL_LOG
|
||
|
||
WIFI_AP_DRIVER = os.path.join(GLOBAL_DIR, 'LSZXNetWork', 'NetworkDriverWin32', 'WiFiDirectLegacyAPDemo.exe')
|
||
WIFI_AP_EXE = 'WiFiDirectLegacyAPDemo.exe'
|
||
DEFAULT_IP_SEGMENT = r"192.168.[0-9]{1,3}.[0-9]{1,3}"
|
||
|
||
|
||
# 获得自身的ip地址
|
||
def get_self_ip_address():
|
||
ip_list = get_machine_ip_address()
|
||
if ip_list:
|
||
return ip_list[0]
|
||
else:
|
||
return "0.0.0.0"
|
||
|
||
|
||
# 获得同个网段下的设备
|
||
def get_machine_ip_address():
|
||
echo = os.popen("arp -a").read()
|
||
ip_list = re.findall(DEFAULT_IP_SEGMENT, echo)
|
||
ip_list = list(filter(lambda x: not x.endswith("255") and not x.endswith("138.1"), ip_list))
|
||
return ip_list
|
||
|
||
|
||
def kill_port(port):
|
||
response = os.popen(f"netstat -nao | findstr \"{port}\"").read()
|
||
pid_mes = re.search(r"(?<= )[0-9]+(?![.:])", response)
|
||
if pid_mes:
|
||
pid = pid_mes.group()
|
||
# 运行一个命令,并将标准输出和标准错误重定向到 DEVNULL
|
||
process = subprocess.Popen(
|
||
["taskkill", "-pid", f"{pid}", "-f"],
|
||
stdout=subprocess.DEVNULL,
|
||
stderr=subprocess.DEVNULL
|
||
)
|
||
# 等待命令执行完成
|
||
process.wait()
|
||
|
||
|
||
# 结束wifi
|
||
def stop_wifi_ap():
|
||
# 运行一个命令,并将标准输出和标准错误重定向到 DEVNULL
|
||
process = subprocess.Popen(
|
||
["taskkill", "/f", "/im", WIFI_AP_EXE],
|
||
stdout=subprocess.DEVNULL,
|
||
stderr=subprocess.DEVNULL
|
||
)
|
||
# 等待命令执行完成
|
||
process.wait()
|
||
|
||
|
||
def get_wifi_ap_status():
|
||
try:
|
||
if isinstance(ping("192.168.137.1", timeout=500, unit="ms"), float):
|
||
return True
|
||
else:
|
||
return False
|
||
except Exception as e:
|
||
GLOBAL_LOG.write(f"AP状态获取出错,原因是:{e.args}。")
|
||
return False
|
||
|
||
|
||
class WiFiAP:
|
||
|
||
def __init__(self):
|
||
stop_wifi_ap()
|
||
time.sleep(0.1)
|
||
self.proc = Popen(
|
||
args=WIFI_AP_DRIVER, shell=True, encoding='utf-8', stdin=PIPE,
|
||
stdout=PIPE
|
||
)
|
||
self.ssid = None
|
||
self.password = None
|
||
self.enable = False
|
||
|
||
def reboot(self):
|
||
try:
|
||
stop_wifi_ap()
|
||
self.proc.kill()
|
||
stop_wifi_ap()
|
||
time.sleep(1)
|
||
self.proc = Popen(
|
||
args=WIFI_AP_DRIVER, shell=True, encoding='utf-8', stdin=PIPE,
|
||
stdout=PIPE
|
||
)
|
||
if self.enable:
|
||
self.set_ssid(ssid=self.ssid)
|
||
self.set_password(password=self.password)
|
||
self.start()
|
||
GLOBAL_LOG.write(f"AP重启成功")
|
||
except Exception as e:
|
||
GLOBAL_LOG.write(f"AP重启出错,原因是:{e.args}。")
|
||
return
|
||
|
||
def send_cmd(self, cmd, timeout=1):
|
||
start_time = time.time()
|
||
while time.time() - start_time < timeout:
|
||
try:
|
||
self.proc.stdin.write(f"{cmd}\n")
|
||
self.proc.stdin.flush()
|
||
break
|
||
except Exception as e:
|
||
GLOBAL_LOG.write(f"AP启动出错,原因是:{e.args}。")
|
||
self.reboot()
|
||
|
||
def set_ssid(self, ssid):
|
||
self.ssid = ssid
|
||
self.send_cmd(f"ssid {ssid}")
|
||
|
||
def set_password(self, password):
|
||
self.password = password
|
||
self.send_cmd(f"pass {password}")
|
||
|
||
def start(self):
|
||
self.send_cmd("start")
|
||
self.enable = True
|
||
|
||
def stop(self):
|
||
self.send_cmd("stop")
|
||
self.enable = False
|
||
|
||
def set_auto_accept(self, mode=True):
|
||
if mode:
|
||
self.send_cmd("autoaccept 1")
|
||
else:
|
||
self.send_cmd("autoaccept 0")
|
||
|
||
def kill_wifi(self):
|
||
self.send_cmd("quit")
|