AutoAnalysisXml.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728
  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 System.Text;
  11. namespace UAS_AutoPass
  12. {
  13. public partial class AutoAnalysisXml : Form
  14. {
  15. DataHelper dh;
  16. DataTable dt;
  17. DataTable DB;
  18. /// <summary>
  19. /// 用户编号
  20. /// </summary>
  21. string iusercode;
  22. /// <summary>
  23. /// 岗位资源
  24. /// </summary>
  25. string isource;
  26. Tip tipform;
  27. Thread InitDB;
  28. /// <summary>
  29. /// 线别
  30. /// </summary>
  31. string ilinecode;
  32. /// <summary>
  33. /// 不良代码组
  34. /// </summary>
  35. string ibadgroup;
  36. /// <summary>
  37. /// 当前工序
  38. /// </summary>
  39. string istepcode;
  40. StringBuilder sql = new StringBuilder();
  41. /// <summary>
  42. /// 缓存的文件
  43. /// </summary>
  44. public static string CachePath = Environment.GetEnvironmentVariable("windir").Substring(0, 1) + @":/UAS_MES/XmlAnalysor/Cache.xml";
  45. /// <summary>
  46. /// 缓存的文件夹
  47. /// </summary>
  48. public static string CachePathFolder = Environment.GetEnvironmentVariable("windir").Substring(0, 1) + @":/UAS_MES/XmlAnalysor/";
  49. /// <summary>
  50. /// 需要解析的文件名
  51. /// </summary>
  52. List<string> _FileName = new List<string>();
  53. public AutoAnalysisXml()
  54. {
  55. tipform = new Tip();
  56. InitializeComponent();
  57. StartPosition = FormStartPosition.CenterScreen;
  58. }
  59. public AutoAnalysisXml(string iUserName, string iSource)
  60. {
  61. tipform = new Tip();
  62. InitializeComponent();
  63. iusercode = iUserName;
  64. isource = iSource.ToUpper();
  65. StartPosition = FormStartPosition.CenterScreen;
  66. }
  67. private void Form1_Load(object sender, EventArgs e)
  68. {
  69. tipform.Show();
  70. CheckForIllegalCrossThreadCalls = false;
  71. FormBorderStyle = FormBorderStyle.FixedSingle;
  72. InitDB = new Thread(ConnectDB);
  73. //添加监控事件
  74. XmlWatcher.Created += new FileSystemEventHandler(XmlWatcher_Created);
  75. SetLoadingWindow stw = new SetLoadingWindow(InitDB, "正在启动程序");
  76. stw.StartPosition = FormStartPosition.CenterScreen;
  77. stw.ShowDialog();
  78. List<string> CacheInf = new List<string>();
  79. DB = (DataTable)dh.ExecuteSql("select ms_pwd,ma_user,ma_address,ma_inneraddress from master where ma_user='MES'", "select");
  80. Master.DataSource = DB;
  81. Master.DisplayMember = "ma_user";
  82. Master.ValueMember = "ma_user";
  83. //获取缓存信息
  84. try
  85. {
  86. FolderPath.Text = BaseUtil.GetCacheData("FolderPath").ToString();
  87. BackUpFolderPath.Text = BaseUtil.GetCacheData("BackUpFolderPath").ToString();
  88. Master.Text = BaseUtil.GetCacheData("Master").ToString();
  89. //AutoStart.Checked = (bool)BaseUtil.GetCacheData("AutoStart");
  90. }
  91. catch (Exception ex) { MessageBox.Show(ex.Message); }
  92. for (int i = 0; i < DB.Rows.Count; i++)
  93. {
  94. if (Master.Text == DB.Rows[i]["ma_user"].ToString())
  95. {
  96. DataHelper.DBConnectionString = "Connection Timeout=0;Pooling=false;Password=select!#%*(;User ID=" + DB.Rows[i]["ma_user"].ToString() + ";Pooling=false;Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.10.10)(PORT=1521)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=orcl)));";
  97. dh = new DataHelper();
  98. }
  99. }
  100. //获取岗位资源相关信息
  101. DataTable dt = (DataTable)dh.ExecuteSql("select sc_linecode,sc_stepcode,st_badgroupcode from source left join step on sc_stepcode=st_code where sc_code='" + isource + "'", "select");
  102. if (dt.Rows.Count > 0)
  103. {
  104. ilinecode = dt.Rows[0]["sc_linecode"].ToString();
  105. istepcode = dt.Rows[0]["sc_stepcode"].ToString();
  106. ibadgroup = dt.Rows[0]["st_badgroupcode"].ToString();
  107. }
  108. }
  109. private void ConnectDB()
  110. {
  111. dh = new DataHelper();
  112. }
  113. private void StartWatch_Click(object sender, EventArgs e)
  114. {
  115. if (FolderPath.Text == "" || BackUpFolderPath.Text == "")
  116. {
  117. OperateResult.AppendText("请选择监控文件夹和备份文件夹\n");
  118. return;
  119. }
  120. else
  121. {
  122. if (!Directory.Exists(FolderPath.Text))
  123. {
  124. OperateResult.AppendText("监控文件夹不存在\n");
  125. return;
  126. }
  127. if (!Directory.Exists(BackUpFolderPath.Text))
  128. {
  129. OperateResult.AppendText("备份文件夹不存在\n");
  130. return;
  131. }
  132. }
  133. if (FolderPath.Text == BackUpFolderPath.Text)
  134. {
  135. OperateResult.AppendText("监控文件夹和备份文件夹不能相同\n");
  136. return;
  137. }
  138. for (int i = 0; i < DB.Rows.Count; i++)
  139. {
  140. if (Master.Text == DB.Rows[i]["ma_user"].ToString())
  141. {
  142. DataHelper.DBConnectionString = "Connection Timeout=0;Pooling=false;Password=select!#%*(;User ID=" + DB.Rows[i]["ma_user"].ToString() + ";Pooling=false;Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.10.10)(PORT=1521)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=orcl)));";
  143. dh = new DataHelper();
  144. LogicHandler.dh = new DataHelper();
  145. }
  146. }
  147. //存在工单必须是已下放状态
  148. if (!dh.CheckExist("make", "ma_statuscode='STARTED' and ma_code='" + ma_code.Text + "'") && ma_code.Text != "")
  149. {
  150. OperateResult.AppendText("工单不存在或者未下放\n");
  151. return;
  152. }
  153. XmlWatcher.Path = FolderPath.Text;
  154. XmlWatcher.Filter = "*.txt";
  155. XmlWatcher.EnableRaisingEvents = true;
  156. //设置缓存数据
  157. BaseUtil.SetCacheData("FolderPath", FolderPath.Text);
  158. BaseUtil.SetCacheData("BackUpFolderPath", BackUpFolderPath.Text);
  159. BaseUtil.SetCacheData("Master", Master.Text);
  160. BaseUtil.SetCacheData("AutoStart", AutoStart.Checked);
  161. //设置按钮不可点击
  162. StartWatch.Enabled = false;
  163. ChooseFolder.Enabled = false;
  164. Master.Enabled = false;
  165. ma_code.Enabled = false;
  166. ChooseBackUpFolder.Enabled = false;
  167. StopWatch.Enabled = true;
  168. OperateResult.AppendText("开始执行监控\n");
  169. }
  170. public void CreateNode(XmlDocument xmlDoc, XmlNode parentNode, string name, string value)
  171. {
  172. XmlNode node = xmlDoc.CreateNode(XmlNodeType.Element, name, null);
  173. node.InnerText = value;
  174. parentNode.AppendChild(node);
  175. }
  176. private void XmlWatcher_Created(object sender, FileSystemEventArgs e)
  177. {
  178. while (true)
  179. {
  180. try
  181. {
  182. using (Stream stream = File.Open(e.FullPath, FileMode.Open, FileAccess.Read, FileShare.Read))
  183. {
  184. if (stream != null)
  185. break;
  186. }
  187. }
  188. catch (Exception ex)
  189. {
  190. Console.WriteLine(ex.Message);
  191. }
  192. }
  193. switch (e.Name.Substring(e.Name.LastIndexOf(".") + 1).ToUpper())
  194. {
  195. case "TXT":
  196. TxtHandleProcess(e.FullPath);
  197. break;
  198. case "XML":
  199. XmlHandleProcess(e.FullPath);
  200. break;
  201. default:
  202. break;
  203. }
  204. }
  205. private void TxtHandleProcess(string FileName)
  206. {
  207. List<string> badcode = new List<string>();
  208. List<string> badlocation = new List<string>();
  209. List<string> badprod = new List<string>();
  210. StreamReader sR = File.OpenText(FileName);
  211. string nextLine;
  212. string[] data = null;
  213. while ((nextLine = sR.ReadLine()) != null)
  214. {
  215. data = nextLine.Split(';');
  216. }
  217. string SoftVersion = "";
  218. string SN = "";
  219. string Machine = "";
  220. string EmName = "";
  221. string Day = "";
  222. string Time = "";
  223. string Result = "";
  224. string TotalPart = "";
  225. string NGPart = "";
  226. int MissTest = 0;
  227. for (int i = 0; i < data.Length; i++)
  228. {
  229. switch (i.ToString())
  230. {
  231. case "0":
  232. SoftVersion = data[i];
  233. break;
  234. case "1":
  235. SN = data[i];
  236. break;
  237. case "2":
  238. Machine = data[i];
  239. break;
  240. case "4":
  241. EmName = data[i];
  242. break;
  243. case "6":
  244. Day = data[i];
  245. break;
  246. case "7":
  247. Time = data[i];
  248. break;
  249. case "8":
  250. Result += data[i];
  251. break;
  252. case "9":
  253. Result += data[i];
  254. break;
  255. case "10":
  256. Result += data[i];
  257. break;
  258. case "11":
  259. Result += data[i];
  260. break;
  261. case "13":
  262. TotalPart = data[i];
  263. break;
  264. case "14":
  265. NGPart = data[i];
  266. break;
  267. default:
  268. break;
  269. }
  270. }
  271. if (SN == "")
  272. {
  273. tipform.startthread("序列号不能为空", "NG");
  274. return;
  275. }
  276. if (data.Length > 15)
  277. {
  278. for (int i = 15; i < data.Length; i++)
  279. {
  280. switch ((i - 15) % 6)
  281. {
  282. case 0:
  283. badlocation.Add(data[i]);
  284. break;
  285. case 1:
  286. badprod.Add(data[i]);
  287. break;
  288. case 2:
  289. badcode.Add(data[i].ToUpper());
  290. break;
  291. default:
  292. break;
  293. }
  294. }
  295. }
  296. sR.Close();
  297. //如果没有不良位号等数据则是15个数据
  298. if (Result == "0001")
  299. {
  300. if (data.Length > 16)
  301. {
  302. MissTest = -1;
  303. Result = "NG";
  304. }
  305. else
  306. {
  307. Result = "OK";
  308. }
  309. }
  310. else
  311. {
  312. Result = "OK";
  313. }
  314. string makecode = "";
  315. string stepcode = "";
  316. string oErrMessage = "";
  317. string oMSID = "";
  318. if (LogicHandler.CheckStepSNAndMacode(ma_code.Text, isource, SN, iusercode, out makecode, out oMSID, out oErrMessage))
  319. {
  320. //获取序列号ID最大的工单
  321. dt = (DataTable)dh.ExecuteSql("select ms_makecode,ms_id,ms_stepcode from makeserial where ms_sncode='" + SN + "' order by ms_id desc", "select");
  322. if (dt.Rows.Count > 0)
  323. {
  324. ma_code.Text = dt.Rows[0]["ms_makecode"].ToString();
  325. makecode = dt.Rows[0]["ms_makecode"].ToString();
  326. stepcode = dt.Rows[0]["ms_stepcode"].ToString();
  327. if (stepcode == istepcode)
  328. {
  329. tipform.startthread("序列号 " + SN + "已执行过" + istepcode, "NG");
  330. OperateResult.AppendText("序列号 " + SN + "已执行过" + istepcode + "\n");
  331. return;
  332. }
  333. switch (Result)
  334. {
  335. case "OK":
  336. if (!LogicHandler.SetStepResult(makecode, isource, SN, "自动过站采集", Result, iusercode, out oErrMessage))
  337. {
  338. OperateResult.AppendText(oErrMessage + "\n");
  339. tipform.startthread(oErrMessage, Result);
  340. return;
  341. }
  342. tipform.startthread("序列号 " + SN + "通过检测", Result);
  343. break;
  344. case "NG":
  345. RecordBadInfo(SN, makecode, FileName, "", badcode.ToArray(), badlocation.ToArray(), badprod.ToArray());
  346. //所有的序列号均采集为良品
  347. if (!LogicHandler.SetTestNGDetail(SN, makecode, iusercode, isource, Result, out oErrMessage))
  348. {
  349. OperateResult.AppendText(oErrMessage + "\n");
  350. tipform.startthread(oErrMessage, Result);
  351. return;
  352. }
  353. tipform.startthread("序列号 " + SN + "未通过检测", Result);
  354. break;
  355. default:
  356. break;
  357. }
  358. LogicHandler.AutoPassLog(SN, isource, makecode, Day + Time, istepcode, ilinecode, SoftVersion, Result == "NG" ? "-1" : "0", "0", MissTest, TotalPart, NGPart);
  359. FileName = FileName.Substring(FileName.LastIndexOf(@"\") + 1);
  360. FileInfo file = new FileInfo(FolderPath.Text + @"\" + FileName);
  361. if (file.Exists)
  362. {
  363. try
  364. {
  365. for (int i = 1; i <= 20; i++)
  366. {
  367. if (!File.Exists(BackUpFolderPath.Text + @"\" + FileName))
  368. {
  369. file.MoveTo(BackUpFolderPath.Text + @"\" + FileName);
  370. OperateResult.AppendText("成功解析文件" + DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss") + " " + FileName + "\n");
  371. break;
  372. }
  373. else if (!File.Exists(BackUpFolderPath.Text + @"\" + FileName.Split('.')[0] + "(" + i + ")" + "." + FileName.Split('.')[1]))
  374. {
  375. file.MoveTo(BackUpFolderPath.Text + @"\" + FileName.Split('.')[0] + "(" + i + ")" + "." + FileName.Split('.')[1]);
  376. OperateResult.AppendText("成功解析文件" + DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss") + " " + FileName + "\n");
  377. break;
  378. }
  379. }
  380. }
  381. catch (Exception ex)
  382. {
  383. OperateResult.AppendText(FileName + ex.Message + "\n");
  384. }
  385. }
  386. }
  387. }
  388. else
  389. {
  390. OperateResult.AppendText(oErrMessage + "\n");
  391. tipform.startthread(oErrMessage, Result);
  392. return;
  393. }
  394. }
  395. /// <summary>
  396. /// 使用进程处理文件,避免界面假死
  397. /// </summary>
  398. private void XmlHandleProcess(string FileName)
  399. {
  400. string test_date = "";
  401. string test_result = "";
  402. string test_sn = "";
  403. string imageurl = "";
  404. string oMSID = "";
  405. string oErrMessage = "";
  406. XmlReader myReader = XmlReader.Create(FolderPath.Text + @"\" + FileName);
  407. OperateResult.AppendText("读取文件" + FileName + "\n");
  408. //获取采集的项目名称
  409. List<string> badcode = new List<string>();
  410. //获取采集项目的结果
  411. List<string> badlocation = new List<string>();
  412. while (myReader.Read())
  413. {
  414. if (myReader.NodeType == XmlNodeType.Element && myReader.Name == "test" && myReader.IsStartElement())
  415. {
  416. test_sn = myReader.GetAttribute("test_sn");
  417. test_result = myReader.GetAttribute("test_result");
  418. test_date = myReader.GetAttribute("test_date");
  419. imageurl = myReader.GetAttribute("imgurl");
  420. }
  421. //if (myReader.NodeType == XmlNodeType.Text)
  422. //{
  423. // if (code_or_location % 2 == 0)
  424. // {
  425. // badcode.Add(myReader.Value);
  426. // code_or_location++;
  427. // }
  428. // else
  429. // {
  430. // badlocation.Add(myReader.Value);
  431. // code_or_location++;
  432. // }
  433. //}
  434. if (myReader.NodeType == XmlNodeType.Element && myReader.Name == "err_reason" && myReader.IsStartElement())
  435. {
  436. badcode.Add(myReader.ReadInnerXml() + " ");
  437. }
  438. if (myReader.NodeType == XmlNodeType.Element && myReader.Name == "err_location" && myReader.IsStartElement())
  439. {
  440. badlocation.Add(myReader.ReadInnerXml() + " ");
  441. }
  442. }
  443. myReader.Close();
  444. //获取文件名的序列号,如SA123456.xml,如果开头为-表示无条码需要自动获取
  445. string sncode = FileName.Substring(0, 1) == "-" ? "" : FileName.Split('.')[0];
  446. string makecode = "";
  447. //获取序列号ID最大的工单号,所有序列号都测试为良品
  448. if (sncode == "")
  449. {
  450. //获取资源,线别,工序相等,并且状态在线的序列号进行分配
  451. sql.Clear();
  452. sql.Append("select ms_sncode,ms_makecode from makeserial left join source on sc_code=ms_sccode where ");
  453. sql.Append("sc_linecode='" + ilinecode + "' and ms_nextstepcode='" + istepcode + "' and ms_status=1 order by ms_id");
  454. dt = (DataTable)dh.ExecuteSql(sql.ToString(), "select");
  455. if (dt.Rows.Count > 0)
  456. {
  457. makecode = dt.Rows[0]["ms_makecode"].ToString();
  458. sncode = dt.Rows[0]["ms_sncode"].ToString();
  459. }
  460. else
  461. {
  462. OperateResult.AppendText("当前线别在" + istepcode + "无可分配序列号");
  463. tipform.startthread("当前线别在" + istepcode + "无可分配序列号", "NG");
  464. return;
  465. }
  466. //-2-NG2017/10/2514:46:29.xml取第二位版号
  467. string combinecode = FileName.Substring(1, 1);
  468. if (LogicHandler.CheckStepSNAndMacode(ma_code.Text == "" ? makecode : ma_code.Text, isource, sncode, iusercode, out makecode, out oMSID, out oErrMessage))
  469. {
  470. //插入日志
  471. LogicHandler.AutoPassLog(sncode, isource, makecode, test_date, istepcode, ilinecode, FileName, test_result == "NG" ? "-1" : "0", "-1", 0, "", "");
  472. //如果是不良品记录日志,用于测试采集判负
  473. if (test_result == "NG")
  474. {
  475. RecordBadInfo(sncode, makecode, FileName, combinecode, badcode.ToArray(), badlocation.ToArray(), null);
  476. }
  477. if (!LogicHandler.SetStepResult(makecode, isource, sncode, "自动过站采集", "OK", iusercode, out oErrMessage))
  478. {
  479. OperateResult.AppendText(oErrMessage + "\n");
  480. tipform.startthread(oErrMessage, "NG");
  481. return;
  482. }
  483. tipform.startthread("序列号 " + sncode + "通过检测", test_result);
  484. }
  485. else
  486. {
  487. OperateResult.AppendText(oErrMessage + "\n");
  488. tipform.startthread(oErrMessage, "NG");
  489. return;
  490. }
  491. }
  492. else
  493. {
  494. string status = "";
  495. string errmessage = "";
  496. bool NoteAlready = false;
  497. LogicHandler.GetMakeInfo(sncode, out makecode, out status, out errmessage);
  498. if (ma_code.Text != makecode && makecode != "" && ma_code.Text != "" && status != "2")
  499. {
  500. string ChangeMakeCode = MessageBox.Show(this.ParentForm, "序列号" + sncode + "所属工单不同,是否切换?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question).ToString();
  501. //如果选择不切换赋值当前界面工单
  502. if (ChangeMakeCode != "Yes")
  503. {
  504. makecode = ma_code.Text;
  505. }
  506. else
  507. {
  508. NoteAlready = true;
  509. }
  510. }
  511. if (LogicHandler.CheckStepSNAndMacode(ma_code.Text == "" ? makecode : ma_code.Text, isource, sncode, iusercode, out makecode, out oMSID, out oErrMessage))
  512. {
  513. if (!NoteAlready)
  514. {
  515. if (makecode != ma_code.Text && ma_code.Text != "")
  516. {
  517. string ChangeMakeCode = MessageBox.Show(this.ParentForm, "序列号所属工单不同,是否切换?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question).ToString();
  518. //如果选择不切换赋值当前界面工单
  519. if (ChangeMakeCode == "Yes")
  520. {
  521. ma_code.Text = makecode;
  522. }
  523. else
  524. {
  525. OperateResult.AppendText(">>请重新采集序列号\n");
  526. tipform.startthread("请重新采集序列号", test_result);
  527. return;
  528. }
  529. }
  530. }
  531. //获取序列号ID最大的工单
  532. dt = (DataTable)dh.ExecuteSql("select ms_makecode,ms_id from makeserial where ms_sncode='" + sncode + "' order by ms_id desc", "select");
  533. if (dt.Rows.Count > 0)
  534. {
  535. ma_code.Text = dt.Rows[0]["ms_makecode"].ToString();
  536. makecode = dt.Rows[0]["ms_makecode"].ToString();
  537. switch (test_result)
  538. {
  539. case "OK":
  540. if (!LogicHandler.SetStepResult(makecode, isource, sncode, "自动过站采集", test_result, iusercode, out oErrMessage))
  541. {
  542. OperateResult.AppendText(oErrMessage + "\n");
  543. tipform.startthread(oErrMessage, test_result);
  544. return;
  545. }
  546. tipform.startthread("序列号 " + sncode + "通过检测", test_result);
  547. break;
  548. case "NG":
  549. RecordBadInfo(sncode, makecode, FileName, "", badcode.ToArray(), badlocation.ToArray(), null);
  550. //所有的序列号均采集为良品
  551. if (!LogicHandler.SetTestNGDetail(sncode, makecode, iusercode, isource, test_result, out oErrMessage))
  552. {
  553. OperateResult.AppendText(oErrMessage + "\n");
  554. tipform.startthread(oErrMessage, test_result);
  555. return;
  556. }
  557. tipform.startthread("序列号 " + sncode + "未通过检测", test_result);
  558. break;
  559. default:
  560. break;
  561. }
  562. LogicHandler.AutoPassLog(sncode, isource, makecode, test_date, istepcode, ilinecode, FileName, test_result == "NG" ? "1" : "0", "0", 0, "", "");
  563. }
  564. }
  565. else
  566. {
  567. OperateResult.AppendText(oErrMessage + "\n");
  568. tipform.startthread(oErrMessage, "NG");
  569. return;
  570. }
  571. }
  572. FileInfo file = new FileInfo(FolderPath.Text + @"\" + FileName);
  573. if (file.Exists)
  574. {
  575. try
  576. {
  577. for (int i = 1; i <= 20; i++)
  578. {
  579. if (!File.Exists(BackUpFolderPath.Text + @"\" + FileName))
  580. {
  581. file.MoveTo(BackUpFolderPath.Text + @"\" + FileName);
  582. OperateResult.AppendText("成功解析文件" + FileName + "\n");
  583. break;
  584. }
  585. else if (!File.Exists(BackUpFolderPath.Text + @"\" + FileName.Split('.')[0] + "(" + i + ")" + "." + FileName.Split('.')[1]))
  586. {
  587. file.MoveTo(BackUpFolderPath.Text + @"\" + FileName.Split('.')[0] + "(" + i + ")" + "." + FileName.Split('.')[1]);
  588. OperateResult.AppendText("成功解析文件" + FileName + "\n");
  589. break;
  590. }
  591. }
  592. }
  593. catch (Exception ex)
  594. {
  595. OperateResult.AppendText(FileName + ex.Message + "\n");
  596. }
  597. }
  598. }
  599. private void RecordBadInfo(string sncode, string makecode, string filename, string combine, string[] badcode, string[] badlocation, string[] soncode)
  600. {
  601. string[] ID = dh.GetSEQ("makebad_seq", badcode.Length);
  602. sql.Clear();
  603. sql.Append("insert into makebad(mb_id,mb_makecode,mb_mscode,mb_sncode,mb_inman,mb_indate,mb_stepcode");
  604. sql.Append(",mb_sourcecode,mb_badcode,mb_bgcode,mb_badtable,mb_status,mb_badlocation,mb_soncode) select :ID");
  605. sql.Append(",ma_code,ms_code,ms_sncode,'" + iusercode + "',sysdate,'" + istepcode + "','" + isource + "',");
  606. sql.Append(":bc_code,'" + ibadgroup + "','','0',:location,:soncode from make left join makeSerial on ms_makecode=ma_code ");
  607. sql.Append("where ms_sncode='" + sncode + "' and ms_makecode='" + makecode + "'");
  608. dh.BatchInsert(sql.ToString(), new string[] { "ID", "bc_code", "location", "soncode" }, ID, badcode, badlocation, soncode);
  609. sql.Clear();
  610. sql.Append("update makebad set mb_badname=(select DISTINCT bc_name from badcode where bc_code=mb_badcode),mb_bgname=(select DISTINCT bg_name from badgroup where bg_code=mb_bgcode) ");
  611. sql.Append("where mb_id=:ID");
  612. dh.BatchInsert(sql.ToString(), new string[] { "ID" }, ID);
  613. //记录判断日志
  614. if (dh.getFieldDataByCondition("source", "sc_scantype", "sc_code='" + isource + "'").ToString() == "REJUCE")
  615. {
  616. LogicHandler.AutoPassJudge(sncode, makecode, isource, filename, ilinecode, combine);
  617. }
  618. }
  619. private void StopWatch_Click(object sender, EventArgs e)
  620. {
  621. XmlWatcher.EnableRaisingEvents = false;
  622. StartWatch.Enabled = true;
  623. ma_code.Enabled = true;
  624. Master.Enabled = true;
  625. ChooseFolder.Enabled = true;
  626. ChooseBackUpFolder.Enabled = true;
  627. StopWatch.Enabled = false;
  628. OperateResult.AppendText("停止执行监控\n");
  629. }
  630. private void Clean_Click(object sender, EventArgs e)
  631. {
  632. OperateResult.Clear();
  633. }
  634. private void ChooseFolder_Click(object sender, EventArgs e)
  635. {
  636. FolderBrowserDialog folder = new FolderBrowserDialog();
  637. folder.Description = "选择监控文件夹";
  638. DialogResult result = folder.ShowDialog();
  639. if (result == DialogResult.OK)
  640. {
  641. FolderPath.Text = folder.SelectedPath;
  642. }
  643. }
  644. private void ChooseBackUpFolder_Click(object sender, EventArgs e)
  645. {
  646. FolderBrowserDialog folder = new FolderBrowserDialog();
  647. folder.Description = "选择备份文件夹";
  648. DialogResult result = folder.ShowDialog();
  649. if (result == DialogResult.OK)
  650. {
  651. BackUpFolderPath.Text = folder.SelectedPath;
  652. }
  653. }
  654. private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  655. {
  656. string ExitConfirm = MessageBox.Show(this, "确认退出?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question).ToString();
  657. if (ExitConfirm != "Yes")
  658. {
  659. WindowState = FormWindowState.Minimized;
  660. e.Cancel = true;
  661. }
  662. }
  663. private void AutoStart_CheckedChanged(object sender, EventArgs e)
  664. {
  665. SetAutoRun();
  666. }
  667. private void SetAutoRun()
  668. {
  669. if (AutoStart.Checked) //设置开机自启动
  670. {
  671. string path = Application.ExecutablePath;
  672. RegistryKey rk = Registry.LocalMachine;
  673. RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
  674. rk2.SetValue("UAS自动过站解析器.exe", path);
  675. rk2.Close();
  676. rk.Close();
  677. }
  678. else //取消开机自启动
  679. {
  680. string path = Application.ExecutablePath;
  681. RegistryKey rk = Registry.LocalMachine;
  682. RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
  683. rk2.DeleteValue("UAS自动过站解析器.exe", false);
  684. rk2.Close();
  685. rk.Close();
  686. }
  687. }
  688. private void BatchAnalysus_Click(object sender, EventArgs e)
  689. {
  690. var AllXmls = Directory.GetFiles(FolderPath.Text, "*.txt");
  691. foreach (var Xml in AllXmls)
  692. {
  693. TxtHandleProcess(Xml);
  694. }
  695. }
  696. private void Timer_Tick(object sender, EventArgs e)
  697. {
  698. dh.ExecuteSql("select sysdate from dual", "select");
  699. }
  700. }
  701. }