AutoAnalysisXml.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. using Microsoft.Win32;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Data;
  5. using System.IO;
  6. using System.Threading;
  7. using System.Windows.Forms;
  8. using System.Xml;
  9. using UAS_AutoPass.ToolClass;
  10. namespace UAS_AutoPass
  11. {
  12. public partial class AutoAnalysisXml : Form
  13. {
  14. DataHelper dh;
  15. DataTable dt;
  16. Thread InitDB;
  17. /// <summary>
  18. /// 缓存的文件
  19. /// </summary>
  20. string CachePath = Environment.GetEnvironmentVariable("windir").Substring(0, 1) + @":/UAS_MES/XmlAnalysor/Cache.xml";
  21. /// <summary>
  22. /// 缓存的文件夹
  23. /// </summary>
  24. string CachePathFolder = Environment.GetEnvironmentVariable("windir").Substring(0, 1) + @":/UAS_MES/XmlAnalysor/";
  25. public AutoAnalysisXml()
  26. {
  27. InitializeComponent();
  28. StartPosition = FormStartPosition.CenterScreen;
  29. }
  30. private void Form1_Load(object sender, EventArgs e)
  31. {
  32. CheckForIllegalCrossThreadCalls = false;
  33. FormBorderStyle = FormBorderStyle.FixedSingle;
  34. InitDB = new Thread(ConnectDB);
  35. //添加监控事件
  36. XmlWatcher.Created += new FileSystemEventHandler(XmlWatcher_Created);
  37. SetLoadingWindow stw = new SetLoadingWindow(InitDB, "正在启动程序");
  38. stw.StartPosition = FormStartPosition.CenterScreen;
  39. stw.ShowDialog();
  40. List<string> CacheInf = new List<string>();
  41. dt = (DataTable)dh.ExecuteSql("select ms_pwd,ma_user,ma_address from master", "select");
  42. Master.DataSource = dt;
  43. Master.DisplayMember = "ma_user";
  44. Master.ValueMember = "ma_user";
  45. try
  46. {
  47. if (!Directory.Exists(CachePathFolder))
  48. Directory.CreateDirectory(CachePathFolder);
  49. XmlReader myReader = XmlReader.Create(CachePath);
  50. while (myReader.Read())
  51. {
  52. if (myReader.NodeType == XmlNodeType.Text)
  53. CacheInf.Add(myReader.Value);
  54. }
  55. myReader.Close();
  56. string[] Info = CacheInf.ToArray();
  57. FolderPath.Text = Info[0];
  58. BackUpFolderPath.Text = Info[1];
  59. Source.Text = Info[2];
  60. Master.Text = Info[3];
  61. AutoStart.Checked = (Info[4] == "True" ? true : false);
  62. }
  63. catch (Exception) { }
  64. StartWatch.PerformClick();
  65. }
  66. private void ConnectDB()
  67. {
  68. dh = new DataHelper();
  69. }
  70. private void StartWatch_Click(object sender, EventArgs e)
  71. {
  72. if (FolderPath.Text == "" || BackUpFolderPath.Text == "")
  73. {
  74. OperateResult.AppendText("请选择监控文件夹和备份文件夹\n");
  75. return;
  76. }
  77. else
  78. {
  79. if (!Directory.Exists(FolderPath.Text))
  80. {
  81. OperateResult.AppendText("监控文件夹不存在\n");
  82. return;
  83. }
  84. if (!Directory.Exists(BackUpFolderPath.Text))
  85. {
  86. OperateResult.AppendText("备份文件夹不存在\n");
  87. return;
  88. }
  89. }
  90. if (FolderPath.Text == BackUpFolderPath.Text)
  91. {
  92. OperateResult.AppendText("监控文件夹和备份文件夹不能相同\n");
  93. return;
  94. }
  95. for (int i = 0; i < dt.Rows.Count; i++)
  96. {
  97. if (Master.Text == dt.Rows[i]["ma_user"].ToString())
  98. {
  99. DataHelper.DBConnectionString = "Data Source=" + dt.Rows[i]["ma_address"] + ";User ID=" + dt.Rows[i]["ma_user"] + ";PassWord=" + dt.Rows[i]["ms_pwd"]; ;
  100. dh = new DataHelper();
  101. }
  102. }
  103. if (!dh.CheckExist("source", "sc_code='" + Source.Text + "' and sc_statuscode='AUDITED'"))
  104. {
  105. OperateResult.AppendText("岗位资源错误或者未审核\n");
  106. return;
  107. }
  108. XmlWatcher.Path = FolderPath.Text;
  109. XmlWatcher.Filter = "*.xml";
  110. XmlWatcher.EnableRaisingEvents = true;
  111. try
  112. {
  113. File.Delete(CachePath);
  114. }
  115. catch (Exception) { }
  116. XmlDocument xmlDoc = new XmlDocument();
  117. //创建类型声明节点
  118. XmlNode node = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", "");
  119. xmlDoc.AppendChild(node);
  120. //创建根节点
  121. XmlElement xeRoot = xmlDoc.CreateElement("CacheInf");
  122. xmlDoc.AppendChild(xeRoot);
  123. CreateNode(xmlDoc, xeRoot, "FolderPath", FolderPath.Text);
  124. CreateNode(xmlDoc, xeRoot, "BackUpFolderPath", BackUpFolderPath.Text);
  125. CreateNode(xmlDoc, xeRoot, "Source", Source.Text);
  126. CreateNode(xmlDoc, xeRoot, "Master", Master.Text);
  127. CreateNode(xmlDoc, xeRoot, "AutoStart", AutoStart.Checked.ToString());
  128. xmlDoc.Save(CachePath);
  129. Source.Enabled = false;
  130. StartWatch.Enabled = false;
  131. ChooseFolder.Enabled = false;
  132. ChooseBackUpFolder.Enabled = false;
  133. StopWatch.Enabled = true;
  134. OperateResult.AppendText("开始执行监控\n");
  135. }
  136. public void CreateNode(XmlDocument xmlDoc, XmlNode parentNode, string name, string value)
  137. {
  138. XmlNode node = xmlDoc.CreateNode(XmlNodeType.Element, name, null);
  139. node.InnerText = value;
  140. parentNode.AppendChild(node);
  141. }
  142. public void SetAutoRun()
  143. {
  144. if (AutoStart.Checked) //设置开机自启动
  145. {
  146. string path = Application.ExecutablePath;
  147. RegistryKey rk = Registry.LocalMachine;
  148. RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
  149. rk2.SetValue("UAS_XML解析器.exe", path);
  150. rk2.Close();
  151. rk.Close();
  152. }
  153. else //取消开机自启动
  154. {
  155. string path = Application.ExecutablePath;
  156. RegistryKey rk = Registry.LocalMachine;
  157. RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
  158. rk2.DeleteValue("UAS_XML解析器.exe", false);
  159. rk2.Close();
  160. rk.Close();
  161. }
  162. }
  163. private void XmlWatcher_Created(object sender, FileSystemEventArgs e)
  164. {
  165. while (true)
  166. {
  167. try
  168. {
  169. using (Stream stream = File.Open(e.FullPath, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
  170. {
  171. if (stream != null)
  172. break;
  173. }
  174. }
  175. catch (Exception ex)
  176. {
  177. Console.WriteLine(ex.Message);
  178. }
  179. }
  180. string testDate = "";
  181. string testTime = "";
  182. XmlReader myReader = XmlReader.Create(FolderPath.Text + @"\" + e.Name);
  183. //获取文件名的序列号,如SA123456.xml
  184. string sncode = e.Name.Split('.')[0];
  185. //获取序列号ID最大的工单号
  186. string iMakeCode = dh.getFieldDataByCondition("makeserial", "ms_makecode", "ms_sncode='" + sncode + "' order by ms_id desc").ToString();
  187. OperateResult.AppendText("读取文件" + e.Name + "\n");
  188. //获取采集的项目名称
  189. List<string> name = new List<string>();
  190. //获取采集项目的结果
  191. List<string> result = new List<string>();
  192. int name_or_result = 0;
  193. while (myReader.Read())
  194. {
  195. if (myReader.NodeType == XmlNodeType.Element && myReader.Name == "test")
  196. {
  197. testDate = myReader.GetAttribute(1);
  198. testTime = myReader.GetAttribute(0);
  199. }
  200. if (myReader.NodeType == XmlNodeType.Text)
  201. {
  202. if (name_or_result % 2 == 0)
  203. {
  204. name.Add(myReader.Value);
  205. name_or_result++;
  206. }
  207. else
  208. {
  209. result.Add(myReader.Value);
  210. name_or_result++;
  211. }
  212. }
  213. }
  214. string date = testDate + " " + testTime;
  215. string sql = "insert into STEPTESTDETAIL(std_id,std_makecode,std_sn,std_subclass1,std_testresult,std_indate,";
  216. sql += "std_rescode,std_testdate,std_testtime,std_date) values(STEPTESTDETAIL_seq.nextval, '" + iMakeCode + "', ";
  217. sql += "'" + sncode + "',:std_subclass1,:std_testresult, sysdate,'" + Source.Text + "',to_char(to_date('" + testDate + "','YYYY/MM/DD'), 'YYYYMMDD'),";
  218. sql += "to_char(to_date('" + testTime + "','hh24:mi:ss'), 'hh24miss'),to_date('" + date + "','YYYY/MM/DD hh24:mi:ss'))";
  219. dh.BatchInsert(sql, new string[] { "std_subclass1", "std_testresult" }, name.ToArray(), result.ToArray());
  220. myReader.Close();
  221. FileInfo file = new FileInfo(FolderPath.Text + @"\" + e.Name);
  222. if (file.Exists)
  223. {
  224. try
  225. {
  226. for (int i = 1; i <= 20; i++)
  227. {
  228. if (!File.Exists(BackUpFolderPath.Text + @"\" + e.Name))
  229. {
  230. file.MoveTo(BackUpFolderPath.Text + @"\" + e.Name);
  231. OperateResult.AppendText("成功解析文件" + e.Name + "\n");
  232. break;
  233. }
  234. else if (!File.Exists(BackUpFolderPath.Text + @"\" + e.Name.Split('.')[0] + "(" + i + ")" + "." + e.Name.Split('.')[1]))
  235. {
  236. file.MoveTo(BackUpFolderPath.Text + @"\" + e.Name.Split('.')[0] + "(" + i + ")" + "." + e.Name.Split('.')[1]);
  237. OperateResult.AppendText("成功解析文件" + e.Name + "\n");
  238. break;
  239. }
  240. }
  241. }
  242. catch (Exception ex)
  243. {
  244. OperateResult.AppendText(e.Name + ex.Message + "\n");
  245. }
  246. }
  247. }
  248. private void StopWatch_Click(object sender, EventArgs e)
  249. {
  250. XmlWatcher.EnableRaisingEvents = false;
  251. Source.Enabled = true;
  252. StartWatch.Enabled = true;
  253. ChooseFolder.Enabled = true;
  254. ChooseBackUpFolder.Enabled = true;
  255. StopWatch.Enabled = false;
  256. OperateResult.AppendText("停止执行监控\n");
  257. }
  258. private void Clean_Click(object sender, EventArgs e)
  259. {
  260. OperateResult.Clear();
  261. }
  262. private void ChooseFolder_Click(object sender, EventArgs e)
  263. {
  264. FolderBrowserDialog folder = new FolderBrowserDialog();
  265. folder.Description = "选择监控文件夹";
  266. DialogResult result = folder.ShowDialog();
  267. if (result == DialogResult.OK)
  268. {
  269. FolderPath.Text = folder.SelectedPath;
  270. }
  271. }
  272. private void ReadNodeFromXML(string FileName)
  273. {
  274. }
  275. private void ChooseBackUpFolder_Click(object sender, EventArgs e)
  276. {
  277. FolderBrowserDialog folder = new FolderBrowserDialog();
  278. folder.Description = "选择备份文件夹";
  279. DialogResult result = folder.ShowDialog();
  280. if (result == DialogResult.OK)
  281. {
  282. BackUpFolderPath.Text = folder.SelectedPath;
  283. }
  284. }
  285. private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  286. {
  287. string ExitConfirm = MessageBox.Show(this, "确认退出?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question).ToString();
  288. if (ExitConfirm != "Yes")
  289. {
  290. WindowState = FormWindowState.Minimized;
  291. e.Cancel = true;
  292. }
  293. }
  294. private void AutoStart_CheckedChanged(object sender, EventArgs e)
  295. {
  296. SetAutoRun();
  297. }
  298. }
  299. }