// SPDX-FileCopyrightText: 2022 - 2026 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later

// 项目自身文件
#include "DeviceManager.h"
#include "DDLog.h"

// Qt库文件
#include <QLoggingCategory>
#include <QFile>
#include <QFileInfo>
#include <QMutexLocker>
#include <QProcess>

// 其它头文件
#include "DeviceCpu.h"
#include "DeviceGpu.h"
#include "DeviceStorage.h"
#include "DeviceMemory.h"
#include "DeviceMonitor.h"
#include "DeviceBios.h"
#include "DeviceBluetooth.h"
#include "DeviceAudio.h"
#include "DeviceNetwork.h"
#include "DeviceImage.h"
#include "DeviceOthers.h"
#include "DevicePower.h"
#include "DevicePrint.h"
#include "DeviceOtherPCI.h"
#include "DeviceComputer.h"
#include "DeviceCdrom.h"
#include "DeviceInput.h"
#include "MacroDefinition.h"
#include <QRegularExpression>   
#include <algorithm> // for std::sort

using namespace DDLog;

DeviceManager    *DeviceManager::sInstance = nullptr;
int DeviceManager::m_CurrentXlsRow = 1;

static QMutex addCmdMutex;

DeviceManager::DeviceManager()
    : m_CpuNum(1)
{
    qCDebug(appLog) << "DeviceManager constructor initialized";
}

DeviceManager::~DeviceManager()
{
    qCDebug(appLog) << "DeviceManager destructor started";
    clear();
}

void DeviceManager::clear()
{
    qCDebug(appLog) << "Starting to clear all device resources";
    
    // 清除所有命令
    m_cmdInfo.clear();

    // 清除内存中的所有设备指针
    foreach (auto device, m_ListDeviceMouse)
        delete device;

    foreach (auto device, m_ListDeviceStorage)
        delete device;

    foreach (auto device, m_ListDeviceMonitor)
        delete device;

    foreach (auto device, m_ListDeviceBluetooth)
        delete device;

    foreach (auto device, m_ListDeviceAudio)
        delete device;

    foreach (auto device, m_ListDeviceImage)
        delete device;

    foreach (auto device, m_ListDeviceKeyboard)
        delete device;

    foreach (auto device, m_ListDeviceOthers)
        delete device;

    foreach (auto device, m_ListDevicePower)
        delete device;

    foreach (auto device, m_ListDevicePrint)
        delete device;

    foreach (auto device, m_ListDeviceOtherPCI)
        delete device;

    foreach (auto device, m_ListDeviceCdrom)
        delete device;

    foreach (auto device, m_ListDeviceComputer)
        delete device;

    foreach (auto device, m_ListDeviceNetwork)
        delete device;

    foreach (auto device, m_ListDeviceBios)
        delete device;

    foreach (auto device, m_ListDeviceGPU)
        delete device;

    foreach (auto device, m_ListDeviceMemory)
        delete device;

    foreach (auto device, m_ListDeviceCPU)
        delete device;

    // 清空存储设备指针的列表
    m_ListDeviceMouse.clear();
    m_ListDeviceStorage.clear();
    m_ListDeviceMonitor.clear();
    m_ListDeviceBluetooth.clear();
    m_ListDeviceAudio.clear();
    m_ListDeviceImage.clear();
    m_ListDeviceKeyboard.clear();
    m_ListDeviceOthers.clear();
    m_ListDevicePower.clear();
    m_ListDevicePrint.clear();
    m_ListDeviceOtherPCI.clear();
    m_ListDeviceCdrom.clear();
    m_ListDeviceComputer.clear();
    m_ListDeviceNetwork.clear();
    m_ListDeviceBios.clear();
    m_ListDeviceGPU.clear();
    m_ListDeviceMemory.clear();
    m_ListDeviceCPU.clear();
    m_DeviceClassMap.clear();
    qCDebug(appLog) << "All device resources cleared successfully";
}

const QList<QPair<QString, QString>> &DeviceManager::getDeviceTypes()
{
    qCDebug(appLog) << "Getting device types";
    // 获取设备类型
    // 清空设备类型列表
    m_ListDeviceType.clear();
    bool addSeperator = false;
    // 添加概况信息
    if (true) {
        qCDebug(appLog) << "Adding overview information";
        m_ListDeviceType.append(QPair<QString, QString>(tr("Overview"), "overview##Overview"));
        addSeperator = true;
    }

    // 添加cpu信息
    if (m_ListDeviceCPU.size() > 0) {
        qCDebug(appLog) << "Adding CPU information";
        m_ListDeviceType.append(QPair<QString, QString>(tr("CPU"), "cpu##CPU"));
        addSeperator = true;
    }

    if (m_CpuNum > 1) {
        qCDebug(appLog) << "Adding CPU quantity information";
        m_ListDeviceType.append(QPair<QString, QString>(tr("CPU quantity"), ""));
        addSeperator = true;
    }

    if (addSeperator) {
        qCDebug(appLog) << "Adding separator";
        m_ListDeviceType.append(QPair<QString, QString>("Separator", "Separator##Separator"));
        addSeperator = false;
    }

    // 板载接口设备
    if (m_ListDeviceBios.size() > 0) {
        qCDebug(appLog) << "Adding Motherboard information";
        m_ListDeviceType.append(QPair<QString, QString>(tr("Motherboard"), "motherboard##Bios"));
        addSeperator = true;
    }

    if (m_ListDeviceMemory.size() > 0) {
        qCDebug(appLog) << "Adding Memory information";
        m_ListDeviceType.append(QPair<QString, QString>(tr("Memory"), "memory##Memory"));
        addSeperator = true;
    }

    if (m_ListDeviceGPU.size() > 0) {
        qCDebug(appLog) << "Adding Display Adapter information";
        m_ListDeviceType.append(QPair<QString, QString>(tr("Display Adapter"), "displayadapter##GPU"));
        addSeperator = true;
    }

    if (m_ListDeviceAudio.size() > 0) {
        qCDebug(appLog) << "Adding Sound Adapter information";
        m_ListDeviceType.append(QPair<QString, QString>(tr("Sound Adapter"), "audiodevice##Audio"));
        addSeperator = true;
    }

    if (m_ListDeviceStorage.size() > 0) {
        qCDebug(appLog) << "Adding Storage information";
        m_ListDeviceType.append(QPair<QString, QString>(tr("Storage"), "storage##Storage"));
        addSeperator = true;
    }

    if (m_ListDeviceOtherPCI.size() > 0) {
        qCDebug(appLog) << "Adding Other PCI Devices information";
        m_ListDeviceType.append(QPair<QString, QString>(tr("Other PCI Devices"), "otherpcidevices##OtherPCI"));
        addSeperator = true;
    }

    if (m_ListDevicePower.size() > 0) {
        qCDebug(appLog) << "Adding Battery information";
        m_ListDeviceType.append(QPair<QString, QString>(tr("Battery"), "battery##Power"));
        addSeperator = true;
    }

    if (addSeperator) {
        qCDebug(appLog) << "Adding separator";
        m_ListDeviceType.append(QPair<QString, QString>("Separator", "Separator##Separator"));
        addSeperator = false;
    }

    // 网络设备
    if (m_ListDeviceBluetooth.size() > 0) {
        qCDebug(appLog) << "Adding Bluetooth information";
        m_ListDeviceType.append(QPair<QString, QString>(tr("Bluetooth"), "bluetooth##Bluetooth"));
        addSeperator = true;
    }

    if (m_ListDeviceNetwork.size() > 0) {
        qCDebug(appLog) << "Adding Network Adapter information";
        m_ListDeviceType.append(QPair<QString, QString>(tr("Network Adapter"), "networkadapter##Network"));
        addSeperator = true;
    }

    if (addSeperator) {
        qCDebug(appLog) << "Adding separator";
        m_ListDeviceType.append(QPair<QString, QString>("Separator", "Separator##Separator"));
        addSeperator = false;
    }

    // 输入设备
    if (m_ListDeviceMouse.size() > 0) {
        qCDebug(appLog) << "Adding Mouse information";
        m_ListDeviceType.append(QPair<QString, QString>(tr("Mouse"), "mouse##Mouse"));
        addSeperator = true;
    }

    if (m_ListDeviceKeyboard.size() > 0) {
        qCDebug(appLog) << "Adding Keyboard information";
        m_ListDeviceType.append(QPair<QString, QString>(tr("Keyboard"), "keyboard##Keyboard"));
        addSeperator = true;
    }

    if (addSeperator) {
        qCDebug(appLog) << "Adding separator";
        m_ListDeviceType.append(QPair<QString, QString>("Separator", "Separator##Separator"));
    }

    // 外设设备
    if (m_ListDeviceMonitor.size() > 0) {
        qCDebug(appLog) << "Adding Monitor information";
        m_ListDeviceType.append(QPair<QString, QString>(tr("Monitor"), "monitor##Monitor"));
    }

    if (m_ListDeviceCdrom.size() > 0) {
        qCDebug(appLog) << "Adding CD-ROM information";
        m_ListDeviceType.append(QPair<QString, QString>(tr("CD-ROM"), "cdrom##Cdrom"));
    }

    if (m_ListDevicePrint.size() > 0) {
        qCDebug(appLog) << "Adding Printer information";
        m_ListDeviceType.append(QPair<QString, QString>(tr("Printer"), "printer##Print"));
    }

    if (m_ListDeviceImage.size() > 0) {
        qCDebug(appLog) << "Adding Camera information";
        m_ListDeviceType.append(QPair<QString, QString>(tr("Camera"), "camera##Image"));
    }

    if (m_ListDeviceOthers.size() > 0) {
        qCDebug(appLog) << "Adding Other Devices information";
        m_ListDeviceType.append(QPair<QString, QString>(tr("Other Devices", "Other Input Devices"), "otherdevices##Others"));
    }

    qCDebug(appLog) << "Finished getting device types";
    return m_ListDeviceType;
}

void DeviceManager::setDeviceListClass()
{
    qCDebug(appLog) << "Setting device list class";
    // 添加设备类型与设备指针列表的映射关系
    m_DeviceClassMap[tr("CPU")] = m_ListDeviceCPU;
    m_DeviceClassMap[tr("Motherboard")] =  m_ListDeviceBios;
    m_DeviceClassMap[tr("Memory")]  =  m_ListDeviceMemory;
    m_DeviceClassMap[tr("Display Adapter")] =  m_ListDeviceGPU;
    m_DeviceClassMap[tr("Sound Adapter")] =  m_ListDeviceAudio;
    m_DeviceClassMap[tr("Storage")]  =  m_ListDeviceStorage;
    m_DeviceClassMap[tr("Other PCI Devices")] =  m_ListDeviceOtherPCI;
    m_DeviceClassMap[tr("Battery")] =  m_ListDevicePower;
    m_DeviceClassMap[tr("Bluetooth")] =  m_ListDeviceBluetooth;
    m_DeviceClassMap[tr("Network Adapter")]  =  m_ListDeviceNetwork;
    m_DeviceClassMap[tr("Mouse")] =  m_ListDeviceMouse;
    m_DeviceClassMap[tr("Keyboard")]  =  m_ListDeviceKeyboard;
    m_DeviceClassMap[tr("Monitor")] =  m_ListDeviceMonitor;
    m_DeviceClassMap[tr("CD-ROM")] =  m_ListDeviceCdrom;
    m_DeviceClassMap[tr("Printer")] =  m_ListDevicePrint;
    m_DeviceClassMap[tr("Camera")] =  m_ListDeviceImage;
    m_DeviceClassMap[tr("Other Devices", "Other Input Devices")] =  m_ListDeviceOthers;
}

bool DeviceManager::getDeviceList(const QString &name, QList<DeviceBaseInfo *> &lst)
{
    qCDebug(appLog) << "Getting device list for name:" << name;
    // name为概况Overview时,没有设备列表
    if (name == tr("Overview"))
        return false;

    // 获取设备指针列表
    if (m_DeviceClassMap.find(name) != m_DeviceClassMap.end()) {
        qCDebug(appLog) << "Found device list for name:" << name;
        lst = m_DeviceClassMap[name];
    }

    return true;
}

