BaseUtil.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Text.RegularExpressions;
  5. using System.Web.Script.Serialization;
  6. using System.Xml;
  7. namespace FileWatcher
  8. {
  9. class BaseUtil
  10. {
  11. public static void CreateXml(string iSN, string iTestResult)
  12. {
  13. //创建XML文档
  14. FileStream fcaches = new FileStream(iSN + ".xml", FileMode.OpenOrCreate, FileAccess.ReadWrite);
  15. fcaches.Close();
  16. FileInfo info = new FileInfo(iSN + ".xml");
  17. if (info.Length == 0)
  18. {
  19. XmlDocument doc = new XmlDocument();
  20. //创建类型声明节点
  21. XmlNode node = doc.CreateXmlDeclaration("1.0", "utf-8", "");
  22. doc.AppendChild(node);
  23. //创建根节点
  24. XmlElement xeRoot = doc.CreateElement("cacheInfo");
  25. doc.AppendChild(xeRoot);
  26. doc.Save(iSN + ".xml");
  27. }
  28. }
  29. public static Dictionary<string, object> ToDictionary(string JsonData)
  30. {
  31. object Data = null;
  32. Dictionary<string, object> Dic = new Dictionary<string, object>();
  33. if (JsonData.StartsWith("["))
  34. {
  35. //如果目标直接就为数组类型,则将会直接输出一个Key为List的List<Dictionary<string, object>>集合
  36. //使用示例List<Dictionary<string, object>> ListDic = (List<Dictionary<string, object>>)Dic["List"];
  37. List<Dictionary<string, object>> List = new List<Dictionary<string, object>>();
  38. MatchCollection ListMatch = Regex.Matches(JsonData, @"{[\s\S]+?}");//使用正则表达式匹配出JSON数组
  39. foreach (Match ListItem in ListMatch)
  40. {
  41. List.Add(ToDictionary(ListItem.ToString()));//递归调用
  42. }
  43. Data = List;
  44. Dic.Add("List", Data);
  45. }
  46. else
  47. {
  48. MatchCollection Match = Regex.Matches(JsonData, @"""(.+?)"": {0,1}(\[[\s\S]+?\]|null|"".+?""|-{0,1}\d*)");//使用正则表达式匹配出JSON数据中的键与值
  49. foreach (Match item in Match)
  50. {
  51. try
  52. {
  53. if (item.Groups[2].ToString().StartsWith("["))
  54. {
  55. //如果目标是数组,将会输出一个Key为当前Json的List<Dictionary<string, object>>集合
  56. //使用示例List<Dictionary<string, object>> ListDic = (List<Dictionary<string, object>>)Dic["Json中的Key"];
  57. List<Dictionary<string, object>> List = new List<Dictionary<string, object>>();
  58. MatchCollection ListMatch = Regex.Matches(item.Groups[2].ToString(), @"{[\s\S]+?}");//使用正则表达式匹配出JSON数组
  59. foreach (Match ListItem in ListMatch)
  60. {
  61. List.Add(ToDictionary(ListItem.ToString()));//递归调用
  62. }
  63. Data = List;
  64. }
  65. else if (item.Groups[2].ToString().ToLower() == "null") Data = null;//如果数据为null(字符串类型),直接转换成null
  66. else Data = item.Groups[2].ToString(); //数据为数字、字符串中的一类,直接写入
  67. Dic.Add(item.Groups[1].ToString(), Data);
  68. }
  69. catch { }
  70. }
  71. }
  72. return Dic;
  73. }
  74. public static Dictionary<string, object> ToDictionary(string JsonData, string Type)
  75. {
  76. //实例化JavaScriptSerializer类的新实例
  77. JavaScriptSerializer jss = new JavaScriptSerializer();
  78. try
  79. {
  80. //将指定的 JSON 字符串转换为 Dictionary<string, object> 类型的对象
  81. return jss.Deserialize<Dictionary<string, object>>(JsonData);
  82. }
  83. catch (Exception ex)
  84. {
  85. throw new Exception(ex.Message);
  86. }
  87. }
  88. public static object GetCacheData(string ParamName)
  89. {
  90. try
  91. {
  92. object returnData = null;
  93. //根据地址读取xml文件
  94. XmlDocument doc = new XmlDocument();
  95. XmlReaderSettings settings = new XmlReaderSettings();
  96. //忽略文档里面的注释
  97. settings.IgnoreComments = true;
  98. XmlReader reader = XmlReader.Create(AutoAnalysisXml.CachePath, settings);
  99. doc.Load(reader);
  100. //先得到根节点
  101. XmlNode rootNode = doc.SelectSingleNode("cacheInfo");
  102. //再由根节点去找制定的节点
  103. XmlNodeList nodeList = rootNode.ChildNodes;
  104. foreach (XmlNode node in nodeList)
  105. {
  106. //找到了这个节点名字
  107. if (node.Name == ParamName)
  108. {
  109. //返回节点的内容
  110. switch (((XmlElement)node).GetAttribute("Type"))
  111. {
  112. case "System.String":
  113. returnData = node.InnerText;
  114. break;
  115. case "System.Int32":
  116. returnData = int.Parse(node.InnerText);
  117. break;
  118. case "System.Boolean":
  119. returnData = node.InnerText == "True" ? true : false;
  120. break;
  121. default:
  122. break;
  123. }
  124. break;
  125. }
  126. }
  127. //关闭reader
  128. reader.Close();
  129. if (returnData == null)
  130. return "";
  131. else
  132. return returnData;
  133. }
  134. catch (Exception e)
  135. {
  136. Console.WriteLine(e.Message + e.StackTrace);
  137. return "";
  138. }
  139. }
  140. public static void SetCacheData(string ParamName, object Value)
  141. {
  142. try
  143. {
  144. //根据地址读取xml文件
  145. XmlDocument doc = new XmlDocument();
  146. XmlReaderSettings settings = new XmlReaderSettings();
  147. //忽略文档里面的注释
  148. settings.IgnoreComments = true;
  149. XmlReader reader = XmlReader.Create(AutoAnalysisXml.CachePath, settings);
  150. doc.Load(reader);
  151. //先得到根节点
  152. XmlNode rootNode = doc.SelectSingleNode("cacheInfo");
  153. //再由根节点去找制定的节点
  154. XmlNodeList nodeList = rootNode.ChildNodes;
  155. bool flag = false;
  156. foreach (XmlNode node in nodeList)
  157. {
  158. //找到了这个节点名字
  159. if (node.Name == ParamName)
  160. {
  161. //就直接赋值
  162. node.InnerText = Value.ToString();
  163. flag = true;
  164. }
  165. }
  166. //如果没有该节点,就创建节点保存结果
  167. if (!flag)
  168. {
  169. //创建节点
  170. XmlElement newNode = doc.CreateElement(ParamName);
  171. XmlAttribute attr = doc.CreateAttribute("Type");
  172. attr.InnerText = Value.GetType().ToString();
  173. newNode.InnerText = Value.ToString();
  174. newNode.SetAttributeNode(attr);
  175. //讲新建的节点挂到根节点上
  176. rootNode.AppendChild(newNode);
  177. }
  178. //关闭Reader
  179. reader.Close();
  180. doc.Save(AutoAnalysisXml.CachePath);
  181. }
  182. catch (Exception ex)
  183. {
  184. Console.WriteLine(ex.Message + ex.StackTrace);
  185. }
  186. }
  187. //播放音频文件
  188. public static void PlaySound(string FileName)
  189. {
  190. //要加载COM组件:Microsoft speech object Library
  191. if (!System.IO.File.Exists(FileName))
  192. {
  193. return;
  194. }
  195. //SpeechLib.SpVoiceClass pp = new SpeechLib.SpVoiceClass();
  196. //SpeechLib.SpFileStreamClass spFs = new SpeechLib.SpFileStreamClass();
  197. //spFs.Open(FileName, SpeechLib.SpeechStreamFileMode.SSFMOpenForRead, true);
  198. //SpeechLib.ISpeechBaseStream Istream = spFs as SpeechLib.ISpeechBaseStream;
  199. //pp.SpeakStream(Istream, SpeechLib.SpeechVoiceSpeakFlags.SVSFIsFilename);
  200. //spFs.Close();
  201. }
  202. public static void GetWriteInfo(string FilePath, out Dictionary<string, string> Dic)
  203. {
  204. Dic = new Dictionary<string, string>();
  205. string txt = "";
  206. StreamReader sr = new StreamReader(FilePath);
  207. while (!sr.EndOfStream)
  208. {
  209. string str = sr.ReadLine();
  210. txt += str + "\n";
  211. }
  212. sr.Close();
  213. sr.Dispose();
  214. Dic.Add("atd_sncode", FilePath.Substring(FilePath.LastIndexOf("\\") + 1).Replace(".txt", ""));
  215. Dic.Add("atd_software", Regex.Match(txt, "Program Name,\\S+").Value.Replace("Program Name,", ""));
  216. Dic.Add("atd_pot", Regex.Match(txt, "Board Segment,\\S+").Value.Replace("Board Segment,", ""));
  217. Dic.Add("atd_size", Regex.Match(txt, "Baord Size \\(L x W\\) \\[mm\\],\\S+").Value.Replace("Baord Size (L x W) [mm],", ""));
  218. Dic.Add("atd_pot1set", Regex.Match(txt, "Pot-1 Set Temp. \\[deg],\\S+").Value.Replace("Pot-1 Set Temp. [deg],", ""));
  219. Dic.Add("atd_pot2set", Regex.Match(txt, "Pot-2 Set Temp. \\[deg],\\S+").Value.Replace("Pot-2 Set Temp. [deg],", ""));
  220. Dic.Add("atd_pot1avgtemp", Regex.Match(txt, "Pot-1 Avg. Temp. \\[deg],\\S+").Value.Replace("Pot-1 Avg. Temp. [deg],", ""));
  221. Dic.Add("atd_pot2avgtemp", Regex.Match(txt, "Pot-2 Avg. Temp. \\[deg],\\S+").Value.Replace("Pot-2 Avg. Temp. [deg],", ""));
  222. Dic.Add("atd_boardtime", Regex.Match(txt, "Machine Duration \\[s],\\S+").Value.Replace("Machine Duration [s],", ""));
  223. //开始时间
  224. MatchCollection starttime = Regex.Matches(txt, "Start TimeStamp,\\S+ \\S+");
  225. for (int i = 0; i < starttime.Count; i++)
  226. {
  227. switch (i)
  228. {
  229. case 0:
  230. Dic.Add("atd_boardstarttime", starttime[i].Value.Replace("Start TimeStamp,", ""));
  231. break;
  232. case 1:
  233. Dic.Add("atd_flstarttime", starttime[i].Value.Replace("Start TimeStamp,", ""));
  234. break;
  235. case 2:
  236. Dic.Add("atd_phstarttime", starttime[i].Value.Replace("Start TimeStamp,", ""));
  237. break;
  238. case 3:
  239. Dic.Add("atd_sostarttime", starttime[i].Value.Replace("Start TimeStamp,", ""));
  240. break;
  241. default:
  242. break;
  243. }
  244. }
  245. MatchCollection endtime = Regex.Matches(txt, "End TimeStamp,\\S+ \\S+");
  246. for (int i = 0; i < starttime.Count; i++)
  247. {
  248. switch (i)
  249. {
  250. case 0:
  251. Dic.Add("atd_boardendtime", endtime[i].Value.Replace("End TimeStamp,", ""));
  252. break;
  253. case 1:
  254. Dic.Add("atd_flendtime", endtime[i].Value.Replace("End TimeStamp,", ""));
  255. break;
  256. case 2:
  257. Dic.Add("atd_phendtime", endtime[i].Value.Replace("End TimeStamp,", ""));
  258. break;
  259. case 3:
  260. Dic.Add("atd_soendtime", endtime[i].Value.Replace("End TimeStamp,", ""));
  261. break;
  262. default:
  263. break;
  264. }
  265. }
  266. MatchCollection duration = Regex.Matches(txt, "Total Zone Duration \\[s],\\S+");
  267. for (int i = 0; i < starttime.Count; i++)
  268. {
  269. switch (i)
  270. {
  271. case 0:
  272. Dic.Add("atd_fltime", duration[i].Value.Replace("Total Zone Duration [s],", ""));
  273. break;
  274. case 1:
  275. Dic.Add("atd_phtime", duration[i].Value.Replace("Total Zone Duration [s],", ""));
  276. break;
  277. case 2:
  278. Dic.Add("atd_sotime", duration[i].Value.Replace("Total Zone Duration [s],", ""));
  279. break;
  280. default:
  281. break;
  282. }
  283. }
  284. }
  285. }
  286. }