From 889044f1c7cdb76254c49dd078fa1ef8e93c3f3a Mon Sep 17 00:00:00 2001
From: LAPTOP-SNT8I5JK\Boounion <Chenluhua@qq.com>
Date: 星期四, 09 一月 2025 11:44:23 +0800
Subject: [PATCH] 1.PLC模拟从文件加载,连接; 2.修复内存泄露问题;

---
 SourceCode/Bond/BoounionPLC/PLC.cpp                          |    7 +++++++
 SourceCode/Bond/BoounionPLC/BoounionPLCDlg.cpp               |    8 ++++++--
 SourceCode/Bond/BoounionPLC/Model.cpp                        |   22 +++++++++++++++++++++-
 SourceCode/Bond/BoounionPLC/PLC.h                            |    1 +
 SourceCode/Bond/x64/BoounionPLC/Debug/Res/logcat_include.ico |    0 
 SourceCode/Bond/BoounionPLC/Model.h                          |    2 +-
 SourceCode/Bond/BoounionPLC/PagePlcList.cpp                  |   11 +++++++++--
 7 files changed, 45 insertions(+), 6 deletions(-)

diff --git a/SourceCode/Bond/BoounionPLC/BoounionPLCDlg.cpp b/SourceCode/Bond/BoounionPLC/BoounionPLCDlg.cpp
index d8fc12f..d31d1c0 100644
--- a/SourceCode/Bond/BoounionPLC/BoounionPLCDlg.cpp
+++ b/SourceCode/Bond/BoounionPLC/BoounionPLCDlg.cpp
@@ -358,6 +358,12 @@
 		m_pPagePlcList = nullptr;
 	}
 	
+	if (m_pPageLogcat != nullptr) {
+		m_pPageLogcat->DestroyWindow();
+		delete m_pPageLogcat;
+		m_pPageLogcat = nullptr;
+	}
+
 	if (m_pMainContainer != nullptr) {
 		m_pMainContainer->DestroyWindow();
 		delete m_pMainContainer;
@@ -447,14 +453,12 @@
 	m_bShowLogWnd = !m_bShowLogWnd;
 
 	if (m_bShowLogWnd) {
-		LOGD("显示日志窗口");
 		m_pPageLogcat->ShowWindow(SW_SHOW);
 		m_pPageLogcat->SetParent(m_pMainContainer);
 		m_pMainContainer->SetBottomWnd(m_pPageLogcat, LOG_WND_HEIGHT);
 		m_pMainContainer->Resize();
 	}
 	else {
-		LOGD("隐藏日志窗口");
 		m_pPageLogcat->ShowWindow(SW_HIDE);
 		m_pPageLogcat->SetParent(this);
 		m_pMainContainer->SetBottomWnd(nullptr, 0);
diff --git a/SourceCode/Bond/BoounionPLC/Model.cpp b/SourceCode/Bond/BoounionPLC/Model.cpp
index e5c97c6..d89bf61 100644
--- a/SourceCode/Bond/BoounionPLC/Model.cpp
+++ b/SourceCode/Bond/BoounionPLC/Model.cpp
@@ -72,11 +72,19 @@
 	g_pModel = this;
 
 
+	// 模拟从文档或数据库加载PLC列表
+	addPlc("Test1", "127.0.0.1", 1001);
+	addPlc("Test2", "127.0.0.1", 1002);
+
+
 	return 0;
 }
 
 int CModel::term()
 {
+	for (auto item : m_mapPlc) {
+		item.second->term();
+	}
 	CLog::GetLog()->SetOnLogCallback(nullptr);
 	return 0;
 }
@@ -288,10 +296,21 @@
 
 void CModel::onTimer(UINT nTimerid)
 {
+	static int ii = 0;
+	ii++;
 
+
+	// 检测是否断开,重连
+	if (ii % 5 == 0) {
+		for (auto item : m_mapPlc) {
+			if (!item.second->isConnected()) {
+				item.second->connect();
+			}
+		}
+	}
 }
 
-std::map<std::string, CPLC*>& CModel::gtPlcMap()
+std::map<std::string, CPLC*>& CModel::getPlcMap()
 {
 	return m_mapPlc;
 }
@@ -301,6 +320,7 @@
 	auto iter = m_mapPlc.find(pszName);
 	if (iter != m_mapPlc.end()) return -1;
 	CPLC* pPLC = new CPLC(pszName, pszIp, port);
+	pPLC->init();
 	m_mapPlc[pszName] = pPLC;
 
 	notifyPtr(RX_CODE_ADD_PLC, pPLC);
diff --git a/SourceCode/Bond/BoounionPLC/Model.h b/SourceCode/Bond/BoounionPLC/Model.h
index 5ea3814..b02b2de 100644
--- a/SourceCode/Bond/BoounionPLC/Model.h
+++ b/SourceCode/Bond/BoounionPLC/Model.h
@@ -16,7 +16,7 @@
 	int init();
 	int term();
 	void onTimer(UINT nTimerid);
-	std::map<std::string, CPLC*>& gtPlcMap();
+	std::map<std::string, CPLC*>& getPlcMap();
 	int addPlc(const char* pszName, const char* pszIp, const unsigned int port);
 	int removePlc(const char* pszName);
 
diff --git a/SourceCode/Bond/BoounionPLC/PLC.cpp b/SourceCode/Bond/BoounionPLC/PLC.cpp
index 92e13e6..526073b 100644
--- a/SourceCode/Bond/BoounionPLC/PLC.cpp
+++ b/SourceCode/Bond/BoounionPLC/PLC.cpp
@@ -185,6 +185,13 @@
 	return m_pChannel != nullptr && m_pChannel->isConnected();
 }
 