QString DeviceManager::convertDeviceTomlClassName(DeviceType deviceType)
{
    qCDebug(appLog) << "Converting device type to TOML class name for deviceType:" << deviceType;
    //与oeminfoxxx.toml文件中的[hardclassname.submember] 保持一致，即 “toml+hardclassname”
    if (deviceType == DT_Null)      {return "";}
    if (deviceType == DT_Computer)  {return "tomlComputer";}
    if (deviceType == DT_Cpu)       {return "tomlCPU";}
    if (deviceType == DT_Bios)      {return "tomlMotherboard";}
    if (deviceType == DT_Memory)    {return "tomlMemory";}
    if (deviceType == DT_Gpu)       {return "tomlDisplayGPU";}
    if (deviceType == DT_Audio)     {return "tomlSoundAudio";}
    if (deviceType == DT_Storage)   {return "tomlStorage";}
    if (deviceType == DT_OtherPCI)  {return "tomlOtherPCI";}
    if (deviceType == DT_Power)     {return "tomlPower";}
    if (deviceType == DT_Bluetoorh) {return "tomlBluetooth";}
    if (deviceType == DT_Network)   {return "tomlNetWork";}
    if (deviceType == DT_Mouse)     {return "tomlMouse";}
    if (deviceType == DT_Keyboard)  {return "tomlKeyboard";}
    if (deviceType == DT_Monitor)   {return "tomlMonitor";}
    if (deviceType == DT_Cdrom)     {return "tomlCDROM";}
    if (deviceType == DT_Print)     {return "tomlPrinter";}
    if (deviceType == DT_Image)     {return "tomlCamera";}
    if (deviceType == DT_Others)    {return "tomlOtherDevices";}
    return "";
}

QList<DeviceBaseInfo *> *DeviceManager::convertDeviceListAddr(DeviceType deviceType)
{
    qCDebug(appLog) << "Converting device type to list address for deviceType:" << deviceType;
//    if (deviceType == DT_Null)      {return &m_ListDeviceOthers;}
    if (deviceType == DT_Computer)  {return &m_ListDeviceComputer;}
    if (deviceType == DT_Cpu)       {return &m_ListDeviceCPU;}
    if (deviceType == DT_Bios)      {return &m_ListDeviceBios;}
    if (deviceType == DT_Memory)    {return &m_ListDeviceMemory;}
    if (deviceType == DT_Gpu)       {return &m_ListDeviceGPU;}
    if (deviceType == DT_Audio)     {return &m_ListDeviceAudio;}
    if (deviceType == DT_Storage)   {return &m_ListDeviceStorage;}
    if (deviceType == DT_OtherPCI)  {return &m_ListDeviceOtherPCI;}
    if (deviceType == DT_Power)     {return &m_ListDevicePower;}
    if (deviceType == DT_Bluetoorh) {return &m_ListDeviceBluetooth;}
    if (deviceType == DT_Network)   {return &m_ListDeviceNetwork;}
    if (deviceType == DT_Mouse)     {return &m_ListDeviceMouse;}
    if (deviceType == DT_Keyboard)  {return &m_ListDeviceKeyboard;}
    if (deviceType == DT_Monitor)   {return &m_ListDeviceMonitor;}
    if (deviceType == DT_Cdrom)     {return &m_ListDeviceCdrom;}
    if (deviceType == DT_Print)     {return &m_ListDevicePrint;}
    if (deviceType == DT_Image)     {return &m_ListDeviceImage;}
    if (deviceType == DT_Others)    {return &m_ListDeviceOthers;}
    return &m_ListDeviceOthers;
}
QList<DeviceBaseInfo *> DeviceManager::convertDeviceList(DeviceType deviceType)
{
    qCDebug(appLog) << "Converting device type to list for deviceType:" << deviceType;
//    if (deviceType == DT_Null)      {return m_ListDeviceOthers;}
    if (deviceType == DT_Computer)  {return m_ListDeviceComputer;}
    if (deviceType == DT_Cpu)       {return m_ListDeviceCPU;}
    if (deviceType == DT_Bios)      {return m_ListDeviceBios;}
    if (deviceType == DT_Memory)    {return m_ListDeviceMemory;}
    if (deviceType == DT_Gpu)       {return m_ListDeviceGPU;}
    if (deviceType == DT_Audio)     {return m_ListDeviceAudio;}
    if (deviceType == DT_Storage)   {return m_ListDeviceStorage;}
    if (deviceType == DT_OtherPCI)  {return m_ListDeviceOtherPCI;}
    if (deviceType == DT_Power)     {return m_ListDevicePower;}
    if (deviceType == DT_Bluetoorh) {return m_ListDeviceBluetooth;}
    if (deviceType == DT_Network)   {return m_ListDeviceNetwork;}
    if (deviceType == DT_Mouse)     {return m_ListDeviceMouse;}
    if (deviceType == DT_Keyboard)  {return m_ListDeviceKeyboard;}
    if (deviceType == DT_Monitor)   {return m_ListDeviceMonitor;}
    if (deviceType == DT_Cdrom)     {return m_ListDeviceCdrom;}
    if (deviceType == DT_Print)     {return m_ListDevicePrint;}
    if (deviceType == DT_Image)     {return m_ListDeviceImage;}
    if (deviceType == DT_Others)    {return m_ListDeviceOthers;}

    return QList<DeviceBaseInfo *>();
}

DeviceBaseInfo *DeviceManager::createDevice(DeviceType deviceType)
{
    qCDebug(appLog) << "Creating device for deviceType:" << deviceType;
    DeviceBaseInfo *vTemp { nullptr };
    if (deviceType == DT_Computer)  {vTemp = new DeviceComputer();  return vTemp;}
    if (deviceType == DT_Cpu)       {vTemp = new DeviceCpu();  return vTemp;}
    if (deviceType == DT_Bios)      {vTemp = new DeviceBios();  return vTemp;}
    if (deviceType == DT_Memory)    {vTemp =  new DeviceMemory();  return vTemp;}
    if (deviceType == DT_Gpu)       {vTemp = new DeviceGpu();  return vTemp;}
    if (deviceType == DT_Audio)     {vTemp = new DeviceAudio();  return vTemp;}
    if (deviceType == DT_Storage)   {vTemp =  new DeviceStorage();  return vTemp;}
    if (deviceType == DT_OtherPCI)  {vTemp = new DeviceOtherPCI();  return vTemp;}
    if (deviceType == DT_Power)     {vTemp = new DevicePower();  return vTemp;}
    if (deviceType == DT_Bluetoorh) {vTemp = new DeviceBluetooth();  return vTemp;}
    if (deviceType == DT_Network)   {vTemp =  new DeviceNetwork();  return vTemp;}
    if (deviceType == DT_Mouse)     {vTemp = new DeviceInput();  return vTemp;}
    if (deviceType == DT_Keyboard)  {vTemp =  new DeviceInput();  return vTemp;}
    if (deviceType == DT_Monitor)   {vTemp = new DeviceMonitor();  return vTemp;}
    if (deviceType == DT_Cdrom)     {vTemp = new DeviceCdrom();  return vTemp;}
    if (deviceType == DT_Print)     {vTemp = new DevicePrint();  return vTemp;}
    if (deviceType == DT_Image)     {vTemp = new DeviceImage();  return vTemp;}
    if (deviceType == DT_Others)    {vTemp = new DeviceOthers();  return vTemp;}
    return vTemp;
}

TomlFixMethod DeviceManager::tomlDeviceMapSet(DeviceType deviceType,  DeviceBaseInfo *device, const QMap<QString, QString> &mapInfo)
{
    qCDebug(appLog) << "Setting TOML device for deviceType:" << deviceType;
    if (!device) {
        return TOML_None;
    }
    TomlFixMethod ret = TOML_None;
    switch (deviceType) {
    case DT_Null:
        break;
    // setInfoFromTomlBase 判断基本信息，若结果为删除该设备，即返回删除，不再调用setInfoFromTomlOneByOne设置其它信息
    case DT_Computer: {
        DeviceComputer *tomldevice = dynamic_cast<DeviceComputer *>(device);
        (TOML_Del == tomldevice->setInfoFromTomlBase(mapInfo)) ? ret = TOML_Del : ret = tomldevice->setInfoFromTomlOneByOne(mapInfo);
    } break;
    case DT_Cpu: {
        DeviceCpu *tomldevice = dynamic_cast<DeviceCpu *>(device);
        (TOML_Del == tomldevice->setInfoFromTomlBase(mapInfo)) ? ret = TOML_Del : ret = tomldevice->setInfoFromTomlOneByOne(mapInfo);
    } break;
    case DT_Bios: {
        DeviceBios *tomldevice = dynamic_cast<DeviceBios *>(device);
        (TOML_Del == tomldevice->setInfoFromTomlBase(mapInfo)) ? ret = TOML_Del : ret = tomldevice->setInfoFromTomlOneByOne(mapInfo);
    } break;
    case DT_Memory: {
        DeviceMemory *tomldevice = dynamic_cast<DeviceMemory *>(device);
        (TOML_Del == tomldevice->setInfoFromTomlBase(mapInfo)) ? ret = TOML_Del : ret = tomldevice->setInfoFromTomlOneByOne(mapInfo);
    } break;
    case DT_Storage: {
        DeviceStorage *tomldevice = dynamic_cast<DeviceStorage *>(device);
        (TOML_Del == tomldevice->setInfoFromTomlBase(mapInfo)) ? ret = TOML_Del : ret = tomldevice->setInfoFromTomlOneByOne(mapInfo);
    } break;
    case DT_Gpu: {
        DeviceGpu *tomldevice = dynamic_cast<DeviceGpu *>(device);
        (TOML_Del == tomldevice->setInfoFromTomlBase(mapInfo)) ? ret = TOML_Del : ret = tomldevice->setInfoFromTomlOneByOne(mapInfo);
    } break;
    case DT_Monitor: {
        DeviceMonitor *tomldevice = dynamic_cast<DeviceMonitor *>(device);
        (TOML_Del == tomldevice->setInfoFromTomlBase(mapInfo)) ? ret = TOML_Del : ret = tomldevice->setInfoFromTomlOneByOne(mapInfo);
    } break;
    case DT_Network: {
        DeviceNetwork *tomldevice = dynamic_cast<DeviceNetwork *>(device);
        (TOML_Del == tomldevice->setInfoFromTomlBase(mapInfo)) ? ret = TOML_Del : ret = tomldevice->setInfoFromTomlOneByOne(mapInfo);
    } break;
    case DT_Audio: {
        DeviceAudio *tomldevice = dynamic_cast<DeviceAudio *>(device);
        (TOML_Del == tomldevice->setInfoFromTomlBase(mapInfo)) ? ret = TOML_Del : ret = tomldevice->setInfoFromTomlOneByOne(mapInfo);
    } break;
    case DT_Bluetoorh: {
        DeviceBluetooth *tomldevice = dynamic_cast<DeviceBluetooth *>(device);
        (TOML_Del == tomldevice->setInfoFromTomlBase(mapInfo)) ? ret = TOML_Del : ret = tomldevice->setInfoFromTomlOneByOne(mapInfo);
    } break;
    case DT_Keyboard: {
        DeviceInput *tomldevice = dynamic_cast<DeviceInput *>(device);
        (TOML_Del == tomldevice->setInfoFromTomlBase(mapInfo)) ? ret = TOML_Del : ret = tomldevice->setInfoFromTomlOneByOne(mapInfo);
    } break;
    case DT_Mouse: {
        DeviceInput *tomldevice = dynamic_cast<DeviceInput *>(device);
        (TOML_Del == tomldevice->setInfoFromTomlBase(mapInfo)) ? ret = TOML_Del : ret = tomldevice->setInfoFromTomlOneByOne(mapInfo);
    } break;
    case DT_Print: {
        DevicePrint *tomldevice = dynamic_cast<DevicePrint *>(device);
        (TOML_Del == tomldevice->setInfoFromTomlBase(mapInfo)) ? ret = TOML_Del : ret = tomldevice->setInfoFromTomlOneByOne(mapInfo);
    } break;
    case DT_Image: {
        DeviceImage *tomldevice = dynamic_cast<DeviceImage *>(device);
        (TOML_Del == tomldevice->setInfoFromTomlBase(mapInfo)) ? ret = TOML_Del : ret = tomldevice->setInfoFromTomlOneByOne(mapInfo);
    } break;
    case DT_Cdrom: {
        DeviceCdrom *tomldevice = dynamic_cast<DeviceCdrom *>(device);
        (TOML_Del == tomldevice->setInfoFromTomlBase(mapInfo)) ? ret = TOML_Del : ret = tomldevice->setInfoFromTomlOneByOne(mapInfo);
    } break;
    case DT_Others: {
        DeviceOthers *tomldevice = dynamic_cast<DeviceOthers *>(device);
        (TOML_Del == tomldevice->setInfoFromTomlBase(mapInfo)) ? ret = TOML_Del : ret = tomldevice->setInfoFromTomlOneByOne(mapInfo);
    } break;
    case DT_OtherPCI: {
        DeviceOtherPCI *tomldevice = dynamic_cast<DeviceOtherPCI *>(device);
        (TOML_Del == tomldevice->setInfoFromTomlBase(mapInfo)) ? ret = TOML_Del : ret = tomldevice->setInfoFromTomlOneByOne(mapInfo);
    } break;
    case DT_Power: {
        DevicePower *tomldevice = dynamic_cast<DevicePower *>(device);
        (TOML_Del == tomldevice->setInfoFromTomlBase(mapInfo)) ? ret = TOML_Del : ret = tomldevice->setInfoFromTomlOneByOne(mapInfo);
    } break;
    }
    return ret;
}

