#include "stdafx.h" #include "Configuration.h" CConfiguration::CConfiguration() { } CConfiguration::CConfiguration(const char* pszFilepath) { m_strFilepath = pszFilepath; loadPLCListFromFile(); } CConfiguration::~CConfiguration() { } void CConfiguration::setFilepath(const char* pszFilepath) { m_strFilepath = pszFilepath; loadPLCListFromFile(); } void CConfiguration::getUnitId(CString& strUnitId) { char szTemp[256]; GetPrivateProfileString("App", _T("UnitId"), _T(""), szTemp, 256, m_strFilepath); strUnitId = szTemp; } void CConfiguration::getBond1(CString& strIp, UINT& port, UINT& doorCount) { char szTemp[256]; // ¶ÁIPºÍ¶Ë¿Ú GetPrivateProfileString("Bond1", _T("IP"), _T(""), szTemp, 256, m_strFilepath); strIp = szTemp; port = GetPrivateProfileInt("Bond1", _T("Port"), 0, m_strFilepath); doorCount = GetPrivateProfileInt("Bond1", _T("DoorCount"), 0, m_strFilepath); } int CConfiguration::getLogcatLevel() { return GetPrivateProfileInt(_T("Logcat"), _T("Level"), 0, m_strFilepath); } void CConfiguration::setLogcatLevel(int level) { WritePrivateProfileString(_T("Logcat"), _T("Level"), std::to_string(level).c_str(), m_strFilepath); } int CConfiguration::setLogcatIncludeText(CString& strInclude) { WritePrivateProfileString(_T("Logcat"), _T("IncludeText"), strInclude, m_strFilepath); return 0; } int CConfiguration::getLogcatIncludeText(CString& strInclude) { char szTemp[256]; GetPrivateProfileString("Logcat", _T("IncludeText"), _T(""), szTemp, 256, m_strFilepath); strInclude = szTemp; return 0; } void CConfiguration::setLogcatIncludeRegex(BOOL bRegex) { WritePrivateProfileString(_T("Logcat"), _T("IncludeRegex"), bRegex ? "1" : "0", m_strFilepath); } BOOL CConfiguration::isLogcatIncludeRegex() { return GetPrivateProfileInt(_T("Logcat"), _T("IncludeRegex"), 0, m_strFilepath); } int CConfiguration::getCustomLogcatIncludeTexts(std::vector& texts) { char szSection[256], szTemp[256]; for (int i = 0; i < 10; i++) { sprintf_s(szSection, 256, "CustomInclude%d", i + 1); GetPrivateProfileString("Logcat", szSection, _T(""), szTemp, 256, m_strFilepath); std::string strInclude(szTemp); if (!strInclude.empty()) { texts.push_back(strInclude); } } return (int)texts.size(); } int CConfiguration::getP2RemoteEqReconnectInterval() { return GetPrivateProfileInt(_T("P2"), _T("RemoteEqReconnectInterval"), 20, m_strFilepath); } void CConfiguration::setP2RemoteEqReconnectInterval(int second) { WritePrivateProfileString(_T("P2"), _T("RemoteEqReconnectInterval"), 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& 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); } }