From 3cb4638bcb93a8fdf4cfea140025bbc299d35d47 Mon Sep 17 00:00:00 2001
From: chenluhua1980 <Chenluhua@qq.com>
Date: 星期二, 18 十一月 2025 17:02:07 +0800
Subject: [PATCH] 1.权限完善。操作日志等

---
 SourceCode/Bond/Servo/PageRecipe.cpp |  308 +++++++++++++++++++++++++++++++++++++++------------
 1 files changed, 235 insertions(+), 73 deletions(-)

diff --git a/SourceCode/Bond/Servo/PageRecipe.cpp b/SourceCode/Bond/Servo/PageRecipe.cpp
index bbbfe21..3965041 100644
--- a/SourceCode/Bond/Servo/PageRecipe.cpp
+++ b/SourceCode/Bond/Servo/PageRecipe.cpp
@@ -8,6 +8,8 @@
 #include "MsgDlg.h"
 #include "InputDialog.h"
 #include "RecipeDeviceBindDlg.h"
+#include "DeviceRecipeParamDlg.h"
+
 
 // CPageRecipe 瀵硅瘽妗�
 
@@ -37,7 +39,7 @@
 	}
 
 	m_listPPID.InsertColumn(0, _T(""), LVCFMT_RIGHT, 0); // 闅愯棌鍒�
-	m_listPPID.InsertColumn(1, _T("No."), LVCFMT_LEFT, width[1]);
+	m_listPPID.InsertColumn(1, _T("No."), LVCFMT_CENTER, width[1]);
 	m_listPPID.InsertColumn(2, _T("PPID"), LVCFMT_LEFT, width[2]);
 	m_listPPID.InsertColumn(3, _T("鐪熺┖鐑樼儰"), LVCFMT_LEFT, width[6]);
 	m_listPPID.InsertColumn(4, _T("Bonder1"), LVCFMT_LEFT, width[4]);
@@ -65,9 +67,9 @@
 
 	m_listPPID.InsertColumn(0, _T(""), LVCFMT_RIGHT, width[0]);
 	m_listPPID.InsertColumn(1, _T("No."), LVCFMT_CENTER, width[1]);
-	m_listPPID.InsertColumn(2, _T("Recipe ID"), LVCFMT_CENTER, width[2]);
-	m_listPPID.InsertColumn(3, _T("Recipe 鍚嶇О"), LVCFMT_CENTER, width[3]);
-	m_listPPID.InsertColumn(4, _T("Recipe 鍙傛暟"), LVCFMT_CENTER, width[4]);
+	m_listPPID.InsertColumn(2, _T("Recipe ID"), LVCFMT_LEFT, width[2]);
+	m_listPPID.InsertColumn(3, _T("Recipe 鍚嶇О"), LVCFMT_LEFT, width[3]);
+	m_listPPID.InsertColumn(4, _T("Recipe 鍙傛暟"), LVCFMT_LEFT, width[4]);
 }
 
 void CPageRecipe::UpdateRecipeByPPID(const CString& strPPID)
@@ -157,7 +159,7 @@
 				str.Format(_T("%d"), recipe.vecDeviceList.at(j).nRecipeID);
 			}
 			else {
-				str.Format(_T("%s"), CA2T(strRecipeName.c_str()));
+				str.Format(_T("%s"), strRecipeName.c_str());
 			}
 	
 			m_listPPID.SetItemText(i, j + 3, str);
@@ -172,48 +174,43 @@
 	m_listPPID.SetColumnWidth(nColCount - 1, LVSCW_AUTOSIZE_USEHEADER);
 }
 
