mrDarker
6 天以前 829fe6c6bc33d53fda9c31fd45a37e1df87befff
SourceCode/Bond/Servo/PageRecipe.cpp
@@ -6,7 +6,10 @@
#include "afxdialogex.h"
#include "PageRecipe.h"
#include "MsgDlg.h"
#include "InputDialog.h"
#include "RecipeDeviceBindDlg.h"
#include "DeviceRecipeParamDlg.h"
// CPageRecipe 对话框
@@ -20,6 +23,53 @@
CPageRecipe::~CPageRecipe()
{
}
void CPageRecipe::InitListCtrlHeaderForMaster()
{
   m_listPPID.DeleteAllItems();
   while (m_listPPID.DeleteColumn(0));
   CString strIniFile, strItem;
   strIniFile.Format(_T("%s\\configuration.ini"), (LPTSTR)(LPCTSTR)theApp.m_strAppDir);
   int width[12] = { 0, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 180 };
   for (int i = 0; i < 12; i++) {
      strItem.Format(_T("Col_%d_Width"), i);
      width[i] = GetPrivateProfileInt("PageRecipeListCtrl", strItem, width[i], strIniFile);
   }
   m_listPPID.InsertColumn(0, _T(""), LVCFMT_RIGHT, 0); // 隐藏列
   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]);
   m_listPPID.InsertColumn(5, _T("Bonder2"), LVCFMT_LEFT, width[5]);
   m_listPPID.InsertColumn(6, _T("后烘冷却"), LVCFMT_LEFT, width[7]);
   m_listPPID.InsertColumn(7, _T("精度检查"), LVCFMT_LEFT, width[8]);
   m_listPPID.InsertColumn(8, _T("EFEM"), LVCFMT_LEFT, width[3]);
   m_listPPID.InsertColumn(9, _T("创建时间"), LVCFMT_LEFT, width[9]);
   m_listPPID.InsertColumn(10, _T("描述"), LVCFMT_LEFT, width[10]);
}
void CPageRecipe::InitListCtrlHeaderForDevice()
{
   m_listPPID.DeleteAllItems();
   while (m_listPPID.DeleteColumn(0));
   CString strIniFile, strItem;
   strIniFile.Format(_T("%s\\configuration.ini"), (LPCTSTR)theApp.m_strAppDir);
   int width[] = { 0, 60, 100, 100, 150 };
   for (int i = 0; i < 5; i++) {
      strItem.Format(_T("Col_Device_%d_Width"), i);
      width[i] = GetPrivateProfileInt(_T("PageRecipeListCtrl"), strItem, width[i], strIniFile);
   }
   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_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)
@@ -88,9 +138,12 @@
   // 遍历数据并插入到CListCtrl中
   for (int i = 0; i < static_cast<int>(vecRecipe.size()); ++i) {
      const RecipeInfo& recipe = vecRecipe[i];
      // 原程序要求PPID有子配方,先注释
      /*
      if (recipe.vecDeviceList.empty() || recipe.vecDeviceList.size() > 6){
         continue;
      }
      */
      m_listPPID.InsertItem(i, _T("")); // 第0列空白
@@ -100,8 +153,18 @@
      m_listPPID.SetItemText(i, 2, CA2T(recipe.strPPID.c_str()));
      for (int j = 0; j < recipe.vecDeviceList.size(); j++){
         int nRecipeID = recipe.vecDeviceList.at(j).nRecipeID;
         std::string strDeviceName = recipe.vecDeviceList.at(j).strDeviceName;
         std::string strRecipeName = RecipeManager::getInstance().getDeviceRecipeName(strDeviceName, nRecipeID);
         CString str;
         str.Format(_T("%d"), recipe.vecDeviceList.at(j).nRecipeID);
         if (strRecipeName.empty()) {
            str.Format(_T("%d"), recipe.vecDeviceList.at(j).nRecipeID);
         }
         else {
            str.Format(_T("%s"), strRecipeName.c_str());
         }
         m_listPPID.SetItemText(i, j + 3, str);
      }
@@ -114,8 +177,9 @@
   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;
@@ -123,16 +187,33 @@
   // 清空当前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(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(SanitizeName(pEq->getName()), item.second, strRecipeName, strDescription);
      }
   }
   // 获取列数