+void CPLC::connect()
+{
+	if (m_pChannel != nullptr && !m_pChannel->isConnected()) {
+		m_pChannel->connect();
+	}
+}
+
 void CPLC::setState(PLCSTATE state)
 {
 	m_state = state;
diff --git a/SourceCode/Bond/BoounionPLC/PLC.h b/SourceCode/Bond/BoounionPLC/PLC.h
index 40bf436..2bfc39e 100644
--- a/SourceCode/Bond/BoounionPLC/PLC.h
+++ b/SourceCode/Bond/BoounionPLC/PLC.h
@@ -48,6 +48,7 @@
 	void sendBroadcast(CComponent* pSender, CIntent* pIntent);
 	void onRecvBroadcast(CComponent* pSender, CIntent* pIntent);
 	bool isConnected();
+	void connect();
 	void setActionInterval(unsigned int nInterval);
 	std::string& getName();
 	std::string& getIp();
diff --git a/SourceCode/Bond/BoounionPLC/PagePlcList.cpp b/SourceCode/Bond/BoounionPLC/PagePlcList.cpp
index 3e0cc18..980fac0 100644
--- a/SourceCode/Bond/BoounionPLC/PagePlcList.cpp
+++ b/SourceCode/Bond/BoounionPLC/PagePlcList.cpp
@@ -174,8 +174,15 @@
 
 void CPagePlcList::ReadPLCList()
 {
-	//m_treeCtrl.InsertItem("PLC1", nullptr, nullptr);
-	//m_treeCtrl.InsertItem("PLC2", nullptr, nullptr);
+	std::map<std::string, CPLC*>& plcs = theApp.m_model.getPlcMap();
+	for (auto item : plcs) {
+		HTREEITEM hItem = m_treeCtrl.InsertItem(item.second->getName().c_str(), nullptr, nullptr);
+		m_treeCtrl.SetItemData(hItem, (DWORD_PTR)item.second);
+	}
+	if (!plcs.empty()) {
+		m_treeCtrl.ShowWindow(SW_SHOW);
+		GetDlgItem(IDC_LABEL_NO_PLC)->ShowWindow(SW_HIDE);
+	}
 }
 
 BOOL CPagePlcList::PreTranslateMessage(MSG* pMsg)
diff --git a/SourceCode/Bond/x64/BoounionPLC/Debug/Res/logcat_include.ico b/SourceCode/Bond/x64/BoounionPLC/Debug/Res/logcat_include.ico
new file mode 100644
index 0000000..275a472
--- /dev/null
+++ b/SourceCode/Bond/x64/BoounionPLC/Debug/Res/logcat_include.ico
Binary files differ

--
Gitblit v1.9.3