mrDarker
6 天以前 829fe6c6bc33d53fda9c31fd45a37e1df87befff
SourceCode/Bond/BoounionPLC/Configuration.cpp
@@ -9,6 +9,7 @@
CConfiguration::CConfiguration(const char* pszFilepath)
{
   m_strFilepath = pszFilepath;
    loadPLCListFromFile();
}
CConfiguration::~CConfiguration()
@@ -18,6 +19,7 @@
void CConfiguration::setFilepath(const char* pszFilepath)
{
   m_strFilepath = pszFilepath;
    loadPLCListFromFile();
}
void CConfiguration::getUnitId(CString& strUnitId)
@@ -103,3 +105,133 @@
      std::to_string(second).c_str(), m_strFilepath);
}
// ================================== PLC 配置操作 ==================================
// 添加 PLC
bool CConfiguration::addPLC(const CString& strName, const CString& strIp, UINT nPort)
{
    // 检查是否重复
    auto it = std::find_if(m_plcList.begin(), m_plcList.end(),
        [&strName](const PlcInfo& plc) { return plc.strName == strName; });
    if (it != m_plcList.end()) {
        return false;
    }
    // 添加 PLC
    PlcInfo plc;
    plc.strName = strName;
    plc.strIp = strIp;
    plc.nPort = nPort;
    m_plcList.push_back(plc);
    savePLCListToFile();
    return true;
}
// 删除 PLC
bool CConfiguration::removePLC(const CString& strName)
{
    auto it = std::find_if(m_plcList.begin(), m_plcList.end(),
        [&strName](const PlcInfo& plc) { return plc.strName == strName; });
    if (it != m_plcList.end()) {
        m_plcList.erase(it);
        savePLCListToFile();
        return true;
    }
    return false;
}
// 更新 PLC
bool CConfiguration::updatePLC(const CString& strOldName, const CString& strNewName, const CString& strNewIp, UINT nNewPort)
{
    auto it = std::find_if(m_plcList.begin(), m_plcList.end(),
        [&strOldName](const PlcInfo& plc) { return plc.strName == strOldName; });
    if (it != m_plcList.end()) {
        it->strName = strNewName;
        it->strIp = strNewIp;
        it->nPort = nNewPort;
        savePLCListToFile();
        return true;
    }
    return false;
}
// 根据名称获取 PLC 信息
bool CConfiguration::getPLCByName(const CString& strName, CString& strIp, UINT& nPort)
{
    auto it = std::find_if(m_plcList.begin(), m_plcList.end(),
        [&strName](const PlcInfo& plc) { return plc.strName == strName; });
    if (it != m_plcList.end()) {
        strIp = it->strIp;
        nPort = it->nPort;
        return true;
    }
    return false;
}
// 获取 PLC 列表的数量
int CConfiguration::getPLCListCount()
{
    return (int)m_plcList.size();
}
// 获取所有的 PLC 信息
void CConfiguration::getAllPLCInfo(std::vector<PlcInfo>& plcList)
{
    plcList.resize(m_plcList.size());
    plcList.assign(m_plcList.begin(), m_plcList.end());
}
// 从文件加载 PLC 列表
void CConfiguration::loadPLCListFromFile()
{
    m_plcList.clear();
    // 读取 PLC 数量
    int nPLCCount = GetPrivateProfileInt("PLCs", "PLCCount", 0, m_strFilepath);
    char szSection[256], szTemp[256];
    for (int i = 0; i < nPLCCount; i++) {
        sprintf_s(szSection, 256, "PLC%d", i + 1);
        GetPrivateProfileString("PLCs", szSection, _T(""), szTemp, 256, m_strFilepath);
        if (szTemp[0] != '\0') {
            CString strInfo(szTemp);
            // 格式是 Name,IP,Port
            int nPos1 = strInfo.Find(_T(','));
            int nPos2 = strInfo.Find(_T(','), nPos1 + 1);
            if (nPos1 != -1 && nPos2 != -1) {
                PlcInfo plcInfo;
                plcInfo.strName = strInfo.Left(nPos1);
                plcInfo.strIp = strInfo.Mid(nPos1 + 1, nPos2 - nPos1 - 1);
                plcInfo.nPort = _ttoi(strInfo.Mid(nPos2 + 1));
                m_plcList.push_back(plcInfo);
            }
        }
    }
}
// 保存 PLC 列表到文件
void CConfiguration::savePLCListToFile()
{
    // 清空文件中原来的 PLC 配置数据
    WritePrivateProfileString("PLCs", NULL, NULL, m_strFilepath);
    // 保存 PLC 数量
    int nPLCCount = (int)m_plcList.size();
    WritePrivateProfileString("PLCs", "PLCCount", std::to_string(nPLCCount).c_str(), m_strFilepath);
   // 重新写入数据
    for (int i = 0; i < nPLCCount; i++) {
        char szSection[256];
        sprintf_s(szSection, 256, "PLC%d", i + 1);
        CString strInfo;
        strInfo.Format(_T("%s,%s,%d"), m_plcList[i].strName, m_plcList[i].strIp, m_plcList[i].nPort);
        WritePrivateProfileString("PLCs", szSection, strInfo, m_strFilepath);
    }
}