# -*- coding: utf-8 -*-

import configparser
import json
import logging
import os
import platform
import re
import subprocess
import sys
import time
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QWidget, QApplication


class WifiTestWinFrm(QWidget):
    # 定义颜色代码
    COLOR_SUCCESS = "\033[1;32;43m"
    COLOR_ERROR = "\033[1;31m"
    COLOR_RESET = "\033[0m"

    def __init__(self):
        super().__init__()
        self.setupUi()
        self.testargs = []
        self.testresult = {'connect': 'N/A', 'active': 'N/A', 'ssid': 'N/A', 'wifimac': 'N/A', 'frequency_band': 'N/A', 'signal_strength': 'N/A', 'dev': 'N/A'}
        self.itemName = ''
        self.testStandardArgs = ''
        self.wifi_details = ''
        self.config = None
        self.logger = None

        self.loggerrinfo()
        self.getitemname()
        self.readjsontestargs(self.itemName)
        self.intercept_test_parameters(self.testStandardArgs)

        if self.connect_wifi(self.testargs[0]['ssid'], self.testargs[0]['password']):
            if self.get_wifi_info():
                if self.chk_wifiinfo(self.testargs[0]['active'], self.testargs[0]['signal_strength']):
                    if self.check_communication():
                        wifimac=self.get_wifi_mac_address().replace(':','').upper()
                        print(wifimac)
                        self.testresult['wifimac'] = wifimac
                        self.updatejsontestargs(self.itemName, self.testresult, "PASS")
                        self.disconnect_wifi()  # 测试完成后断开无线网络连接
                        sys.exit(0)
                    else:
                        self.updatejsontestargs(self.itemName, self.testresult, "FAIL")
                else:
                    self.updatejsontestargs(self.itemName, self.testresult, "FAIL")
            else:
                self.updatejsontestargs(self.itemName, self.testresult, "FAIL")
        else:
            self.updatejsontestargs(self.itemName, self.testresult, "FAIL")

    def setupUi(self):
        self.setObjectName("Form")
        self.resize(711, 223)
        icon = QtGui.QIcon()
        icon.addPixmap(QtGui.QPixmap("Conf/ICON/WifiTestWinFrm.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.setWindowIcon(icon)
        self.gridLayout = QtWidgets.QGridLayout(self)
        self.gridLayout.setObjectName("gridLayout")
        self.verticalLayout = QtWidgets.QVBoxLayout()
        self.verticalLayout.setObjectName("verticalLayout")
        self.horizontalLayout = QtWidgets.QHBoxLayout()
        self.horizontalLayout.setObjectName("horizontalLayout")
        self.verticalLayout_2 = QtWidgets.QVBoxLayout()
        self.verticalLayout_2.setObjectName("verticalLayout_2")
        self.lbl_logo = QtWidgets.QLabel(self)
        self.lbl_logo.setText("")
        self.lbl_logo.setPixmap(QtGui.QPixmap("../IMAGE/logo.jpg"))
        self.lbl_logo.setAlignment(QtCore.Qt.AlignCenter)
        self.lbl_logo.setObjectName("lbl_logo")
        self.verticalLayout_2.addWidget(self.lbl_logo)
        self.lbl_ItemName = QtWidgets.QLabel(self)
        font = QtGui.QFont()
        font.setPointSize(20)
        font.setBold(True)
        font.setWeight(75)
        self.lbl_ItemName.setFont(font)
        self.lbl_ItemName.setStyleSheet("background-color: rgb(85, 255, 127);\n"
                                        "color: rgb(85, 85, 127);")
        self.lbl_ItemName.setAlignment(QtCore.Qt.AlignCenter)
        self.lbl_ItemName.setObjectName("lbl_ItemName")
        self.verticalLayout_2.addWidget(self.lbl_ItemName)
        self.horizontalLayout.addLayout(self.verticalLayout_2)
        self.verticalLayout_3 = QtWidgets.QVBoxLayout()
        self.verticalLayout_3.setObjectName("verticalLayout_3")
        self.label_4 = QtWidgets.QLabel(self)
        font = QtGui.QFont()
        font.setPointSize(12)
        font.setBold(True)
        font.setWeight(75)
        self.label_4.setFont(font)
        self.label_4.setStyleSheet("color: rgb(85, 85, 127);")
        self.label_4.setAlignment(QtCore.Qt.AlignCenter)
        self.label_4.setObjectName("label_4")
        self.verticalLayout_3.addWidget(self.label_4)
        self.horizontalLayout_4 = QtWidgets.QHBoxLayout()
        self.horizontalLayout_4.setObjectName("horizontalLayout_4")
        self.label_6 = QtWidgets.QLabel(self)
        font = QtGui.QFont()
        font.setPointSize(11)
        font.setBold(True)
        font.setWeight(75)
        self.label_6.setFont(font)
        self.label_6.setStyleSheet("background-color: rgb(255, 170, 127);")
        self.label_6.setAlignment(QtCore.Qt.AlignCenter)
        self.label_6.setObjectName("label_6")
        self.horizontalLayout_4.addWidget(self.label_6)
        self.label_2 = QtWidgets.QLabel(self)
        font = QtGui.QFont()
        font.setPointSize(11)
        font.setBold(True)
        font.setWeight(75)
        self.label_2.setFont(font)
        self.label_2.setStyleSheet("background-color: rgb(255, 170, 127);")
        self.label_2.setAlignment(QtCore.Qt.AlignCenter)
        self.label_2.setObjectName("label_2")
        self.horizontalLayout_4.addWidget(self.label_2)
        self.verticalLayout_3.addLayout(self.horizontalLayout_4)
        self.horizontalLayout_2 = QtWidgets.QHBoxLayout()
        self.horizontalLayout_2.setObjectName("horizontalLayout_2")
        self.lbl_ssid_Args = QtWidgets.QLabel(self)
        self.lbl_ssid_Args.setStyleSheet("color: rgb(170, 170, 127);")
        self.lbl_ssid_Args.setAlignment(QtCore.Qt.AlignCenter)
        self.lbl_ssid_Args.setObjectName("lbl_ssid_Args")
        self.horizontalLayout_2.addWidget(self.lbl_ssid_Args)
        self.lbl_Signal_Strength_Args = QtWidgets.QLabel(self)
        self.lbl_Signal_Strength_Args.setStyleSheet("color: rgb(170, 170, 127);")
        self.lbl_Signal_Strength_Args.setAlignment(QtCore.Qt.AlignCenter)
        self.lbl_Signal_Strength_Args.setObjectName("lbl_Signal_Strength_Args")
        self.horizontalLayout_2.addWidget(self.lbl_Signal_Strength_Args)
        self.verticalLayout_3.addLayout(self.horizontalLayout_2)
        self.horizontalLayout.addLayout(self.verticalLayout_3)
        self.verticalLayout_5 = QtWidgets.QVBoxLayout()
        self.verticalLayout_5.setObjectName("verticalLayout_5")
        self.label_5 = QtWidgets.QLabel(self)
        font = QtGui.QFont()
        font.setPointSize(12)
        font.setBold(True)
        font.setWeight(75)
        self.label_5.setFont(font)
        self.label_5.setStyleSheet("color: rgb(85, 85, 127);")
        self.label_5.setAlignment(QtCore.Qt.AlignCenter)
        self.label_5.setObjectName("label_5")
        self.verticalLayout_5.addWidget(self.label_5)
        self.horizontalLayout_6 = QtWidgets.QHBoxLayout()
        self.horizontalLayout_6.setObjectName("horizontalLayout_6")
        self.label_11 = QtWidgets.QLabel(self)
        font = QtGui.QFont()
        font.setPointSize(11)
        font.setBold(True)
        font.setWeight(75)
        self.label_11.setFont(font)
        self.label_11.setStyleSheet("background-color: rgb(85, 170, 127);")
        self.label_11.setAlignment(QtCore.Qt.AlignCenter)
        self.label_11.setObjectName("label_11")
        self.horizontalLayout_6.addWidget(self.label_11)
        self.label_10 = QtWidgets.QLabel(self)
        font = QtGui.QFont()
        font.setPointSize(11)
        font.setBold(True)
        font.setWeight(75)
        self.label_10.setFont(font)
        self.label_10.setStyleSheet("background-color: rgb(85, 170, 127);")
        self.label_10.setAlignment(QtCore.Qt.AlignCenter)
        self.label_10.setObjectName("label_10")
        self.horizontalLayout_6.addWidget(self.label_10)
        self.label_9 = QtWidgets.QLabel(self)
        font = QtGui.QFont()
        font.setPointSize(11)
        font.setBold(True)
        font.setWeight(75)
        self.label_9.setFont(font)
        self.label_9.setStyleSheet("background-color: rgb(85, 170, 127);")
        self.label_9.setAlignment(QtCore.Qt.AlignCenter)
        self.label_9.setObjectName("label_9")
        self.horizontalLayout_6.addWidget(self.label_9)
        self.verticalLayout_5.addLayout(self.horizontalLayout_6)
        self.horizontalLayout_5 = QtWidgets.QHBoxLayout()
        self.horizontalLayout_5.setObjectName("horizontalLayout_5")
        self.lbl_ConnectionStatus = QtWidgets.QLabel(self)
        self.lbl_ConnectionStatus.setAlignment(QtCore.Qt.AlignCenter)
        self.lbl_ConnectionStatus.setObjectName("lbl_ConnectionStatus")
        self.horizontalLayout_5.addWidget(self.lbl_ConnectionStatus)
        self.lbl_Signal_Strength = QtWidgets.QLabel(self)
        self.lbl_Signal_Strength.setAlignment(QtCore.Qt.AlignCenter)
        self.lbl_Signal_Strength.setObjectName("lbl_Signal_Strength")
        self.horizontalLayout_5.addWidget(self.lbl_Signal_Strength)
        self.label_12 = QtWidgets.QLabel(self)
        self.label_12.setAlignment(QtCore.Qt.AlignCenter)
        self.label_12.setObjectName("label_12")
        self.horizontalLayout_5.addWidget(self.label_12)
        self.verticalLayout_5.addLayout(self.horizontalLayout_5)
        self.horizontalLayout.addLayout(self.verticalLayout_5)
        self.verticalLayout.addLayout(self.horizontalLayout)
        self.lbl_ShowInfo = QtWidgets.QLabel(self)
        font = QtGui.QFont()
        font.setPointSize(14)
        self.lbl_ShowInfo.setFont(font)
        self.lbl_ShowInfo.setStyleSheet("background-color: rgb(0, 0, 0);\n"
                                        "color: rgb(255, 255, 127);")
        self.lbl_ShowInfo.setAlignment(QtCore.Qt.AlignCenter)
        self.lbl_ShowInfo.setObjectName("lbl_ShowInfo")
        self.verticalLayout.addWidget(self.lbl_ShowInfo)
        self.lbl_ShowInfo.setWordWrap(True)  # 启用自动换行
        self.verticalLayout.setStretch(0, 4)
        self.verticalLayout.setStretch(1, 6)
        self.gridLayout.addLayout(self.verticalLayout, 0, 0, 1, 1)

        self.retranslateUi()
        QtCore.QMetaObject.connectSlotsByName(self)
        self.setWindowFlags(QtCore.Qt.WindowMinimizeButtonHint | QtCore.Qt.WindowCloseButtonHint)  # 只显示最小化按钮和关闭按钮

    def retranslateUi(self):
        _translate = QtCore.QCoreApplication.translate
        self.setWindowTitle(_translate("Form", "Wifi 硬件检测"))
        self.lbl_ItemName.setText(_translate("Form", "WiFi测试"))
        self.label_4.setText(_translate("Form", "配置信息"))
        self.label_6.setText(_translate("Form", "ssid"))
        self.label_2.setText(_translate("Form", "信号强度"))
        self.lbl_ssid_Args.setText(_translate("Form", "N/A"))
        self.lbl_Signal_Strength_Args.setText(_translate("Form", "N/A"))
        self.label_5.setText(_translate("Form", "检测结果"))
        self.label_11.setText(_translate("Form", "连接"))
        self.label_10.setText(_translate("Form", "信号"))
        self.label_9.setText(_translate("Form", "通讯"))
        self.lbl_ConnectionStatus.setText(_translate("Form", "N/A"))
        self.lbl_Signal_Strength.setText(_translate("Form", "N/A"))
        self.label_12.setText(_translate("Form", "N/A"))
        self.lbl_ShowInfo.setText(_translate("Form", "WiFi硬件设备检测中.."))

    # 更新测试参数json,itemName:项目名称,readValue:读取值,testResult:测试结果
    def updatejsontestargs(self, itemName, readValue, testResult):
        try:
            self.testArgs = self.readjsoninfo('./Conf/TestArgs.json')
            if self.testArgs is None:
                raise Exception("Failed to read configuration, aborting update.")
            for js in self.testArgs:
                if itemName in js['ItemScript']:
                    js['Read'] = readValue
                    js['TestResult'] = testResult
            with open("./Conf/TestArgs.json", "w") as write_file:
                json.dump(self.testArgs, write_file, indent=4)
            return True
        except Exception as e:
            self.showlog(f"Update TestArgs.json ItemName: {itemName} Info Err: {str(e)}", False)
            return False

    # 读取json信息,fileName:文件名称
    def readjsoninfo(self, fileName):
        try:
            if os.path.exists(fileName):
                with open(fileName, 'r', encoding='utf-8') as f:
                    return json.loads(f.read())
            return None
        except Exception as e:
            self.showlog("Read " + fileName + " Err:" + str(e), False)
            sys.exit(1)

    '''
    核对wifi信息
    active:状态是否为活动
    signal_strength:信号强度
    '''
    def chk_wifiinfo(self, active, signal_strength):
        try:
            self.lbl_Signal_Strength.setText(f'{self.testresult["signal_strength"]}%')
            if self.testresult['active'] == active:
                self.showlog('核对Wifi活动状态通过!!', True)
            else:
                self.showlog(f'核对Wifi活动状态失败,参数:{active},侦测:{self.testresult["active"]}', False)
                return False
            if int(self.testresult['signal_strength']) >= int(signal_strength):
                self.lbl_Signal_Strength.setStyleSheet("color: green;")
                self.label_12.setText('正常')
                self.label_12.setStyleSheet("color: green;")
                self.showlog('核对wifi连接信息强度通过!!', True)
                return True
            else:
                self.lbl_ConnectionStatus.setStyleSheet("color: red;")
                self.label_12.setText('异常')
                self.label_12.setStyleSheet("color: red;")
                self.showlog(f'核对Wifi连接信号强度失败,参数:{signal_strength}%,侦测{self.testresult["signal_strength"]}', False)
                return False
        except subprocess.CalledProcessError as e:
            self.showlog(f"check wifi info err: {e}", False)
            return False

    def get_wifi_info(self):
        os_type = platform.system()
        try:
            if os_type == 'Linux':
                nmcli_output = subprocess.check_output(
                    ['nmcli', '-t', '-f', 'IN-USE,SSID,SIGNAL,BSSID,FREQ,DEVICE', 'dev', 'wifi']).decode('utf-8')
                self.parse_linux_output(nmcli_output)
                return True
            elif os_type == 'Windows':
                time.sleep(2)
                output = subprocess.check_output(['netsh', 'wlan', 'show', 'interfaces']).decode('cp936')
                self.parse_windows_output(output)
                return True
        except subprocess.CalledProcessError as e:
            self.showlog(f"Failed to get WiFi information: {e}", False)
            return False

    def parse_windows_output(self, output):
        if "已连接" in output:
            self.testresult['connect'] = 'OK'
        self.testresult['ssid'] = re.search(r"SSID\s+:\s+([^\r\n]+)", output).group(1).strip()
        self.testresult['signal_strength'] = re.search(r"信号\s+:\s+(\d+)%", output).group(1).strip()
        self.testresult['wifimac'] = re.search(r"物理地址\s+:\s+([^\r\n]+)", output).group(1).strip()
        self.testresult['frequency_band'] = re.search(r"频带\s+:\s+([^\r\n]+)", output).group(1).strip()

    def parse_linux_output(self, nmcli_output):
        lines = nmcli_output.strip().split('\n')
        for line in lines:
            if line.startswith('*'):
                parts = line.split(':')
                self.testresult['active'] = 'Active'
                self.testresult['ssid'] = parts[1].strip()
                self.testresult['signal_strength'] = parts[2].strip()
                self.testresult['wifimac'] = parts[3].strip()
                self.testresult['frequency_band'] = parts[4].strip()
                self.testresult['dev'] = parts[5].strip()
                break

    # 检查当前是否已连接到指定的SSID
    def is_connected_to_ssid(self, ssid):
        os_type = platform.system()
        try:
            if os_type == 'Linux':
                output = subprocess.check_output(['nmcli', '-t', '-f', 'active,ssid', 'connection', 'show', '--active']).decode('utf-8')
                for line in output.splitlines():
                    if line.startswith("yes:") and ssid in line:
                        return True
            elif os_type == 'Windows':
                output = subprocess.check_output(['netsh', 'wlan', 'show', 'interfaces']).decode('cp936')
                connected_ssid = re.search(r"SSID\s+:\s+([^\r\n]+)", output).group(1).strip()
                if ssid == connected_ssid:
                    return True
        except subprocess.CalledProcessError as e:
            self.showlog(f"Failed to check current WiFi connection: {e}", False)
        return False

    # 连接无线网络
    def connect_wifi(self, ssid, password, retries=5):
        if self.is_connected_to_ssid(ssid):
            self.showlog(f"Already connected to SSID: {ssid}", True)
            self.testresult['connect'] = 'OK'
            self.lbl_ConnectionStatus.setText("已连接")
            self.lbl_ConnectionStatus.setStyleSheet("color: green;")
            return True

        os_type = platform.system()
        try:
            if os_type == 'Linux':
                for attempt in range(retries):
                    self.showlog(f"尝试连接无线网络:第 {attempt + 1} 次", True)
                    subprocess.run(['/usr/bin/nmcli', 'radio', 'wifi', 'on'], check=True)
                    result = subprocess.run(['/usr/bin/nmcli', 'dev', 'wifi', 'connect', ssid, 'password', password],
                                            stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=False)
                    self.showlog(result.stdout.decode('utf-8'), True)
                    if result.returncode == 0:
                        self.lbl_ShowInfo.setText("无线网卡连接网络成功!!")
                        self.testresult['connect'] = 'OK'
                        self.lbl_ConnectionStatus.setText("已连接")
                        self.lbl_ConnectionStatus.setStyleSheet("color: green;")
                        return True
                    else:
                        self.showlog(f"第 {attempt + 1} 次连接失败: {result.stderr.decode('utf-8')}", False)
                        time.sleep(1)  # 等待1秒再重试
                self.lbl_ShowInfo.setText("无线网卡连接失败!!")
                self.lbl_ConnectionStatus.setText("未连接")
                self.lbl_ConnectionStatus.setStyleSheet("color: red;")
                return False
            elif os_type == 'Windows':
                for attempt in range(retries):
                    self.showlog(f"尝试连接无线网络:第 {attempt + 1} 次", True)
                    result = subprocess.run(['netsh', 'wlan', 'connect', 'name={}'.format(ssid)], check=True)
                    if result.returncode == 0:
                        self.lbl_ShowInfo.setText("无线网卡连接网络成功!!")
                        self.testresult['connect'] = 'OK'
                        self.lbl_ConnectionStatus.setText("已连接")
                        self.lbl_ConnectionStatus.setStyleSheet("color: green;")
                        return True
                    else:
                        self.showlog(f"第 {attempt + 1} 次连接失败", False)
                        time.sleep(1)  # 等待1秒再重试
                self.lbl_ShowInfo.setText("无线网卡连接失败!!")
                self.lbl_ConnectionStatus.setText("未连接")
                self.lbl_ConnectionStatus.setStyleSheet("color: red;")
                return False
        except subprocess.CalledProcessError as e:
            self.showlog(f"Failed to connect to WiFi: {e}", False)
            self.lbl_ConnectionStatus.setText("连接错误")
            self.lbl_ConnectionStatus.setStyleSheet("color: red;")
            return False

    def disconnect_wifi(self):
        try:
            # 列出所有网络接口
            result = subprocess.run(['nmcli', 'device'], capture_output=True, text=True)
            output = result.stdout

            # 打印网络接口列表
            print(output)

            # 查找WiFi接口
            wifi_interface = None
            for line in output.split('\n'):
                if 'wifi' in line.lower():
                    parts = line.split()
                    if len(parts) > 0:
                        wifi_interface = parts[0]
                        break

            if not wifi_interface:
                print('No WiFi interface found.')
                return

            # 断开WiFi接口
            disconnect_result = subprocess.run(['nmcli', 'device', 'disconnect', wifi_interface], capture_output=True,
                                               text=True)

            if disconnect_result.returncode == 0:
                print(f'Successfully disconnected {wifi_interface}')
            else:
                print(f'Failed to disconnect {wifi_interface}')
                print(disconnect_result.stderr)
        except Exception as e:
            print(f'An error occurred: {e}')

    def intercept_test_parameters(self, argsvalues):
        try:
            testargs = argsvalues.split('|')
            ssid = testargs[0].split('=')[1]
            self.lbl_ssid_Args.setText(ssid)
            password = testargs[1].split('=')[1]
            signal_strength = testargs[2].split('=')[1]
            self.lbl_Signal_Strength_Args.setText(signal_strength)
            active = testargs[3].split('=')[1]
            self.testargs.append({'ssid': ssid, 'password': password, 'signal_strength': signal_strength, 'active': active})
            self.lbl_ShowInfo.setText('读取测试硬件wifi配置信息完成!!')
            return True
        except Exception as e:
            self.showlog("intercept test parameters Err:" + str(e), False)
            sys.exit(1)

    def readjsontestargs(self, itemName):
        try:
            if os.path.exists('./Conf/TestArgs.json'):
                with open('./Conf/TestArgs.json', 'r', encoding='utf-8') as f:
                    self.testArgs = json.loads(f.read())
            else:
                self.showlog("Not Find ./Conf/TestArgs.json File", False)
                sys.exit(1)
            for js in self.testArgs:
                if itemName in js['ItemScript']:
                    self.testStandardArgs = js['Standard']
                    return True
            self.showlog(f"Read ./Conf/TestArgs.json ItemName:{self.itemName} Info is Empty!!", False)
            sys.exit(1)
        except Exception as e:
            self.showlog("Read ./Conf/TestArgs.json or ItemName: " + itemName + " Info Err:" + str(e), False)
            sys.exit(1)

    def getitemname(self):
        try:
            full_path = sys.argv[0]
            file_name = os.path.basename(full_path)
            self.itemName = file_name
            return True
        except Exception as e:
            self.showlog(f'Get Item Name Err:{str(e)}', False)
            sys.exit(1)

    def loggerrinfo(self):
        try:
            log_directory = './log'
            if not os.path.exists(log_directory):
                os.makedirs(log_directory)
            self.config = configparser.ConfigParser()
            self.logger = logging.getLogger('my_logger')
            self.logger.setLevel(logging.DEBUG)
            file_handler = logging.FileHandler(f'{log_directory}/log.txt', encoding='utf-8')
            formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
            file_handler.setFormatter(formatter)
            self.logger.addHandler(file_handler)
            return True
        except Exception as e:
            self.showlog(f'Create logger_info Error: {str(e)}', False)
            sys.exit(1)

    def showlog(self, log, isPass):
        if isinstance(log, list):
            log = ', '.join(str(item) for item in log)
        try:
            if isPass:
                self.lbl_ShowInfo.setStyleSheet("background-color: rgb(0, 0, 0); color: green;")
            else:
                self.lbl_ShowInfo.setStyleSheet("background-color: rgb(0, 0, 0); color: red;")
            self.lbl_ShowInfo.setText(log)
            if isPass:
                self.logger.info(str(log))
            else:
                self.logger.error(str(log))
        except Exception as e:
            print("\033[1;31m" + str(e) + " \033[0m")
            sys.exit(1)

    def center(self):
        screen_geometry = app.desktop().availableGeometry()
        x = (screen_geometry.width() - self.width()) // 2
        y = (screen_geometry.height() - self.height()) // 2
        self.move(x, y)

    def closeEvent(self, event):
        self.disconnect_wifi()  # 测试完成后断开无线网络连接
        sys.exit(1)

    def check_communication(self):
        try:
            # 示例代码,通过ping一个网站来验证通信是否正常
            response = subprocess.run(['ping', '-c', '4', '10.2.2.163'], stdout=subprocess.PIPE)
            if response.returncode == 0:
                self.label_12.setText('正常')
                self.label_12.setStyleSheet("color: green;")
                self.showlog('通信检测通过!!', True)
                return True
            else:
                self.label_12.setText('异常')
                self.label_12.setStyleSheet("color: red;")
                self.showlog('通信检测失败', False)
                return False
        except subprocess.CalledProcessError as e:
            self.showlog(f"check communication error: {e}", False)
            self.label_12.setText('异常')
            self.label_12.setStyleSheet("color: red;")
            return False

    def get_wifi_mac_address(self):
        try:
            # 获取所有网络接口及其详细信息
            result = subprocess.run(['ifconfig'], capture_output=True, text=True)
            output = result.stdout

            # 打印网络接口列表
            print(output)

            # 使用正则表达式查找WiFi接口的MAC地址
            mac_address = None
            wifi_interface = None
            for line in output.split('\n\n'):
                if 'wlp' in line:
                    wifi_interface = re.search(r'^(\S+)', line).group(1)
                    mac_address_match = re.search(r'ether\s+([0-9a-fA-F:]+)', line)
                    if mac_address_match:
                        mac_address = mac_address_match.group(1)
                        break

            if wifi_interface and mac_address:
                print(f'WiFi Interface: {wifi_interface}')
                print(f'MAC Address: {mac_address}')
                return mac_address
            else:
                print('No WiFi interface found or no MAC address available.')
                return None
        except Exception as e:
            print(f'An error occurred: {e}')
            return None


if __name__ == '__main__':
    app = QApplication(sys.argv)
    win = WifiTestWinFrm()
    win.center()
    win.show()
    sys.exit(app.exec_())

点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部