-void CPageRecipe::FillRecipeListToListCtrl(SERVO::CRecipeList* pList)
+void CPageRecipe::FillRecipeListToListCtrl(SERVO::CEquipment* pEq)
 {
+	SERVO::CRecipeList* pRecipeList = pEq->getRecipeList(0);
 	CListCtrl* pListCtrl = (CListCtrl*)GetDlgItem(IDC_LIST_PPID);
 	if (pListCtrl == nullptr || !::IsWindow(pListCtrl->m_hWnd)) {
 		return;
 	}
 
-	CComboBox* pComboBox = (CComboBox*)GetDlgItem(IDC_COMBO_EQUIPMENT);
-	if (pComboBox == nullptr || !::IsWindow(pComboBox->m_hWnd)) {
-		return;
-	}
-
-	int nEqSel = pComboBox->GetCurSel();
-	if (nEqSel == CB_ERR) {
-		return;
-	}
-
-	auto& mgr = RecipeManager::getInstance();
-	SERVO::CEquipment* pEq = (SERVO::CEquipment*)pComboBox->GetItemDataPtr(nEqSel);
-	if (pEq == nullptr) {
-		return;
-	}
-
 	// 娓呯┖褰撳墠CListCtrl涓殑鎵�鏈夐」
 	pListCtrl->DeleteAllItems();
-	if (pList == nullptr) {
+	if (pRecipeList == nullptr) {
 		return;
 	}
 
 	// 閬嶅巻鏁版嵁骞舵彃鍏ュ埌CListCtrl涓�
-	std::map<int, short>& ids = pList->getIds();
+	auto& mgr = RecipeManager::getInstance();
+	std::map<int, short>& ids = pRecipeList->getIds();
+	auto rawDatas = pRecipeList->getParamsRawData();
 	for (auto item : ids) {
 		int index = m_listPPID.InsertItem(m_listPPID.GetItemCount(), _T(""));
 		m_listPPID.SetItemText(index, 1, std::to_string(item.first).c_str());
 		m_listPPID.SetItemText(index, 2, std::to_string(item.second).c_str());
 
-		std::string strRecipeName = mgr.getDeviceRecipeName(pEq->getName(), item.second);
+		std::string strRecipeName = mgr.getDeviceRecipeName(SanitizeName(pEq->getName()), item.second);
+		m_listPPID.SetItemText(index, 3, strRecipeName.c_str());
+
+		std::string strDescription;
+		auto iter = rawDatas.find(item.second);
+		if (iter != rawDatas.end()) {
+			pEq->parsingParams((const char*)iter->second.data(), iter->second.size(), strDescription);
+			m_listPPID.SetItemText(index, 4, strDescription.c_str());
+		}
+
 		if (strRecipeName.empty()) {
 			strRecipeName = std::to_string(item.second);
-			mgr.addDeviceRecipe(pEq->getName(), item.second, strRecipeName);
+			mgr.addDeviceRecipe(SanitizeName(pEq->getName()), item.second, strRecipeName, strDescription);
 		}
-		m_listPPID.SetItemText(index, 3, strRecipeName.c_str());
 	}
 
 	// 鑾峰彇鍒楁暟
@@ -374,8 +371,12 @@
 					pEq[i] == nullptr ? _T("Master") : pEq[i]->getName().c_str());
 				pComboBox->SetItemDataPtr(i, pEq[i]);
 
+				// 璇诲彇鍥炴潵
+				char szBuffer[_MAX_PATH];
 				if (pEq[i]) {
-					pEq[i]->masterRecipeListRequest(0, nullptr);
+					sprintf_s(szBuffer, _MAX_PATH, "%s\\Recipe\\EQ%d_Unit0.recipelist", (LPTSTR)(LPCTSTR)theApp.m_strAppDir, pEq[i]->getID());
+					std::string strFilepath(szBuffer);
+					pEq[i]->readRecipeList(0, strFilepath);
 				}
 			}
 			pComboBox->SetCurSel(0);