QString DeviceManager::tomlDeviceReadKeyValue(DeviceType deviceType, DeviceBaseInfo *device, const QString &key)
{
    if (!device)
        return QString("");

    switch (deviceType) {
    case DT_Null:
        break;
    case DT_Computer: {
        DeviceComputer *tomldevice = dynamic_cast<DeviceComputer *>(device);
        return tomldevice->readDeviceInfoKeyValue(key);
    };
    case DT_Cpu: {
        DeviceCpu *tomldevice = dynamic_cast<DeviceCpu *>(device);
        return tomldevice->readDeviceInfoKeyValue(key);
    };
    case DT_Bios: {
        DeviceBios *tomldevice = dynamic_cast<DeviceBios *>(device);
        return tomldevice->readDeviceInfoKeyValue(key);
    };
    case DT_Memory: {
        DeviceMemory *tomldevice = dynamic_cast<DeviceMemory *>(device);
        return tomldevice->readDeviceInfoKeyValue(key);
    };
     case DT_Storage: {
        DeviceStorage *tomldevice = dynamic_cast<DeviceStorage *>(device);
        return tomldevice->readDeviceInfoKeyValue(key);
    };
    case DT_Gpu: {
        DeviceGpu *tomldevice = dynamic_cast<DeviceGpu *>(device);
        return tomldevice->readDeviceInfoKeyValue(key);
    };
    case DT_Monitor: {
        DeviceMonitor *tomldevice = dynamic_cast<DeviceMonitor *>(device);
        return tomldevice->readDeviceInfoKeyValue(key);
    };
    case DT_Network: {
        DeviceNetwork *tomldevice = dynamic_cast<DeviceNetwork *>(device);
        return tomldevice->readDeviceInfoKeyValue(key);
    };
    case DT_Audio: {
        DeviceAudio *tomldevice = dynamic_cast<DeviceAudio *>(device);
        return tomldevice->readDeviceInfoKeyValue(key);
    };
    case DT_Bluetoorh: {
        DeviceBluetooth *tomldevice = dynamic_cast<DeviceBluetooth *>(device);
        return tomldevice->readDeviceInfoKeyValue(key);
    };
    case DT_Keyboard: {
        DeviceInput *tomldevice = dynamic_cast<DeviceInput *>(device);
        return tomldevice->readDeviceInfoKeyValue(key);
    };
    case DT_Mouse: {
        DeviceInput *tomldevice = dynamic_cast<DeviceInput *>(device);
        return tomldevice->readDeviceInfoKeyValue(key);
    };
    case DT_Print: {
        DevicePrint *tomldevice = dynamic_cast<DevicePrint *>(device);
        return tomldevice->readDeviceInfoKeyValue(key);
    };
    case DT_Image: {
        DeviceImage *tomldevice = dynamic_cast<DeviceImage *>(device);
        return tomldevice->readDeviceInfoKeyValue(key);
    };
    case DT_Cdrom: {
        DeviceCdrom *tomldevice = dynamic_cast<DeviceCdrom *>(device);
        return tomldevice->readDeviceInfoKeyValue(key);
    };
    case DT_Others: {
        DeviceOthers *tomldevice = dynamic_cast<DeviceOthers *>(device);
        return tomldevice->readDeviceInfoKeyValue(key);
    };
    case DT_OtherPCI: {
        DeviceOtherPCI *tomldevice = dynamic_cast<DeviceOtherPCI *>(device);
        return tomldevice->readDeviceInfoKeyValue(key);
    };
    case DT_Power: {
        DevicePower *tomldevice = dynamic_cast<DevicePower *>(device);
        return tomldevice->readDeviceInfoKeyValue(key);
    };
    }
    return QString("");
}

bool DeviceManager::tomlSetBytomlmatchkey(DeviceType deviceType, DeviceBaseInfo *device, const QString &tomltomlmatchkey, const QString &tomltomlconfigdemanding)
{
    Q_UNUSED(deviceType)
    Q_UNUSED(tomltomlconfigdemanding)

    QMap<QString, QString> itemMap;
    QStringList keyValues = tomltomlmatchkey.split(",");
    foreach (const QString &keyValue, keyValues) {
        QStringList wordlst = keyValue.split("=");
        if (2 == wordlst.size()) {

            QString key = wordlst[0].remove('"').trimmed();
            QString valuetmp = wordlst[1];
            QStringList valuelst;
            QString value;
            if (valuetmp.contains("#")) {
                valuelst = valuetmp.split("#");
                value = valuelst[0].remove('\"').trimmed();
            } else
                value = valuetmp.remove('\"').trimmed();

            itemMap.insert(key, value);
        }
    }
    if(itemMap.size() == 0 )
        return false;

    for (auto key : itemMap.keys()) {
        QString tomlKey = key;
        QString tomlVal = itemMap.value(key);
        QString deviceVal = device->readDeviceInfoKeyValue(tomlKey);
        if((0 != deviceVal.compare(tomlVal, Qt::CaseInsensitive))) {
            return false;
        }
    }

    return true;
}

void DeviceManager::tomlDeviceSet(DeviceType deviceType)
{
    qCDebug(appLog) << "Setting TOML device for deviceType:" << deviceType;
    QString deviceTypeName = convertDeviceTomlClassName(deviceType);
    const QList<QMap<QString, QString>> &tomlMapLst = cmdInfo(deviceTypeName);
    for (int j = 0; j < tomlMapLst.size(); j++) { // 加载从toml中获取的信息
        bool fixSameOne = false;                  //初始值设为该项信息没有用过
        bool isSameOne = false;

        QString tomltomlmatchkey = tomlMapLst[j]["tomlmatchkey"];
        QString tomltomlconfigdemanding = PhysID(tomlMapLst[j], "tomlconfigdemanding");
        QList<DeviceBaseInfo *> lst = convertDeviceList(deviceType);
        for (int i = 0; i < lst.size(); i++) {   // toml中获取的信息 与 原设备信息遍历，相比较作处理
            DeviceBaseInfo *device = lst[i];
            QString modalias = PhysID(tomlMapLst[j], "Modalias");
            QString vid = PhysID(tomlMapLst[j], "Vendor_ID");
            QString pid = PhysID(tomlMapLst[j], "Product_ID");
            QString vendor = PhysID(tomlMapLst[j], "Vendor");
            QString name = PhysID(tomlMapLst[j], "Name");
            bool isSameOne = false;                   //初始值设为该项信息不属于该设备


            isSameOne = tomlSetBytomlmatchkey(deviceType, device, tomltomlmatchkey,tomltomlconfigdemanding);

            if (tomltomlconfigdemanding == "adjust") {
                fixSameOne = true;   //标记为该项信息有用过 ，
                if (isSameOne)
                    tomlDeviceMapSet(deviceType, device, tomlMapLst[j]);
            } else if (tomltomlconfigdemanding == "delete") {
                fixSameOne = true;   //标记为该项信息有用过 ，
                if (isSameOne) {
                    tomlDeviceDel(deviceType, device); //toml 去掉该设备
                    delete (device);
                }
            } else if (tomltomlconfigdemanding == "add" && !fixSameOne) {

            } else {
                //取出toml中获取的关键字信息设备唯一标识硬件IDS "Modalias"， "Vendor_ID"， "Vendor"，"Name"；作比较处理
                if (!isSameOne)
                    isSameOne = findByModalias(deviceType, device, modalias);
                if (!isSameOne) {
                    isSameOne = findByVIDPID(deviceType, device, vid, pid);
                }
                if (!isSameOne) {
                    isSameOne = findByVendorName(deviceType, device, vendor, name);
                }
                if (isSameOne&& !fixSameOne) {   //存在 就合并信息
                    fixSameOne = true;   //标记为该项信息有用过 ，
                    if (TOML_Del == tomlDeviceMapSet(deviceType, device, tomlMapLst[j])) {
                        tomlDeviceDel(deviceType, device); //toml 去掉该设备
                        delete (device);
                    }
                }
            }
        }  //与原设备信息遍历相比完再作添加设备
        if (tomltomlconfigdemanding == "add" && !fixSameOne) {
            fixSameOne = true;   //标记为该项信息有用过
            DeviceBaseInfo *newDevice = createDevice(deviceType);
            tomlDeviceMapSet(deviceType, newDevice, tomlMapLst[j]);
            tomlDeviceAdd(deviceType, newDevice); //加
        }
    } //end of for (int j = 0;...
}
QString DeviceManager::PhysID(const QMap<QString, QString> &mapInfo, const QString &key)
{
    qCDebug(appLog) << "Getting PhysID for key:" << key;
    if (mapInfo.contains(key)) {  //toml key
        QString value = mapInfo[key].trimmed();
        if (!value.isEmpty()) {
            value = value.toLower().remove("_nouse"); //"_nouse" 为toml方案中约定的去除该信息项的关键字
            qCDebug(appLog) << "PhysID for key:" << key << "is:" << value;
            return value;
        }
    }
    qCDebug(appLog) << "PhysID for key:" << key << "is empty";
    return "";
}

void DeviceManager::tomlDeviceDel(DeviceType deviceType, DeviceBaseInfo *const device)
{
    qCDebug(appLog) << "Deleting TOML device for deviceType:" << deviceType;
    if (!device) {
        qCDebug(appLog) << "Device is null";
        return;
    }
    QList<DeviceBaseInfo *> *lst = convertDeviceListAddr(deviceType);
    lst->removeOne(device);
}

void DeviceManager::tomlDeviceAdd(DeviceType deviceType, DeviceBaseInfo *const device)
{
    qCDebug(appLog) << "Adding TOML device for deviceType:" << deviceType;
    if (!device) {
        qCDebug(appLog) << "Device is null";
        return;
    }
    QList<DeviceBaseInfo *> *lst = convertDeviceListAddr(deviceType);
    lst->append(device);
}

bool DeviceManager::findByModalias(DeviceType deviceType, DeviceBaseInfo *device, const QString &modalias)
{
    qCDebug(appLog) << "Finding by Modalias for deviceType:" << deviceType;

    if (modalias.isEmpty()) {
        qCDebug(appLog) << "Modalias is empty";
        return false;
    }

    {
        if (!device) {
            qCDebug(appLog) << "Device is null";
            return false;
        }

        QString tmp_Modalias =  device->getModalias();
        if ((0 == modalias.compare(tmp_Modalias, Qt::CaseInsensitive))) { //modalias相同即认为是同一设备
            qCDebug(appLog) << "Modalias is the same";
            return true;
        }

        QString tmp_VIDAndPID =  device->getVIDAndPID().toLower();
        //如果是非系统产生的标准modalias， 而是自己构建的，则判断 VIDAndPID与之相同即认为是同一设备
        if (!modalias.startsWith("pci") && !modalias.startsWith("usb") && !tmp_VIDAndPID.isEmpty()) {
            QString tmp_vidpid = tmp_VIDAndPID.remove("0x");
            QString tmp_vid  = tmp_vidpid.mid(0, 4);
            QString tmp_pid  = tmp_vidpid.mid(4, 4);

            if ((modalias.contains(tmp_vid, Qt::CaseInsensitive)) && (modalias.contains(tmp_pid, Qt::CaseInsensitive))) {
                qCDebug(appLog) << "VIDAndPID is the same";
                return true;
            }
        }
    }
    qCDebug(appLog) << "Modalias is not the same";
    return false;
}

