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/PagePlcList.cpp |   66 +++++++++++++++++++++++++++++++-
 1 files changed, 63 insertions(+), 3 deletions(-)

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

--
Gitblit v1.9.3