From ecd10e09a6d85b77204814b813c62691a9deec39 Mon Sep 17 00:00:00 2001
From: mrDarker <mr.darker@163.com>
Date: 星期五, 06 六月 2025 17:58:14 +0800
Subject: [PATCH] 1. 通过文件读取多个设备的信号,并显示在界面

---
 SourceCode/Bond/Servo/CEquipmentPage1.cpp |  197 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 196 insertions(+), 1 deletions(-)

diff --git a/SourceCode/Bond/Servo/CEquipmentPage1.cpp b/SourceCode/Bond/Servo/CEquipmentPage1.cpp
index a6a80f4..f410b17 100644
--- a/SourceCode/Bond/Servo/CEquipmentPage1.cpp
+++ b/SourceCode/Bond/Servo/CEquipmentPage1.cpp
@@ -9,12 +9,17 @@
 
 // CEquipmentPage1 瀵硅瘽妗�
 
+#define SIGNAL_GRID_ROWS 8
+#define SIGNAL_GRID_COLS 8
+#define SIGNAL_GRID_SIZE (SIGNAL_GRID_ROWS * SIGNAL_GRID_COLS)
+
 IMPLEMENT_DYNAMIC(CEquipmentPage1, CHMPropertyPage)
 
 CEquipmentPage1::CEquipmentPage1(CWnd* pParent /*=nullptr*/)
 	: CHMPropertyPage(IDD_PAGE_EQUIPMENT1, pParent)
 {
 	m_pEquipment = nullptr;
+	m_nCurrentDeviceID = 0;
 }
 
 CEquipmentPage1::~CEquipmentPage1()
@@ -28,6 +33,9 @@
 
 
 BEGIN_MESSAGE_MAP(CEquipmentPage1, CHMPropertyPage)
+	ON_WM_CTLCOLOR()
+	ON_WM_DESTROY()
+	ON_WM_SIZE()
 END_MESSAGE_MAP()
 
 
@@ -35,12 +43,142 @@
 void CEquipmentPage1::OnApply()
 {
 	__super::OnApply();
-	AfxMessageBox("CEquipmentPage1::OnApply()");
 }
 
 void CEquipmentPage1::setEquipment(SERVO::CEquipment* pEquipment)
 {
 	m_pEquipment = pEquipment;
+
+	if (m_pEquipment != nullptr) {
+		InitSignalListForDevice(pEquipment->getID());
+		LoadSignalPanelUI();
+	}
+	else {
+		m_nCurrentDeviceID = 0;
+		m_vSignalList.clear();
+		m_ctrlSignalPanel.ClearAll();
+	}
+}
+
+void CEquipmentPage1::LoadSignalPanelUI()
+{
+	if (!::IsWindow(m_ctrlSignalPanel.GetSafeHwnd())) {
+		return;
+	}
+
+	for (int i = 0; i < SIGNAL_GRID_SIZE; ++i) {
+		int nRow = i / SIGNAL_GRID_COLS;
+		int nCol = i % SIGNAL_GRID_COLS;
+
+		const auto& signal = m_vSignalList[i];
+		m_ctrlSignalPanel.SetSlotText(nRow, nCol, signal.strName);
+		m_ctrlSignalPanel.SetSlotClickable(nRow, nCol, signal.bClickable);
+	}
+}
+
+void CEquipmentPage1::LoadSignalListFromCSV(const CString& strFilePath)
+{
+	m_mapSignalListByID.clear();
+
+	CStdioFile file;
+	if (!file.Open(strFilePath, CFile::modeRead | CFile::typeText)) {
+		AfxMessageBox(_T("鏃犳硶鎵撳紑淇″彿瀹氫箟鏂囦欢: ") + strFilePath);
+		return;
+	}
+
+	CString strLine;
+	int nLineNum = 0;
+
+	while (file.ReadString(strLine)) {
+		++nLineNum;
+		strLine.Trim();
+		if (strLine.IsEmpty() || strLine.Left(1) == _T("#") || strLine.Left(2) == _T("//")) {
+			continue; // 璺宠繃娉ㄩ噴琛屾垨绌鸿
+		}
+
+		// 鍒嗗壊涓� tokens
+		std::vector<CString> tokens;
+		int curPos = 0;
+		CString token = strLine.Tokenize(_T(","), curPos);
+		while (!token.IsEmpty()) {
+			token.Trim(); // 鍘婚櫎姣忎釜瀛楁鐨勭┖鏍�
+			tokens.push_back(token);
+			token = strLine.Tokenize(_T(","), curPos);
+		}
+
+		// 鏍煎紡妫�鏌�
+		if (tokens.size() < 3) {
+			TRACE(_T("CSV 鏍煎紡閿欒 [琛� %d]锛氬瓧娈垫暟涓嶈冻銆俓n"), nLineNum);
+			continue;
+		}
+
+		// 瑙f瀽瀛楁
+		int nDeviceID = _ttoi(tokens[0]);
+		CString strSignalName = tokens[1];
+		bool bClickable = _ttoi(tokens[2]) ? TRUE : FALSE;
+
+		if (nDeviceID <= 0 || strSignalName.IsEmpty()) {
+			TRACE(_T("CSV 鏁版嵁鏃犳晥 [琛� %d]锛氳澶嘔D鎴栦俊鍙峰悕涓虹┖銆俓n"), nLineNum);
+			continue;
+		}
+
+		SignalInfo info = { strSignalName, false, bClickable };
+		m_mapSignalListByID[nDeviceID].push_back(info);
+	}
+
+	file.Close();
+	TRACE(_T("淇″彿瀹氫箟鍔犺浇瀹屾垚锛屽叡 %d 涓澶囥�俓n"), (int)m_mapSignalListByID.size());
+}
+
+void CEquipmentPage1::InitSignalListForDevice(int nDeviceID)
+{
+	m_nCurrentDeviceID = nDeviceID;
+	m_vSignalList.clear();
+
+	auto it = m_mapSignalListByID.find(nDeviceID);
+	if (it != m_mapSignalListByID.end()) {
+		m_vSignalList = it->second;
+		UpdateAllSignalStatesFromDevice();
+	}
+	else {
+		TRACE(_T("Warning: No signals found for DeviceID=%d\n"), nDeviceID);
+	}
+
+	// 琛ラ綈鍒� 64 椤�
+	while (m_vSignalList.size() < SIGNAL_GRID_SIZE) {
+		m_vSignalList.push_back({ _T(""), false, false });
+	}
+}
+
+void CEquipmentPage1::UpdateSignalState(int nRow, int nCol, bool bNewState)
+{
+	if (!::IsWindow(m_ctrlSignalPanel.GetSafeHwnd())) {
+		return;
+	}
+
+	int nIndex = nRow * SIGNAL_GRID_COLS + nCol;
+	if (nIndex < 0 || nIndex >= static_cast<int>(m_vSignalList.size())) {
+		return;
+	}
+
+	m_vSignalList[nIndex].bCurrentState = bNewState;
+	m_ctrlSignalPanel.SetSlotStatus(nRow, nCol, bNewState);
+
+	TRACE(_T("UpdateSignalState: [%d, %d] = %d\n"), nRow, nCol, bNewState);
+}
+
+void CEquipmentPage1::UpdateAllSignalStatesFromDevice()
+{
+	if (!m_pEquipment) {
+		return;
+	}
+
+	for (int nRow = 0; nRow < SIGNAL_GRID_ROWS; ++nRow) {
+		for (int nCol = 0; nCol < SIGNAL_GRID_COLS; ++nCol) {
+			BOOL bCurrentState = m_pEquipment->isLinkSignalOn(nRow, nCol);
+			UpdateSignalState(nRow, nCol, bCurrentState);
+		}
+	}
 }
 
 BOOL CEquipmentPage1::OnInitDialog()
