BaseUtil.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Text.RegularExpressions;
  5. using System.Xml;
  6. namespace UAS_AutoPass.ToolClass
  7. {
  8. class BaseUtil
  9. {
  10. public static void CreateXml(string iSN, string iTestResult)
  11. {
  12. //创建XML文档
  13. FileStream fcaches = new FileStream(iSN + ".xml", FileMode.OpenOrCreate, FileAccess.ReadWrite);
  14. fcaches.Close();
  15. FileInfo info = new FileInfo(iSN + ".xml");
  16. if (info.Length == 0)
  17. {
  18. XmlDocument doc = new XmlDocument();
  19. //创建类型声明节点
  20. XmlNode node = doc.CreateXmlDeclaration("1.0", "utf-8", "");
  21. doc.AppendChild(node);
  22. //创建根节点
  23. XmlElement xeRoot = doc.CreateElement("cacheInfo");
  24. doc.AppendChild(xeRoot);
  25. doc.Save(iSN + ".xml");
  26. }
  27. }
  28. public static object GetCacheData(string ParamName)
  29. {
  30. try
  31. {
  32. object returnData = null;
  33. //根据地址读取xml文件
  34. XmlDocument doc = new XmlDocument();
  35. XmlReaderSettings settings = new XmlReaderSettings();
  36. //忽略文档里面的注释
  37. settings.IgnoreComments = true;
  38. XmlReader reader = XmlReader.Create(AutoAnalysisXml.CachePath, settings);
  39. doc.Load(reader);
  40. //先得到根节点
  41. XmlNode rootNode = doc.SelectSingleNode("cacheInfo");
  42. //再由根节点去找制定的节点
  43. XmlNodeList nodeList = rootNode.ChildNodes;
  44. foreach (XmlNode node in nodeList)
  45. {
  46. //找到了这个节点名字
  47. if (node.Name == ParamName)
  48. {
  49. //返回节点的内容
  50. switch (((XmlElement)node).GetAttribute("Type"))
  51. {
  52. case "System.String":
  53. returnData = node.InnerText;
  54. break;
  55. case "System.Int32":
  56. returnData = int.Parse(node.InnerText);
  57. break;
  58. case "System.Boolean":
  59. returnData = node.InnerText == "True" ? true : false;
  60. break;
  61. default:
  62. break;
  63. }
  64. break;
  65. }
  66. }
  67. //关闭reader
  68. reader.Close();
  69. if (returnData == null)
  70. return "";
  71. else
  72. return returnData;
  73. }
  74. catch (Exception e)
  75. {
  76. Console.WriteLine(e.Message + e.StackTrace);
  77. return "";
  78. }
  79. }
  80. public static void SetCacheData(string ParamName, object Value)
  81. {
  82. try
  83. {
  84. //根据地址读取xml文件
  85. XmlDocument doc = new XmlDocument();
  86. XmlReaderSettings settings = new XmlReaderSettings();
  87. //忽略文档里面的注释
  88. settings.IgnoreComments = true;
  89. XmlReader reader = XmlReader.Create(AutoAnalysisXml.CachePath, settings);
  90. doc.Load(reader);
  91. //先得到根节点
  92. XmlNode rootNode = doc.SelectSingleNode("cacheInfo");
  93. //再由根节点去找制定的节点
  94. XmlNodeList nodeList = rootNode.ChildNodes;
  95. bool flag = false;
  96. foreach (XmlNode node in nodeList)
  97. {
  98. //找到了这个节点名字
  99. if (node.Name == ParamName)
  100. {
  101. //就直接赋值
  102. node.InnerText = Value.ToString();
  103. flag = true;
  104. }
  105. }
  106. //如果没有该节点,就创建节点保存结果
  107. if (!flag)
  108. {
  109. //创建节点
  110. XmlElement newNode = doc.CreateElement(ParamName);
  111. XmlAttribute attr = doc.CreateAttribute("Type");
  112. attr.InnerText = Value.GetType().ToString();
  113. newNode.InnerText = Value.ToString();
  114. newNode.SetAttributeNode(attr);
  115. //讲新建的节点挂到根节点上
  116. rootNode.AppendChild(newNode);
  117. }
  118. //关闭Reader
  119. reader.Close();
  120. doc.Save(AutoAnalysisXml.CachePath);
  121. }
  122. catch (Exception ex)
  123. {
  124. Console.WriteLine(ex.Message + ex.StackTrace);
  125. }
  126. }
  127. //播放音频文件
  128. public static void PlaySound(string FileName)
  129. {
  130. //要加载COM组件:Microsoft speech object Library
  131. if (!System.IO.File.Exists(FileName))
  132. {
  133. return;
  134. }
  135. SpeechLib.SpVoiceClass pp = new SpeechLib.SpVoiceClass();
  136. SpeechLib.SpFileStreamClass spFs = new SpeechLib.SpFileStreamClass();
  137. spFs.Open(FileName, SpeechLib.SpeechStreamFileMode.SSFMOpenForRead, true);
  138. SpeechLib.ISpeechBaseStream Istream = spFs as SpeechLib.ISpeechBaseStream;
  139. pp.SpeakStream(Istream, SpeechLib.SpeechVoiceSpeakFlags.SVSFIsFilename);
  140. spFs.Close();
  141. }
  142. public static void GetWriteInfo(string FilePath, out Dictionary<string, string> Dic)
  143. {
  144. Dic = new Dictionary<string, string>();
  145. string txt = "";
  146. StreamReader sr = new StreamReader(FilePath);
  147. while (!sr.EndOfStream)
  148. {
  149. string str = sr.ReadLine();
  150. txt += str + "\n";
  151. }
  152. sr.Close();
  153. sr.Dispose();
  154. Dic.Add("atd_sncode", FilePath.Substring(FilePath.LastIndexOf("\\") + 1).Replace(".txt", ""));
  155. Dic.Add("atd_software", Regex.Match(txt, "Program Name,\\S+").Value.Replace("Program Name,", ""));
  156. Dic.Add("atd_pot", Regex.Match(txt, "Board Segment,\\S+").Value.Replace("Board Segment,", ""));
  157. Dic.Add("atd_size", Regex.Match(txt, "Baord Size \\(L x W\\) \\[mm\\],\\S+").Value.Replace("Baord Size (L x W) [mm],", ""));
  158. Dic.Add("atd_pot1set", Regex.Match(txt, "Pot-1 Set Temp. \\[deg],\\S+").Value.Replace("Pot-1 Set Temp. [deg],", ""));
  159. Dic.Add("atd_pot2set", Regex.Match(txt, "Pot-2 Set Temp. \\[deg],\\S+").Value.Replace("Pot-2 Set Temp. [deg],", ""));
  160. Dic.Add("atd_pot1avgtemp", Regex.Match(txt, "Pot-1 Avg. Temp. \\[deg],\\S+").Value.Replace("Pot-1 Avg. Temp. [deg],", ""));
  161. Dic.Add("atd_pot2avgtemp", Regex.Match(txt, "Pot-2 Avg. Temp. \\[deg],\\S+").Value.Replace("Pot-2 Avg. Temp. [deg],", ""));
  162. Dic.Add("atd_boardtime", Regex.Match(txt, "Machine Duration \\[s],\\S+").Value.Replace("Machine Duration [s],", ""));
  163. //开始时间
  164. MatchCollection starttime = Regex.Matches(txt, "Start TimeStamp,\\S+ \\S+");
  165. for (int i = 0; i < starttime.Count; i++)
  166. {
  167. switch (i)
  168. {
  169. case 0:
  170. Dic.Add("atd_boardstarttime", starttime[i].Value.Replace("Start TimeStamp,", ""));
  171. break;
  172. case 1:
  173. Dic.Add("atd_flstarttime", starttime[i].Value.Replace("Start TimeStamp,", ""));
  174. break;
  175. case 2:
  176. Dic.Add("atd_phstarttime", starttime[i].Value.Replace("Start TimeStamp,", ""));
  177. break;
  178. case 3:
  179. Dic.Add("atd_sostarttime", starttime[i].Value.Replace("Start TimeStamp,", ""));
  180. break;
  181. default:
  182. break;
  183. }
  184. }
  185. MatchCollection endtime = Regex.Matches(txt, "End TimeStamp,\\S+ \\S+");
  186. for (int i = 0; i < starttime.Count; i++)
  187. {
  188. switch (i)
  189. {
  190. case 0:
  191. Dic.Add("atd_boardendtime", endtime[i].Value.Replace("End TimeStamp,", ""));
  192. break;
  193. case 1:
  194. Dic.Add("atd_flendtime", endtime[i].Value.Replace("End TimeStamp,", ""));
  195. break;
  196. case 2:
  197. Dic.Add("atd_phendtime", endtime[i].Value.Replace("End TimeStamp,", ""));
  198. break;
  199. case 3:
  200. Dic.Add("atd_soendtime", endtime[i].Value.Replace("End TimeStamp,", ""));
  201. break;
  202. default:
  203. break;
  204. }
  205. }
  206. MatchCollection duration = Regex.Matches(txt, "Total Zone Duration \\[s],\\S+");
  207. for (int i = 0; i < starttime.Count; i++)
  208. {
  209. switch (i)
  210. {
  211. case 0:
  212. Dic.Add("atd_fltime", duration[i].Value.Replace("Total Zone Duration [s],", ""));
  213. break;
  214. case 1:
  215. Dic.Add("atd_phtime", duration[i].Value.Replace("Total Zone Duration [s],", ""));
  216. break;
  217. case 2:
  218. Dic.Add("atd_sotime", duration[i].Value.Replace("Total Zone Duration [s],", ""));
  219. break;
  220. default:
  221. break;
  222. }
  223. }
  224. }
  225. }
  226. }