InsertDeviceDlg.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. // InsertDeviceDlg.cpp : 实现文件
  2. //
  3. #include "stdafx.h"
  4. #include "RunTest.h"
  5. #include "IniReader.h"
  6. #include "InsertDeviceDlg.h"
  7. #include ".\insertdevicedlg.h"
  8. // CInsertDeviceDlg 对话框
  9. IMPLEMENT_DYNAMIC(CInsertDeviceDlg, CDialog)
  10. CInsertDeviceDlg::CInsertDeviceDlg(CWnd* pParent /*=NULL*/)
  11. : CDialog(CInsertDeviceDlg::IDD, pParent)
  12. {
  13. m_bModify = FALSE;
  14. m_strAddr = "";
  15. m_strName = "";
  16. m_strPort = "";
  17. m_nStarAddr = -1;
  18. m_nEndAddr = -1;
  19. m_bMulti = FALSE;
  20. }
  21. CInsertDeviceDlg::~CInsertDeviceDlg()
  22. {
  23. }
  24. void CInsertDeviceDlg::DoDataExchange(CDataExchange* pDX)
  25. {
  26. CDialog::DoDataExchange(pDX);
  27. DDX_Control(pDX, IDC_TREE1, m_CtrlTree);
  28. DDX_Control(pDX, IDCANCLE, m_btnClose);
  29. DDX_Control(pDX, IDOK, m_btnAdd);
  30. }
  31. BEGIN_MESSAGE_MAP(CInsertDeviceDlg, CDialog)
  32. ON_BN_CLICKED(IDOK, OnBnClickedOk)
  33. ON_BN_CLICKED(IDCANCLE, OnBnClickedCancle)
  34. ON_BN_CLICKED(BTN_MODIFY, OnBnClickedModify)
  35. // ON_NOTIFY(TVN_ITEMEXPANDED, IDC_TREE1, &CInsertDeviceDlg::OnTvnItemexpandedTree1)
  36. END_MESSAGE_MAP()
  37. // CInsertDeviceDlg 消息处理程序
  38. BOOL CInsertDeviceDlg::OnInitDialog()
  39. {
  40. CDialog::OnInitDialog();
  41. InitSTButtons();
  42. //////////////////
  43. CString strPath;
  44. TCHAR szFull[_MAX_PATH];
  45. TCHAR szDrive[_MAX_DRIVE];
  46. TCHAR szDir[_MAX_DIR];
  47. ::GetModuleFileName(NULL, szFull, sizeof(szFull)/sizeof(TCHAR));
  48. _tsplitpath(szFull, szDrive, szDir, NULL, NULL);
  49. _tcscpy(szFull, szDrive);
  50. _tcscat(szFull, szDir);
  51. strPath = CString(szFull);
  52. CString strIni;
  53. strIni.Format("%s\\config.ini",strPath);
  54. CIniReader IniReader(strIni);
  55. //m_strDepotPath = IniReader.GetKeyValueText("name","DeviceLibrary");
  56. //m_strRutDepotPath = IniReader.GetKeyValueText("name","RunUnitLibrary");
  57. m_strDepotPath.Format("%s\\%s",strPath,IniReader.GetKeyValueText("name","DeviceLibrary"));
  58. m_strRutDepotPath.Format("%s\\%s",strPath,IniReader.GetKeyValueText("name","RunUnitLibrary"));
  59. // TODO: 在此添加额外的初始化
  60. // [3/29/2013 Zero.t]
  61. m_imgList.Create(IDB_BITMAP1,16, 6, RGB(255,255,255));
  62. m_CtrlTree.SetImageList(&m_imgList,TVSIL_NORMAL);
  63. hRunRootItem = m_CtrlTree.InsertItem("当前运行时设备库设备",2,2);
  64. InitTreeCtrlInfo();
  65. return TRUE; // return TRUE unless you set the focus to a control
  66. // 异常: OCX 属性页应返回 FALSE
  67. }
  68. void CInsertDeviceDlg::InitSTButtons()
  69. {
  70. short shBtnColor = 30;
  71. m_btnClose.SetIcon(IDI_ICON4);
  72. m_btnClose.OffsetColor(CButtonST::BTNST_COLOR_BK_IN, shBtnColor);
  73. m_btnClose.SetColor(CButtonST::BTNST_COLOR_FG_IN, RGB(0, 128, 0));
  74. m_btnAdd.SetIcon(IDI_ICON5);
  75. m_btnAdd.OffsetColor(CButtonST::BTNST_COLOR_BK_IN, shBtnColor);
  76. m_btnAdd.SetColor(CButtonST::BTNST_COLOR_FG_IN, RGB(0, 128, 0));
  77. /*m_btnRunTest.SetIcon(IDI_ICON3);
  78. m_btnRunTest.OffsetColor(CButtonST::BTNST_COLOR_BK_IN, shBtnColor);
  79. m_btnRunTest.SetColor(CButtonST::BTNST_COLOR_FG_IN, RGB(0, 128, 0));*/
  80. }
  81. void CInsertDeviceDlg::InitTreeCtrlInfo()
  82. {
  83. CMarkup xml;
  84. if ( !xml.Load(m_strDepotPath) )
  85. {
  86. LOG4C((LOG_NOTICE,"设备库不存在"));
  87. return;
  88. }
  89. xml.FindElem();
  90. xml.IntoElem();
  91. int nCount = 0;
  92. BOOL bNoDevice = TRUE;
  93. BOOL bNoFactory = TRUE;
  94. BOOL bNoType = TRUE;
  95. while( xml.FindElem("object_depot"))
  96. {
  97. CString strDevice = xml.GetAttrib("name");
  98. xml.FindChildElem("document");
  99. CString strFactory = xml.GetChildAttrib("Factory");
  100. CString strType = xml.GetChildAttrib("deviceType");
  101. STTreeInfo tTreeInfo = {0};
  102. vector <STTreeInfo>::iterator it;
  103. for ( it = vtSTTreeInfo.begin(); it != vtSTTreeInfo.end(); it++ )
  104. {
  105. tTreeInfo = *it;
  106. if ( tTreeInfo.strType == strType && tTreeInfo.hType != NULL )
  107. {
  108. bNoType = FALSE;
  109. STFactory tFactory = {0};
  110. vector <STFactory>::iterator itFactory;
  111. for ( itFactory = tTreeInfo.vtFactory.begin(); itFactory != tTreeInfo.vtFactory.end(); itFactory++)
  112. {
  113. tFactory = *itFactory;
  114. if ( tFactory.strFactory == strFactory && tFactory.hFactory != NULL )
  115. {
  116. bNoFactory = FALSE;
  117. STDevice tDevice = {0};
  118. vector <STDevice>::iterator itDevice;
  119. for( itDevice = tFactory.vtDevice.begin(); itDevice != tFactory.vtDevice.end(); itDevice++)
  120. {
  121. tDevice = *itDevice;
  122. if ( tDevice.strDevice == strDevice && tDevice.hDevice != NULL )
  123. {
  124. bNoDevice = FALSE;
  125. break;
  126. }
  127. }
  128. if ( bNoDevice )
  129. {
  130. tDevice.strDevice = strDevice;
  131. tDevice.hDevice = m_CtrlTree.InsertItem(strDevice,3,3,tFactory.hFactory);
  132. tFactory.vtDevice.push_back(tDevice);
  133. *itFactory = tFactory;
  134. }
  135. else
  136. {
  137. bNoDevice = TRUE;
  138. }
  139. break;
  140. }
  141. }
  142. if ( bNoFactory )
  143. {
  144. STFactory st_Factory = {0};
  145. st_Factory.strFactory = strFactory;
  146. st_Factory.hFactory = m_CtrlTree.InsertItem(strFactory,0,1,tTreeInfo.hType);
  147. //st_Factory.hFactory = m_CtrlTree.InsertItem(strFactory,tTreeInfo.hType);
  148. STDevice stDevice = {0};
  149. stDevice.strDevice = strDevice;
  150. stDevice.hDevice = m_CtrlTree.InsertItem(strDevice,3,3,st_Factory.hFactory);
  151. st_Factory.vtDevice.push_back(stDevice);
  152. tTreeInfo.vtFactory.push_back(st_Factory);
  153. }else
  154. {
  155. bNoFactory = TRUE;
  156. }
  157. *it = tTreeInfo;
  158. break;
  159. }
  160. }
  161. if ( bNoType )
  162. {
  163. STTreeInfo st_TreeInfo={0};
  164. st_TreeInfo.strType = strType;
  165. st_TreeInfo.hType = m_CtrlTree.InsertItem(strType,0,1,hRunRootItem);
  166. STFactory st_Factory = {0};
  167. st_Factory.strFactory = strFactory;
  168. st_Factory.hFactory = m_CtrlTree.InsertItem(strFactory,0,1,st_TreeInfo.hType);
  169. //st_Factory.hFactory = m_CtrlTree.InsertItem(strFactory,st_TreeInfo.hType);
  170. STDevice st_Device = {0};
  171. st_Device.strDevice = strDevice;
  172. st_Device.hDevice = m_CtrlTree.InsertItem(strDevice,3,3,st_Factory.hFactory);
  173. st_Factory.vtDevice.push_back(st_Device);
  174. st_TreeInfo.vtFactory.push_back(st_Factory);
  175. vtSTTreeInfo.push_back(st_TreeInfo);
  176. }
  177. else
  178. {
  179. bNoType = TRUE;
  180. }
  181. nCount++;
  182. }
  183. CString strCount;
  184. strCount.Format("当前可使用设备总数:%d",nCount);
  185. SetDlgItemText(TX_COUNT,strCount);
  186. xml.OutOfElem();
  187. }
  188. void CInsertDeviceDlg::OnBnClickedOk()//添加设备;
  189. {
  190. int iPort,iAddr,iRate,iDataBit, iStopBit,iParity, iRespTime,iInterval;
  191. iPort = GetDlgItemInt(IDC_EDIT2);
  192. iRate = GetDlgItemInt(IDC_EDIT4);
  193. iDataBit = GetDlgItemInt(IDC_EDIT5);
  194. iStopBit = GetDlgItemInt(IDC_EDIT6);
  195. iParity = GetDlgItemInt(IDC_EDIT7);
  196. iRespTime = GetDlgItemInt(IDC_EDIT8);
  197. iInterval = GetDlgItemInt(IDC_EDIT9);
  198. //GetDlgItemText(IDC_EDIT2,m_strPort);
  199. m_strPort.Format("%d",iPort);
  200. CString strAddr ;
  201. GetDlgItemText(IDC_EDIT3,strAddr);
  202. int nLenth = strAddr.GetLength();
  203. int nIndex = strAddr.Find("-");
  204. CObjectdepot tag;
  205. tag.SetDevicesDepotPath(m_strDepotPath);
  206. tag.SetRuntimeDevicesDepotPath(m_strRutDepotPath);
  207. if ( nIndex == -1 )
  208. {
  209. iAddr = GetDlgItemInt(IDC_EDIT3);
  210. if ( iPort == 0 || iAddr == 0 )
  211. {
  212. MessageBox("请完整填写串口属性");
  213. m_bModify = FALSE;
  214. return;
  215. }
  216. if( !tag.InsertSubElement(iPort,iAddr,m_strName.GetBuffer()) )
  217. {
  218. m_strName.ReleaseBuffer();
  219. MessageBox("已存或库错误");
  220. m_bModify = FALSE;
  221. return;
  222. }
  223. m_strAddr.Format("%d",iAddr);
  224. }
  225. else
  226. {
  227. if ( iPort == 0 || strAddr == "" )
  228. {
  229. MessageBox("请完整填写串口属性");
  230. m_bModify = FALSE;
  231. return;
  232. }
  233. CString str = strAddr.Left(nIndex);
  234. m_nStarAddr = atoi(str.GetBuffer());
  235. str.ReleaseBuffer();
  236. str = strAddr.Right(nLenth - nIndex -1);
  237. m_nEndAddr = atoi(str.GetBuffer());
  238. str.ReleaseBuffer();
  239. m_bMulti = TRUE;
  240. //int nCount = iAddr2 - iAddr;
  241. for ( int i = m_nStarAddr; i <= m_nEndAddr; i++)
  242. {
  243. if( !tag.InsertSubElement(iPort,i,m_strName.GetBuffer()) )
  244. {
  245. m_strName.ReleaseBuffer();
  246. MessageBox("已存在");
  247. m_bModify = FALSE;
  248. m_nEndAddr = i-1;
  249. break;
  250. }
  251. Sleep(50);
  252. }
  253. }
  254. if ( m_bModify )
  255. {
  256. tag.ModifySubPhysical(iPort,iRate,iDataBit,iStopBit,iParity,iRespTime,iInterval);
  257. }
  258. OnOK();
  259. }
  260. void CInsertDeviceDlg::OnBnClickedCancle()
  261. {
  262. // TODO: 在此添加控件通知处理程序代码
  263. OnCancel();
  264. }
  265. void CInsertDeviceDlg::OnBnClickedModify()
  266. {
  267. CString strBName = "";
  268. GetDlgItemText(BTN_MODIFY,strBName);
  269. if ( strBName == "修改属性" )
  270. {
  271. ((CWnd*)(GetDlgItem(IDC_EDIT4)))->EnableWindow();
  272. ((CWnd*)(GetDlgItem(IDC_EDIT5)))->EnableWindow();
  273. ((CWnd*)(GetDlgItem(IDC_EDIT6)))->EnableWindow();
  274. ((CWnd*)(GetDlgItem(IDC_EDIT7)))->EnableWindow();
  275. ((CWnd*)(GetDlgItem(IDC_EDIT8)))->EnableWindow();
  276. ((CWnd*)(GetDlgItem(IDC_EDIT9)))->EnableWindow();
  277. SetDlgItemText(BTN_MODIFY,"应用属性");
  278. GetDlgItemText(BTN_MODIFY,strBName);
  279. }else if( strBName == "应用属性" )
  280. {
  281. ((CWnd*)(GetDlgItem(IDC_EDIT4)))->EnableWindow(FALSE);
  282. ((CWnd*)(GetDlgItem(IDC_EDIT5)))->EnableWindow(FALSE);
  283. ((CWnd*)(GetDlgItem(IDC_EDIT6)))->EnableWindow(FALSE);
  284. ((CWnd*)(GetDlgItem(IDC_EDIT7)))->EnableWindow(FALSE);
  285. ((CWnd*)(GetDlgItem(IDC_EDIT8)))->EnableWindow(FALSE);
  286. ((CWnd*)(GetDlgItem(IDC_EDIT9)))->EnableWindow(FALSE);
  287. SetDlgItemText(BTN_MODIFY,"修改属性");
  288. m_bModify = TRUE;
  289. }
  290. }
  291. BOOL CInsertDeviceDlg::OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult)
  292. {
  293. if ( !::IsWindow(m_CtrlTree))
  294. {
  295. return CDialog::OnNotify(wParam, lParam, pResult);
  296. }
  297. LPNMHDR pNmhdr = (LPNMHDR)lParam;
  298. int idCtrl = LOWORD(wParam); // 获取控件ID;
  299. // 如果消息对象是该资源(树);
  300. if( idCtrl == IDC_TREE1 )
  301. {
  302. CPoint point;
  303. ::GetCursorPos(&point);
  304. m_CtrlTree.ScreenToClient(&point);
  305. switch( pNmhdr->code )
  306. {
  307. case NM_CLICK: // 单击;
  308. {
  309. // Get Click item's
  310. UINT uFlags;
  311. HTREEITEM hItem = m_CtrlTree.HitTest(point, &uFlags);
  312. if((hItem != NULL) && (TVHT_ONITEM & uFlags))
  313. {
  314. m_CtrlTree.SelectItem(hItem);
  315. }
  316. // [3/27/2013 Zero.t]
  317. SetDlgItemText(IDC_EDIT2,"");
  318. SetDlgItemText(IDC_EDIT3,"");
  319. SetDlgItemText(IDC_EDIT4,"");
  320. SetDlgItemText(IDC_EDIT5,"");
  321. SetDlgItemText(IDC_EDIT6,"");
  322. SetDlgItemText(IDC_EDIT7,"");
  323. SetDlgItemText(IDC_EDIT8,"");
  324. SetDlgItemText(IDC_EDIT9,"");
  325. GetDlgItem(BTN_MODIFY)->EnableWindow(FALSE);//修改
  326. GetDlgItem(IDOK)->EnableWindow(FALSE);//添加
  327. return true;//Already process the notify
  328. }
  329. case NM_RCLICK: // 右击;
  330. {
  331. // Get RightClick item's
  332. UINT uFlags;
  333. HTREEITEM hItem = m_CtrlTree.HitTest(point, &uFlags);
  334. m_CtrlTree.SelectItem(hItem);
  335. if((hItem != NULL) && (TVHT_ONITEM & uFlags))
  336. {
  337. GetCursorPos(&m_mousePoint); // 保存当前鼠标位置;
  338. HTREEITEM hSelectedItem = m_CtrlTree.GetSelectedItem();
  339. if ( !m_CtrlTree.ItemHasChildren(hSelectedItem))
  340. {
  341. m_strName = m_CtrlTree.GetItemText(hSelectedItem);
  342. #if 1
  343. CObjectdepot tObjectdepot;
  344. tObjectdepot.SetDevicesDepotPath(m_strDepotPath);
  345. tObjectdepot.SetRuntimeDevicesDepotPath(m_strRutDepotPath);
  346. STDepotObject *pDepotObject = tObjectdepot.LookupElement(m_strName) ;
  347. if ( pDepotObject == NULL)
  348. return FALSE;
  349. SetDlgItemInt(IDC_EDIT4,pDepotObject->st_depotphy.iRate);
  350. SetDlgItemInt(IDC_EDIT5,pDepotObject->st_depotphy.iDBit);
  351. SetDlgItemInt(IDC_EDIT6,pDepotObject->st_depotphy.iSBit);
  352. SetDlgItemInt(IDC_EDIT7,pDepotObject->st_depotphy.iPaty);
  353. SetDlgItemInt(IDC_EDIT8,pDepotObject->st_depotphy.iRsTm);
  354. SetDlgItemInt(IDC_EDIT9,pDepotObject->st_depotphy.iIntl);
  355. Sleep(50);
  356. if ( pDepotObject )
  357. {
  358. delete pDepotObject;
  359. pDepotObject = NULL;
  360. }
  361. #endif
  362. }
  363. }
  364. return true;//Already process the notify
  365. }
  366. case NM_DBLCLK: // 左键 双击;
  367. {
  368. UINT uFlags;
  369. HTREEITEM hItem = m_CtrlTree.HitTest(point, &uFlags);
  370. m_CtrlTree.SelectItem(hItem);
  371. if((hItem != NULL) && (TVHT_ONITEM & uFlags))
  372. {
  373. GetCursorPos(&m_mousePoint); // 保存当前鼠标位置;
  374. HTREEITEM hSelectedItem = m_CtrlTree.GetSelectedItem();
  375. if ( !m_CtrlTree.ItemHasChildren(hSelectedItem))
  376. {
  377. m_strName = m_CtrlTree.GetItemText(hSelectedItem);
  378. CObjectdepot tObjectdepot;
  379. tObjectdepot.SetDevicesDepotPath(m_strDepotPath);
  380. tObjectdepot.SetRuntimeDevicesDepotPath(m_strRutDepotPath);
  381. STDepotObject *pDepotObject = tObjectdepot.LookupElement(m_strName);
  382. if ( pDepotObject == NULL)
  383. return FALSE;
  384. SetDlgItemInt(IDC_EDIT4,pDepotObject->st_depotphy.iRate);
  385. SetDlgItemInt(IDC_EDIT5,pDepotObject->st_depotphy.iDBit);
  386. SetDlgItemInt(IDC_EDIT6,pDepotObject->st_depotphy.iSBit);
  387. SetDlgItemInt(IDC_EDIT7,pDepotObject->st_depotphy.iPaty);
  388. SetDlgItemInt(IDC_EDIT8,pDepotObject->st_depotphy.iRsTm);
  389. SetDlgItemInt(IDC_EDIT9,pDepotObject->st_depotphy.iIntl);
  390. UpdateData();
  391. if ( pDepotObject )
  392. {
  393. //ZeroMemory(pDepotObject,sizeof(STDepotObject));// ★★暂时未知什么原因,要在delete前ZeroMemory;
  394. delete pDepotObject;
  395. pDepotObject = NULL;
  396. }
  397. //(CWnd*)GetDlgItem(IDC_BUTTON4)->EnableWindow(TRUE);//停止
  398. GetDlgItem(BTN_MODIFY)->EnableWindow(TRUE);//修改
  399. GetDlgItem(IDOK)->EnableWindow();//添加
  400. }
  401. }
  402. return true;
  403. }
  404. }
  405. return false; // no match
  406. }
  407. return CDialog::OnNotify(wParam, lParam, pResult);
  408. }