bool DeviceManager::findByVIDPID(DeviceType deviceType, DeviceBaseInfo *device, const QString &vid, const QString &pid)
{
    qCDebug(appLog) << "Finding by VIDPID for deviceType:" << deviceType;
    if (vid.isEmpty() ||  pid.isEmpty()) {
        qCDebug(appLog) << "VID or PID is empty";
        return false;
    }
    {
        if (!device) {
            qCDebug(appLog) << "Device is null";
            return false;
        }

        QString tmp_refvid = vid.toLower();
        if (tmp_refvid.contains("0x")) {
            qCDebug(appLog) << "VID contains 0x";
            tmp_refvid.remove("0x");
        }
        QString tmp_refpid = pid.toLower();
        if (tmp_refpid.contains("0x")) {
            qCDebug(appLog) << "PID contains 0x";
            tmp_refpid.remove("0x");
        }

        QString tmp_vid =  device->getVID().toLower();
        QString tmp_pid =  device->getPID().toLower();
        if (tmp_vid.contains("0x")) {
            qCDebug(appLog) << "VID contains 0x";
            tmp_vid.remove("0x");
        }
        if (tmp_pid.contains("0x")) {
            qCDebug(appLog) << "PID contains 0x";
            tmp_pid.remove("0x");
        }

        if (tmp_vid.isEmpty() || tmp_pid.isEmpty()) {
            QString tmp_VIDAndPID =  device->getVIDAndPID().toLower();  ////VID和PID相同即认为是同一设备
            if (tmp_VIDAndPID.contains(tmp_refvid) && tmp_VIDAndPID.contains(tmp_refpid)) {
                qCDebug(appLog) << "VIDAndPID is the same";
                return true;
            }
        } else {
            if ((0 == tmp_refvid.compare(tmp_vid, Qt::CaseInsensitive)) && (0 == tmp_refpid.compare(tmp_pid, Qt::CaseInsensitive))) {
                qCDebug(appLog) << "VID and PID are the same";
                return true;
            }
        }
    }
    qCDebug(appLog) << "VID and PID are not the same";
    return false;
}

bool DeviceManager::findByVendorName(DeviceType deviceType, DeviceBaseInfo *device, const QString &vendor, const QString &name)
{
    qCDebug(appLog) << "Finding by VendorName for deviceType:" << deviceType;
    if (name.isEmpty()) {
        qCDebug(appLog) << "Name is empty";
        return false;
    }
    if (vendor.isEmpty()) {
        qCDebug(appLog) << "Vendor is empty";
        if ((deviceType != DT_Bios) && (deviceType != DT_Computer))
            return false;
    }
    if (!device)
        return false;
    QString tmp_vendor;
    QString tmp_name;
    if (deviceType == DT_Bios) {     //因BIOS有5个被定名的信息，故作特殊处理
        DeviceBios *devicebios = dynamic_cast<DeviceBios *>(device);
        tmp_name    =  devicebios->tomlname();
        if (0 == name.compare(tmp_name, Qt::CaseInsensitive))
            return true;
    } else {
        //vendor 和name相同即认为是同一设备
        tmp_vendor  =  device->vendor();
        tmp_name    =  device->name();
        if ((0 == vendor.compare(tmp_vendor, Qt::CaseInsensitive)) && (0 == name.compare(tmp_name, Qt::CaseInsensitive)))
            return true;


        if (deviceType == DT_Computer) {  // //因Computer信息只有一条list，且不允增加list，只能替换信息，故作特殊处理
            qCInfo(appLog) << " computer Name" <<  tmp_name << "toml:" << name;
            return true;
        }
    }
    qCDebug(appLog) << "VendorName is not the same";
    return false;
}

DeviceBaseInfo *DeviceManager::getBluetoothAtIndex(int index)
{
    qCDebug(appLog) << "Getting Bluetooth device at index:" << index;
    if (m_ListDeviceBluetooth.size() <= index) {
        qCDebug(appLog) << "Index is out of range";
        return nullptr;
    }
    return m_ListDeviceBluetooth[index];
}

void DeviceManager::addMouseDevice(DeviceInput *const device)
{
    // qCDebug(appLog) << "Adding mouse device";
    // 如果不是重复设备则添加到设备列表
    m_ListDeviceMouse.append(device);
}

DeviceBaseInfo *DeviceManager::getMouseDevice(const QString &unique_id)
{
    qCDebug(appLog) << "Getting mouse device with unique ID:" << unique_id;
    if (unique_id.isEmpty()) {
        qCDebug(appLog) << "Unique ID is empty";
        return nullptr;
    }
    for (QList<DeviceBaseInfo *>::iterator it = m_ListDeviceMouse.begin(); it != m_ListDeviceMouse.end(); ++it) {
        DeviceInput *mouse = dynamic_cast<DeviceInput *>(*it);
        if (mouse && mouse->uniqueID() == unique_id) {
            return *it;
        }
    }
    qCDebug(appLog) << "Mouse device with unique ID:" << unique_id << "not found";
    return nullptr;
}

bool DeviceManager::addMouseInfoFromLshw(const QMap<QString, QString> &mapInfo)
{
    qCDebug(appLog) << "Adding mouse info from lshw";
    // 从lshw中添加鼠标信息
    QList<DeviceBaseInfo *>::iterator it = m_ListDeviceMouse.begin();
    for (; it != m_ListDeviceMouse.end(); ++it) {
        DeviceInput *device = dynamic_cast<DeviceInput *>(*it);
        if (!device)
            continue;

        if (device->setInfoFromlshw(mapInfo)) {
            qCDebug(appLog) << "Mouse info added from lshw";
            return true;
        }
    }
    qCDebug(appLog) << "Mouse info not added from lshw";
    return false;
}

void DeviceManager::addCpuDevice(DeviceCpu *const device)
{
    // qCDebug(appLog) << "Adding CPU device";
    // 添加CPU设备
    m_ListDeviceCPU.append(device);
}

void DeviceManager::addStorageDeivce(DeviceStorage *const device)
{
    // qCDebug(appLog) << "Adding storage device";
    m_ListDeviceStorage.append(device);
}

void DeviceManager::addLshwinfoIntoStorageDevice(const QMap<QString, QString> &mapInfo)
{
    qCDebug(appLog) << "Adding lshw info into storage device";
    // 从lshw中添加存储设备信息
    QList<DeviceBaseInfo *>::iterator it = m_ListDeviceStorage.begin();
    for (; it != m_ListDeviceStorage.end(); ++it) {
        DeviceStorage *device = dynamic_cast<DeviceStorage *>(*it);
        if (!device)
            continue;

        if (device->addInfoFromlshw(mapInfo))
            return;
    }
}

void DeviceManager::addLshwinfoIntoNVMEStorageDevice(const QMap<QString, QString> &mapInfo)
{
    qCDebug(appLog) << "Adding NVME info into storage device";
    // 从lshw中添加NVME存储设备信息
    QList<DeviceBaseInfo *>::iterator it = m_ListDeviceStorage.begin();
    for (; it != m_ListDeviceStorage.end(); ++it) {
        DeviceStorage *device = dynamic_cast<DeviceStorage *>(*it);
        if (!device)
            continue;

        if (device->addNVMEInfoFromlshw(mapInfo))
            return;
    }
}

void DeviceManager::setStorageInfoFromSmartctl(const QString &name, const QMap<QString, QString> &mapInfo)
{
    qCDebug(appLog) << "Adding smartctl info into storage device";
    // // 从smartctl中添加存储设备信息
    QList<DeviceBaseInfo *>::iterator it = m_ListDeviceStorage.begin();
    for (; it != m_ListDeviceStorage.end(); ++it) {
        DeviceStorage *device = dynamic_cast<DeviceStorage *>(*it);
        if (!device)
            continue;

        if (device->addInfoFromSmartctl(name, mapInfo))
            return;
    }
}

void DeviceManager::mergeDisk()
{
    qCDebug(appLog) << "Merging disk";
    QList<int> m_ListStorageIndex;
    QMap<QString, QList<int> > allSerialIDs;
    for (int i = 0; i < m_ListDeviceStorage.size(); ++i) {
        DeviceStorage *device = dynamic_cast<DeviceStorage *>(m_ListDeviceStorage[i]);
        if (!device->getDiskSerialID().isEmpty()) {
            allSerialIDs[device->getDiskSerialID()].append(i);
        }
    }
    for (auto serialIDs : allSerialIDs) {
        if (serialIDs.size() < 2)
            continue;
        DeviceStorage *fDevice = dynamic_cast<DeviceStorage *>(m_ListDeviceStorage[serialIDs[0] ]);
        for (int i = serialIDs.size() - 1; i > 0; --i) {
            DeviceStorage *curDevice = dynamic_cast<DeviceStorage *>(m_ListDeviceStorage[serialIDs[i] ]);
            fDevice->appendDisk(curDevice);
            m_ListStorageIndex.append(serialIDs[i]);
        }
    }

    std::sort(m_ListStorageIndex.begin(), m_ListStorageIndex.end(), std::greater<int>());
    for(auto index : m_ListStorageIndex) {
        DeviceStorage *curDevice = dynamic_cast<DeviceStorage *>(m_ListDeviceStorage[index]);
        m_ListDeviceStorage.removeAt(index);
        delete curDevice;
    }

    for (int i = 0; i < m_ListDeviceStorage.size(); ++i) {
        DeviceStorage *device = dynamic_cast<DeviceStorage *>(m_ListDeviceStorage[i]);
        device->unitConvertByDecimal();
    }
}

void DeviceManager::checkDiskSize()
{
    qCDebug(appLog) << "Checking disk size";
    for (int i = 0; i < m_ListDeviceStorage.size(); ++i) {
        DeviceStorage *device = dynamic_cast<DeviceStorage *>(m_ListDeviceStorage[i]);
        device->checkDiskSize();
    }
}

void DeviceManager::orderDiskByType()
{
    // 自定义排序
    auto compareDevices = [](DeviceBaseInfo* baseInfo1, DeviceBaseInfo* baseInfo2) {
        DeviceStorage *storageA = dynamic_cast<DeviceStorage *>(baseInfo1);
        DeviceStorage *storageB = dynamic_cast<DeviceStorage *>(baseInfo2);

        if (storageA == nullptr || storageB == nullptr) {
            return false;
        }

        // 判断是否为 USB 设备（包括 USB 接口的 SSD）
        auto isUSBDevice = [](DeviceStorage *device) {
            return device->interface().contains("USB") ||
                   (device->interface().contains("USB") && device->mediaType().contains("SSD"));
        };

        // 1. 首先按照 UFS 优先
        if (storageA->interface().contains("UFS") && !storageB->interface().contains("UFS"))
            return true;
        if (!storageA->interface().contains("UFS") && storageB->interface().contains("UFS"))
            return false;

        // 2. 然后按照 SSD、HDD 排序（排除 USB 接口的 SSD）
        if (!isUSBDevice(storageA) && !isUSBDevice(storageB)) {
            if (storageA->mediaType().contains("SSD") && !storageB->mediaType().contains("SSD"))
                return true;
            if (!storageA->mediaType().contains("SSD") && storageB->mediaType().contains("SSD"))
                return false;
            if (storageA->mediaType().contains("HDD") && !storageB->mediaType().contains("HDD"))
                return true;
            if (!storageA->mediaType().contains("HDD") && storageB->mediaType().contains("HDD"))
                return false;
        }

        // 3. 最后按照 USB 设备排序（包括 USB 接口的 SSD）
        if (isUSBDevice(storageA) && !isUSBDevice(storageB))
            return false;  // USB 设备排在后面
        if (!isUSBDevice(storageA) && isUSBDevice(storageB))
            return true;

        // 如果所有条件都相同，保持原有顺序
        return false;
    };

    // 使用自定义比较函数对列表进行排序
    std::sort(m_ListDeviceStorage.begin(), m_ListDeviceStorage.end(), compareDevices);
}

bool DeviceManager::setStorageDeviceMediaType(const QString &name, const QString &value)
{
    qCDebug(appLog) << "Setting storage device media type";
    // 设置存储设备介质类型
    QList<DeviceBaseInfo *>::iterator it = m_ListDeviceStorage.begin();
    for (; it != m_ListDeviceStorage.end(); ++it) {
        DeviceStorage *device = dynamic_cast<DeviceStorage *>(*it);
        if (!device)
            continue;

        // 设置介质类型
        if (device->setMediaType(name, value))
            return true;
    }

    return  false;
}

