AutoAnalysisXml.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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. using BenQGuru.eMES.DLLService;
  11. using System.Text;
  12. namespace UAS_AutoPass
  13. {
  14. public partial class AutoAnalysisXml : Form
  15. {
  16. DataHelper dh;
  17. DataTable dt;
  18. MESHelper helper = new MESHelper();
  19. string iusercode;
  20. string isource;
  21. Thread InitDB;
  22. StringBuilder sql = new StringBuilder();
  23. /// <summary>
  24. /// 缓存的文件
  25. /// </summary>
  26. public static string CachePath = Environment.GetEnvironmentVariable("windir").Substring(0, 1) + @":/UAS_MES/XmlAnalysor/Cache.xml";
  27. /// <summary>
  28. /// 缓存的文件夹
  29. /// </summary>
  30. public static string CachePathFolder = Environment.GetEnvironmentVariable("windir").Substring(0, 1) + @":/UAS_MES/XmlAnalysor/";
  31. public AutoAnalysisXml()
  32. {
  33. InitializeComponent();
  34. StartPosition = FormStartPosition.CenterScreen;
  35. }
  36. public AutoAnalysisXml(string iUserName, string iSource)
  37. {
  38. InitializeComponent();
  39. iusercode = iUserName;
  40. isource = iSource;
  41. StartPosition = FormStartPosition.CenterScreen;
  42. }
  43. private void Form1_Load(object sender, EventArgs e)
  44. {
  45. CheckForIllegalCrossThreadCalls = false;
  46. FormBorderStyle = FormBorderStyle.FixedSingle;
  47. InitDB = new Thread(ConnectDB);
  48. //添加监控事件
  49. XmlWatcher.Created += new FileSystemEventHandler(XmlWatcher_Created);
  50. SetLoadingWindow stw = new SetLoadingWindow(InitDB, "正在启动程序");
  51. stw.StartPosition = FormStartPosition.CenterScreen;
  52. stw.ShowDialog();
  53. List<string> CacheInf = new List<string>();
  54. dt = (DataTable)dh.ExecuteSql("select ms_pwd,ma_user,ma_address from master", "select");
  55. Master.DataSource = dt;
  56. Master.DisplayMember = "ma_user";
  57. Master.ValueMember = "ma_user";
  58. try
  59. {
  60. FolderPath.Text = BaseUtil.GetCacheData("FolderPath").ToString();
  61. BackUpFolderPath.Text = BaseUtil.GetCacheData("BackUpFolderPath").ToString();
  62. Master.Text = BaseUtil.GetCacheData("Master").ToString();
  63. AutoStart.Checked = (bool)BaseUtil.GetCacheData("AutoStart");
  64. }
  65. catch (Exception ex) { Console.WriteLine(ex.Message); }
  66. }
  67. private void ConnectDB()
  68. {
  69. dh = new DataHelper();
  70. }
  71. private void StartWatch_Click(object sender, EventArgs e)
  72. {
  73. if (FolderPath.Text == "" || BackUpFolderPath.Text == "")
  74. {
  75. OperateResult.AppendText("请选择监控文件夹和备份文件夹\n");
  76. return;
  77. }
  78. else
  79. {
  80. if (!Directory.Exists(FolderPath.Text))
  81. {
  82. OperateResult.AppendText("监控文件夹不存在\n");
  83. return;
  84. }
  85. if (!Directory.Exists(BackUpFolderPath.Text))
  86. {
  87. OperateResult.AppendText("备份文件夹不存在\n");
  88. return;
  89. }
  90. }
  91. if (FolderPath.Text == BackUpFolderPath.Text)
  92. {
  93. OperateResult.AppendText("监控文件夹和备份文件夹不能相同\n");
  94. return;
  95. }
  96. for (int i = 0; i < dt.Rows.Count; i++)
  97. {
  98. if (Master.Text == dt.Rows[i]["ma_user"].ToString())
  99. {
  100. DataHelper.DBConnectionString = "Data Source=" + dt.Rows[i]["ma_address"] + ";User ID=" + dt.Rows[i]["ma_user"] + ";PassWord=" + dt.Rows[i]["ms_pwd"]; ;
  101. dh = new DataHelper();
  102. }
  103. }
  104. if (!dh.CheckExist("make", "ma_statuscode='STARTED' and ma_code='" + MakeCode.Text + "'"))
  105. {
  106. OperateResult.AppendText("工单不存在或者未下放\n");
  107. return;
  108. }
  109. XmlWatcher.Path = FolderPath.Text;
  110. XmlWatcher.Filter = "*.xml";
  111. XmlWatcher.EnableRaisingEvents = true;
  112. BaseUtil.SetCacheData("FolderPath", FolderPath.Text);
  113. BaseUtil.SetCacheData("BackUpFolderPath", BackUpFolderPath.Text);
  114. BaseUtil.SetCacheData("Master", Master.Text);
  115. BaseUtil.SetCacheData("AutoStart", AutoStart.Checked);
  116. StartWatch.Enabled = false;
  117. ChooseFolder.Enabled = false;
  118. MakeCode.Enabled = false;
  119. ChooseBackUpFolder.Enabled = false;
  120. StopWatch.Enabled = true;
  121. OperateResult.AppendText("开始执行监控\n");
  122. }
  123. public void CreateNode(XmlDocument xmlDoc, XmlNode parentNode, string name, string value)
  124. {
  125. XmlNode node = xmlDoc.CreateNode(XmlNodeType.Element, name, null);
  126. node.InnerText = value;
  127. parentNode.AppendChild(node);
  128. }
  129. public void SetAutoRun()
  130. {
  131. if (AutoStart.Checked) //设置开机自启动
  132. {
  133. string path = Application.ExecutablePath;
  134. RegistryKey rk = Registry.LocalMachine;
  135. RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
  136. rk2.SetValue("UAS_XML解析器.exe", path);
  137. rk2.Close();
  138. rk.Close();
  139. }
  140. else //取消开机自启动
  141. {
  142. string path = Application.ExecutablePath;
  143. RegistryKey rk = Registry.LocalMachine;
  144. RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
  145. rk2.DeleteValue("UAS_XML解析器.exe", false);
  146. rk2.Close();
  147. rk.Close();
  148. }
  149. }
  150. private void XmlWatcher_Created(object sender, FileSystemEventArgs e)
  151. {
  152. while (true)
  153. {
  154. try
  155. {
  156. using (Stream stream = File.Open(e.FullPath, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
  157. {
  158. if (stream != null)
  159. break;
  160. }
  161. }
  162. catch (Exception ex)
  163. {
  164. Console.WriteLine(ex.Message);
  165. }
  166. }
  167. string test_date = "";
  168. string test_result = "";
  169. string test_sn = "";
  170. string imageurl = "";
  171. XmlReader myReader = XmlReader.Create(FolderPath.Text + @"\" + e.Name);
  172. //获取文件名的序列号,如SA123456.xml
  173. string sncode = e.Name.Split('.')[0];
  174. //获取序列号ID最大的工单号
  175. string iMakeCode = dh.getFieldDataByCondition("makeserial", "ms_makecode", "ms_sncode='" + sncode + "' order by ms_id desc").ToString();
  176. OperateResult.AppendText("读取文件" + e.Name + "\n");
  177. //获取采集的项目名称
  178. List<string> badcode = new List<string>();
  179. //获取采集项目的结果
  180. List<string> badlocation = new List<string>();
  181. int code_or_location = 0;
  182. while (myReader.Read())
  183. {
  184. if (myReader.NodeType == XmlNodeType.Element && myReader.Name == "test" && myReader.IsStartElement())
  185. {
  186. test_sn = myReader.GetAttribute("test_sn");
  187. test_result = myReader.GetAttribute("test_result");
  188. test_date = myReader.GetAttribute("test_date");
  189. imageurl = myReader.GetAttribute("imgurl");
  190. }
  191. if (myReader.NodeType == XmlNodeType.Text)
  192. {
  193. if (code_or_location % 2 == 0)
  194. {
  195. badcode.Add(myReader.Value);
  196. code_or_location++;
  197. }
  198. else
  199. {
  200. badlocation.Add(myReader.Value);
  201. code_or_location++;
  202. }
  203. }
  204. }
  205. myReader.Close();
  206. string ErrMessage = "";
  207. if (helper.GoMo(MakeCode.Text, sncode, isource, out ErrMessage))
  208. {
  209. string BadCode = "";
  210. if (test_result == "NG")
  211. {
  212. for (int i = 0; i < badcode.Count; i++)
  213. {
  214. BadCode += badcode[i] + ",";
  215. }
  216. }
  217. if (helper.SetStepFinish(MakeCode.Text, isource, sncode, "自动过站采集", test_result, iusercode, BadCode.Substring(0, BadCode.Length - 1), out ErrMessage))
  218. {
  219. //sql.Clear();
  220. //sql.Append("insert into makebadrsloc(mbl_id,mbl_mbrid,mbl_loc,mbl_badcode,mbl_brcode,");
  221. //sql.Append("mbl_sncode,mbl_makecode,mbl_indate ,mbl_inman) select makebadrsloc_seq.nextval," + mbr_id);
  222. //sql.Append(",:location,'" + bc_code.Text + "','" + nr_code.Text + "','" + GetSNCode.Text + "','" + macode + "',");
  223. //sql.Append("sysdate,'" + User.UserCode + "' from dual");
  224. }
  225. else
  226. {
  227. OperateResult.AppendText(ErrMessage + "\n");
  228. return;
  229. }
  230. }
  231. else
  232. {
  233. OperateResult.AppendText(ErrMessage + "\n");
  234. return;
  235. }
  236. FileInfo file = new FileInfo(FolderPath.Text + @"\" + e.Name);
  237. if (file.Exists)
  238. {
  239. try
  240. {
  241. for (int i = 1; i <= 20; i++)
  242. {
  243. if (!File.Exists(BackUpFolderPath.Text + @"\" + e.Name))
  244. {
  245. file.MoveTo(BackUpFolderPath.Text + @"\" + e.Name);
  246. OperateResult.AppendText("成功解析文件" + e.Name + "\n");
  247. break;
  248. }
  249. else if (!File.Exists(BackUpFolderPath.Text + @"\" + e.Name.Split('.')[0] + "(" + i + ")" + "." + e.Name.Split('.')[1]))
  250. {
  251. file.MoveTo(BackUpFolderPath.Text + @"\" + e.Name.Split('.')[0] + "(" + i + ")" + "." + e.Name.Split('.')[1]);
  252. OperateResult.AppendText("成功解析文件" + e.Name + "\n");
  253. break;
  254. }
  255. }
  256. }
  257. catch (Exception ex)
  258. {
  259. OperateResult.AppendText(e.Name + ex.Message + "\n");
  260. }
  261. }
  262. }
  263. private void StopWatch_Click(object sender, EventArgs e)
  264. {
  265. XmlWatcher.EnableRaisingEvents = false;
  266. StartWatch.Enabled = true;
  267. MakeCode.Enabled = true;
  268. ChooseFolder.Enabled = true;
  269. ChooseBackUpFolder.Enabled = true;
  270. StopWatch.Enabled = false;
  271. OperateResult.AppendText("停止执行监控\n");
  272. }
  273. private void Clean_Click(object sender, EventArgs e)
  274. {
  275. OperateResult.Clear();
  276. }
  277. private void ChooseFolder_Click(object sender, EventArgs e)
  278. {
  279. FolderBrowserDialog folder = new FolderBrowserDialog();
  280. folder.Description = "选择监控文件夹";
  281. DialogResult result = folder.ShowDialog();
  282. if (result == DialogResult.OK)
  283. {
  284. FolderPath.Text = folder.SelectedPath;
  285. }
  286. }
  287. private void ReadNodeFromXML(string FileName)
  288. {
  289. }
  290. private void ChooseBackUpFolder_Click(object sender, EventArgs e)
  291. {
  292. FolderBrowserDialog folder = new FolderBrowserDialog();
  293. folder.Description = "选择备份文件夹";
  294. DialogResult result = folder.ShowDialog();
  295. if (result == DialogResult.OK)
  296. {
  297. BackUpFolderPath.Text = folder.SelectedPath;
  298. }
  299. }
  300. private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  301. {
  302. string ExitConfirm = MessageBox.Show(this, "确认退出?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question).ToString();
  303. if (ExitConfirm != "Yes")
  304. {
  305. WindowState = FormWindowState.Minimized;
  306. e.Cancel = true;
  307. }
  308. }
  309. private void AutoStart_CheckedChanged(object sender, EventArgs e)
  310. {
  311. SetAutoRun();
  312. }
  313. }
  314. }