MainTps.cs 35 KB

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