@@ -48,7 +186,64 @@
 	CHMPropertyPage::OnInitDialog();
 
 	// TODO:  鍦ㄦ娣诲姞棰濆鐨勫垵濮嬪寲
+	m_ctrlSignalPanel.Create(AfxRegisterWndClass(0), _T("SignalGrid"), WS_CHILD | WS_VISIBLE, CRect(0, 0, 100, 100), this, 1002);
+	m_ctrlSignalPanel.SetColors(RGB(0, 200, 0), RGB(220, 220, 220));
+	m_ctrlSignalPanel.SetGridSize(SIGNAL_GRID_ROWS, SIGNAL_GRID_COLS);
+	m_ctrlSignalPanel.SetTextFont(_T("Microsoft YaHei"), 10);
+	m_ctrlSignalPanel.SetSlotClickCallback([this](int nRow, int nCol) {
+		int index = nRow * SIGNAL_GRID_COLS + nCol;
+		if (index >= 0 && index < (int)m_vSignalList.size() && m_vSignalList[index].bClickable && m_pEquipment != nullptr) {
+			// 璇诲彇褰撳墠鐘舵�佸苟鍒囨崲
+			const BOOL bCurrentState = m_pEquipment->isLinkSignalOn(nRow, nCol);
+			m_pEquipment->setLinkSignal(nRow, nCol, !bCurrentState);
+		}
+	});
+
+	{
+		TCHAR szPath[MAX_PATH] = {};
+		GetModuleFileName(nullptr, szPath, MAX_PATH);
+		PathRemoveFileSpec(szPath);
+		CString strCSVFile = CString(szPath) + _T("\\Config\\signals.csv");
+		LoadSignalListFromCSV(strCSVFile);
+	}
+
+	// 濡傛灉璁惧宸茶缃紝鍒欏垵濮嬪寲淇″彿鍒楄〃
+	if (m_pEquipment != nullptr) {
+		InitSignalListForDevice(m_pEquipment->getID());
+		LoadSignalPanelUI();
+	}
 
 	return TRUE;  // return TRUE unless you set the focus to a control
 				  // 寮傚父: OCX 灞炴�ч〉搴旇繑鍥� FALSE
 }
+
+HBRUSH CEquipmentPage1::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
+{
+	HBRUSH hbr = CHMPropertyPage::OnCtlColor(pDC, pWnd, nCtlColor);
+
+	// TODO:  鍦ㄦ鏇存敼 DC 鐨勪换浣曠壒鎬�
+
+	// TODO:  濡傛灉榛樿鐨勪笉鏄墍闇�鐢荤瑪锛屽垯杩斿洖鍙︿竴涓敾绗�
+	return hbr;
+}
+
+void CEquipmentPage1::OnDestroy()
+{
+	CHMPropertyPage::OnDestroy();
+
+	// TODO: 鍦ㄦ澶勬坊鍔犳秷鎭鐞嗙▼搴忎唬鐮�
+	m_ctrlSignalPanel.DestroyWindow();
+}
+
+void CEquipmentPage1::OnSize(UINT nType, int cx, int cy)
+{
+	CHMPropertyPage::OnSize(nType, cx, cy);
+
+	// TODO: 鍦ㄦ澶勬坊鍔犳秷鎭鐞嗙▼搴忎唬鐮�
+	if (::IsWindow(m_ctrlSignalPanel.GetSafeHwnd())) {
+		CRect rc;
+		GetClientRect(&rc);
+		rc.DeflateRect(10, 10);
+		m_ctrlSignalPanel.MoveWindow(rc);
+	}
+}

--
Gitblit v1.9.3