You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
200 lines
5.3 KiB
C++
200 lines
5.3 KiB
C++
#pragma execution_character_set("utf-8")
|
|
#include "common.h"
|
|
#include <fstream>
|
|
#include <QDir>
|
|
#include <QSettings>
|
|
|
|
#include <QDebug>
|
|
|
|
using namespace nlohmann;
|
|
|
|
Common::Common()
|
|
{
|
|
|
|
}
|
|
|
|
bool Common::loadConfig(const QString &filePath)
|
|
{
|
|
try
|
|
{
|
|
// 提取网络配置
|
|
QString iniFilePath = filePath + "/network.ini";
|
|
QSettings* configIni = new QSettings(iniFilePath, QSettings::IniFormat);
|
|
m_ipAddr = configIni->value("network/ip").toString();
|
|
m_srcPort = configIni->value("network/src_port").toInt();
|
|
m_dstPort = configIni->value("network/dst_port").toInt();
|
|
|
|
m_configFilePath = filePath + "/Config.json";
|
|
std::ifstream jsonStream(m_configFilePath.toLocal8Bit());
|
|
m_configJson = json::parse(jsonStream);
|
|
jsonStream.close();
|
|
// 提取工作模式
|
|
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>(mode), selected, formatStr };
|
|
}
|
|
FilterSelectedProducts();
|
|
return true;
|
|
}
|
|
catch(const std::exception& e)
|
|
{
|
|
LError(QString("Load config file error: %1").arg(e.what()));
|
|
return false;
|
|
}
|
|
}
|
|
|
|
bool Common::saveConfig(const QMap<int, ProductConfig> &productsCfg, const QMap<int, ModeConfig> &workmodeCfg)
|
|
{
|
|
setProductConfig(productsCfg);
|
|
setWorkModeConfigs(workmodeCfg);
|
|
saveConfig();
|
|
return true;
|
|
}
|
|
|
|
bool Common::updateProductsConfig(const QMap<int, ProductConfig> &productsCfg)
|
|
{
|
|
setProductConfig(productsCfg);
|
|
saveConfig();
|
|
return true;
|
|
}
|
|
|
|
QString Common::getIPAddr() const
|
|
{
|
|
return m_ipAddr;
|
|
}
|
|
|
|
int Common::getSrcPort() const
|
|
{
|
|
return m_srcPort;
|
|
}
|
|
|
|
int Common::getDstPort() const
|
|
{
|
|
return m_dstPort;
|
|
}
|
|
|
|
const QString Common::getCurrWorkModeStr() const
|
|
{
|
|
if (m_currentMode.load() == -1)
|
|
{
|
|
return getWorkModeStr(-1);
|
|
}
|
|
|
|
QString reStr = m_isRepair.load() == 1 ? "反修" : "正常";
|
|
QString logStr = QString("%1-%2").arg(getWorkModeStr(m_currentMode.load())).arg(reStr);
|
|
return logStr;
|
|
}
|
|
|
|
bool Common::saveConfig()
|
|
{
|
|
std::ofstream outFile(m_configFilePath.toLocal8Bit());
|
|
if (!outFile)
|
|
{
|
|
LError("Write config json file error!");
|
|
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<int, ProductConfig> Common::getProductConfigs()
|
|
{
|
|
return m_productConfigs;
|
|
}
|
|
|
|
void Common::setProductConfig(const QMap<int, ProductConfig> &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<int, ModeConfig> Common::getWorkModeConfigs()
|
|
{
|
|
return m_modeConfigs;
|
|
}
|
|
|
|
void Common::setWorkModeConfigs(const QMap<int, ModeConfig> &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<QString, Mode> &Common::getSelectedProducts() const
|
|
{
|
|
return m_selectedProduct;
|
|
}
|
|
|
|
int Common::getCurrentWorkMode() const
|
|
{
|
|
return m_currentMode.load();
|
|
}
|
|
|
|
void Common::setCurrentWorkMode(const int workMode)
|
|
{
|
|
m_currentMode.store(workMode);
|
|
}
|
|
|
|
bool Common::getRepairFlag() const
|
|
{
|
|
return m_isRepair.load();
|
|
}
|
|
|
|
void Common::setRepairFlag(const bool isRepair)
|
|
{
|
|
m_isRepair.store(isRepair);
|
|
}
|
|
|
|
QString Common::getWorkModeStr(int mode)
|
|
{
|
|
static QMap<int, QString> modeMap = { {-1, "未连接"}, {2, "GLOBAL_M2M"}, {3, "LOCAL_M2M"}};
|
|
if (!modeMap.contains(mode))
|
|
{
|
|
return "unknown mode";
|
|
}
|
|
return modeMap[mode];
|
|
}
|