@@ -157,7 +238,8 @@
   ON_BN_CLICKED(IDC_BUTTON_DELETE, &CPageRecipe::OnBnClickedButtonDelete)
   ON_BN_CLICKED(IDC_BUTTON_DELETE_ALL, &CPageRecipe::OnBnClickedButtonDeleteAll)
   ON_BN_CLICKED(IDC_BUTTON_REFRESH, &CPageRecipe::OnBnClickedButtonRefresh)
   ON_NOTIFY(LVN_ITEMCHANGED, IDC_LIST_PPID, &CPageRecipe::OnLvnItemChangedListPPID)
   ON_NOTIFY(NM_CLICK, IDC_LIST_PPID, &CPageRecipe::OnClickListPPID)
   ON_NOTIFY(NM_DBLCLK, IDC_LIST_PPID, &CPageRecipe::OnDblclkListPPID)
   ON_CBN_SELCHANGE(IDC_COMBO_EQUIPMENT, &CPageRecipe::OnCbnSelchangeComboEquipment)
END_MESSAGE_MAP()
@@ -168,15 +250,6 @@
{
   CDialogEx::OnInitDialog();
   // 读出列宽
   CString strIniFile, strItem;
   strIniFile.Format(_T("%s\\configuration.ini"), (LPTSTR)(LPCTSTR)theApp.m_strAppDir);
   int width[12] = { 0, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 180 };
   for (int i = 0; i < 12; i++) {
      strItem.Format(_T("Col_%d_Width"), i);
      width[i] = GetPrivateProfileInt("PageRecipeListCtrl", strItem, width[i], strIniFile);
   }
   // TODO:  在此添加额外的初始化
   CListCtrl* pListCtrl = (CListCtrl*)GetDlgItem(IDC_LIST_PPID);
   DWORD dwStyle = pListCtrl->GetExtendedStyle();
@@ -186,18 +259,7 @@
   HIMAGELIST imageList = ImageList_Create(24, 24, ILC_COLOR24, 1, 1);
   ListView_SetImageList(pListCtrl->GetSafeHwnd(), imageList, LVSIL_SMALL);
   pListCtrl->InsertColumn(0, _T(""), LVCFMT_RIGHT, 0); // 隐藏列
   pListCtrl->InsertColumn(1, _T("No."), LVCFMT_LEFT, width[1]);
   pListCtrl->InsertColumn(2, _T("PPID/Recipe ID"), LVCFMT_LEFT, width[2]);
   pListCtrl->InsertColumn(3, _T("真空烘烤"), LVCFMT_LEFT, width[6]);
   pListCtrl->InsertColumn(4, _T("Bonder1"), LVCFMT_LEFT, width[4]);
   pListCtrl->InsertColumn(5, _T("Bonder2"), LVCFMT_LEFT, width[5]);
   pListCtrl->InsertColumn(6, _T("后烘冷却"), LVCFMT_LEFT, width[7]);
   pListCtrl->InsertColumn(7, _T("精度检查"), LVCFMT_LEFT, width[8]);
   pListCtrl->InsertColumn(8, _T("EFEM"), LVCFMT_LEFT, width[3]);
   pListCtrl->InsertColumn(9, _T("创建时间"), LVCFMT_LEFT, width[9]);
   pListCtrl->InsertColumn(10, _T("描述"), LVCFMT_LEFT, width[10]);
   pListCtrl->SetColumnWidth(10, LVSCW_AUTOSIZE_USEHEADER);
   InitListCtrlHeaderForMaster();
   // 获取所有数据
   auto vecData = RecipeManager::getInstance().getAllRecipes();
@@ -255,19 +317,37 @@
void CPageRecipe::OnDestroy()
{
   CDialogEx::OnDestroy();
   CComboBox* pComboBox = (CComboBox*)GetDlgItem(IDC_COMBO_EQUIPMENT);
   int nSel = pComboBox->GetCurSel();
   SERVO::CEquipment* pEq = (SERVO::CEquipment*)pComboBox->GetItemDataPtr(nSel);
   // 保存列宽
   CString strIniFile, strItem, strTemp;
   strIniFile.Format(_T("%s\\configuration.ini"), (LPTSTR)(LPCTSTR)theApp.m_strAppDir);
   strIniFile.Format(_T("%s\\configuration.ini"), (LPCTSTR)theApp.m_strAppDir);
   CHeaderCtrl* pHeader = m_listPPID.GetHeaderCtrl();
   if (!pHeader || pHeader->GetItemCount() == 0) {
      return;
   }
   for (int i = 0; i < pHeader->GetItemCount(); i++) {
      RECT rect;
      pHeader->GetItemRect(i, &rect);
      strItem.Format(_T("Col_%d_Width"), i);
      if (!pHeader->GetItemRect(i, &rect)) {
         continue;
      }
      if (pEq == nullptr) {
         strItem.Format(_T("Col_%d_Width"), i);
      }
      else {
         strItem.Format(_T("Col_Device_%d_Width"), i);
      }
      strTemp.Format(_T("%d"), rect.right - rect.left);
      WritePrivateProfileString("PageRecipeListCtrl", strItem, strTemp, strIniFile);
      WritePrivateProfileString(_T("PageRecipeListCtrl"), strItem, strTemp, strIniFile);
   }
   CDialogEx::OnDestroy();
}
void CPageRecipe::OnShowWindow(BOOL bShow, UINT nStatus)
@@ -293,6 +373,14 @@
            pComboBox->InsertString(i,
               pEq[i] == nullptr ? _T("Master") : pEq[i]->getName().c_str());
            pComboBox->SetItemDataPtr(i, pEq[i]);
            // 读取回来
            char szBuffer[_MAX_PATH];
            if (pEq[i]) {
               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);
      }