void DeviceManager::addGpuDevice(DeviceGpu *const device)
{
    // qCDebug(appLog) << "Adding GPU device";
    // 添加显示适配器
    m_ListDeviceGPU.append(device);
}

void DeviceManager::setGpuInfoFromLshw(const QMap<QString, QString> &mapInfo)
{
    qCDebug(appLog) << "Setting GPU info from lshw";
    // 从lshw中添加显示适配器信息
    QList<DeviceBaseInfo *>::iterator it = m_ListDeviceGPU.begin();
    for (; it != m_ListDeviceGPU.end(); ++it) {
        DeviceGpu *device = dynamic_cast<DeviceGpu *>(*it);
        if (!device)
            continue;

        device->setLshwInfo(mapInfo);
    }
}

void DeviceManager::setGpuInfoFromXrandr(const QMap<QString, QString> &mapInfo)
{
    qCDebug(appLog) << "Setting GPU info from xrandr";
    // 从xrandr中添加显示适配器信息
    QList<DeviceBaseInfo *>::iterator it = m_ListDeviceGPU.begin();
    for (; it != m_ListDeviceGPU.end(); ++it) {
        DeviceGpu *device = dynamic_cast<DeviceGpu *>(*it);
        if (!device)
            continue;

        device->setXrandrInfo(mapInfo);
    }
}

void DeviceManager::setGpuSizeFromDmesg(const QMap<QString, QString> &mapInfo)
{
    qCDebug(appLog) << "Setting GPU size from dmesg";
    // 从dmesg中设置显卡大小
    QList<DeviceBaseInfo *>::iterator it = m_ListDeviceGPU.begin();
    for (; it != m_ListDeviceGPU.end(); ++it) {
        DeviceGpu *device = dynamic_cast<DeviceGpu *>(*it);
        if (!device)
            continue;

        device->setDmesgInfo(mapInfo);
    }
}

void DeviceManager::addMemoryDevice(DeviceMemory *const device)
{
    // qCDebug(appLog) << "Adding memory device";
    // 添加内存
    m_ListDeviceMemory.append(device);
}

void DeviceManager::setMemoryInfoFromDmidecode(const QMap<QString, QString> &mapInfo)
{
    qCDebug(appLog) << "Setting memory info from dmidecode";
    // 从dmidecode中添加内存信息
    QList<DeviceBaseInfo *>::iterator it = m_ListDeviceMemory.begin();
    for (; it != m_ListDeviceMemory.end(); ++it) {
        DeviceMemory *device = dynamic_cast<DeviceMemory *>(*it);
        if (!device)
            continue;

        if (device->setInfoFromDmidecode(mapInfo))
            return;
    }
}

void DeviceManager::addMonitor(DeviceMonitor *const device)
{
    // qCDebug(appLog) << "Adding monitor device";
    // 添加显示设备
    m_ListDeviceMonitor.append(device);
}

void DeviceManager::setMonitorInfoFromXrandr(const QString &main, const QString &edid, const QString &rate, const QString &xrandr)
{
    qCDebug(appLog) << "Setting monitor info from xrandr";
    // 从xrandr中添加显示设备信息
    QList<DeviceBaseInfo *>::iterator it = m_ListDeviceMonitor.begin();
    for (; it != m_ListDeviceMonitor.end(); ++it) {
        DeviceMonitor *device = dynamic_cast<DeviceMonitor *>(*it);
        if (!device)
            continue;

        if (device->setInfoFromXradr(main, edid, rate, xrandr))
            return;
    }
}

void DeviceManager::setMonitorInfoFromDbus(const QMap<QString, QString> &mapInfo)
{
    qCDebug(appLog) << "Setting monitor info from dbus";
    // 从 dbus 中添加显示设备信息
    QList<DeviceBaseInfo *>::iterator it = m_ListDeviceMonitor.begin();
    for (; it != m_ListDeviceMonitor.end(); ++it) {
        DeviceMonitor *device = dynamic_cast<DeviceMonitor *>(*it);
        if (!device)
            continue;

        device->setInfoFromDbus(mapInfo);

    }
}

void DeviceManager::addBiosDevice(DeviceBios *const device)
{
    // qCDebug(appLog) << "Adding bios device";
    // 添加主板信息
    m_ListDeviceBios.append(device);
}

void DeviceManager::setLanguageInfo(const QMap<QString, QString> &mapInfo)
{
    qCDebug(appLog) << "Setting language info";
    // 设置语言信息
    QList<DeviceBaseInfo *>::iterator it = m_ListDeviceBios.begin();
    for (; it != m_ListDeviceBios.end(); ++it) {
        DeviceBios *device = dynamic_cast<DeviceBios *>(*it);
        if (!device)
            continue;

        if (device->setBiosLanguageInfo(mapInfo))
            return;
    }
}

void DeviceManager::addBluetoothDevice(DeviceBluetooth *const device)
{
    // qCDebug(appLog) << "Adding bluetooth device";
    m_ListDeviceBluetooth.append(device);
}

void DeviceManager::setBluetoothInfoFromLshw(const QMap<QString, QString> &mapInfo)
{
    qCDebug(appLog) << "Setting bluetooth info from lshw";
    // 从lshw中获取蓝牙信息
    QList<DeviceBaseInfo *>::iterator it = m_ListDeviceBluetooth.begin();
    for (; it != m_ListDeviceBluetooth.end(); ++it) {
        DeviceBluetooth *device = dynamic_cast<DeviceBluetooth *>(*it);
        if (!device)
            continue;

        if (device->setInfoFromLshw(mapInfo))
            return;
    }
}

bool DeviceManager::setBluetoothInfoFromHwinfo(const QMap<QString, QString> &mapInfo)
{
    qCDebug(appLog) << "Setting bluetooth info from hwinfo";
    // 从hwinfo中获取蓝牙信息
    QList<DeviceBaseInfo *>::iterator it = m_ListDeviceBluetooth.begin();
    for (; it != m_ListDeviceBluetooth.end(); ++it) {
        DeviceBluetooth *device = dynamic_cast<DeviceBluetooth *>(*it);
        if (!device)
            continue;

        if (device->setInfoFromHwinfo(mapInfo))
            return true;
    }
    return false;
}

DeviceBaseInfo *DeviceManager::getBluetoothDevice(const QString &unique_id)
{
    qCDebug(appLog) << "Getting bluetooth device with unique ID:" << unique_id;
    for (QList<DeviceBaseInfo *>::iterator it = m_ListDeviceBluetooth.begin(); it != m_ListDeviceBluetooth.end(); ++it) {
        DeviceBluetooth *bt = dynamic_cast<DeviceBluetooth *>(*it);
        if (bt && bt->uniqueID() == unique_id) {
            return *it;
        }
    }
    return nullptr;
}

void DeviceManager::addAudioDevice(DeviceAudio *const device)
{
    // qCDebug(appLog) << "Adding audio device";
    m_ListDeviceAudio.append(device);
}

void DeviceManager::delAudioDevice(DeviceAudio *const device)
{
    // qCDebug(appLog) << "Deleting audio device";
    m_ListDeviceAudio.removeOne(device);
}

void DeviceManager::deleteDisableDuplicate_AudioDevice(void)
{
    qCDebug(appLog) << "Deleting disable duplicate audio device";
    QHash<QString, DeviceAudio*> enabledDevices;
    QHash<QString, int> enableDevicePriority;
    for (auto it = m_ListDeviceAudio.begin(); it != m_ListDeviceAudio.end(); ++it) {
        DeviceAudio* audio = dynamic_cast<DeviceAudio*>(*it);
        if (!audio) {
            continue;
        }
        if (audio->enable() || !enabledDevices.contains(audio->uniqueID())) {
            enabledDevices[audio->uniqueID()] = audio;
            if (!enableDevicePriority.contains(audio->uniqueID())) {
                if (audio->enable() || ! audio->driver().contains("usb")) {
                    enableDevicePriority.insert(audio->uniqueID(), 2);
                } else {
                    enableDevicePriority.insert(audio->uniqueID(), 1);
                }
            } else {
                enableDevicePriority[audio->uniqueID()] = enableDevicePriority[audio->uniqueID()] + 1;
            }
        } else {
            if (enabledDevices.contains(audio->uniqueID()) && enabledDevices[audio->uniqueID()]->enable()) {
                if (!enableDevicePriority.contains(audio->uniqueID())) {
                    if (audio->enable()) {
                        enableDevicePriority.insert(audio->uniqueID(), 2);
                    } else {
                        enableDevicePriority.insert(audio->uniqueID(), 1);
                    }
                } else {
                    enableDevicePriority[audio->uniqueID()] = enableDevicePriority[audio->uniqueID()] + 1;
                }
                audio->setSysPath(enabledDevices[audio->uniqueID()]->sysPath());
                delete enabledDevices[audio->uniqueID()];
                enabledDevices[audio->uniqueID()] = audio;
            }
        }
    }
    qWarning()<<"delete before: "<< m_ListDeviceAudio.size();
    qWarning() << enableDevicePriority;
    m_ListDeviceAudio.clear();
    for(auto it : enabledDevices.keys()){
        if (enableDevicePriority.value(it) <= 1) {
            continue;
        }
        m_ListDeviceAudio.push_back(enabledDevices.value(it));
        qDebug() << enabledDevices.value(it)->name() << "is enable: " << enabledDevices.value(it)->enable();
        if (enabledDevices.value(it)->enable()){
            continue;
        }
        setAudioDeviceEnable(enabledDevices.value(it), enabledDevices.value(it)->enable());
    }
    qWarning()<<"delete after: "<< m_ListDeviceAudio.size();
}

void DeviceManager::setAudioDeviceEnable(DeviceAudio * const devivce, bool enable)
{
    EnableDeviceStatus status ;

    int i = 0;
    do{
        status = devivce->setEnable(enable);
        if (status == EDS_Success) {
            break;
        }
        qWarning() << "retry: " << ++i;
    }while (i < 3);

    qWarning() << [](EnableDeviceStatus status) {
        switch(status) {
        case EDS_Cancle: return "Device enable operation was cancelled";
        case EDS_NoSerial: return "Device enable failed: No serial number available";
        case EDS_Faild: return "-----Device enable failed due to unknown error";
        case EDS_Success: return "Device enabled successfully";
        default: return "Unknown device enable status";
        }
    }(status);
}

DeviceBaseInfo *DeviceManager::getAudioDevice(const QString &path)
{
    qCDebug(appLog) << "Getting audio device with path:" << path;
    for (QList<DeviceBaseInfo *>::iterator it = m_ListDeviceAudio.begin(); it != m_ListDeviceAudio.end(); ++it) {
        DeviceAudio *audio = dynamic_cast<DeviceAudio *>(*it);
        QString tpath = audio->uniqueID();
        // 判断该设备是否已经存在，1.1:1.1 -> 1.1:1.0
        if (audio && path == tpath.replace(QRegularExpression("[1-9]$"), "0")) {
            return *it;
        }
        QString syspath = audio->sysPath();   //remove dumplicate syspath
        if (audio && path == syspath) {
            return *it;
        }
        //remove dumplicate by toml Modalias
        QString Modalias =  audio->getModalias();
        if (audio && path == Modalias) {
            return *it;
        }
        QString vidpid =  audio->getVIDAndPID();
        if (audio && path == vidpid) {
            return *it;
        }
    }
    qCDebug(appLog) << "Audio device with path:" << path << "not found";
    return nullptr;
}

void DeviceManager::setAudioInfoFromLshw(const QMap<QString, QString> &mapInfo)
{
    qCDebug(appLog) << "Setting audio info from lshw";
    // 从lshw中获取音频适配器信息
    QList<DeviceBaseInfo *>::iterator it = m_ListDeviceAudio.begin();
    for (; it != m_ListDeviceAudio.end(); ++it) {
        DeviceAudio *device = dynamic_cast<DeviceAudio *>(*it);
        if (!device)
            continue;

        if (device->setInfoFromLshw(mapInfo))
            return;
    }
}

void DeviceManager::setAudioChipFromDmesg(const QString &info)
{
    qCDebug(appLog) << "Setting audio chip from dmesg";
    // 从dmesg中获取声卡芯片型号
    QList<DeviceBaseInfo *>::iterator it = m_ListDeviceAudio.begin();
    for (; it != m_ListDeviceAudio.end(); ++it) {
        DeviceAudio *device = dynamic_cast<DeviceAudio *>(*it);
        if (!device)
            continue;

        device->setAudioChipFromDmesg(info);
    }
}

