#include "common.h" #include #include #include using namespace nlohmann; Common::Common() { } bool Common::loadConfig(const QString &filePath) { m_configFilePath = filePath; std::ifstream jsonStream(filePath.toLocal8Bit()); m_configJson = json::parse(jsonStream); jsonStream.close(); // 提取网络配置 auto netCfg = m_configJson.at("network"); m_ipAddr = QString::fromStdString(netCfg["ip"]); m_srcPort = netCfg["src_port"]; m_dstPort = netCfg["dst_port"]; // 提取工作模式 auto workmodes = m_configJson.at("workmode"); for (const auto& workmode : workmodes) { QString name = QString::fromStdString(workmode["name"]); int index = workmode["index"]; QString path = QString::fromStdString(workmode["exe_path"]); QString reworkPath = QString::fromStdString(workmode["rework_exe_path"]); m_modeConfigs[index] = ModeConfig{index, name, path, reworkPath}; } // 提取产品配置 auto products = m_configJson.at("products"); for (const auto& product : products) { int index = product["index"]; bool selected = product["selected"]; int mode = product["mode"]; QString formatStr = QString::fromStdString(product["format"]); m_productConfigs[index] = ProductConfig{index, static_cast(mode), selected, formatStr}; } FilterSelectedProducts(); return true; } bool Common::saveConfig(const QMap &productsCfg, const QMap &workmodeCfg) { setProductConfig(productsCfg); setWorkModeConfigs(workmodeCfg); saveConfig(); return true; } QString Common::getHostAddress() const { return m_ipAddr; } int Common::getSrcPort() const { return m_srcPort; } int Common::getDstPort() const { return m_dstPort; } bool Common::saveConfig() { std::ofstream outFile(m_configFilePath.toLocal8Bit()); if (!outFile) { qDebug() << "无法打开文件进行写入!"; return false; } outFile << m_configJson.dump(4); outFile.close(); return true; } void Common::FilterSelectedProducts() { // 提取用于生产产品列表 m_selectedProduct.clear(); for (const auto& productCfg : m_productConfigs) { if (productCfg.selected) { m_selectedProduct[productCfg.productFormat] = productCfg.workMode; } } } QMap Common::getProductConfigs() { return m_productConfigs; } void Common::setProductConfig(const QMap &productsCfg) { m_productConfigs = productsCfg; // 更新产品配置 for (auto& product : m_configJson["products"]) { int index = product["index"]; product["selected"] = m_productConfigs[index].selected; product["format"] = m_productConfigs[index].productFormat.toStdString(); product["mode"] = m_productConfigs[index].workMode; } FilterSelectedProducts(); } QMap Common::getWorkModeConfigs() { return m_modeConfigs; } void Common::setWorkModeConfigs(const QMap &workmodeCfg) { m_modeConfigs = workmodeCfg; // 更新工作模式配置 for (auto& workmode : m_configJson["workmode"]) { int index = workmode["index"]; workmode["name"] = m_modeConfigs[index].name.toStdString(); workmode["exe_path"] = m_modeConfigs[index].workPath.toStdString(); workmode["rework_exe_path"] = m_modeConfigs[index].reworkPath.toStdString(); } } const QMap &Common::getSelectedProducts() const { return m_selectedProduct; } int Common::getCurrentWorkMode() const { return m_currentMode.load(); } void Common::setCurrentWorkMode(const int workMode) { m_currentMode.store(workMode); }