From 22137ea1df173a3c58a641eb9cd12999258faf18 Mon Sep 17 00:00:00 2001
From: LAPTOP-SNT8I5JK\Boounion <Chenluhua@qq.com>
Date: 星期二, 07 一月 2025 17:00:49 +0800
Subject: [PATCH] 1.模拟添加PLC,删除PLC

---
 SourceCode/Bond/BoounionPLC/PLC.cpp                     |   33 ++++++++
 SourceCode/Bond/BoounionPLC/BoounionPLCDlg.cpp          |   12 ++
 SourceCode/Bond/BoounionPLC/Model.cpp                   |   31 +++++++
 SourceCode/Bond/BoounionPLC/Common.h                    |    3 
 SourceCode/Bond/BoounionPLC/PLC.h                       |   21 +++++
 SourceCode/Bond/BoounionPLC/BoounionPLC.vcxproj.filters |    6 +
 SourceCode/Bond/BoounionPLC/Model.h                     |    6 +
 SourceCode/Bond/BoounionPLC/PagePlcList.h               |    3 
 SourceCode/Bond/BoounionPLC/BoounionPLC.vcxproj         |    2 
 SourceCode/Bond/BoounionPLC/PagePlcList.cpp             |   66 +++++++++++++++
 10 files changed, 178 insertions(+), 5 deletions(-)

diff --git a/SourceCode/Bond/BoounionPLC/BoounionPLC.vcxproj b/SourceCode/Bond/BoounionPLC/BoounionPLC.vcxproj
index 1b7c789..9f22244 100644
--- a/SourceCode/Bond/BoounionPLC/BoounionPLC.vcxproj
+++ b/SourceCode/Bond/BoounionPLC/BoounionPLC.vcxproj
@@ -200,6 +200,7 @@
     <ClInclude Include="Log.h" />
     <ClInclude Include="Model.h" />
     <ClInclude Include="PagePlcList.h" />
+    <ClInclude Include="PLC.h" />
     <ClInclude Include="Resource.h" />
     <ClInclude Include="stdafx.h" />
     <ClInclude Include="targetver.h" />
@@ -216,6 +217,7 @@
     <ClCompile Include="Log.cpp" />
     <ClCompile Include="Model.cpp" />
     <ClCompile Include="PagePlcList.cpp" />
+    <ClCompile Include="PLC.cpp" />
     <ClCompile Include="stdafx.cpp">
       <PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Create</PrecompiledHeader>
       <PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Create</PrecompiledHeader>
diff --git a/SourceCode/Bond/BoounionPLC/BoounionPLC.vcxproj.filters b/SourceCode/Bond/BoounionPLC/BoounionPLC.vcxproj.filters
index 3cad77d..0b107a9 100644
--- a/SourceCode/Bond/BoounionPLC/BoounionPLC.vcxproj.filters
+++ b/SourceCode/Bond/BoounionPLC/BoounionPLC.vcxproj.filters
@@ -63,6 +63,9 @@
     <ClInclude Include="ApredTreeCtrl.h">
       <Filter>澶存枃浠�</Filter>
     </ClInclude>
+    <ClInclude Include="PLC.h">
+      <Filter>澶存枃浠�</Filter>
+    </ClInclude>
   </ItemGroup>
   <ItemGroup>
     <ClCompile Include="BoounionPLC.cpp">
@@ -101,6 +104,9 @@
     <ClCompile Include="ApredTreeCtrl.cpp">
       <Filter>婧愭枃浠�</Filter>
     </ClCompile>
+    <ClCompile Include="PLC.cpp">
+      <Filter>婧愭枃浠�</Filter>
+    </ClCompile>
   </ItemGroup>
   <ItemGroup>
     <ResourceCompile Include="BoounionPLC.rc">
diff --git a/SourceCode/Bond/BoounionPLC/BoounionPLCDlg.cpp b/SourceCode/Bond/BoounionPLC/BoounionPLCDlg.cpp
index f23fc67..2ebdd74 100644
--- a/SourceCode/Bond/BoounionPLC/BoounionPLCDlg.cpp
+++ b/SourceCode/Bond/BoounionPLC/BoounionPLCDlg.cpp
@@ -111,6 +111,7 @@
 	//  执行此操作
 	SetIcon(m_hIcon, TRUE);			// 设置大图标
 	SetIcon(m_hIcon, FALSE);		// 设置小图标
+	theApp.m_model.init();
 
 
 	// toolbar