void DeviceManager::addNetworkDevice(DeviceNetwork *const device)
{
    // qCDebug(appLog) << "Adding network device";
    // 添加网络适配器
    m_ListDeviceNetwork.append(device);
}

DeviceBaseInfo *DeviceManager::getNetworkDevice(const QString &unique_id)
{
    qCDebug(appLog) << "Getting network device with unique ID:" << unique_id;
    for (QList<DeviceBaseInfo *>::iterator it = m_ListDeviceNetwork.begin(); it != m_ListDeviceNetwork.end(); ++it) {
        DeviceNetwork *net = dynamic_cast<DeviceNetwork *>(*it);
        if (!unique_id.isEmpty() && net && net->uniqueID() == unique_id) {
            return *it;
        }
    }
    qCDebug(appLog) << "Network device with unique ID:" << unique_id << "not found";
    return nullptr;
}

void DeviceManager::correctNetworkLinkStatus(const QString &linkStatus, const QString &networkDriver)
{
    qCDebug(appLog) << "Correcting network link status";
    if (m_ListDeviceNetwork.size() == 0) {
        qCDebug(appLog) << "Network device list is empty";
        return;
    }
    QList<DeviceBaseInfo *>::iterator it = m_ListDeviceNetwork.begin();
    for (; it != m_ListDeviceNetwork.end(); ++it) {
        DeviceNetwork *device = dynamic_cast<DeviceNetwork *>(*it);
        if (!device)
            continue;
        if (networkDriver == device->logicalName())
            device->correctCurrentLinkStatus(linkStatus);
    }
}

QStringList DeviceManager::networkDriver()
{
    qCDebug(appLog) << "Getting network driver";
    m_networkDriver.clear();
    QList<DeviceBaseInfo *>::iterator it = m_ListDeviceNetwork.begin();
    for (; it != m_ListDeviceNetwork.end(); ++it) {
        DeviceNetwork *device = dynamic_cast<DeviceNetwork *>(*it);
        if (!device)
            continue;
        //保存各个网卡的逻辑名称，用于判断具体网卡
        m_networkDriver.append(device->logicalName());
    }
    qCDebug(appLog) << "Network driver:" << m_networkDriver;
    return m_networkDriver;
}

void DeviceManager::correctPowerInfo(const QMap<QString, QMap<QString, QString>> &mapInfo)
{
    qCDebug(appLog) << "Correcting power info";
    if (m_ListDevicePower.size() == 0) {
        qCDebug(appLog) << "Power device list is empty";
        return;
    }
    QList<DeviceBaseInfo *>::iterator it = m_ListDevicePower.begin();
    for (; it != m_ListDevicePower.end(); ++it) {
        DevicePower *device = dynamic_cast<DevicePower *>(*it);
        if (!device)
            continue;

        //根据获取到的数据，重新设置电池信息
        device->setInfoFromUpower(mapInfo["upower"]);
        device->setDaemonInfo(mapInfo["Daemon"]);
    }
}

void DeviceManager::addImageDevice(DeviceImage *const device)
{
    // qCDebug(appLog) << "Adding image device";
    // 添加图像设备
    m_ListDeviceImage.append(device);
}

DeviceBaseInfo *DeviceManager::getImageDevice(const QString &unique_id)
{
    qCDebug(appLog) << "Getting image device with unique ID:" << unique_id;
    for (QList<DeviceBaseInfo *>::iterator it = m_ListDeviceImage.begin(); it != m_ListDeviceImage.end(); ++it) {
        DeviceImage *image = dynamic_cast<DeviceImage *>(*it);
        if (image && image->uniqueID() == unique_id) {
            return *it;
        }
    }
    qCDebug(appLog) << "Image device with unique ID:" << unique_id << "not found";
    return nullptr;
}

void DeviceManager::setCameraInfoFromLshw(const QMap<QString, QString> &mapInfo)
{
    qCDebug(appLog) << "Setting camera info from lshw";
    // 从lshw获取图像设备信息
    QList<DeviceBaseInfo *>::iterator it = m_ListDeviceImage.begin();
    for (; it != m_ListDeviceImage.end(); ++it) {
        DeviceImage *device = dynamic_cast<DeviceImage *>(*it);
        if (!device)
            continue;

        device->setInfoFromLshw(mapInfo);
    }
}

void DeviceManager::addKeyboardDevice(DeviceInput *const device)
{
    // qCDebug(appLog) << "Adding keyboard device";

    QString vid = device->getVID();
    QString pid = device->getPID();
    QString interface = device->getInterface();

    // 仅USB键盘具备有效的VID/PID,需要通过lsusb校验以过滤无效/幽灵USB设备。
    // PS/2、I2C、蓝牙等内置键盘没有VID/PID(hwinfo中为 "Vendor: 0xNNNN" 单段格式,
    // 无法解析出VID/PID),应直接添加,避免被误删导致键盘无法显示。
    if (!interface.contains("USB", Qt::CaseInsensitive)) {
        m_ListDeviceKeyboard.append(device);
        qCDebug(appLog) << "Non-USB keyboard added directly, interface:" << interface;
        return;
    }

    if (vid.isEmpty() || pid.isEmpty()) {
        qCDebug(appLog) << "USB keyboard VID or PID is empty, device not added";
        device->deleteLater();
        return;
    }

    // Use DeviceManager's validation function
    QString normalizedVid, normalizedPid;
    if (!validateKeyboardVidPid(vid, pid, normalizedVid, normalizedPid)) {
        qCWarning(appLog) << "VID or PID validation failed. VID:" << vid << "PID:" << pid;
        device->deleteLater();
        return;
    }

    QProcess process;
    process.start("lsusb");
    process.waitForFinished(3000);
    
    if (process.exitCode() != 0) {
        qCWarning(appLog) << "Failed to execute lsusb command, adding keyboard device without verification";
        device->deleteLater();
        return;
    }
    
    QString lsusbOutput = process.readAllStandardOutput();

    // lsusb output: Bus xxx Device xxx: ID xxxx:xxxx Description
    QRegularExpression regex(QString("ID\\s+([0-9a-fA-F]{4}):([0-9a-fA-F]{4})"));
    QRegularExpressionMatchIterator iterator = regex.globalMatch(lsusbOutput);
    
    bool deviceExists = false;
    while (iterator.hasNext()) {
        QRegularExpressionMatch match = iterator.next();
        QString lsusbVid = match.captured(1).toLower();
        QString lsusbPid = match.captured(2).toLower();
        
        if (lsusbVid == normalizedVid && lsusbPid == normalizedPid) {
            deviceExists = true;
            qCDebug(appLog) << "Found matching device in lsusb output:" << lsusbVid << ":" << lsusbPid;
            break;
        }
    }
    
    if (deviceExists) {
        m_ListDeviceKeyboard.append(device);
        qCDebug(appLog) << "Keyboard device added successfully";
    } else {
        qCDebug(appLog) << "Keyboard device not found in lsusb output, device not added";
        device->deleteLater();
    }
}

void DeviceManager::setKeyboardInfoFromLshw(const QMap<QString, QString> &mapInfo)
{
    qCDebug(appLog) << "Setting keyboard info from lshw";
    // 从lshw获取键盘信息
    QList<DeviceBaseInfo *>::iterator it = m_ListDeviceKeyboard.begin();
    for (; it != m_ListDeviceKeyboard.end(); ++it) {
        DeviceInput *device = dynamic_cast<DeviceInput *>(*it);
        if (!device)
            continue;

        if (device->setInfoFromlshw(mapInfo))
            return;
    }
}

void DeviceManager::addOthersDevice(DeviceOthers *const device)
{
    qCDebug(appLog) << "Adding others device";
    // 添加其他设备
    bool isOtherDevice = true;
    foreach (auto disk, m_ListDeviceStorage) {
        DeviceStorage *deviceDisk = dynamic_cast<DeviceStorage *>(disk);
        if (!deviceDisk)
            continue;

        // 排除存储设备
        if (deviceDisk->keyFromStorage() == device->logicalName() && device->logicalName() != "") {
            isOtherDevice = false;
            break;
        }
    }

    // 添加其他设备
    if (isOtherDevice)
        m_ListDeviceOthers.append(device);
}

DeviceBaseInfo *DeviceManager::getOthersDevice(const QString &unique_id)
{
    qCDebug(appLog) << "Getting others device with unique ID:" << unique_id;
    if (unique_id.isEmpty()) {
        return nullptr;
    }
    for (QList<DeviceBaseInfo *>::iterator it = m_ListDeviceOthers.begin(); it != m_ListDeviceOthers.end(); ++it) {
        DeviceOthers *other = dynamic_cast<DeviceOthers *>(*it);
        if (other && other->uniqueID() == unique_id) {
            return *it;
        }
    }
    qCDebug(appLog) << "Others device with unique ID:" << unique_id << "not found";
    return nullptr;
}

void DeviceManager::addOthersDeviceFromHwinfo(DeviceOthers *const device)
{
    qCDebug(appLog) << "Adding others device from hwinfo";
    // 从hwinfo中获取其他设备信息
    foreach (auto cur, m_ListDeviceOthers) {
        DeviceOthers *deviceOthers = dynamic_cast<DeviceOthers *>(cur);
        if (!deviceOthers)
            continue;

        if (deviceOthers->busInfo() == device->busInfo() && deviceOthers->busInfo() != "")
            return;
    }
    qCDebug(appLog) << "Adding others device from hwinfo";
    m_ListDeviceOthers.append(device);
}

void DeviceManager::setOthersDeviceInfoFromLshw(const QMap<QString, QString> &mapInfo)
{
    qCDebug(appLog) << "Setting others device info from lshw";
    //从lshw中获取其他设备信息
    QList<DeviceBaseInfo *>::iterator it = m_ListDeviceOthers.begin();
    for (; it != m_ListDeviceOthers.end(); ++it) {
        DeviceOthers *device = dynamic_cast<DeviceOthers *>(*it);
        if (!device)
            continue;

        device->setInfoFromLshw(mapInfo);
    }
}

void DeviceManager::setCpuRefreshInfoFromlscpu(const QMap<QString, QString> &mapInfo)
{
    qCDebug(appLog) << "Setting CPU refresh info from lscpu";
    QList<DeviceBaseInfo *>::iterator it = m_ListDeviceCPU.begin();
    for (; it != m_ListDeviceCPU.end(); ++it) {
        DeviceCpu *device = dynamic_cast<DeviceCpu *>(*it);
        if (!device)
            continue;

        device->setCurFreq(mapInfo["CPU MHz"]);
    }
}

void DeviceManager::addPowerDevice(DevicePower *const device)
{
    // qCDebug(appLog) << "Adding power device";
    // 添加电池设备
    m_ListDevicePower.append(device);
}

void DeviceManager::addPrintDevice(DevicePrint *const device)
{
    // qCDebug(appLog) << "Adding print device";
    // 添加打印机信息
    m_ListDevicePrint.append(device);
}

void DeviceManager::addOtherPCIDevice(DeviceOtherPCI *const device)
{
    // qCDebug(appLog) << "Adding other PCI device";
    // 添加其他PCI设备
    m_ListDeviceOtherPCI.append(device);
}

void DeviceManager::addComputerDevice(DeviceComputer *const device)
{
    // qCDebug(appLog) << "Adding computer device";
    // 添加计算机设备
    m_ListDeviceComputer.append(device);
}

void DeviceManager::addCdromDevice(DeviceCdrom *const device)
{
    // qCDebug(appLog) << "Adding CDROM device";
    // 添加CDROM
    m_ListDeviceCdrom.append(device);
}

void DeviceManager::addLshwinfoIntoCdromDevice(const QMap<QString, QString> &mapInfo)
{
    qCDebug(appLog) << "Adding CDROM info from lshw";
    // 从lshw中添加CDROM信息
    QList<DeviceBaseInfo *>::iterator it = m_ListDeviceCdrom.begin();
    for (; it != m_ListDeviceCdrom.end(); ++it) {
        DeviceCdrom *device = dynamic_cast<DeviceCdrom *>(*it);
        if (!device)
            continue;

        device->setInfoFromLshw(mapInfo);
    }
}

void DeviceManager::addBusId(const QStringList &busId)
{
    // qCDebug(appLog) << "Adding bus ID";
    // 添加设备总线信息
    m_BusIdList.append(busId);
}

