LAPTOP-SNT8I5JK\Boounion
2025-06-26 135d1d829b36eb06c8acc5b8782a77acda767198
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#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<std::string>& 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<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);
    }
}