MainTps.cs 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.ComponentModel;
  7. using Tps_LQ_Transmitter.com;
  8. using System.IO;
  9. using Tps_LQ_Transmitter.models;
  10. using System.Data;
  11. using AppLibs.Devices;
  12. namespace Tps_LQ_Transmitter
  13. {
  14. public delegate void DltShowMsg(MsgType msgType, string msg);
  15. public delegate void DltCellValueChanged(int row, int cell, bool ok, object value);
  16. /// <summary>
  17. /// 一个测试文件对应的测试项目
  18. /// </summary>
  19. public class MainTps
  20. {
  21. /// <summary>
  22. /// 测试人员
  23. /// </summary>
  24. public string Tester { get; set; }
  25. //测试地点
  26. public string Place { get; set; }
  27. //环境温度
  28. public string Temperature { get; set; }
  29. //当前测试的产品序列号
  30. public string Serial { get; set; }
  31. //环境湿度
  32. public string Humidity { get; set; }
  33. //待测产品
  34. public string Product { get; set; }
  35. //测试项目
  36. public string TestProject { get; set; }
  37. //配置文件路径
  38. string FilePath;
  39. /// <summary>
  40. /// 设备节点,表示当前测试程序包含的试验设备,绑定到界面的设备列表中
  41. /// </summary>
  42. public IList<DeviceNode> Devices;
  43. /// <summary>
  44. /// 测试节点,表示当前测试程序含有的测试指标,绑定到界面的指标列表中
  45. /// </summary>
  46. public IList<TestNode> TestNodes;
  47. /// <summary>
  48. /// 手工输入表
  49. /// </summary>
  50. public DataTable ManualTable;
  51. /// <summary>
  52. /// 自动测试表
  53. /// </summary>
  54. public DataTable TestTable;
  55. /// <summary>
  56. /// 测试值字典
  57. /// </summary>
  58. public Dictionary<string,double> TestValueDict;
  59. public event DltCellValueChanged ManualTableCellChanged;
  60. public event DltCellValueChanged TestTableCellChanged;
  61. DataTable InitTestTable,InitManualTable; //原始数据的界面,用于初始化或者重置
  62. /// <summary>
  63. /// 测试指标的结构
  64. /// </summary>
  65. List<BaseModelStruct> baseModelStructs;
  66. public MainTps()
  67. {
  68. Devices = new BindingList<DeviceNode>();
  69. TestNodes = new BindingList<TestNode>();
  70. baseModelStructs = new List<BaseModelStruct>();
  71. LoadBaseModelStructs();
  72. }
  73. #region 模板基础方法
  74. /// <summary>
  75. /// 加载配置文件
  76. /// </summary>
  77. /// <param name="filePath"></param>
  78. /// <returns></returns>
  79. public bool LoadConfigFile(string filePath)
  80. {
  81. bool ok = true;
  82. if (File.Exists(filePath) == false)
  83. {
  84. ShowMessage(MsgType.Error, string.Format("找不到配置文件:{0}", filePath));
  85. return false;
  86. }
  87. this.FilePath = filePath;
  88. using (Spire.Xls.Workbook workbook = new Spire.Xls.Workbook())
  89. {
  90. workbook.LoadFromFile(filePath);
  91. //处理Excel数据,更多请参考官方Demo
  92. Spire.Xls.Worksheet sheet = workbook.Worksheets["自动测试项总表"];
  93. if(sheet == null)
  94. {
  95. ShowMessage(MsgType.Error, "文件中找不到‘自动测试项总表’");
  96. return false;
  97. }
  98. int blankRows = 0; //避免出错
  99. int num = 0;
  100. //默认最多两百行
  101. while (num++ < 200)
  102. {
  103. //索引从1开始
  104. string col1 = sheet.Range[num, 1].Value2 != null ? sheet.Range[num, 1].Value2.ToString():"";
  105. col1 = col1.Trim();
  106. if (string.IsNullOrEmpty(col1))
  107. {
  108. blankRows++;
  109. if(blankRows > 5)
  110. {
  111. //连续5个空行以上则退出
  112. break;
  113. }
  114. }
  115. else
  116. {
  117. blankRows = 0;
  118. }
  119. if (col1.Equals("设备配置表"))
  120. {
  121. ok &= readDevices(sheet,num);
  122. //解析行数跳过,实际上还得再加上
  123. num = num + this.Devices.Count+2;
  124. }
  125. else if (col1.Equals("测试指标配置表"))
  126. {
  127. ok &= readTestNodes(workbook,sheet,num);
  128. var items = (from p in TestNodes select p.Name).Distinct().ToArray();
  129. //解析行数跳过,实际上还得再加上
  130. num = num + items.Count() + 3;
  131. }
  132. }
  133. //加载人工测试项总表
  134. sheet = workbook.Worksheets["人工测试项总表"];
  135. if (sheet != null)
  136. {
  137. blankRows = 0; //避免出错
  138. num = 0;
  139. //默认最多两百行
  140. while (num++ < 200)
  141. {
  142. //索引从1开始
  143. string col1 = sheet.Range[num, 1].Value2 != null ? sheet.Range[num, 1].Value2.ToString() : "";
  144. col1 = col1.Trim();
  145. if (string.IsNullOrEmpty(col1))
  146. {
  147. blankRows++;
  148. if (blankRows > 5)
  149. {
  150. //连续5个空行以上则退出
  151. break;
  152. }
  153. }
  154. else
  155. {
  156. blankRows = 0;
  157. }
  158. if (col1.Equals("人工测试项总表"))
  159. {
  160. this.ManualTable = readTable(sheet, num);
  161. if (this.ManualTable != null)
  162. {
  163. this.InitManualTable = this.ManualTable.Copy();
  164. }
  165. }
  166. }
  167. }
  168. //加载人工测试项总表
  169. sheet = workbook.Worksheets["自动测试记录表"];
  170. if (sheet != null)
  171. {
  172. blankRows = 0; //避免出错
  173. num = 0;
  174. //默认最多两百行
  175. while (num++ < 200)
  176. {
  177. //索引从1开始
  178. string col1 = sheet.Range[num, 1].Value2 != null ? sheet.Range[num, 1].Value2.ToString() : "";
  179. col1 = col1.Trim();
  180. if (string.IsNullOrEmpty(col1))
  181. {
  182. blankRows++;
  183. if (blankRows > 5)
  184. {
  185. //连续5个空行以上则退出
  186. break;
  187. }
  188. }
  189. else
  190. {
  191. blankRows = 0;
  192. }
  193. if (col1.Equals("自动测试记录表"))
  194. {
  195. this.TestTable = readTable(sheet, num);
  196. if(this.TestTable != null)
  197. {
  198. this.InitTestTable = this.TestTable.Copy();
  199. }
  200. }
  201. }
  202. }
  203. }
  204. return ok;
  205. }
  206. /// <summary>
  207. /// 读设备配置表
  208. /// </summary>
  209. /// <param name="rowIndex">设备配置表的起始位置</param>
  210. bool readDevices(Spire.Xls.Worksheet sheet, int rowIndex)
  211. {
  212. //检查表的格式正确
  213. string col1 = sheet.Range[rowIndex + 1, 1].Value2 != null ? sheet.Range[rowIndex + 1, 1].Value2.ToString() : "";
  214. string col2 = sheet.Range[rowIndex + 1, 2].Value2 != null ? sheet.Range[rowIndex + 1, 2].Value2.ToString() : "";
  215. string col3 = sheet.Range[rowIndex + 1, 3].Value2 != null ? sheet.Range[rowIndex + 1, 3].Value2.ToString() : "";
  216. string col4 = sheet.Range[rowIndex + 1, 4].Value2 != null ? sheet.Range[rowIndex + 1, 4].Value2.ToString() : "";
  217. if (!(col1 == "设备名称" && col2 == "设备类型" && col3 == "设备型号" && col4 == "控制句柄"))
  218. {
  219. ShowMessage(MsgType.Error, "'设备配置表'行首标题位置和格式不正确");
  220. return false;
  221. }
  222. int num = rowIndex + 1;
  223. //默认最多30行
  224. while (num++ < rowIndex + 30)
  225. {
  226. col1 = sheet.Range[num, 1].Value2 != null ? sheet.Range[num, 1].Value2.ToString() : "";
  227. col2 = sheet.Range[num, 2].Value2 != null ? sheet.Range[num, 2].Value2.ToString() : "";
  228. col3 = sheet.Range[num, 3].Value2 != null ? sheet.Range[num, 3].Value2.ToString() : "";
  229. col4 = sheet.Range[num, 4].Value2 != null ? sheet.Range[num, 4].Value2.ToString() : "";
  230. if (string.IsNullOrEmpty(col1) || string.IsNullOrEmpty(col2) || string.IsNullOrEmpty(col3))
  231. {
  232. break;
  233. }
  234. DeviceNode dn = new DeviceNode();
  235. dn.DeviceName = col1;
  236. dn.DeviceType = col2;
  237. dn.DeviceModel = col3;
  238. dn.DeviceHandle = col4;
  239. this.Devices.Add(dn);
  240. }
  241. return true;
  242. }
  243. /// <summary>
  244. /// 读测试指标配置表
  245. /// </summary>
  246. /// <param name="rowIndex">测试指标配置表的起始位置</param>
  247. bool readTestNodes(Spire.Xls.Workbook workbook,Spire.Xls.Worksheet sheet, int rowIndex)
  248. {
  249. List<TestNode> models = new List<TestNode>();
  250. //检查表的格式正确
  251. string col1 = sheet.Range[rowIndex + 2, 1].Value2 != null ? sheet.Range[rowIndex + 2, 1].Value2.ToString() : "";
  252. string col2 = sheet.Range[rowIndex + 2, 2].Value2 != null ? sheet.Range[rowIndex + 2, 2].Value2.ToString() : "";
  253. string col3 = sheet.Range[rowIndex + 2, 3].Value2 != null ? sheet.Range[rowIndex + 2, 3].Value2.ToString() : "";
  254. string col4 = sheet.Range[rowIndex + 2, 4].Value2 != null ? sheet.Range[rowIndex + 2, 4].Value2.ToString() : "";
  255. if (!(col1 == "测试指标" && col2 == "默认勾选" && col3 == "测试模板" && col4 == "测试顺序"))
  256. {
  257. ShowMessage(MsgType.Error, "'测试指标配置表'行首标题位置和格式不正确");
  258. return false;
  259. }
  260. int num = rowIndex + 2;
  261. //默认最多100行,先解析测试指标个数
  262. while (num++ < rowIndex + 100)
  263. {
  264. col1 = sheet.Range[num, 1].Value2 != null ? sheet.Range[num, 1].Value2.ToString() : "";
  265. col2 = sheet.Range[num, 2].Value2 != null ? sheet.Range[num, 2].Value2.ToString() : "";
  266. col3 = sheet.Range[num, 3].Value2 != null ? sheet.Range[num, 3].Value2.ToString() : "";
  267. col4 = sheet.Range[num, 4].Value2 != null ? sheet.Range[num, 4].Value2.ToString() : "";
  268. if (string.IsNullOrEmpty(col1) || string.IsNullOrEmpty(col3))
  269. {
  270. break;
  271. }
  272. TestNode tn = new TestNode();
  273. tn.Name = col1;
  274. if(col2 == "是")
  275. {
  276. tn.IsSelected = true;
  277. }
  278. else
  279. {
  280. tn.IsSelected = false;
  281. }
  282. tn.Template = col3;
  283. int order = Int32.MaxValue;
  284. if (Int32.TryParse(col4, out order))
  285. tn.Order = order;
  286. models.Add(tn);
  287. }
  288. //再解析各通道的配置数据
  289. int colIndex = 5; //通道数据从第5列开始
  290. while(true)
  291. {
  292. col1 = sheet.Range[rowIndex + 3, colIndex].Value2 != null ? sheet.Range[rowIndex + 3, colIndex].Value2.ToString() : "";
  293. col2 = sheet.Range[rowIndex + 3, colIndex + 1].Value2 != null ? sheet.Range[rowIndex + 3, colIndex + 1].Value2.ToString() : "";
  294. //有新通道数据则继续,否则退出
  295. if(string.IsNullOrEmpty(col1) || string.IsNullOrEmpty(col2) || col2 != "启用")
  296. {
  297. break;
  298. }
  299. string channel = sheet.Range[rowIndex + 1, colIndex].Value2 != null ? sheet.Range[rowIndex + 1, colIndex].Value2.ToString() : "";
  300. //按指标个数来解析通道数据
  301. for (int i = 0; i < models.Count; i++)
  302. {
  303. col1 = sheet.Range[rowIndex + 3 + i, colIndex].Value2 != null ? sheet.Range[rowIndex + 3 + i, colIndex].Value2.ToString() : "";
  304. col2 = sheet.Range[rowIndex + 3 + i, colIndex + 1].Value2 != null ? sheet.Range[rowIndex + 3 + i, colIndex + 1].Value2.ToString() : "";
  305. if (col2 != "启用")
  306. continue;
  307. TestNode newNode = models[i].Copy();
  308. newNode.Channel = channel; //通道名
  309. newNode.ParaConfigTable = col1; //测试配置表
  310. //复制已有参数表,或者从文件解析参数表
  311. TestNode preNode = TestNodes.FirstOrDefault(x => x.ParaConfigTable == col1);
  312. if(preNode != null)
  313. {
  314. //存在则复制,避免读文件费时间
  315. newNode.Parameters = preNode.Parameters.Copy();
  316. }
  317. else
  318. {
  319. //否则找到文件中对应的配置表
  320. Spire.Xls.Worksheet paraSheet = workbook.Worksheets[col1];
  321. if (paraSheet == null)
  322. {
  323. ShowMessage(MsgType.Error, "文件中找不到参数配置表:"+ col1);
  324. continue;
  325. }
  326. //解析参数配置表
  327. newNode.Parameters = readTestParameter(paraSheet);
  328. }
  329. //如果已经创建了同名的处理对象,则直接使用,否则则新建,采用单例模式
  330. //如果注释了下面的if,就是多例模式,即同名处理模块是不同的软件对象
  331. TestNode tn = this.TestNodes.FirstOrDefault(x => x.Template == newNode.Template);
  332. if(tn != null && tn.TestModel != null)
  333. {
  334. newNode.TestModel = tn.TestModel;
  335. }
  336. else
  337. {
  338. //根据测试指标的名称,创建BaseModel指标处理对象
  339. newNode.TestModel = CreateModel(newNode.Template);
  340. }
  341. this.TestNodes.Add(newNode);
  342. }
  343. colIndex += 2; //跳过2列
  344. }
  345. return true;
  346. }
  347. BaseModel CreateModel(string template)
  348. {
  349. //如果没有这个名字的软件模块,则返回null
  350. BaseModelStruct bms = baseModelStructs.FirstOrDefault(x => x.ModelName == template);
  351. if (bms == null)
  352. {
  353. ShowMessage(MsgType.Error, "找不到名称为:" + template +" 的测试指标模块。");
  354. return null;
  355. }
  356. BaseModel model = (BaseModel)Activator.CreateInstance(bms.type);
  357. model.tps = this;
  358. model.MessageEvent += ShowMessage;
  359. return model;
  360. }
  361. void LoadBaseModelStructs()
  362. {
  363. try
  364. {
  365. Type[] types = this.GetType().Assembly.GetTypes();
  366. foreach (var item in types)
  367. {
  368. //排除接口本身
  369. if (item.Name != "BaseModel")
  370. {
  371. if (typeof(BaseModel).IsAssignableFrom(item))
  372. {
  373. BaseModel model = (BaseModel)Activator.CreateInstance(item);
  374. BaseModelStruct bms = new BaseModelStruct();
  375. bms.ModelName = model.TemplateName;
  376. bms.type = item;
  377. this.baseModelStructs.Add(bms);
  378. model = null;
  379. }
  380. }
  381. }
  382. }
  383. catch(Exception e)
  384. {
  385. ShowMessage(MsgType.Error, "实例化BaseModel测试指标模块时出现错误,某个测试指标模块创建时发生错误,请检查代码!详细错误见程序日志文件!");
  386. Bundle.log.WriteLog(AppLibs.Host.LogLevel.Error, "类:MainTps,方法:LoadBaseModelStructs", e.Message + e.StackTrace);
  387. }
  388. }
  389. /// <summary>
  390. /// 读参数配置表
  391. /// </summary>
  392. /// <returns></returns>
  393. TestParameters readTestParameter(Spire.Xls.Worksheet sheet)
  394. {
  395. TestParameters pars = new TestParameters();
  396. int blankRows = 0; //避免出错
  397. int num = 0;
  398. //默认最多两百行
  399. while (num++ < 200)
  400. {
  401. //索引从1开始
  402. string col1 = sheet.Range[num, 1].Value2 != null ? sheet.Range[num, 1].Value2.ToString() : "";
  403. col1 = col1.Trim();
  404. if (string.IsNullOrEmpty(col1))
  405. {
  406. blankRows++;
  407. if (blankRows > 5)
  408. {
  409. //连续5个空行以上则退出
  410. break;
  411. }
  412. }
  413. else
  414. {
  415. blankRows = 0;
  416. }
  417. if (col1.Equals("参数表"))
  418. {
  419. ///获取表中的参数
  420. List<ParameterNode> nodes = readParameters(sheet,num);
  421. foreach (var item in nodes)
  422. {
  423. if(pars.Parameters.ContainsKey(item.Name))
  424. {
  425. ShowMessage(MsgType.Error, string.Format("参数配置表:{0} 存在同名参数:{1},无法重复添加参数", sheet.Name, item.Name));
  426. continue;
  427. }
  428. pars.Parameters.Add(item.Name, item);
  429. }
  430. //解析行数跳过,实际上还得再加上
  431. num = num + nodes.Count +1;
  432. }
  433. else if (col1.Equals("测试显示表"))
  434. {
  435. //获取表中的原始显示界面
  436. pars.InitialTable = readTable( sheet, num);
  437. //解析行数跳过,实际上还得再加上
  438. num = num + pars.InitialTable.Rows.Count + 1;
  439. }
  440. }
  441. return pars;
  442. }
  443. /// <summary>
  444. /// 读参数表
  445. /// </summary>
  446. /// <param name="sheet"></param>
  447. /// <returns></returns>
  448. List<ParameterNode> readParameters(Spire.Xls.Worksheet sheet,int rowIndex)
  449. {
  450. List<ParameterNode> nodes = new List<ParameterNode>();
  451. string col1 = sheet.Range[rowIndex + 1, 1].Value2 != null ? sheet.Range[rowIndex + 1, 1].Value2.ToString() : "";
  452. string col2 = sheet.Range[rowIndex + 1, 2].Value2 != null ? sheet.Range[rowIndex + 1, 2].Value2.ToString() : "";
  453. string col3 = sheet.Range[rowIndex + 1, 3].Value2 != null ? sheet.Range[rowIndex + 1, 3].Value2.ToString() : "";
  454. string col4 = sheet.Range[rowIndex + 1, 4].Value2 != null ? sheet.Range[rowIndex + 1, 4].Value2.ToString() : "";
  455. if (!(col1 == "名称" && col2 == "类型" && col3 == "值"))
  456. {
  457. ShowMessage(MsgType.Error, "'参数表'行首标题位置和格式不正确");
  458. return nodes;
  459. }
  460. int num = rowIndex + 1;
  461. //默认最多30行
  462. while (num++ < rowIndex + 200)
  463. {
  464. col1 = sheet.Range[num, 1].Value2 != null ? sheet.Range[num, 1].Value2.ToString() : "";
  465. col2 = sheet.Range[num, 2].Value2 != null ? sheet.Range[num, 2].Value2.ToString() : "";
  466. col3 = sheet.Range[num, 3].Value2 != null ? sheet.Range[num, 3].Value2.ToString() : "";
  467. col4 = sheet.Range[num, 4].Value2 != null ? sheet.Range[num, 4].Value2.ToString() : "";
  468. if (string.IsNullOrEmpty(col1) || string.IsNullOrEmpty(col2) || string.IsNullOrEmpty(col3))
  469. {
  470. break;
  471. }
  472. ParameterNode pn = new ParameterNode();
  473. pn.Name = col1;
  474. pn.Type = col2;
  475. pn.StrValue = col3;
  476. pn.Mark = col4;
  477. nodes.Add(pn);
  478. }
  479. return nodes;
  480. }
  481. /// <summary>
  482. /// 读表格,人工测试表和测试显示表
  483. /// </summary>
  484. /// <param name="sheet"></param>
  485. /// <returns></returns>
  486. DataTable readTable(Spire.Xls.Worksheet sheet, int rowIndex)
  487. {
  488. //先创建列,再创建行
  489. DataTable dt = new DataTable();
  490. int num = 0;
  491. while (num++ < 50)
  492. {
  493. string col = sheet.Range[rowIndex + 1, num].Value2 != null ? sheet.Range[rowIndex + 1, num].Value2.ToString() : "";
  494. if (string.IsNullOrEmpty(col))
  495. break;
  496. dt.Columns.Add(col);
  497. }
  498. //再创建行
  499. num = rowIndex + 1;
  500. //默认最多200行
  501. while (num++ < rowIndex + 200)
  502. {
  503. //首列无值就认为表格结束
  504. string val = sheet.Range[num, 1].Value2 != null ? sheet.Range[num, 1].Value2.ToString() : "";
  505. if (string.IsNullOrEmpty(val.Trim()))
  506. break;
  507. DataRow dr = dt.NewRow();
  508. dt.Rows.Add(dr);
  509. for (int i = 0; i < dt.Columns.Count; i++)
  510. {
  511. val = sheet.Range[num, i+1].Value2 != null ? sheet.Range[num, i+1].Value2.ToString() : "";
  512. dr[i] = val;
  513. }
  514. }
  515. return dt;
  516. }
  517. /// <summary>
  518. /// 检查仪器是否正常配置
  519. /// </summary>
  520. /// <returns></returns>
  521. public bool CheckDevices()
  522. {
  523. bool ok = true;
  524. AppLibs.Devices.IDeviceHost devHost = Bundle.host.GetFirstOrDefaultService<AppLibs.Devices.IDeviceHost>();
  525. foreach (DeviceNode device in this.Devices)
  526. {
  527. DeviceParameter dev = devHost.GetFirstOrDefaultDeviceID(device.DeviceHandle);
  528. if (dev == null || !(device.DeviceType == dev.DeviceType && device.DeviceModel == dev.DeviceModel))
  529. {
  530. device.State = false;
  531. ok = false;
  532. }
  533. else
  534. {
  535. device.State = true;
  536. continue;
  537. }
  538. }
  539. return ok;
  540. }
  541. /// <summary>
  542. ///
  543. /// </summary>
  544. /// <param name="isOrderByChannel">按照指标分组还是按照通道分组</param>
  545. public void Start(bool isOrderByChannel)
  546. {
  547. ShowMessage(MsgType.Info, string.Format("序列号:{0} 测试开始",Serial));
  548. isStop = false;
  549. //清空测试数据
  550. // BaseModel.TestResults.Clear();
  551. //选择勾选项
  552. List<TestNode> nodes = this.TestNodes.Where(x => x.IsSelected == true).ToList();
  553. if(isOrderByChannel)
  554. {
  555. //按照通道分组
  556. foreach (IGrouping<string, TestNode> group in nodes.GroupBy(x => x.Channel))
  557. {
  558. //排序后执行
  559. foreach (TestNode testnode in group.OrderBy(a => a.Order))
  560. {
  561. if(isStop == false)
  562. {
  563. ShowMessage(MsgType.Info, string.Format("开始执行测试指标:{0}/{1}", testnode.Channel, testnode.Name));
  564. if(testnode.TestModel != null)
  565. {
  566. //测试中途取消了勾选项
  567. if(testnode.IsSelected)
  568. {
  569. currModel = testnode.TestModel;
  570. testnode.TestModel.Start(testnode);
  571. }
  572. }
  573. else
  574. {
  575. ShowMessage(MsgType.Error, string.Format("测试指标:{0}/{1} 没有实例化执行模块", testnode.Channel, testnode.Name));
  576. }
  577. }
  578. else
  579. {
  580. break;
  581. }
  582. }
  583. }
  584. }
  585. else
  586. {
  587. //按照指标名称进行
  588. foreach (IGrouping<string, TestNode> group in nodes.GroupBy(x => x.Name))
  589. {
  590. //排序后执行
  591. foreach (TestNode testnode in group.OrderBy(a => a.Order))
  592. {
  593. if (isStop == false)
  594. {
  595. ShowMessage(MsgType.Info, string.Format("开始执行测试指标:{0}/{1}", testnode.Channel, testnode.Name));
  596. if (testnode.TestModel != null)
  597. {
  598. currModel = testnode.TestModel;
  599. testnode.TestModel.Start(testnode);
  600. }
  601. else
  602. {
  603. ShowMessage(MsgType.Error, string.Format("测试指标:{0}/{1} 没有实例化执行模块", testnode.Channel, testnode.Name));
  604. }
  605. }
  606. else
  607. {
  608. break;
  609. }
  610. }
  611. }
  612. }
  613. //保存自动测试记录表
  614. SaveTestTable();
  615. ShowMessage(MsgType.Info, string.Format("序列号:{0} 测试完成", Serial));
  616. }
  617. bool isStop = false;
  618. BaseModel currModel = null;
  619. public void Stop()
  620. {
  621. isStop = true;
  622. if(currModel!= null)
  623. {
  624. currModel.Stop();
  625. }
  626. ShowMessage(MsgType.Info, string.Format("序列号:{0} 测试中止", Serial));
  627. }
  628. public event DltShowMsg MessageEvent;
  629. /// <summary>
  630. /// 本对象显示消息,以及测试指标显示消息都经过这个方法
  631. /// </summary>
  632. /// <param name="msgType"></param>
  633. /// <param name="msg"></param>
  634. void ShowMessage(MsgType msgType, string msg)
  635. {
  636. if(MessageEvent != null)
  637. {
  638. MessageEvent(msgType, msg);
  639. }
  640. }
  641. public void ResetTestTable()
  642. {
  643. if(this.InitTestTable != null)
  644. {
  645. TestTable = this.InitTestTable.Copy();
  646. }
  647. }
  648. public void ResetManualTable()
  649. {
  650. if (this.InitManualTable != null)
  651. {
  652. ManualTable = this.InitManualTable.Copy();
  653. }
  654. }
  655. public AppLibs.Devices.IVISA GetDevice(string name)
  656. {
  657. DeviceNode node = this.Devices.FirstOrDefault(x => x.DeviceName == name);
  658. //不存在或者状态不对,都返回空
  659. if(node == null || node.State == false)
  660. {
  661. return null;
  662. }
  663. else
  664. {
  665. AppLibs.Devices.IDeviceHost devHost = Bundle.host.GetFirstOrDefaultService<AppLibs.Devices.IDeviceHost>();
  666. if (devHost == null)
  667. return null;
  668. DeviceParameter dev = devHost.GetFirstOrDefaultDeviceID(node.DeviceHandle);
  669. //要地址,名称,型号一致
  670. if (dev == null || !(node.DeviceType == dev.DeviceType && node.DeviceModel == dev.DeviceModel))
  671. {
  672. return null;
  673. }
  674. else
  675. {
  676. return (IVISA)dev;
  677. }
  678. }
  679. }
  680. public void SetManualTableCellValue(int row, int col, bool ok, object value)
  681. {
  682. if (ManualTableCellChanged != null)
  683. ManualTableCellChanged(row, col,ok, value);
  684. }
  685. public void SetTestTableCellValue(int row, int col, bool ok, object value)
  686. {
  687. if (TestTableCellChanged != null)
  688. TestTableCellChanged(row, col, ok, value);
  689. }
  690. void SaveTestTable()
  691. {
  692. if(string.IsNullOrEmpty(Serial))
  693. {
  694. ShowMessage(MsgType.Info, "序列号为空,数据不保存");
  695. return;
  696. }
  697. AppLibs.Function.IReportHost report = Bundle.host.GetFirstOrDefaultService<AppLibs.Function.IReportHost>();
  698. if(report != null)
  699. {
  700. AppLibs.Models.TableInfo tableInfo = new AppLibs.Models.TableInfo();
  701. tableInfo.data = TestTable;
  702. tableInfo.table = new AppLibs.Models.Table();
  703. tableInfo.table.Result = "合格";
  704. tableInfo.table.Temperature = Temperature;
  705. tableInfo.table.Tester = Tester;
  706. tableInfo.table.Time = DateTime.Now;
  707. tableInfo.table.Place = Place;
  708. tableInfo.table.Serial = Serial;
  709. tableInfo.table.Humidity = Humidity;
  710. tableInfo.table.Type = TestProject;
  711. tableInfo.table.ProductCode = Product;
  712. tableInfo.table.ProductName = Product;
  713. tableInfo.table.Name = TestProject + "#" + Product;
  714. if (report.WriteAndReplaceTable(tableInfo))
  715. {
  716. ShowMessage(MsgType.Info, "自动测试记录表保存成功");
  717. }
  718. else
  719. {
  720. ShowMessage(MsgType.Info, "自动测试记录表保存失败");
  721. }
  722. }
  723. else
  724. {
  725. ShowMessage(MsgType.Info, "软件未包含数据存储模块");
  726. }
  727. }
  728. //判断某个测试项是否勾选
  729. public bool IsTestItemSelected(string channel , string name)
  730. {
  731. var node = this.TestNodes.FirstOrDefault(x => x.Channel == channel && x.Name == name);
  732. if (node == null)
  733. return false;
  734. return node.IsSelected;
  735. }
  736. #endregion
  737. #region 二次修改的方法
  738. #endregion
  739. }
  740. }