const QStringList &DeviceManager::getBusId()
{
    // qCDebug(appLog) << "Getting bus ID";
    return m_BusIdList;
}

void DeviceManager::addCmdInfo(const QMap<QString, QList<QMap<QString, QString> > > &cmdInfo)
{
    // qCDebug(appLog) << "Adding command info";
    QMutexLocker locker(&addCmdMutex);
    // 添加命令信息
    foreach (const QString &key, cmdInfo.keys()) {
        if (m_cmdInfo.find(key) == m_cmdInfo.end())
            m_cmdInfo.insert(key, cmdInfo[key]);
        else
            m_cmdInfo[key].append(cmdInfo[key]);
    }
}

const QList<QMap<QString, QString>> &DeviceManager::cmdInfo(const QString &key)
{
    // qCDebug(appLog) << "Getting command info";
    QMutexLocker locker(&addCmdMutex);
    return m_cmdInfo[key];
}

bool DeviceManager::exportToTxt(const QString &filePath)
{
    qCDebug(appLog) << "Exporting to txt file";
    // 导出设备信息到txt文件
    QFile txtFile(filePath);
    if (false == txtFile.open(QIODevice::WriteOnly))
        return false;

    QTextStream out(&txtFile);
    overviewToTxt(out);
    EXPORT_TO_TXT(out, m_ListDeviceCPU, QObject::tr("CPU"), QObject::tr("No CPU found"));
    EXPORT_TO_TXT(out, m_ListDeviceBios, QObject::tr("Motherboard"), QObject::tr("No motherboard found"));
    EXPORT_TO_TXT(out, m_ListDeviceMemory, QObject::tr("Memory"), QObject::tr("No memory found"));
    EXPORT_TO_TXT(out, m_ListDeviceStorage, QObject::tr("Storage"), QObject::tr("No disk found"));
    EXPORT_TO_TXT(out, m_ListDeviceGPU, QObject::tr("Display Adapter"), QObject::tr("No GPU found"));
    EXPORT_TO_TXT(out, m_ListDeviceMonitor, QObject::tr("Monitor"), QObject::tr("No monitor found"));
    EXPORT_TO_TXT(out, m_ListDeviceNetwork, QObject::tr("Network Adapter"), QObject::tr("No network adapter found"));
    EXPORT_TO_TXT(out, m_ListDeviceAudio, QObject::tr("Sound Adapter"), QObject::tr("No audio device found"));
    EXPORT_TO_TXT(out, m_ListDeviceBluetooth, QObject::tr("Bluetooth"), QObject::tr("No Bluetooth device found"));
    EXPORT_TO_TXT(out, m_ListDeviceOtherPCI, QObject::tr("Other PCI Devices"), QObject::tr("No other PCI devices found"));
    EXPORT_TO_TXT(out, m_ListDevicePower, QObject::tr("Power"), QObject::tr("No battery found"));
    EXPORT_TO_TXT(out, m_ListDeviceKeyboard, QObject::tr("Keyboard"), QObject::tr("No keyboard found"));
    EXPORT_TO_TXT(out, m_ListDeviceMouse, QObject::tr("Mouse"), QObject::tr("No mouse found"));
    EXPORT_TO_TXT(out, m_ListDevicePrint, QObject::tr("Printer"), QObject::tr("No printer found"));
    EXPORT_TO_TXT(out, m_ListDeviceImage, QObject::tr("Camera"), QObject::tr("No camera found"));
    EXPORT_TO_TXT(out, m_ListDeviceCdrom, QObject::tr("CD-ROM"), QObject::tr("No CD-ROM found"));
    EXPORT_TO_TXT(out, m_ListDeviceOthers, QObject::tr("Other Devices"), QObject::tr("No other devices found"));
    txtFile.close();

    return true;
}

bool DeviceManager::exportToXlsx(const QString &filePath)
{
    qCDebug(appLog) << "Exporting to xlsx file";
    // 导出设备信息到xlsx表格
    QXlsx::Document xlsx;
    QXlsx::Format boldFont;
    overviewToXlsx(xlsx, boldFont);
    EXPORT_TO_XLSX(xlsx, boldFont, m_ListDeviceCPU, QObject::tr("CPU"), QObject::tr("No CPU found"));
    EXPORT_TO_XLSX(xlsx, boldFont, m_ListDeviceBios, QObject::tr("Motherboard"), QObject::tr("No motherboard found"));
    EXPORT_TO_XLSX(xlsx, boldFont, m_ListDeviceMemory, QObject::tr("Memory"), QObject::tr("No memory found"));
    EXPORT_TO_XLSX(xlsx, boldFont, m_ListDeviceStorage, QObject::tr("Storage"), QObject::tr("No disk found"));
    EXPORT_TO_XLSX(xlsx, boldFont, m_ListDeviceGPU, QObject::tr("Display Adapter"), QObject::tr("No GPU found"));
    EXPORT_TO_XLSX(xlsx, boldFont, m_ListDeviceMonitor, QObject::tr("Monitor"), QObject::tr("No monitor found"));
    EXPORT_TO_XLSX(xlsx, boldFont, m_ListDeviceNetwork, QObject::tr("Network Adapter"), QObject::tr("No network adapter found"));
    EXPORT_TO_XLSX(xlsx, boldFont, m_ListDeviceAudio, QObject::tr("Sound Adapter"), QObject::tr("No audio device found"));
    EXPORT_TO_XLSX(xlsx, boldFont, m_ListDeviceBluetooth, QObject::tr("Bluetooth"), QObject::tr("No Bluetooth device found"));
    EXPORT_TO_XLSX(xlsx, boldFont, m_ListDeviceOtherPCI, QObject::tr("Other PCI Devices"), QObject::tr("No other PCI devices found"));
    EXPORT_TO_XLSX(xlsx, boldFont, m_ListDevicePower, QObject::tr("Power"), QObject::tr("No battery found"));
    EXPORT_TO_XLSX(xlsx, boldFont, m_ListDeviceKeyboard, QObject::tr("Keyboard"), QObject::tr("No keyboard found"));
    EXPORT_TO_XLSX(xlsx, boldFont, m_ListDeviceMouse, QObject::tr("Mouse"), QObject::tr("No mouse found"));
    EXPORT_TO_XLSX(xlsx, boldFont, m_ListDevicePrint, QObject::tr("Printer"), QObject::tr("No printer found"));
    EXPORT_TO_XLSX(xlsx, boldFont, m_ListDeviceImage, QObject::tr("Camera"), QObject::tr("No camera found"));
    EXPORT_TO_XLSX(xlsx, boldFont, m_ListDeviceCdrom, QObject::tr("CD-ROM"), QObject::tr("No CD-ROM found"));
    EXPORT_TO_XLSX(xlsx, boldFont, m_ListDeviceOthers, QObject::tr("Other Devices"), QObject::tr("No other devices found"));
    m_CurrentXlsRow = 1;
    xlsx.saveAs(filePath);

    return true;
}

bool DeviceManager::exportToDoc(const QString &filePath)
{
    qCDebug(appLog) << "Exporting to doc file";
    // 导出设备信息到doc文件
    Docx::Document doc(":/template.docx");
    overviewToDoc(doc);
    EXPORT_TO_DOC(doc, m_ListDeviceCPU, QObject::tr("CPU"), QObject::tr("No CPU found"));
    EXPORT_TO_DOC(doc, m_ListDeviceBios, QObject::tr("Motherboard"), QObject::tr("No motherboard found"));
    EXPORT_TO_DOC(doc, m_ListDeviceMemory, QObject::tr("Memory"), QObject::tr("No memory found"));
    EXPORT_TO_DOC(doc, m_ListDeviceStorage, QObject::tr("Storage"), QObject::tr("No disk found"));
    EXPORT_TO_DOC(doc, m_ListDeviceGPU, QObject::tr("Display Adapter"), QObject::tr("No GPU found"));
    EXPORT_TO_DOC(doc, m_ListDeviceMonitor, QObject::tr("Monitor"), QObject::tr("No monitor found"));
    EXPORT_TO_DOC(doc, m_ListDeviceNetwork, QObject::tr("Network Adapter"), QObject::tr("No network adapter found"));
    EXPORT_TO_DOC(doc, m_ListDeviceAudio, QObject::tr("Sound Adapter"), QObject::tr("No audio device found"));
    EXPORT_TO_DOC(doc, m_ListDeviceBluetooth, QObject::tr("Bluetooth"), QObject::tr("No Bluetooth device found"));
    EXPORT_TO_DOC(doc, m_ListDeviceOtherPCI, QObject::tr("Other PCI Devices"), QObject::tr("No other PCI devices found"));
    EXPORT_TO_DOC(doc, m_ListDevicePower, QObject::tr("Power"), QObject::tr("No battery found"));
    EXPORT_TO_DOC(doc, m_ListDeviceKeyboard, QObject::tr("Keyboard"), QObject::tr("No keyboard found"));
    EXPORT_TO_DOC(doc, m_ListDeviceMouse, QObject::tr("Mouse"), QObject::tr("No mouse found"));
    EXPORT_TO_DOC(doc, m_ListDevicePrint, QObject::tr("Printer"), QObject::tr("No printer found"));
    EXPORT_TO_DOC(doc, m_ListDeviceImage, QObject::tr("Camera"), QObject::tr("No camera found"));
    EXPORT_TO_DOC(doc, m_ListDeviceCdrom, QObject::tr("CD-ROM"), QObject::tr("No CD-ROM found"));
    EXPORT_TO_DOC(doc, m_ListDeviceOthers, QObject::tr("Other Devices"), QObject::tr("No other devices found"));

    doc.save(filePath);

    return true;
}

bool DeviceManager::exportToHtml(const QString &filePath)
{
    qCDebug(appLog) << "Exporting to html file";
    // 导出设备信息到html文件
    QFile html(filePath);
    if (false == html.open(QIODevice::WriteOnly))
        return false;

    html.write("<!DOCTYPE html>\n");
    html.write("<html>\n");
    html.write("<body>\n");

    overviewToHtml(html);

    EXPORT_TO_HTML(html, m_ListDeviceCPU, QObject::tr("CPU"), QObject::tr("No CPU found"));
    EXPORT_TO_HTML(html, m_ListDeviceBios, QObject::tr("Motherboard"), QObject::tr("No motherboard found"));
    EXPORT_TO_HTML(html, m_ListDeviceMemory, QObject::tr("Memory"), QObject::tr("No memory found"));
    EXPORT_TO_HTML(html, m_ListDeviceStorage, QObject::tr("Storage"), QObject::tr("No disk found"));
    EXPORT_TO_HTML(html, m_ListDeviceGPU, QObject::tr("Display Adapter"), QObject::tr("No GPU found"));
    EXPORT_TO_HTML(html, m_ListDeviceMonitor, QObject::tr("Monitor"), QObject::tr("No monitor found"));
    EXPORT_TO_HTML(html, m_ListDeviceNetwork, QObject::tr("Network Adapter"), QObject::tr("No network adapter found"));
    EXPORT_TO_HTML(html, m_ListDeviceAudio, QObject::tr("Sound Adapter"), QObject::tr("No audio device found"));
    EXPORT_TO_HTML(html, m_ListDeviceBluetooth, QObject::tr("Bluetooth"), QObject::tr("No Bluetooth device found"));
    EXPORT_TO_HTML(html, m_ListDeviceOtherPCI, QObject::tr("Other PCI Devices"), QObject::tr("No other PCI devices found"));
    EXPORT_TO_HTML(html, m_ListDevicePower, QObject::tr("Power"), QObject::tr("No battery found"));
    EXPORT_TO_HTML(html, m_ListDeviceKeyboard, QObject::tr("Keyboard"), QObject::tr("No keyboard found"));
    EXPORT_TO_HTML(html, m_ListDeviceMouse, QObject::tr("Mouse"), QObject::tr("No mouse found"));
    EXPORT_TO_HTML(html, m_ListDevicePrint, QObject::tr("Printer"), QObject::tr("No printer found"));
    EXPORT_TO_HTML(html, m_ListDeviceImage, QObject::tr("Camera"), QObject::tr("No camera found"));
    EXPORT_TO_HTML(html, m_ListDeviceCdrom, QObject::tr("CD-ROM"), QObject::tr("No CD-ROM found"));
    EXPORT_TO_HTML(html, m_ListDeviceOthers, QObject::tr("Other Devices"), QObject::tr("No other devices found"));

    html.write("</body>\n");                                                \
    html.write("</html>\n");

    html.close();

    return true;
}