@@ -301,13 +389,12 @@
void CPageRecipe::OnBnClickedButtonNew()
{
   // TODO: 在此添加控件通知处理程序代码
   //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) {
@@ -367,20 +454,60 @@
void CPageRecipe::OnBnClickedButtonModify()
{
   int rc = UX_CanExecute(L"recipe");
   if (rc != 1) {
      AfxMessageBox("操作权限不足,请联系管理人员!");
      return;
   }
   UX_RecordAction(L"recipe");
   // TODO: 在此添加控件通知处理程序代码
   CComboBox* pComboBox = (CComboBox*)GetDlgItem(IDC_COMBO_EQUIPMENT);
   if (pComboBox == nullptr || !::IsWindow(pComboBox->m_hWnd)) {
      return;
   }
   POSITION pos = m_listPPID.GetFirstSelectedItemPosition();
   if (!pos) {
      AfxMessageBox(_T("请先选择一条配方记录进行修改!"));
      return;
   }
   int nSel = m_listPPID.GetNextSelectedItem(pos);
   CString strPPID = m_listPPID.GetItemText(nSel, 2);
   UpdateRecipeByPPID(strPPID);
   int nLine = m_listPPID.GetNextSelectedItem(pos);
   CString strID = m_listPPID.GetItemText(nLine, 2);
   int nSel = pComboBox->GetCurSel();
   SERVO::CEquipment* pEq = (SERVO::CEquipment*)pComboBox->GetItemDataPtr(nSel);
   if (pEq == nullptr) {
      UpdateRecipeByPPID(strID);
   }
   else {
      CInputDialog dlg(_T("修改配方名称"), _T("请输入配方名称:"));
      if (dlg.DoModal() != IDOK) {
         return;
      }
      CString strText = dlg.GetInputText();
      if (strText.IsEmpty()) {
         AfxMessageBox(_T("配方名称不能为空!"));
         return;
      }
      if (RecipeManager::getInstance().updateDeviceRecipeName(SanitizeName(pEq->getName()), _ttoi(strID), std::string(CT2A(strText)))) {
         m_listPPID.SetItemText(nLine, 3, strText);
      }
   }
}
void CPageRecipe::OnBnClickedButtonDelete()
{
   int rc = UX_CanExecute(L"recipe");
   if (rc != 1) {
      AfxMessageBox("操作权限不足,请联系管理人员!");
      return;
   }
   UX_RecordAction(L"recipe");
   // TODO: 在此添加控件通知处理程序代码
   POSITION pos = m_listPPID.GetFirstSelectedItemPosition();
   if (!pos) { 
@@ -407,6 +534,13 @@
void CPageRecipe::OnBnClickedButtonDeleteAll()
{
   int rc = UX_CanExecute(L"recipe");
   if (rc != 1) {
      AfxMessageBox("操作权限不足,请联系管理人员!");
      return;
   }
   UX_RecordAction(L"recipe");
   // TODO: 在此添加控件通知处理程序代码
   if (IDYES != AfxMessageBox(_T("确定要删除全部配方记录吗?"), MB_YESNO | MB_ICONWARNING)) {
      return;
@@ -438,67 +572,242 @@
      FillDataToListCtrl(vecData);
   }
   else {
      // enable port
      CMsgDlg msgDlg("请等待", "正在获取配方...");
      pEq->masterRecipeListRequest(0, [&](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);
         }
      });
      // 获取配方列表
      CMsgDlg msgDlg("请等待", "正在获取配方列表...");
      msgDlg.SetData((DWORD_PTR)this);
      msgDlg.SetDataEx((DWORD_PTR)pEq);
      msgDlg.BeginThread(SyncThreadFunction);
      msgDlg.DoModal();
   }
}
void CPageRecipe::OnLvnItemChangedListPPID(NMHDR* pNMHDR, LRESULT* pResult)
void CPageRecipe::OnClickListPPID(NMHDR* pNMHDR, LRESULT* pResult)
{
   LPNMLISTVIEW pNMLV = reinterpret_cast<LPNMLISTVIEW>(pNMHDR);
   LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast<LPNMITEMACTIVATE>(pNMHDR);
   // TODO: 在此添加控件通知处理程序代码
   *pResult = 0;
   CComboBox* pComboBox = (CComboBox*)GetDlgItem(IDC_COMBO_EQUIPMENT);
   int nEqSel = pComboBox->GetCurSel();
   int selectedCount = ListView_GetSelectedCount(m_listPPID.GetSafeHwnd());
   if (pComboBox == nullptr) {
      return;
   }
   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);
   int nItem = pNMItemActivate->iItem;
   int nEqSel = pComboBox->GetCurSel();
   GetDlgItem(IDC_BUTTON_NEW)->EnableWindow(nEqSel == 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)
{
   LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast<LPNMITEMACTIVATE>(pNMHDR);
   // TODO: 在此添加控件通知处理程序代码
   *pResult = 0;
   int nItem = pNMItemActivate->iItem;
   if (nItem < 0) {
      return;
   }
   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_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);
   SERVO::CEquipment* pEq = (SERVO::CEquipment*)pComboBox->GetItemDataPtr(nEqSel);
   if (pEq == nullptr) {
      InitListCtrlHeaderForMaster();
      auto vecData = RecipeManager::getInstance().getAllRecipes();
      FillDataToListCtrl(vecData);
   }
   else {
      SERVO::CRecipeList* pRecipeList = pEq->getRecipeList(0);
      FillRecipeListToListCtrl(pRecipeList);
      InitListCtrlHeaderForDevice();
      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("正在获取参数"));
      for (auto item : ids) {
         int recipeId = item.second;
         CString strMsg;
         strMsg.Format(_T("正在获取配方 %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);
}