@@ -385,13 +386,12 @@
 
 void CPageRecipe::OnBnClickedButtonNew()
 {
-	// TODO: 鍦ㄦ娣诲姞鎺т欢閫氱煡澶勭悊绋嬪簭浠g爜
-	//CComboBox* pComboBox = (CComboBox*)GetDlgItem(IDC_COMBO_EQUIPMENT);
-	//int nSel = pComboBox->GetCurSel();
-	//SERVO::CEquipment* pEq = (SERVO::CEquipment*)pComboBox->GetItemDataPtr(nSel);
-	//if (pEq == nullptr) {
-	//	return;
-	//}
+	int rc = UX_CanExecute(L"recipe");
+	if (rc != 1) {
+		AfxMessageBox("鎿嶄綔鏉冮檺涓嶈冻锛岃鑱旂郴绠$悊浜哄憳锛�");
+		return;
+	}
+	UX_RecordAction(L"recipe");
 
 	CRecipeDeviceBindDlg dlg(this);
 	if (dlg.DoModal() == IDOK) {
@@ -451,6 +451,13 @@
 
 void CPageRecipe::OnBnClickedButtonModify()
 {
+	int rc = UX_CanExecute(L"recipe");
+	if (rc != 1) {
+		AfxMessageBox("鎿嶄綔鏉冮檺涓嶈冻锛岃鑱旂郴绠$悊浜哄憳锛�");
+		return;
+	}
+	UX_RecordAction(L"recipe");
+
 	// TODO: 鍦ㄦ娣诲姞鎺т欢閫氱煡澶勭悊绋嬪簭浠g爜
 	CComboBox* pComboBox = (CComboBox*)GetDlgItem(IDC_COMBO_EQUIPMENT);
 	if (pComboBox == nullptr || !::IsWindow(pComboBox->m_hWnd)) {
@@ -482,7 +489,8 @@
 			AfxMessageBox(_T("閰嶆柟鍚嶇О涓嶈兘涓虹┖锛�"));
 			return;
 		}
-		if (RecipeManager::getInstance().updateDeviceRecipe(pEq->getName(), _ttoi(strID), std::string(CT2A(strText)))) {
+
+		if (RecipeManager::getInstance().updateDeviceRecipeName(SanitizeName(pEq->getName()), _ttoi(strID), std::string(CT2A(strText)))) {
 			m_listPPID.SetItemText(nLine, 3, strText);
 		}
 	}
@@ -490,6 +498,13 @@
 
 void CPageRecipe::OnBnClickedButtonDelete()
 {
+	int rc = UX_CanExecute(L"recipe");
+	if (rc != 1) {
+		AfxMessageBox("鎿嶄綔鏉冮檺涓嶈冻锛岃鑱旂郴绠$悊浜哄憳锛�");
+		return;
+	}
+	UX_RecordAction(L"recipe");
+
 	// TODO: 鍦ㄦ娣诲姞鎺т欢閫氱煡澶勭悊绋嬪簭浠g爜
 	POSITION pos = m_listPPID.GetFirstSelectedItemPosition();
 	if (!pos) { 
@@ -516,6 +531,13 @@
 
 void CPageRecipe::OnBnClickedButtonDeleteAll()
 {
+	int rc = UX_CanExecute(L"recipe");
+	if (rc != 1) {
+		AfxMessageBox("鎿嶄綔鏉冮檺涓嶈冻锛岃鑱旂郴绠$悊浜哄憳锛�");
+		return;
+	}
+	UX_RecordAction(L"recipe");
+
 	// TODO: 鍦ㄦ娣诲姞鎺т欢閫氱煡澶勭悊绋嬪簭浠g爜
 	if (IDYES != AfxMessageBox(_T("纭畾瑕佸垹闄ゅ叏閮ㄩ厤鏂硅褰曞悧锛�"), MB_YESNO | MB_ICONWARNING)) {
 		return;
@@ -547,32 +569,11 @@
 		FillDataToListCtrl(vecData);
 	}
 	else {
-		// enable port
-		CMsgDlg msgDlg("璇风瓑寰�", "姝e湪鑾峰彇閰嶆柟...");
-		pEq->masterRecipeListRequest(0, [&, pEq](int status) -> void {
-			if (status == SS_FAILED || status == SS_TIMEOUT) {
-				CString strMsg;
-				strMsg.Format(status == SS_FAILED ? _T("鑾峰彇閰嶆柟澶辫触锛�") : _T("鑾峰彇閰嶆柟瓒呮椂锛�"));
-				msgDlg.DelayClose(3000);
-				msgDlg.SetIcon(MSG_BOX_ERROR);
-				msgDlg.SetTitle(_T("鎿嶄綔澶辫触"));
-				msgDlg.SetMessage((LPTSTR)(LPCTSTR)strMsg);
-				msgDlg.SetMarquee(FALSE, 0);
-				msgDlg.SetCompleteCode(-1);
-			}
-			else if (status == SS_COMPLETE) {
-				CString strMsg;
-				strMsg.Format(_T("鑾峰彇閰嶆柟瀹屾垚锛�"));
-				msgDlg.DelayClose(3000);
-				msgDlg.SetIcon(MSG_BOX_SUCCEED);
-				msgDlg.SetTitle(_T("鎿嶄綔鎴愬姛"));
-				msgDlg.SetMessage((LPTSTR)(LPCTSTR)strMsg);
-				msgDlg.SetMarquee(FALSE, 0);
-				msgDlg.SetCompleteCode(0);
-				SERVO::CRecipeList* pRecipeList = pEq->getRecipeList(0);
-				FillRecipeListToListCtrl(pRecipeList);
-			}
-		});
+		// 鑾峰彇閰嶆柟鍒楄〃
+		CMsgDlg msgDlg("璇风瓑寰�", "姝e湪鑾峰彇閰嶆柟鍒楄〃...");
+		msgDlg.SetData((DWORD_PTR)this);
+		msgDlg.SetDataEx((DWORD_PTR)pEq);
+		msgDlg.BeginThread(SyncThreadFunction);
 		msgDlg.DoModal();
 	}
 }
@@ -592,9 +593,9 @@
 	int nEqSel = pComboBox->GetCurSel();
 
 	GetDlgItem(IDC_BUTTON_NEW)->EnableWindow(nEqSel == 0);
-	GetDlgItem(IDC_BUTTON_MODIFY)->EnableWindow(nItem > 0);
-	GetDlgItem(IDC_BUTTON_DELETE)->EnableWindow(nEqSel == 0 && nItem > 0);
-	GetDlgItem(IDC_BUTTON_DELETE_ALL)->EnableWindow(nEqSel == 0 && nItem > 0);
+	GetDlgItem(IDC_BUTTON_MODIFY)->EnableWindow(nItem != -1);
+	GetDlgItem(IDC_BUTTON_DELETE)->EnableWindow(nEqSel == 0 && nItem != -1);
+	GetDlgItem(IDC_BUTTON_DELETE_ALL)->EnableWindow(nEqSel == 0 && nItem != -1);
 }
 
 void CPageRecipe::OnDblclkListPPID(NMHDR* pNMHDR, LRESULT* pResult)
@@ -608,29 +609,37 @@
 		return;
 	}
 
-	CString strText = m_listPPID.GetItemText(nItem, 2);
+	CString strRecipeID = m_listPPID.GetItemText(nItem, 2);
+	CString strRecipeName = m_listPPID.GetItemText(nItem, 3);
 	CComboBox* pComboBox = (CComboBox*)GetDlgItem(IDC_COMBO_EQUIPMENT);
 	int nEqSel = pComboBox->GetCurSel();
 	if (nEqSel == CB_ERR) {
 		return;
 	}
 
+	int nRecipeID = _ttoi(strRecipeID);
 	SERVO::CEquipment* pEq = (SERVO::CEquipment*)pComboBox->GetItemDataPtr(nEqSel);
 	if (pEq == nullptr) {
 		return;
 	}
+
+	CDeviceRecipeParamDlg dlg(this);
+	dlg.setDeviceRecipeID(nRecipeID);
+	dlg.setDeviceRecipeName(strRecipeName);
+	dlg.setEquipment(pEq);
+	dlg.DoModal();
 }
 
 void CPageRecipe::OnCbnSelchangeComboEquipment()
 {
 	CComboBox* pComboBox = (CComboBox*)GetDlgItem(IDC_COMBO_EQUIPMENT);
 	int nEqSel = pComboBox->GetCurSel();
-	int selectedCount = ListView_GetSelectedCount(m_listPPID.GetSafeHwnd());
+	int nItem = ListView_GetSelectedCount(m_listPPID.GetSafeHwnd());
 
-	GetDlgItem(IDC_BUTTON_NEW)->EnableWindow(nEqSel == 0/* && selectedCount > 0*/);
-	GetDlgItem(IDC_BUTTON_MODIFY)->EnableWindow(nEqSel == 0 && selectedCount > 0);
-	GetDlgItem(IDC_BUTTON_DELETE)->EnableWindow(nEqSel == 0 && selectedCount > 0);
-	GetDlgItem(IDC_BUTTON_DELETE_ALL)->EnableWindow(nEqSel == 0 && selectedCount > 0);
+	GetDlgItem(IDC_BUTTON_NEW)->EnableWindow(nEqSel == 0);
+	GetDlgItem(IDC_BUTTON_MODIFY)->EnableWindow(nEqSel == 0 && nItem != -1);
+	GetDlgItem(IDC_BUTTON_DELETE)->EnableWindow(nEqSel == 0 && nItem != -1);
+	GetDlgItem(IDC_BUTTON_DELETE_ALL)->EnableWindow(nEqSel == 0 && nItem != -1);
 	GetDlgItem(IDC_EDIT_KEYWORD)->EnableWindow(nEqSel == 0);
 	GetDlgItem(IDC_BUTTON_SEARCH)->EnableWindow(nEqSel == 0);
 
@@ -642,7 +651,160 @@
 	}
 	else {
 		InitListCtrlHeaderForDevice();
-		SERVO::CRecipeList* pRecipeList = pEq->getRecipeList(0);
-		FillRecipeListToListCtrl(pRecipeList);
+		FillRecipeListToListCtrl(pEq);
 	}
+}
+
+UINT CPageRecipe::SyncThreadFunction(LPVOID lpvData)
+{
+	CMsgDlg* pMsgDlg = (CMsgDlg*)lpvData;
+	CPageRecipe* pPageRecipe = (CPageRecipe*)pMsgDlg->GetData();
+	return pPageRecipe->SyncThreadFunctionInner(pMsgDlg);
+}
+
+UINT CPageRecipe::SyncThreadFunctionInner(CMsgDlg* pMsgDlg)
+{
+	SERVO::CEquipment* pEq = (SERVO::CEquipment*)pMsgDlg->GetDataEx();
+	HANDLE hEvent = ::CreateEvent(NULL, TRUE, FALSE, NULL);
+	int nStep = 0;
+
+	// 鍑嗗閰嶆柟璺緞
+	char szBuffer[_MAX_PATH];
+	sprintf_s(szBuffer, _MAX_PATH, "%s\\Recipe\\EQ%d_Unit0.recipelist", (LPTSTR)(LPCTSTR)theApp.m_strAppDir, pEq->getID());
+	std::string strFilepath(szBuffer);
+
+	pEq->masterRecipeListRequest(0, [&, pEq, pMsgDlg, hEvent](int status) -> void {
+		Sleep(300);
+		if (status == SS_FAILED || status == SS_TIMEOUT) {
+			CString strMsg;
+			strMsg.Format(status == SS_FAILED ? _T("鑾峰彇閰嶆柟鍒楄〃澶辫触锛�") : _T("鑾峰彇閰嶆柟鍒楄〃瓒呮椂锛�"));
+			pMsgDlg->SetIcon(MSG_BOX_ERROR);
+			pMsgDlg->SetTitle(_T("鎿嶄綔澶辫触"));
+			pMsgDlg->SetMessage((LPTSTR)(LPCTSTR)strMsg);
+			SetEvent(hEvent);
+		}
+		else if (status == SS_LIST_COMPLETE) {
+			CString strMsg;
+			strMsg.Format(_T("鑾峰彇閰嶆柟鍒楄〃瀹屾垚锛�"));
+			pMsgDlg->SetTitle(_T("鎿嶄綔鎴愬姛"));
+			pMsgDlg->SetMessage((LPTSTR)(LPCTSTR)strMsg);
+			SERVO::CRecipeList* pRecipeList = pEq->getRecipeList(0);
+			if (pRecipeList != nullptr && !pRecipeList->getIds().empty()) {
+				nStep = 1;
+			}
+			SetEvent(hEvent);
+		}
+	});
+	::WaitForSingleObject(hEvent, INFINITE);
+	if (nStep != 1) {
+		pEq->saveRecipeList(0, strFilepath);
+		pMsgDlg->SetIcon(MSG_BOX_SUCCEED);
+		pMsgDlg->SetMarquee(FALSE, 0);
+		pMsgDlg->SetCompleteCode(-1);
+		pMsgDlg->DelayClose(3000);
+	}
+	ResetEvent(hEvent);
+
+
+	// 鍙傛暟鍒楄〃
+	if (nStep == 1) {
+		SERVO::CRecipeList* pRecipeList = pEq->getRecipeList(0);
+		ASSERT(pRecipeList);
+		auto& ids = pRecipeList->getIds();
+		pMsgDlg->SetTitle(_T("姝e湪鑾峰彇鍙傛暟"));
+		for (auto item : ids) {
+			int recipeId = item.second;
+			CString strMsg;
+			strMsg.Format(_T("姝e湪鑾峰彇閰嶆柟 %d 鍙傛暟..."), item.second);
+			pMsgDlg->SetMessage((LPTSTR)(LPCTSTR)strMsg);
+			pEq->recipeParameterRequest(0, recipeId, 0, [&, pEq, pMsgDlg, recipeId, hEvent](int status) -> void {
+				Sleep(500);
+				if (status == SS_FAILED || status == SS_TIMEOUT) {
+					CString strMsg;
+					strMsg.Format(status == SS_FAILED ? _T("鑾峰彇閰嶆柟 %d 鍙傛暟澶辫触锛�") : _T("鑾峰彇閰嶆柟 %d 鍙傛暟瓒呮椂锛�"), recipeId);
+					pMsgDlg->SetMessage((LPTSTR)(LPCTSTR)strMsg);
+
+					Sleep(30);
+					SetEvent(hEvent);
+				}
+				else if (status == SS_PARAMS_COMPLETE) {
+					CString strMsg;
+					strMsg.Format(_T("鑾峰彇閰嶆柟 %d 鍙傛暟瀹屾垚锛�"), item.second);
+					pMsgDlg->SetMessage((LPTSTR)(LPCTSTR)strMsg);
+
+					Sleep(30);
+					SetEvent(hEvent);
+				}
+				});
+			::WaitForSingleObject(hEvent, INFINITE);
+			ResetEvent(hEvent);
+		}
+		
+
+		pEq->saveRecipeList(0, strFilepath);
+		pMsgDlg->SetIcon(MSG_BOX_SUCCEED);
+		pMsgDlg->SetTitle(_T("鎿嶄綔瀹屾垚"));
+		pMsgDlg->SetCompleteCode(0);
+		pMsgDlg->SetMarquee(FALSE, 0);
+		pMsgDlg->DelayClose(3000);
+	};
+
+	FillRecipeListToListCtrl(pEq);
+	CloseHandle(hEvent);
+
+
+	// 鍦ㄦ鎵撳嵃閰嶆柟鍙傛暟浠ヤ究鏍稿鏁版嵁
+	SERVO::CRecipeList* pRecipeList = pEq->getRecipeList(0);
+	ASSERT(pRecipeList);
+	auto rawDatas = pRecipeList->getParamsRawData();
+	for (auto item : rawDatas) {
+		TRACE("================= 閰嶆柟 %d\n", item.first);
+
+		std::vector<CParam> params;
+		pEq->parsingParams((const char*)item.second.data(), item.second.size(), params);
+		for (auto p : params) {
+			if (p.getValueType() == PVT_INT) {
+				TRACE("%s: %d\n", p.getName().c_str(), p.getIntValue());
+			}
+			else if (p.getValueType() == PVT_DOUBLE) {
+				TRACE("%s: %f\n", p.getName().c_str(), p.getDoubleValue());
+			}
+		}
+
+	}
+
+	return 0;
+}
+
+std::string CPageRecipe::SanitizeName(const std::string& name)
+{
+	std::string result;
+	result.reserve(name.size());
+
+	for (char c : name) {
+		if (c == '(' || c == '锛�') {
+			break;
+		}
+
+		unsigned char uc = static_cast<unsigned char>(c);
+		if (std::isalnum(uc) || c == '_') {
+			result.push_back(c);
+		}
+		else if (std::isspace(uc)) {
+			continue;
+		}
+		else {
+			result.push_back('_');
+		}
+	}
+	return result;
+}
+
+BOOL CPageRecipe::PreTranslateMessage(MSG* pMsg)
+{
+	if (pMsg->wParam == VK_RETURN || pMsg->wParam == VK_ESCAPE) {
+		return TRUE;
+	}
+
+	return CDialogEx::PreTranslateMessage(pMsg);
 }
\ No newline at end of file

--
Gitblit v1.9.3