int DeviceManager::currentXlsRow()
{
    // qCDebug(appLog) << "Getting current XLS row";
    return m_CurrentXlsRow++;
}

void DeviceManager::overviewToTxt(QTextStream &out)
{
    qCDebug(appLog) << "Exporting overview to txt";
    // 概况信息导出到txt
    out << "[" << tr("Overview") << "]\n-------------------------------------------------";
    out << "\n";

    // 导出设备信息
    out.setFieldWidth(21);
    out.setFieldAlignment(QTextStream::FieldAlignment::AlignLeft);
    out << tr("Device") + ": ";
    out.setFieldWidth(0);
    out << m_OveriewMap["Overview"];
    out << "\n";

    // 导出操作系统信息
    out.setFieldWidth(21);
    out.setFieldAlignment(QTextStream::FieldAlignment::AlignLeft);
    out << tr("OS") + ": ";
    out.setFieldWidth(0);
    out << m_OveriewMap["OS"];
    out << "\n";

    foreach (auto iter, m_ListDeviceType) {
        if (iter.first == tr("Overview"))
            continue;

        // 导出设备的概况信息
        if (m_OveriewMap.find(iter.first) != m_OveriewMap.end()) {
            out.setFieldWidth(21);
            out.setFieldAlignment(QTextStream::FieldAlignment::AlignLeft);
            out << iter.first + ": ";
            out.setFieldWidth(0);
            out << m_OveriewMap[iter.first];
            out << "\n";
        }
    }
    out << "\n";
}

void DeviceManager::overviewToHtml(QFile &html)
{
    qCDebug(appLog) << "Exporting overview to html";
    // 导出概况信息到html
    html.write((QString("<h2>") + "[" + tr("Overview") + "]" + "</h2>").toUtf8());
    QDomDocument doc;

    // 导出设备信息到html
    infoToHtml(doc, "Device", m_OveriewMap["Overview"]);

    // 导出操作系统信息到html
    infoToHtml(doc, "OS", m_OveriewMap["OS"]);

    // 导出设备概况信息到html
    foreach (auto iter, m_ListDeviceType) {
        if (iter.first == tr("Overview"))
            continue;

        if (m_OveriewMap.find(iter.first) != m_OveriewMap.end())
            infoToHtml(doc, iter.first, m_OveriewMap[iter.first]);
    }

    doc.appendChild(doc.createElement("br"));
    html.write(doc.toString().toStdString().data());
}

void DeviceManager::overviewToDoc(Docx::Document &doc)
{
    qCDebug(appLog) << "Exporting overview to doc";
    // 导出概况信息到doc文件
    doc.addHeading("[" + tr("Overview") + "]");
    doc.addParagraph("-------------------------------------------------");

    // 导出设备信息到doc
    QString line = tr("Device") + ":  " + m_OveriewMap["Overview"];
    doc.addParagraph(line);

    // 导出操作系统信息到doc
    line = tr("OS") + ":  " + m_OveriewMap["OS"];
    doc.addParagraph(line);

    // 导出设备概况信息到doc
    foreach (auto iter, m_ListDeviceType) {
        if (iter.first == tr("Overview"))
            continue;

        if (m_OveriewMap.find(iter.first) != m_OveriewMap.end()) {
            line = iter.first + ":  " + m_OveriewMap[iter.first];
            doc.addParagraph(line);
        }
    }
    doc.addParagraph("\n");
}

void DeviceManager::overviewToXlsx(QXlsx::Document &xlsx, QXlsx::Format &boldFont)
{
    qCDebug(appLog) << "Exporting overview to xlsx";
    // 导出概况信息到xlsx文件
    boldFont.setFontBold(true);
    xlsx.write(m_CurrentXlsRow++, 1, "[" + tr("Overview") + "]", boldFont);

    // 设置字体格式
    boldFont.setFontBold(false);
    boldFont.setFontSize(10);

    // 导出设备信息到xlsx文件
    xlsx.write(m_CurrentXlsRow, 1, tr("Device"), boldFont);
    xlsx.write(m_CurrentXlsRow++, 2, m_OveriewMap["Overview"], boldFont);

    // 导出操作系统信息到xlsx文件
    xlsx.write(m_CurrentXlsRow, 1, tr("OS"), boldFont);
    xlsx.write(m_CurrentXlsRow++, 2, m_OveriewMap["OS"], boldFont);

    // 导出设备概况信息到xlsx文件
    foreach (auto iter, m_ListDeviceType) {
        if (iter.first == tr("Overview"))
            continue;

        if (m_OveriewMap.find(iter.first) != m_OveriewMap.end()) {

            xlsx.write(m_CurrentXlsRow, 1, iter.first, boldFont);
            xlsx.write(m_CurrentXlsRow++, 2, m_OveriewMap[iter.first], boldFont);
        }
    }
    m_CurrentXlsRow++;
}

void DeviceManager::infoToHtml(QDomDocument &doc, const QString &key, const QString &value)
{
    qCDebug(appLog) << "Exporting info to html";
    // 导出设备信息到html
    // 创建表格,设置表格属性
    QDomElement table = doc.createElement("table");
    table.setAttribute("border", "0");
    table.setAttribute("width", "100%");
    table.setAttribute("cellpadding", "3");

    QDomElement tr = doc.createElement("tr");

    // 创建单元格,设置单元格属性
    QDomElement td = doc.createElement("td");
    td.setAttribute("width", "15%");
    td.setAttribute("style", "text-align:left;");

    // 设置单元格内容
    QDomText nameText = doc.createTextNode(QObject::tr(key.toStdString().c_str()) + ": ");
    td.appendChild(nameText);
    tr.appendChild(td);

    // 创建单元格,设置单元格属性
    QDomElement td2 = doc.createElement("td");
    td2.setAttribute("width", "85%");

    // 设置单元格内容
    QDomText valueText;
    valueText = doc.createTextNode(value);
    td2.appendChild(valueText);

    tr.appendChild(td2);

    // 插入表格
    table.appendChild(tr);

    doc.appendChild(table);
}

const QMap<QString, QString>  &DeviceManager::getDeviceOverview()
{
    qCDebug(appLog) << "Getting device overview";
    // 获取所有设备的概况信息
    m_OveriewMap.clear();

    auto iter = m_DeviceClassMap.begin();

    // 根据设备指针类,获取所有设备概况信息
    for (; iter != m_DeviceClassMap.end(); ++iter) {
        if (m_OveriewMap.find(iter.key()) == m_OveriewMap.end()) {
            foreach (auto device, iter.value()) {
                QString ov = device->getOverviewInfo();

                // 每一类别获取首个设备概况信息
                if (false == ov.isEmpty()) {
                    if (m_OveriewMap.find(iter.key()) == m_OveriewMap.end()) {
                        m_OveriewMap[iter.key()] = ov;
                    } else {
                        // 每一类别获取第n个设备概况信息
                        m_OveriewMap[iter.key()] += "/";
                        m_OveriewMap[iter.key()] += ov;
                    }
                }
            }
        }
    }

    // 设备名称 and 操作系统
    if (m_ListDeviceComputer.size() > 0) {
        m_OveriewMap["Overview"] = m_ListDeviceComputer[0]->getOverviewInfo();
        m_OveriewMap["OS"] = dynamic_cast<DeviceComputer *>(m_ListDeviceComputer[0])->getOSInfo();
    }


    // CPU 概况显示 样式"Intel(R) Core(TM) i3-9100F CPU @ 3.60GHz (四核 / 四逻辑处理器)"
    if (!m_ListDeviceCPU.isEmpty())
        m_OveriewMap[tr("CPU")] = m_ListDeviceCPU[0] ->getOverviewInfo();

    if (m_CpuNum > 1)
        m_OveriewMap[tr("CPU quantity")] = QString::number(m_CpuNum);

    return m_OveriewMap;
}

const QMap<QString, QMap<QString, QStringList> > &DeviceManager::getDeviceDriverPool()
{
    qCDebug(appLog) << "Getting device driver pool";
    // 获取所有设备驱动与设备名称设备类别的对应关系
    auto iter = m_DeviceClassMap.begin();

    for (; iter != m_DeviceClassMap.end(); ++iter) {

        foreach (auto device, iter.value()) {
            if (m_DeviceDriverPool.find(device->driver()) != m_DeviceDriverPool.end()) {
                // 驱动内容不为空时添加
                if (false == device->driver().isEmpty())
                    m_DeviceDriverPool[device->driver()][iter.key()] += device->name();
            } else {
                if (false == device->driver().isEmpty())
                    m_DeviceDriverPool[device->driver()][iter.key()] += device->name();
            }

        }
    }
    return m_DeviceDriverPool;
}

void DeviceManager::addInputInfo(const QString &key, const QMap<QString, QString> &mapInfo)
{
    qCDebug(appLog) << "Adding input info";
    // 添加输入设备信息
    if (m_InputDeviceInfo.find(key) == m_InputDeviceInfo.end())
        m_InputDeviceInfo.insert(key, mapInfo);
}
const QMap<QString, QString> &DeviceManager::inputInfo(const QString &key)
{
    // qCDebug(appLog) << "Getting input info";
    return m_InputDeviceInfo[key];
}

bool DeviceManager::isDeviceExistInPairedDevice(const QString &mac)
{
    qCDebug(appLog) << "Checking if device exists in paired device";
    // 获取蓝牙设备配对信息
    const QList<QMap<QString, QString> >  &cmdInfo = DeviceManager::instance()->cmdInfo("bt_device");

    // 判断mac是否在蓝牙设备列表中
    foreach (auto item, cmdInfo) {
        for (auto it = item.begin(); it != item.end(); ++it) {
            if (it.value() == mac)
                return true;
        }
    }
    return false;
}

void DeviceManager::setCpuNum(int num)
{
    // qCDebug(appLog) << "Setting CPU number to:" << num;
    m_CpuNum = num;
}

void DeviceManager::setCpuFrequencyIsCur(const bool &flag)
{
    qCDebug(appLog) << "Setting CPU frequency is current";
    QList<DeviceBaseInfo *>::iterator it = m_ListDeviceCPU.begin();
    for (; it != m_ListDeviceCPU.end(); ++it) {
        DeviceCpu *device = dynamic_cast<DeviceCpu *>(*it);
        if (!device)
            continue;

        device->setFrequencyIsCur(flag);
    }
}

void DeviceManager::setCpuHeaderInfo(const QList<QList<QPair<QString, QString> > > &info)
{
    m_ListCpuHeaderInfo = info;
}

void DeviceManager::getCpuHeaderInfo(QList<QList<QPair<QString, QString> > > &info) const
{
    info = m_ListCpuHeaderInfo;
}

bool DeviceManager::validateKeyboardVidPid(const QString &vid, const QString &pid, QString &normalizedVid, QString &normalizedPid)
{
    qCDebug(appLog) << "Validating keyboard VID:" << vid << "PID:" << pid;

    // Check if VID or PID is empty
    if (vid.isEmpty() || pid.isEmpty()) {
        qCWarning(appLog) << "VID or PID is empty for keyboard validation";
        return false;
    }

    // Normalize VID
    normalizedVid = vid.toLower();
    if (normalizedVid.startsWith("0x")) {
        normalizedVid = normalizedVid.mid(2);
    }

    // Normalize PID
    normalizedPid = pid.toLower();
    if (normalizedPid.startsWith("0x")) {
        normalizedPid = normalizedPid.mid(2);
    }

    // Validate VID and PID format (should be 4 characters each after normalization)
    if (normalizedVid.length() != 4 || normalizedPid.length() != 4) {
        qCWarning(appLog) << "Invalid VID or PID format after normalization. VID:" << normalizedVid << "PID:" << normalizedPid;
        return false;
    }

    // Validate that VID and PID contain only hex characters
    QRegularExpression hexPattern("^[0-9a-f]{4}$");
    if (!hexPattern.match(normalizedVid).hasMatch() || !hexPattern.match(normalizedPid).hasMatch()) {
        qCWarning(appLog) << "VID or PID contains non-hex characters. VID:" << normalizedVid << "PID:" << normalizedPid;
        return false;
    }

    qCDebug(appLog) << "Keyboard VID and PID validation successful. Normalized VID:" << normalizedVid << "Normalized PID:" << normalizedPid;
    return true;
}