@@ -353,10 +354,17 @@
 {
 	int id = (int)lParam;
 	if (id == IDC_BUTTON_ADD) {
-		AfxMessageBox("IDC_BUTTON_ADD");
+		static int i = 0;
+		char szName[256];
+		sprintf_s(szName, 256, "PLC%d", ++i);
+		theApp.m_model.addPlc(szName, "192.168.1.188", 1001);
 	}
 	else if (id == IDC_BUTTON_DELETE) {
-		AfxMessageBox("IDC_BUTTON_DELETE");
+		static int i = 0;
+		i += 2;
+		char szName[256];
+		sprintf_s(szName, 256, "PLC%d", i);
+		theApp.m_model.removePlc(szName);
 	}
 	else if (id == IDC_BUTTON_SETTINGS) {
 
diff --git a/SourceCode/Bond/BoounionPLC/Common.h b/SourceCode/Bond/BoounionPLC/Common.h
index f3816ef..e58f33c 100644
--- a/SourceCode/Bond/BoounionPLC/Common.h
+++ b/SourceCode/Bond/BoounionPLC/Common.h
@@ -4,6 +4,9 @@
 /* Rx Code */
 #define RX_CODE_TEST					0
 #define RX_CODE_LOG						1000
+#define RX_CODE_ADD_PLC					1001
+#define RX_CODE_REMOVE_PLC				1002
+
 
 /* 定制颜色 */
 #define APP_MAIN_DLG_BACKGROUND			RGB(255, 255, 255)	
diff --git a/SourceCode/Bond/BoounionPLC/Model.cpp b/SourceCode/Bond/BoounionPLC/Model.cpp
index 307a16a..e5c97c6 100644
--- a/SourceCode/Bond/BoounionPLC/Model.cpp
+++ b/SourceCode/Bond/BoounionPLC/Model.cpp
@@ -23,6 +23,10 @@
 
 CModel::~CModel()
 {
+	for (auto item : m_mapPlc) {
+		delete item.second;
+	}
+	m_mapPlc.clear();
 }
 
 IObservable* CModel::getObservable()
@@ -286,3 +290,30 @@
 {
 
 }
+
+std::map<std::string, CPLC*>& CModel::gtPlcMap()
+{
+	return m_mapPlc;
+}
+
+int CModel::addPlc(const char* pszName, const char* pszIp, const unsigned int port)
+{
+	auto iter = m_mapPlc.find(pszName);
+	if (iter != m_mapPlc.end()) return -1;
+	CPLC* pPLC = new CPLC(pszName, pszIp, port);
+	m_mapPlc[pszName] = pPLC;
+
+	notifyPtr(RX_CODE_ADD_PLC, pPLC);
+	return 0;
+}
+
+int CModel::removePlc(const char* pszName)
+{
+	auto iter = m_mapPlc.find(pszName);
+	if (iter == m_mapPlc.end()) return -1;
+
+	notifyPtr(RX_CODE_REMOVE_PLC, iter->second);
+	delete iter->second;
+	m_mapPlc.erase(iter);
+	return 0;
+}
diff --git a/SourceCode/Bond/BoounionPLC/Model.h b/SourceCode/Bond/BoounionPLC/Model.h
index b4a4992..5ea3814 100644
--- a/SourceCode/Bond/BoounionPLC/Model.h
+++ b/SourceCode/Bond/BoounionPLC/Model.h
@@ -1,5 +1,7 @@
 #pragma once
 #include "Configuration.h"
+#include "PLC.h"
+#include <map>
 
 
 class CModel
@@ -14,6 +16,9 @@
 	int init();
 	int term();
 	void onTimer(UINT nTimerid);
+	std::map<std::string, CPLC*>& gtPlcMap();
+	int addPlc(const char* pszName, const char* pszIp, const unsigned int port);
+	int removePlc(const char* pszName);
 
 public:
 	int notify(int code);
@@ -37,6 +42,7 @@
 	IObservable* m_pObservable;
 	IObservableEmitter* m_pObservableEmitter;
 	CString m_strWorkDir;
+	std::map<std::string, CPLC*> m_mapPlc;
 
 private:
 	int m_nTimerID;
diff --git a/SourceCode/Bond/BoounionPLC/PLC.cpp b/SourceCode/Bond/BoounionPLC/PLC.cpp
new file mode 100644
index 0000000..153d52a
--- /dev/null
+++ b/SourceCode/Bond/BoounionPLC/PLC.cpp
@@ -0,0 +1,33 @@
+#include "stdafx.h"
+#include "PLC.h"
+
+
+CPLC::CPLC()
+{
+}
+
+CPLC::CPLC(const char* pszName, const char* pszIp, const unsigned int port)
+{
+	m_strName = pszName;
+	m_strIp = pszIp;
+	m_nPort = port;
+}
+
+CPLC::~CPLC()
+{
+}
+
+std::string& CPLC::getName()
+{
+	return m_strName;
+}
+
+std::string& CPLC::getIp()
+{
+	return m_strIp;
+}
+
+unsigned int CPLC::getPort()
+{
+	return m_nPort;
+}
diff --git a/SourceCode/Bond/BoounionPLC/PLC.h b/SourceCode/Bond/BoounionPLC/PLC.h
new file mode 100644
index 0000000..7922f2f
--- /dev/null
+++ b/SourceCode/Bond/BoounionPLC/PLC.h
@@ -0,0 +1,21 @@
+#pragma once
+#include <string>
+
+class CPLC
+{
+public:
+	CPLC();
+	CPLC(const char* pszName, const char* pszIp, const unsigned int port);
+	~CPLC();
+
+public:
+	std::string& getName();
+	std::string& getIp();
+	unsigned int getPort();
+
+private:
+	std::string m_strName;
+	std::string m_strIp;
+	unsigned int m_nPort;
+};
+
diff --git a/SourceCode/Bond/BoounionPLC/PagePlcList.cpp b/SourceCode/Bond/BoounionPLC/PagePlcList.cpp
index 0f3a48c..87a3ef7 100644
--- a/SourceCode/Bond/BoounionPLC/PagePlcList.cpp
+++ b/SourceCode/Bond/BoounionPLC/PagePlcList.cpp
@@ -17,6 +17,7 @@
 {
 	m_crBkgnd = PAGE_PLC_LIST_BACKGROUND;
 	m_hbrBkgnd = nullptr;
+	m_pObserver = nullptr;
 }
 
 CPagePlcList::~CPagePlcList()
@@ -40,9 +41,51 @@
 // CPagePlcList 消息处理程序
 
 
+void CPagePlcList::InitRxWindows()
+{
+	/* code */
+	// 订阅数据
+	IRxWindows* pRxWindows = RX_GetRxWindows();
+	pRxWindows->enableLog(5);
+	if (m_pObserver == NULL) {
+		m_pObserver = pRxWindows->allocObserver([&](IAny* pAny) -> void {
+			// onNext
+			pAny->addRef();
+			int code = pAny->getCode();
+			if (RX_CODE_ADD_PLC == code) {
+				CPLC* pPlc;
+				if (pAny->getPtrValue("ptr", (void*&)pPlc)) {
+					HTREEITEM hItem = m_treeCtrl.InsertItem(pPlc->getName().c_str(), nullptr, nullptr);
+					m_treeCtrl.SetItemData(hItem, (DWORD_PTR)pPlc);
+				}
+			}
+			else if (RX_CODE_REMOVE_PLC == code) {
+				CPLC* pPlc;
+				if (pAny->getPtrValue("ptr", (void*&)pPlc)) {
+					HTREEITEM hItem = FindItem(pPlc);
+					if (hItem != nullptr) {
+						m_treeCtrl.DeleteItem(hItem);
+					}
+				}
+			}
+
+			pAny->release();
+		}, [&]() -> void {
+			// onComplete
+		}, [&](IThrowable* pThrowable) -> void {
+			// onErrorm
+			pThrowable->printf();
+		});
+
+		theApp.m_model.getObservable()->observeOn(pRxWindows->mainThread())
+			->subscribe(m_pObserver);
+	}
+}
+
 BOOL CPagePlcList::OnInitDialog()
 {
 	CDialogEx::OnInitDialog();
+	InitRxWindows();
 
 
 	m_treeCtrl.SetBkColor(PAGE_PLC_LIST_BACKGROUND);
@@ -74,7 +117,10 @@
 {
 	CDialogEx::OnDestroy();
 
-	// TODO: 在此处添加消息处理程序代码
+	if (m_pObserver != nullptr) {
+		m_pObserver->unsubscribe();
+		m_pObserver = NULL;
+	}
 }
 
 void CPagePlcList::OnSize(UINT nType, int cx, int cy)
@@ -92,8 +138,8 @@
 
 void CPagePlcList::ReadPLCList()
 {
-	m_treeCtrl.InsertItem("PLC1", nullptr, nullptr);
-	m_treeCtrl.InsertItem("PLC2", nullptr, nullptr);
+	//m_treeCtrl.InsertItem("PLC1", nullptr, nullptr);
+	//m_treeCtrl.InsertItem("PLC2", nullptr, nullptr);
 }
 
 BOOL CPagePlcList::PreTranslateMessage(MSG* pMsg)
@@ -104,3 +150,17 @@
 
 	return CDialogEx::PreTranslateMessage(pMsg);
 }
+
+HTREEITEM CPagePlcList::FindItem(CPLC* pPlc)
+{
+
+	HTREEITEM item = m_treeCtrl.GetChildItem(nullptr);
+	while (item != nullptr) {
+		item = m_treeCtrl.GetNextItem(item, TVGN_NEXT);
+		if (m_treeCtrl.GetItemData(item) == (DWORD_PTR)pPlc) {
+			return item;
+		}
+	}
+
+	return nullptr;
+}
\ No newline at end of file
diff --git a/SourceCode/Bond/BoounionPLC/PagePlcList.h b/SourceCode/Bond/BoounionPLC/PagePlcList.h
index d243892..baeff62 100644
--- a/SourceCode/Bond/BoounionPLC/PagePlcList.h
+++ b/SourceCode/Bond/BoounionPLC/PagePlcList.h
@@ -14,10 +14,13 @@
 
 
 private:
+	void InitRxWindows();
 	void ReadPLCList();
+	HTREEITEM FindItem(CPLC* pPlc);
 
 
 private:
+	IObserver* m_pObserver;
 	CApredTreeCtrl m_treeCtrl;
 	COLORREF m_crBkgnd;
 	HBRUSH m_hbrBkgnd;

--
Gitblit v1.9.3