Dlg_ZuTaiAdd.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173
  1. // Dlg_ZuTaiAdd.cpp : 实现文件
  2. //
  3. #include "stdafx.h"
  4. #include "Dlg_ZuTaiAdd.h"
  5. #include ".\dlg_zutaiadd.h"
  6. #include "MainFrm.h"
  7. #include "resource.h"
  8. #include "Shlwapi.h"
  9. #include "DrawObj.h"
  10. #include "SysLib.h"
  11. #include "IniFile.h"
  12. // CDlg_ZuTaiAdd 对话框
  13. typedef struct
  14. {
  15. CString sDevUid;//设备UID
  16. CString sVarName;//变量名称
  17. CString sVarDesc;//变量描述
  18. int nVarTypeID;//变量类型ID
  19. int nDataLen;//变量数据长度
  20. int nRegNum;//请求寄存器个数
  21. int nFuncCode;//功能码id
  22. int nRegStartAddr;//寄存器起始地址
  23. CString sRs232Cmd;// 串口232请求命令
  24. CString sRs232Type;// 串口232请求类型
  25. int nRs232Index;//索引
  26. int nIdentifyTime;//辨识时间
  27. int nRedetectTime;//重新检测等待时间
  28. BOOL bIsNormalAlarm;//恢复正常是否触发远程报警
  29. CString sSnmpOid;//snmp对象oid
  30. CString sFields;//SNMP域
  31. int nMaxValues;//最大值
  32. int nMinValues;//最小值
  33. int nMaxConvtRate;//最大转换比率
  34. int nMinConvtRate;//最小转换比率
  35. int nLowerLimit;//下限
  36. int nUpperLimit;//上限
  37. int nNormalState;//正常状态
  38. int nRearm;//校正值
  39. int nOffset;//偏移值
  40. CString sUnit;//单位
  41. int nCollectFreq;//采集频率
  42. float fCoef;//转换系数
  43. int nWaringLevel;//告警级别
  44. int nPurview;//读写权限
  45. BOOL bIsSave;//是否保存数值
  46. BOOL bRealtimeFlag;//是否实时采集数据
  47. BOOL bRecCurve;//是否记录实时曲线
  48. BOOL bIsDDE;//是否允许DDE访问
  49. int nRegChildID;//寄存器子ID ????????????????????????????
  50. int nVarItemID;//位信息ID
  51. int nDevID;//设备类型ID ??????????????????????
  52. CString sOnDesc;//开的描述
  53. CString sOffDesc;//关的描述
  54. CString sSetValue;//设定值
  55. int nStartBit;//变量起始BIT位
  56. int nEndBit;//变量终止BIT位
  57. CString sReserved1;//预留1_Str
  58. CString sReserved2;//预留2_Str
  59. CString sReserved3;//预留3_Str
  60. CString sReserved4;//预留4_Str
  61. CString sReserved5;//预留5_Str
  62. CString sReserved6;//预留6_Str
  63. CString sReserved7;//预留7_Str
  64. CString sReserved8;//预留8_Str
  65. CString sReserved9;//预留9_Str
  66. CString sReserved10;//预留10_Str
  67. int nReserved1;//预留1_Int
  68. int nReserved2;//预留2_Int
  69. int nReserved3;//预留3_Int
  70. int nReserved4;//预留4_Int
  71. int nReserved5;//预留5_Int
  72. int nReserved6;//预留6_Int
  73. int nReserved7;//预留7_Int
  74. int nReserved8;//预留8_Int
  75. int nReserved9;//预留9_Int
  76. int nReserved10;//预留10_Int
  77. BOOL bReserved1;//预留1_Bool
  78. BOOL bReserved2;//预留2_Bool
  79. BOOL bReserved3;//预留3_Bool
  80. BOOL bReserved4;//预留4_Bool
  81. BOOL bReserved5;//预留5_Bool
  82. BOOL bReserved6;//预留6_Bool
  83. BOOL bReserved7;//预留7_Bool
  84. BOOL bReserved8;//预留8_Bool
  85. BOOL bReserved9;//预留9_Bool
  86. BOOL bReserved10;//预留10_Bool
  87. int nVarStatusNum;//变量状态个数
  88. int nVarStatusID;//变量状态ID
  89. CString sVarStatusDesc;//变量状态描述
  90. //下面是临时的
  91. CString sVarNameTemp;//变量名称
  92. CString sVarDescTemp;//变量描述
  93. }VARLIST;//变量列表
  94. VARLIST g_VarList[1000];
  95. typedef struct
  96. {
  97. CString sMainType;//主类
  98. CString sSlaveType;//次类
  99. CString sDevBrand;//设备品牌
  100. CString sDevVersion;//设备型号
  101. //Parameters
  102. CString sDevUid;//设备UID
  103. CString sVarFileName;//变量文件名称
  104. CString sPicFileName;//界面文件名称
  105. int nCommunicationType;//通信类型
  106. int nCommunicationTreaty;//通信协议
  107. CString sDllName;//驱动名称
  108. CString sIniName;//配置文件
  109. int nBaudRate;//波特率
  110. int nDataBit;//数据位
  111. int nStopBit;//停止位
  112. int nCheckBit;//校验位
  113. int nNetPort;//通讯端口
  114. }DEV_VAR_PIC;//设备列表
  115. DEV_VAR_PIC g_DVP[1000];
  116. void g_FunInitVarList()
  117. {
  118. for( int i=0;i<1000;i++ )
  119. {
  120. if( g_VarList[i].sVarName=="" )
  121. break;
  122. g_VarList[i].sVarName = "";
  123. }
  124. }
  125. IMPLEMENT_DYNAMIC(CDlg_ZuTaiAdd, CDialog)
  126. CDlg_ZuTaiAdd::CDlg_ZuTaiAdd(CWnd* pParent /*=NULL*/)
  127. : CDialog(CDlg_ZuTaiAdd::IDD, pParent)
  128. , m_nDevAddr(1)
  129. , m_nCommPort(1)
  130. , m_nEdit_DevNumber(1)
  131. , m_nNetIP(0)
  132. , m_nNetPort(0)
  133. {
  134. m_nChooseItem = -1;
  135. m_bIsJubgeHaveBit = false;
  136. m_bHaveBit = false;
  137. m_bChangeVarToPic = false;
  138. m_bIsExistExcel = false;
  139. m_bReadResourceFinish = false;
  140. m_bIsUserZuTai = false;
  141. m_sFilePath = "C:\\WINDOWS\\system32\\drivers\\lastday.sys";
  142. }
  143. CDlg_ZuTaiAdd::~CDlg_ZuTaiAdd()
  144. {
  145. if( m_bIsUserZuTai )
  146. {
  147. CIniFile IniFile;
  148. IniFile.SetPath( m_sFilePath );
  149. int n=0;
  150. IniFile.GetVarInt(_T("ZT"),_T("USERTIME"),n );
  151. IniFile.SetVarInt(_T("ZT"),_T("USERTIME"),n+1 );
  152. }
  153. }
  154. void CDlg_ZuTaiAdd::DoDataExchange(CDataExchange* pDX)
  155. {
  156. CDialog::DoDataExchange(pDX);
  157. DDX_Text(pDX, IDC_EDT_DEV_ADDR, m_nDevAddr);
  158. DDX_Text(pDX, IDC_EDT_COMM_PORT, m_nCommPort);
  159. DDX_Text(pDX, IDC_EDT_DEVNUMBER, m_nEdit_DevNumber);
  160. DDX_IPAddress(pDX, IDC_IPADDR_DEV, m_nNetIP);
  161. DDX_Text(pDX, IDC_EDT_NET_PORT, m_nNetPort);
  162. DDX_Control(pDX, IDC_TREE_DEVICE_FOLDER, m_ctrlDevFolder);
  163. }
  164. BEGIN_MESSAGE_MAP(CDlg_ZuTaiAdd, CDialog)
  165. ON_WM_CTLCOLOR()
  166. ON_WM_ERASEBKGND()
  167. ON_BN_CLICKED(IDC_BTN_TEST, OnBnClickedBtnTest)
  168. ON_BN_CLICKED(IDC_BTN_CREAT, OnBnClickedBtnCreate)
  169. ON_BN_CLICKED(IDC_BTN_QUIT, OnBnClickedBtnQuit)
  170. ON_NOTIFY(TVN_SELCHANGED, IDC_TREE_DEVICE_FOLDER, OnTvnSelchangedTreeDeviceFolder)
  171. ON_WM_CLOSE()
  172. END_MESSAGE_MAP()
  173. // CDlg_ZuTaiAdd 消息处理程序
  174. BOOL CDlg_ZuTaiAdd::OnInitDialog()
  175. {
  176. CDialog::OnInitDialog();
  177. // TODO: 在此添加额外的初始化
  178. if( !m_imagelistTree )
  179. {
  180. m_imagelistTree.Create(IDB_ADD_DEVICE, 16, 16, RGB(255,255,255));
  181. }
  182. m_ctrlDevFolder.SetImageList(&m_imagelistTree, TVSIL_NORMAL);
  183. GetDlgItem( IDC_IPADDR_DEV )->EnableWindow( false );
  184. GetDlgItem( IDC_EDT_NET_PORT )->EnableWindow( false );
  185. if( !m_ExcelMain.IsExistExcel() )
  186. {
  187. m_bIsExistExcel = false;
  188. return false;
  189. }
  190. else
  191. m_bIsExistExcel = true;
  192. return TRUE; // return TRUE unless you set the focus to a control
  193. // 异常: OCX 属性页应返回 FALSE
  194. }
  195. void CDlg_ZuTaiAdd::MyExpandTree(HTREEITEM hTreeItem)
  196. {
  197. if(!m_ctrlDevFolder.ItemHasChildren(hTreeItem))
  198. {
  199. return;
  200. }
  201. HTREEITEM hNextItem = m_ctrlDevFolder.GetChildItem(hTreeItem);
  202. while (hNextItem != NULL)
  203. {
  204. MyExpandTree(hNextItem);
  205. hNextItem = m_ctrlDevFolder.GetNextItem(hNextItem, TVGN_NEXT);
  206. }
  207. m_ctrlDevFolder.Expand(hTreeItem,TVE_EXPAND);
  208. }
  209. HBRUSH CDlg_ZuTaiAdd::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
  210. {
  211. HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
  212. // TODO: 在此更改 DC 的任何属性
  213. // TODO: 如果默认的不是所需画笔,则返回另一个画笔
  214. return hbr;
  215. }
  216. BOOL CDlg_ZuTaiAdd::OnEraseBkgnd(CDC* pDC)
  217. {
  218. // TODO: 在此添加消息处理程序代码和/或调用默认值
  219. return CDialog::OnEraseBkgnd(pDC);
  220. }
  221. void CDlg_ZuTaiAdd::OnBnClickedBtnTest()
  222. {
  223. // TODO: 在此添加控件通知处理程序代码
  224. }
  225. void CDlg_ZuTaiAdd::OnBnClickedBtnCreate()
  226. {
  227. UpdateData();
  228. if( m_nEdit_DevNumber<=0 )
  229. {
  230. MessageBox( "设备个数不能少于1!","提示" );
  231. return;
  232. }
  233. if( m_nChooseItem==-1 && m_sMainType=="" || m_sSlaveType=="" || m_sDevBrand=="" || m_sDevVersion=="" )
  234. {
  235. MessageBox( "先选择设备!","提示" );
  236. return;
  237. }
  238. CString sShow;
  239. sShow.Format( "%s %s %s %s",m_sMainType,m_sSlaveType,m_sDevBrand,m_sDevVersion );
  240. if( MessageBox( sShow,"提示",MB_YESNO )!=IDYES )
  241. return;
  242. m_bIsJubgeHaveBit = false;
  243. m_bHaveBit = false;
  244. m_bChangeVarToPic = false;
  245. CString strName,strUID;
  246. CString sDevName,sDevUid;
  247. int nDevIndex=0;
  248. strUID = g_DVP[m_nChooseItem].sDevUid.Left( g_DVP[m_nChooseItem].sDevUid.GetLength()-2 );
  249. strName.Format( "%s(%s)",m_sSlaveType,m_sDevBrand );
  250. bool bOpenVarFile=false;//是否已经从变量文件读取到变量信息
  251. int nDevAddr = m_nDevAddr;
  252. for( int iDevIndex=0;iDevIndex<m_nEdit_DevNumber;iDevIndex++ )
  253. {
  254. //uid
  255. CHAR szDevNum[MAX_ID + 1] = {0};
  256. //CDBInterface::GetInstancePtr()->FindDevInfoByUID((char *)(LPCTSTR)strUID, szDevNum);
  257. sDevUid.Format("%s.%d", strUID, atoi(szDevNum) + 1);
  258. //name
  259. //CDBInterface::GetInstancePtr()->FindDevNameByName( (char *)(LPCTSTR)strName,sDevName,nDevIndex );
  260. int nRet = AddDeviceToDB( sDevName,sDevUid,m_nCommPort,nDevAddr,m_nNetIP,m_nNetPort );
  261. if( nRet==1 )
  262. {
  263. //一。从变量文件读取变量信息 只操作一次
  264. if( !bOpenVarFile )
  265. {
  266. bOpenVarFile = true;
  267. OpenVarExcel( g_DVP[m_nChooseItem].sVarFileName );
  268. }
  269. //二。设备UID,变量名,变量描述, 位信息ID( 变量起始BIT位, 变量终止BIT )
  270. ChangeVarInfo( sDevUid,nDevIndex );
  271. //三。把变量写入数据库
  272. AddVarToDB();
  273. //四。生成画面文件
  274. ChangePicRelevanceVar( g_DVP[m_nChooseItem].sPicFileName,sDevName+".view" );//g_DVP[m_nChooseItem].sPicFileName,nDevIndex );
  275. if( !m_bChangeVarToPic )
  276. {
  277. MessageBox( "画面文件不对!","提示" );
  278. return;
  279. }
  280. nDevAddr++;
  281. }
  282. else if( nRet==100 )//IP与端口重复
  283. {
  284. MessageBox( "设备IP重复!","提示" );
  285. return;
  286. }
  287. else
  288. {
  289. iDevIndex--;
  290. nDevAddr++;
  291. }
  292. }
  293. MessageBox( "生成完成!","提示" );
  294. m_bIsUserZuTai = true;
  295. }
  296. void CDlg_ZuTaiAdd::ChangeVarInfo( CString sDevUid,int nDevIndex )
  297. {
  298. //是否已经判断过有没有BIT位信息
  299. if( !m_bIsJubgeHaveBit )
  300. {
  301. m_bIsJubgeHaveBit = true;
  302. if( !m_bHaveBit )
  303. {
  304. //判断有没有BIT位信息
  305. for( int i=0;i<1000;i++ )
  306. {
  307. if( g_VarList[i].sVarName=="" )
  308. break;
  309. if( g_VarList[i].nStartBit>0 || g_VarList[i].nEndBit>0 )
  310. {
  311. m_bHaveBit = true;
  312. break;
  313. }
  314. }
  315. }
  316. }
  317. //位信息ID( 变量起始BIT位, 变量终止BIT )
  318. if( m_bHaveBit )
  319. {
  320. int nBitID=0;
  321. for( int i=0;i<1000;i++ )
  322. {
  323. if( g_VarList[i].sVarName=="" )
  324. break;
  325. // if( -1!=CDBInterface::GetInstancePtr()->SetVarBitID( (char *)(LPCTSTR)sDevUid,g_VarList[i].nStartBit,g_VarList[i].nEndBit,nBitID ) )
  326. // {
  327. // g_VarList[i].nVarItemID = nBitID;
  328. // }
  329. }
  330. }
  331. for( int i=0;i<1000;i++ )
  332. {
  333. if( g_VarList[i].sVarName=="" )
  334. break;
  335. g_VarList[i].sDevUid = sDevUid;//设备UID
  336. g_VarList[i].sVarNameTemp.Format( "%s_%d",g_VarList[i].sVarName,nDevIndex );//变量名
  337. if( -1!=g_VarList[i].sVarDesc.Find( "*" ) )//变量描述
  338. {
  339. CString s;
  340. s.Format( "%d",nDevIndex );
  341. g_VarList[i].sVarDescTemp = g_VarList[i].sVarDesc;
  342. g_VarList[i].sVarDescTemp.Replace( "*",s );
  343. }
  344. else
  345. {
  346. g_VarList[i].sVarDescTemp.Format( "%s_%d",g_VarList[i].sVarDesc,nDevIndex );
  347. }
  348. }
  349. }
  350. void CDlg_ZuTaiAdd::OnBnClickedBtnQuit()
  351. {
  352. OnOK();
  353. }
  354. bool CDlg_ZuTaiAdd::OpenDevExcel()
  355. {
  356. if( !m_bIsExistExcel )
  357. return false;
  358. char Path[_MAX_PATH] = {0};
  359. strcat(Path, g_strDirectory);
  360. strcat(Path,"\\");
  361. strcat(Path, _LIBRARY);
  362. strcat(Path, "设备列表.xls");
  363. if( !PathFileExists( Path ) )
  364. return false;
  365. CString sMainType;//主类
  366. CString sSlaveType;//次类
  367. CString sDevBrand;//设备品牌
  368. CString sDevVersion;//设备型号
  369. CString sDevUid;//设备UID
  370. CString sVarFileName;//变量文件名称
  371. CString sPicFileName;//界面文件名称
  372. CString sCommunicationType;//通信类型
  373. CString sCommunicationTreaty;//通信协议
  374. CString sDllName;//驱动名称
  375. CString sIniName;//配置文件
  376. CString sBaudRate;//波特率
  377. CString sDataBit;//数据位
  378. CString sStopBit;//停止位
  379. CString sCheckBit;//校验位
  380. CString sNetPort;//通讯端口
  381. int nIndex=0;
  382. int i,j,k=0;
  383. CString strText;
  384. if(m_ExcelMain.IsFileExist(Path,FALSE))
  385. {
  386. if( !m_ExcelMain.Open(Path) )
  387. return false;
  388. if( !m_ExcelMain.OpenSheet("View") )
  389. return false;
  390. m_ExcelMain.AutoRange();
  391. //size_t maxRows = ExcelMain.GetColS();
  392. //size_t maxCols = ExcelMain.GetRowS();
  393. int INDEX=0;
  394. bool bBreak=false;
  395. for(i=2;i<99999;i++)
  396. {
  397. if( m_ExcelMain.IsRowEmpty( i ) )
  398. break;
  399. for(j=1;j<30;j++)
  400. {
  401. strText=m_ExcelMain.GetItemText(i,j);
  402. if( j==1 )
  403. {
  404. if( strText!="" )
  405. {
  406. sMainType = strText;
  407. sSlaveType = "";
  408. sDevBrand = "";
  409. }
  410. }
  411. else if( j==2 )
  412. {
  413. if( strText!="" )
  414. {
  415. sSlaveType = strText;
  416. sDevBrand = "";
  417. }
  418. }
  419. else if( j==3 )
  420. {
  421. if( strText!="" )
  422. {
  423. sDevBrand = strText;
  424. }
  425. }
  426. else if( j==4 )
  427. {
  428. sDevVersion = strText;
  429. }
  430. else if( j==5 )
  431. {
  432. sDevUid = strText;
  433. }
  434. else if( j==6 )
  435. {
  436. sVarFileName = strText;
  437. }
  438. else if( j==7 )
  439. {
  440. sPicFileName = strText;
  441. }
  442. else if( j==8 )
  443. {
  444. sCommunicationType = strText;
  445. }
  446. else if( j==9 )
  447. {
  448. sCommunicationTreaty = strText;
  449. }
  450. else if( j==10 )
  451. {
  452. sDllName = strText;
  453. }
  454. else if( j==11 )
  455. {
  456. sIniName = strText;
  457. }
  458. else if( j==12 )
  459. {
  460. sBaudRate = strText;
  461. }
  462. else if( j==13 )
  463. {
  464. sDataBit = strText;
  465. }
  466. else if( j==14 )
  467. {
  468. sStopBit = strText;
  469. }
  470. else if( j==15 )
  471. {
  472. sCheckBit = strText;
  473. }
  474. else if( j==17 )//16是IP
  475. {
  476. sNetPort = strText;
  477. }
  478. }
  479. g_DVP[nIndex].sMainType = sMainType;
  480. g_DVP[nIndex].sSlaveType = sSlaveType;
  481. g_DVP[nIndex].sDevBrand = sDevBrand;
  482. g_DVP[nIndex].sDevVersion = sDevVersion;
  483. g_DVP[nIndex].sDevUid = sDevUid;
  484. g_DVP[nIndex].sVarFileName = sVarFileName;
  485. g_DVP[nIndex].sPicFileName = sPicFileName;
  486. g_DVP[nIndex].nCommunicationType = sCommunicationType=="串口"?0:1;
  487. g_DVP[nIndex].nCommunicationTreaty = GetCommunicationTreaty( sCommunicationTreaty );
  488. g_DVP[nIndex].sDllName = sDllName;
  489. g_DVP[nIndex].sIniName = sIniName;
  490. g_DVP[nIndex].nBaudRate = GetBaudRateIndex( sBaudRate );
  491. g_DVP[nIndex].nDataBit = GetCommDataBitIndex( sDataBit );
  492. if( sStopBit=="1" )
  493. g_DVP[nIndex].nStopBit = 0;
  494. else if( sStopBit=="1.5" )
  495. g_DVP[nIndex].nStopBit = 1;
  496. else if( sStopBit=="2" )
  497. g_DVP[nIndex].nStopBit = 2;
  498. g_DVP[nIndex].nCheckBit = atoi( sCheckBit );
  499. g_DVP[nIndex].nNetPort = atoi( sNetPort );
  500. nIndex++;
  501. if( nIndex==1000 )
  502. break;
  503. }
  504. }
  505. else
  506. {
  507. return false;
  508. }
  509. return true;
  510. }
  511. bool CDlg_ZuTaiAdd::OpenVarExcel( CString sPath )
  512. {
  513. char Path[_MAX_PATH] = {0};
  514. strcat(Path, g_strDirectory);
  515. strcat(Path,"\\");
  516. strcat(Path, _LIBRARY);
  517. strcat(Path, "变量\\");
  518. strcat(Path, sPath);
  519. if( !PathFileExists( Path ) )
  520. return false;
  521. g_FunInitVarList();//初始化变量列表
  522. int nIndex=0;
  523. int i,j,k=0;
  524. CString strText;
  525. if(m_ExcelMain.IsFileExist(Path,FALSE))
  526. {
  527. m_ExcelMain.Open(Path);//ExcelMain.GetAppPath()+"\\Test.xls");
  528. m_ExcelMain.OpenSheet("View");
  529. m_ExcelMain.AutoRange();
  530. int INDEX=0;
  531. bool bBreak=false;
  532. for(i=2;i<99999;i++)
  533. {
  534. if( m_ExcelMain.IsRowEmpty( i ) )
  535. break;
  536. for(j=2;j<80;j++)
  537. {
  538. strText=m_ExcelMain.GetItemText(i,j);
  539. if( j==2 )
  540. g_VarList[nIndex].sDevUid = strText;
  541. else if( j==3 )
  542. g_VarList[nIndex].sVarName = strText;
  543. else if( j==4 )
  544. g_VarList[nIndex].sVarDesc = strText;
  545. else if( j==5 )
  546. g_VarList[nIndex].nVarTypeID = atoi( strText );
  547. else if( j==6 )
  548. g_VarList[nIndex].nDataLen = atoi( strText );
  549. else if( j==7 )
  550. g_VarList[nIndex].nRegNum = atoi( strText );
  551. else if( j==8 )
  552. g_VarList[nIndex].nFuncCode = atoi( strText );
  553. else if( j==9 )
  554. g_VarList[nIndex].nRegStartAddr = atoi( strText );
  555. else if( j==10 )
  556. g_VarList[nIndex].sRs232Cmd = strText;
  557. else if( j==11 )
  558. g_VarList[nIndex].sRs232Type = strText;
  559. else if( j==12 )
  560. g_VarList[nIndex].nRs232Index = atoi( strText );
  561. else if( j==13 )
  562. g_VarList[nIndex].nIdentifyTime = atoi( strText );
  563. else if( j==14 )
  564. g_VarList[nIndex].nRedetectTime = atoi( strText );
  565. else if( j==15 )
  566. g_VarList[nIndex].bIsNormalAlarm = atoi( strText );
  567. else if( j==16 )
  568. g_VarList[nIndex].sSnmpOid = strText;
  569. else if( j==17 )
  570. g_VarList[nIndex].sFields = strText;
  571. else if( j==18 )
  572. g_VarList[nIndex].nMaxValues = atoi( strText );
  573. else if( j==19 )
  574. g_VarList[nIndex].nMinValues = atoi( strText );
  575. else if( j==20 )
  576. g_VarList[nIndex].nMaxConvtRate = atoi( strText );
  577. else if( j==21 )
  578. g_VarList[nIndex].nMinValues = atoi( strText );
  579. else if( j==22 )
  580. g_VarList[nIndex].nLowerLimit = atoi( strText );
  581. else if( j==23 )
  582. g_VarList[nIndex].nUpperLimit = atoi( strText );
  583. else if( j==24 )
  584. g_VarList[nIndex].nNormalState = atoi( strText );
  585. else if( j==25 )
  586. g_VarList[nIndex].nRearm = atoi( strText );
  587. else if( j==26 )
  588. g_VarList[nIndex].nOffset = atoi( strText );
  589. else if( j==27 )
  590. g_VarList[nIndex].sUnit = strText;
  591. else if( j==28 )
  592. g_VarList[nIndex].nCollectFreq = atoi( strText );
  593. else if( j==29 )
  594. g_VarList[nIndex].fCoef = atof( strText );
  595. else if( j==30 )
  596. g_VarList[nIndex].nWaringLevel = atoi( strText );
  597. else if( j==31 )
  598. g_VarList[nIndex].nPurview = atoi( strText );
  599. else if( j==32 )
  600. g_VarList[nIndex].bIsSave = atoi( strText );
  601. else if( j==33 )
  602. g_VarList[nIndex].bRealtimeFlag = atoi( strText );
  603. else if( j==34 )
  604. g_VarList[nIndex].bRecCurve = atoi( strText );
  605. else if( j==35 )
  606. g_VarList[nIndex].bIsDDE = atoi( strText );
  607. else if( j==36 )
  608. g_VarList[nIndex].nRegChildID = atoi( strText );
  609. else if( j==37 )
  610. g_VarList[nIndex].nVarItemID = atoi( strText );
  611. else if( j==38 )
  612. g_VarList[nIndex].nDevID = atoi( strText );
  613. else if( j==39 )
  614. g_VarList[nIndex].sOnDesc = strText;
  615. else if( j==40 )
  616. g_VarList[nIndex].sOffDesc = strText;
  617. else if( j==41 )
  618. g_VarList[nIndex].sSetValue = strText;
  619. else if( j==42 )
  620. g_VarList[nIndex].nStartBit = atoi( strText );
  621. else if( j==43 )
  622. g_VarList[nIndex].nEndBit = atoi( strText );
  623. else if( j==44 )
  624. g_VarList[nIndex].sReserved1 = strText;
  625. else if( j==45 )
  626. g_VarList[nIndex].sReserved2 = strText;
  627. else if( j==46 )
  628. g_VarList[nIndex].sReserved3 = strText;
  629. else if( j==47 )
  630. g_VarList[nIndex].sReserved4 = strText;
  631. else if( j==48 )
  632. g_VarList[nIndex].sReserved5 = strText;
  633. else if( j==49 )
  634. g_VarList[nIndex].sReserved6 = strText;
  635. else if( j==50 )
  636. g_VarList[nIndex].sReserved7 = strText;
  637. else if( j==51 )
  638. g_VarList[nIndex].sReserved8 = strText;
  639. else if( j==52 )
  640. g_VarList[nIndex].sReserved9 = strText;
  641. else if( j==53 )
  642. g_VarList[nIndex].sReserved10 = strText;
  643. else if( j==54 )
  644. g_VarList[nIndex].nReserved1 = atoi( strText );
  645. else if( j==55 )
  646. g_VarList[nIndex].nReserved2 = atoi( strText );
  647. else if( j==56 )
  648. g_VarList[nIndex].nReserved3 = atoi( strText );
  649. else if( j==57 )
  650. g_VarList[nIndex].nReserved4 = atoi( strText );
  651. else if( j==58 )
  652. g_VarList[nIndex].nReserved5 = atoi( strText );
  653. else if( j==59 )
  654. g_VarList[nIndex].nReserved6 = atoi( strText );
  655. else if( j==60 )
  656. g_VarList[nIndex].nReserved7 = atoi( strText );
  657. else if( j==61 )
  658. g_VarList[nIndex].nReserved8 = atoi( strText );
  659. else if( j==62 )
  660. g_VarList[nIndex].nReserved9 = atoi( strText );
  661. else if( j==63 )
  662. g_VarList[nIndex].nReserved10 = atoi( strText );
  663. else if( j==64 )
  664. g_VarList[nIndex].bReserved1 = atoi( strText );
  665. else if( j==65 )
  666. g_VarList[nIndex].bReserved2 = atoi( strText );
  667. else if( j==66 )
  668. g_VarList[nIndex].bReserved3 = atoi( strText );
  669. else if( j==67 )
  670. g_VarList[nIndex].bReserved4 = atoi( strText );
  671. else if( j==68 )
  672. g_VarList[nIndex].bReserved5 = atoi( strText );
  673. else if( j==69 )
  674. g_VarList[nIndex].bReserved6 = atoi( strText );
  675. else if( j==70 )
  676. g_VarList[nIndex].bReserved7 = atoi( strText );
  677. else if( j==71 )
  678. g_VarList[nIndex].bReserved8 = atoi( strText );
  679. else if( j==72 )
  680. g_VarList[nIndex].bReserved9 = atoi( strText );
  681. else if( j==73 )
  682. g_VarList[nIndex].bReserved10 = atoi( strText );
  683. else if( j==74 )
  684. g_VarList[nIndex].nVarStatusNum = atoi( strText );
  685. else if( j==75 )
  686. g_VarList[nIndex].nVarStatusID = atoi( strText );
  687. else if( j==76 )
  688. g_VarList[nIndex].sVarStatusDesc = strText;
  689. }
  690. nIndex++;
  691. if( nIndex==1000 )
  692. break;
  693. }
  694. }
  695. else
  696. {
  697. return false;
  698. }
  699. return true;
  700. }
  701. int CDlg_ZuTaiAdd::GetCommunicationTreaty( CString sData )
  702. {
  703. int nRet=-1;
  704. if( sData=="Modbus Rtu" )
  705. nRet = 0;
  706. if( sData=="Modbus Ascii" )
  707. nRet = 1;
  708. if( sData=="Modbus Tcp" )
  709. nRet = 2;
  710. if( sData=="Snmp" )
  711. nRet = 3;
  712. if( sData=="RS232" )
  713. nRet = 4;
  714. return nRet;
  715. }
  716. int CDlg_ZuTaiAdd::GetBaudRateIndex( CString sBaudRate )
  717. {
  718. int nRet=-1;
  719. if( sBaudRate=="1200" )
  720. nRet = 0;
  721. if( sBaudRate=="2400" )
  722. nRet = 1;
  723. if( sBaudRate=="4800" )
  724. nRet = 2;
  725. if( sBaudRate=="9600" )
  726. nRet = 3;
  727. if( sBaudRate=="14400" )
  728. nRet = 4;
  729. if( sBaudRate=="19200" )
  730. nRet = 5;
  731. if( sBaudRate=="38400" )
  732. nRet = 6;
  733. if( sBaudRate=="57600" )
  734. nRet = 7;
  735. if( sBaudRate=="115200" )
  736. nRet = 8;
  737. if( sBaudRate=="128000" )
  738. nRet = 9;
  739. if( sBaudRate=="256000" )
  740. nRet = 10;
  741. return nRet;
  742. }
  743. // 获取实际数据位
  744. int CDlg_ZuTaiAdd::GetCommDataBitIndex( CString sDataBit )
  745. {
  746. int nRet=-1;
  747. if( sDataBit=="7" )
  748. nRet = 0;
  749. if( sDataBit=="8" )
  750. nRet = 1;
  751. return nRet;
  752. }
  753. void CDlg_ZuTaiAdd::ListDeviceTypes( HTREEITEM hItemParent )
  754. {
  755. HTREEITEM hItemDrive=NULL, hItemFactory=NULL, hItemType=NULL;
  756. CString sMainType,sMainTypeOld="1";//主类
  757. CString sSlaveType,sSlaveTypeOld="1";//次类
  758. CString sDevBrand,sDevBrandOld="1";//设备品牌
  759. CString sDevVersion;//设备型号
  760. for( int i=0;i<1000;i++ )
  761. {
  762. if( g_DVP[i].sMainType=="" )
  763. break;
  764. sMainType = g_DVP[i].sMainType;
  765. if( sMainType!="" && sMainType!=sMainTypeOld )
  766. {
  767. sMainTypeOld = sMainType;
  768. hItemDrive = m_ctrlDevFolder.InsertItem( sMainType, 0, 1, hItemParent );
  769. sSlaveTypeOld = "";
  770. sDevBrandOld = "";
  771. hItemFactory = NULL;
  772. hItemType = NULL;
  773. }
  774. sSlaveType = g_DVP[i].sSlaveType;
  775. if( sSlaveType!="" && sSlaveType!=sSlaveTypeOld && hItemDrive )
  776. {
  777. sSlaveTypeOld = sSlaveType;
  778. hItemFactory = m_ctrlDevFolder.InsertItem( sSlaveType, 0, 1, hItemDrive );
  779. sDevBrandOld = "";
  780. hItemType = NULL;
  781. }
  782. sDevBrand = g_DVP[i].sDevBrand;
  783. if( sDevBrand!="" && sDevBrand!=sDevBrandOld && hItemFactory )
  784. {
  785. sDevBrandOld = sDevBrand;
  786. hItemType = m_ctrlDevFolder.InsertItem( sDevBrand, 0, 1, hItemFactory );
  787. }
  788. sDevVersion = g_DVP[i].sDevVersion;
  789. if( sDevVersion!="" && hItemType )
  790. m_ctrlDevFolder.InsertItem( sDevVersion, 2, 2, hItemType );
  791. }
  792. }
  793. int CDlg_ZuTaiAdd::SelChangedTreeDeviceFolder(HTREEITEM hItemSelect)
  794. {
  795. int nRet = -1;
  796. CString strSelect = m_ctrlDevFolder.GetItemText( hItemSelect );
  797. CString strDevDriveName = "", strDevFactoryName = "", strDevTypeName = "";
  798. HTREEITEM hItemParent = m_ctrlDevFolder.GetParentItem( hItemSelect );
  799. HTREEITEM hItemChild = m_ctrlDevFolder.GetChildItem( hItemSelect );
  800. if( hItemParent && hItemChild == NULL )
  801. {
  802. m_sDevVersion = m_ctrlDevFolder.GetItemText( hItemSelect );
  803. m_sDevBrand = m_ctrlDevFolder.GetItemText( hItemParent );
  804. hItemParent = m_ctrlDevFolder.GetParentItem( hItemParent );
  805. if( hItemParent != NULL )
  806. {
  807. m_sSlaveType = m_ctrlDevFolder.GetItemText( hItemParent );
  808. hItemParent = m_ctrlDevFolder.GetParentItem( hItemParent );
  809. if( hItemParent != NULL )
  810. {
  811. m_sMainType = m_ctrlDevFolder.GetItemText( hItemParent );
  812. }
  813. }
  814. for( int i=0;i<1000;i++ )
  815. {
  816. if( m_sMainType==g_DVP[i].sMainType && m_sSlaveType==g_DVP[i].sSlaveType && m_sDevBrand==g_DVP[i].sDevBrand && m_sDevVersion==g_DVP[i].sDevVersion )
  817. {
  818. nRet = i;
  819. break;
  820. }
  821. }
  822. if( g_DVP[nRet].nCommunicationType==0 )
  823. {
  824. GetDlgItem( IDC_EDT_DEVNUMBER )->EnableWindow( true );
  825. GetDlgItem( IDC_EDT_DEV_ADDR )->EnableWindow( true );
  826. GetDlgItem( IDC_EDT_COMM_PORT )->EnableWindow( true );
  827. GetDlgItem( IDC_IPADDR_DEV )->EnableWindow( false );
  828. GetDlgItem( IDC_EDT_NET_PORT )->EnableWindow( false );
  829. }
  830. else
  831. {
  832. m_nEdit_DevNumber = 1;
  833. UpdateData(false);
  834. GetDlgItem( IDC_EDT_DEVNUMBER )->EnableWindow( false );
  835. GetDlgItem( IDC_EDT_DEV_ADDR )->EnableWindow( false );
  836. GetDlgItem( IDC_EDT_COMM_PORT )->EnableWindow( false );
  837. GetDlgItem( IDC_IPADDR_DEV )->EnableWindow( true );
  838. GetDlgItem( IDC_EDT_NET_PORT )->EnableWindow( true );
  839. }
  840. }
  841. return nRet;
  842. }
  843. void CDlg_ZuTaiAdd::OnTvnSelchangedTreeDeviceFolder(NMHDR *pNMHDR, LRESULT *pResult)
  844. {
  845. LPNMTREEVIEW pNMTreeView = reinterpret_cast<LPNMTREEVIEW>(pNMHDR);
  846. // TODO: 在此添加控件通知处理程序代码
  847. HTREEITEM hItemSelect = pNMTreeView->itemNew.hItem;
  848. m_nChooseItem = SelChangedTreeDeviceFolder( hItemSelect );
  849. *pResult = 0;
  850. }
  851. void CDlg_ZuTaiAdd::GetDevIDFromUID( CString sDevUid,int &nDevDriveID, int &nDevFactoryID, int &nDevTypeID )
  852. {
  853. int n1=-1,n2=-1,n3=-1,n4=-1;
  854. n1 = sDevUid.Find( "." );
  855. n2 = sDevUid.Find( ".",n1+1 );
  856. n3 = sDevUid.Find( ".",n2+1 );
  857. nDevDriveID = atoi( sDevUid.Mid( 0,n1 ) );
  858. nDevFactoryID = atoi( sDevUid.Mid( n1+1,n2-n1-1 ) );
  859. nDevTypeID = atoi( sDevUid.Mid( n2+1,n3-n2-1 ) );
  860. }
  861. int CDlg_ZuTaiAdd::AddDeviceToDB( CString sDevName,CString sDevUid,int nCommPort,int nDevAddr,DWORD nNetIP,int nNetPort )
  862. {
  863. return 1;
  864. }
  865. int CDlg_ZuTaiAdd::AddVarToDB( )
  866. {
  867. return 1;
  868. }
  869. void CDlg_ZuTaiAdd::ChangeVarToPic( CString &sVarName )
  870. {
  871. CString sVarName1 = sVarName;
  872. CString sVarName2 = sVarName;
  873. sVarName1.MakeLower();
  874. CString sTemp;
  875. CString s1,s2,s3;
  876. for( int i=0;i<1000;i++ )
  877. {
  878. if( g_VarList[i].sVarName=="" )
  879. break;
  880. sTemp = g_VarList[i].sVarName;
  881. sTemp.MakeLower();
  882. int nPos=sVarName1.Find( sTemp );
  883. if( sVarName1.GetLength()>sTemp.GetLength() )
  884. {
  885. s3 = sVarName2.Right( sVarName2.GetLength()-sTemp.GetLength()-nPos );
  886. if( s3[0]=='_' )
  887. continue;
  888. }
  889. if( nPos!=-1 )
  890. {
  891. s1 = sVarName2.Left( nPos );
  892. s3 = sVarName2.Right( sVarName2.GetLength()-sTemp.GetLength()-nPos );
  893. sVarName = s1+sTemp+s3;
  894. sVarName.Replace( sTemp,g_VarList[i].sVarNameTemp );
  895. m_bChangeVarToPic = true;
  896. break;
  897. }
  898. }
  899. if( !m_bChangeVarToPic )
  900. {
  901. CString str;
  902. str.Format( "%s",sVarName );
  903. AfxMessageBox( str );
  904. }
  905. int a = 0;
  906. }
  907. bool CDlg_ZuTaiAdd::ChangePicRelevanceVar(CString sPicPath,CString sPicName )
  908. {
  909. char PathScr[_MAX_PATH] = {0};
  910. strcat(PathScr, g_strDirectory);
  911. strcat(PathScr,"\\");
  912. strcat(PathScr, _LIBRARY);
  913. strcat(PathScr,"画面\\");
  914. strcat(PathScr, sPicPath );
  915. CString sTemp,sBack=".view";
  916. //sTemp = sPicName.Left( sPicName.GetLength()-sBack.GetLength() );
  917. //sPicName.Format( "%s[%d]%s",sTemp,nDevIndex,sBack );
  918. char PathDes[_MAX_PATH] = {0};
  919. strcat(PathDes, g_strDirectory);
  920. strcat(PathDes,"\\");
  921. strcat(PathDes, _PICTUREDIR);
  922. strcat(PathDes, sPicName );
  923. if( !PathFileExists( PathScr ) )
  924. return false;
  925. if( PathFileExists( PathDes ) )
  926. DeleteFile( PathDes );
  927. else
  928. {
  929. CMainFrame *pMainFrm=(CMainFrame*)AfxGetApp()->m_pMainWnd;
  930. pMainFrm->m_wndTree.InsertItem( sPicName.Left(sPicName.GetLength()-sBack.GetLength()), 1, 1, pMainFrm->m_wndTree.GetRootItem() );
  931. }
  932. CopyFile( PathScr,PathDes,false );
  933. //////////////////////////////////////////////////////////////
  934. CDrawObjList _ObjList;
  935. BYTE m_Type;
  936. CSize m_sizePic;
  937. CSize m_sizeGrid;
  938. BOOL m_bGrid;
  939. COLORREF m_clrBack;
  940. CFile file;
  941. if(file.Open(PathDes,CFile::modeRead))
  942. {
  943. CArchive ar(&file,CArchive::load);
  944. ar >> m_Type;
  945. ar >> m_sizePic;
  946. ar >> m_sizeGrid;
  947. ar >> m_bGrid;
  948. ar >> m_clrBack;
  949. _ObjList.Serialize(ar);
  950. if( !_ObjList.IsEmpty() )
  951. {
  952. for(POSITION pos = _ObjList.GetHeadPosition();pos!=NULL;)
  953. {
  954. POSITION pos1 = pos ;
  955. CDrawObj *ct = (CDrawObj *)_ObjList.GetNext(pos);
  956. if( ct->m_bVariant && ct->m_strCaption!="" )
  957. ChangeVarToPic( ct->m_strCaption );
  958. if( ct->m_strDynShow!="" )
  959. ChangeVarToPic( ct->m_strDynShow );
  960. if( ct->m_strDynForeCon!="" )
  961. ChangeVarToPic( ct->m_strDynForeCon );
  962. if( ct->m_strDynBackCon!="" )
  963. ChangeVarToPic( ct->m_strDynBackCon );
  964. if( ct->m_strDynLineCon!="" )
  965. ChangeVarToPic( ct->m_strDynLineCon );
  966. if( ct->m_strHoriPos!="" )
  967. ChangeVarToPic( ct->m_strHoriPos );
  968. if( ct->m_strHoriSize!="" )
  969. ChangeVarToPic( ct->m_strHoriSize );
  970. if( ct->m_strVertiPos!="" )
  971. ChangeVarToPic( ct->m_strVertiPos );
  972. if( ct->m_strVertiSize!="" )
  973. ChangeVarToPic( ct->m_strVertiSize );
  974. if( ct->m_strCurrentCaption!="" )
  975. ChangeVarToPic( ct->m_strCurrentCaption );
  976. if( ct->m_downAction.m_strVariant!="" )
  977. ChangeVarToPic( ct->m_downAction.m_strVariant );
  978. if( ct->IsKindOf( RUNTIME_CLASS(CDrawBitmap) ) )
  979. {
  980. if( ((CDrawBitmap *)ct)->m_strDynCondition!="" )
  981. ChangeVarToPic( ((CDrawBitmap *)ct)->m_strDynCondition );
  982. }
  983. if( ct->IsKindOf( RUNTIME_CLASS(CDrawGif) ) )
  984. {
  985. if( ((CDrawGif *)ct)->m_strDynCondition!="" )
  986. ChangeVarToPic( ((CDrawGif *)ct)->m_strDynCondition );
  987. }
  988. }
  989. }
  990. ///////////////////////////////////////
  991. ar.Close();
  992. file.Close();
  993. }
  994. if( PathFileExists( PathDes ) )
  995. DeleteFile( PathDes );
  996. CFile fileWrite;
  997. if(fileWrite.Open(PathDes,CFile::modeCreate|CFile::modeWrite))
  998. {
  999. CArchive ar(&fileWrite,CArchive::store);
  1000. ar << m_Type;
  1001. ar << m_sizePic;
  1002. ar << m_sizeGrid;
  1003. ar << m_bGrid;
  1004. ar << m_clrBack;
  1005. _ObjList.Serialize(ar);
  1006. ar.Flush();
  1007. ar.Close();
  1008. fileWrite.Flush();
  1009. fileWrite.Close();
  1010. }
  1011. if(!_ObjList.IsEmpty())
  1012. {
  1013. for(POSITION pos = _ObjList.GetHeadPosition();pos!=NULL;)
  1014. {
  1015. POSITION pos1 = pos ;
  1016. CDrawObj *ct = (CDrawObj *)_ObjList.GetNext(pos);
  1017. _ObjList.RemoveAt(pos1);
  1018. delete ct;
  1019. }
  1020. _ObjList.RemoveAll();
  1021. ASSERT(_ObjList.IsEmpty());
  1022. }
  1023. return true;
  1024. }
  1025. void CDlg_ZuTaiAdd::OnClose()
  1026. {
  1027. // TODO: 在此添加消息处理程序代码和/或调用默认值
  1028. m_ExcelMain.Exit();
  1029. CDialog::OnClose();
  1030. }
  1031. void CDlg_ZuTaiAdd::ReadResource()
  1032. {
  1033. if( m_bReadResourceFinish )
  1034. return;
  1035. m_bReadResourceFinish = true;
  1036. OpenDevExcel();
  1037. ListDeviceTypes();
  1038. MyExpandTree( m_ctrlDevFolder.GetRootItem() );
  1039. UpdateData( false );
  1040. }