AutoAnalysisXml.cs 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747
  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. string nextLine;
  206. private void TxtHandleProcess(string FileName)
  207. {
  208. List<string> badcode = new List<string>();
  209. List<string> badlocation = new List<string>();
  210. List<string> badprod = new List<string>();
  211. StreamReader sR = File.OpenText(FileName);
  212. string[] data1 = null;
  213. while ((nextLine = sR.ReadLine()) != null)
  214. {
  215. data1 = nextLine.Split(';');
  216. }
  217. string SoftVersion = data1[0];
  218. List<string[]> dic = new List<string[]>();
  219. List<string> sndata = new List<string>();
  220. for (int i = 0; i < data1.Length; i++)
  221. {
  222. //如果软件版本相同的时候,则表示一个新的SN
  223. if ((SoftVersion == data1[i] && i != 0) || i == data1.Length - 1)
  224. {
  225. dic.Add(sndata.ToArray());
  226. sndata.Clear();
  227. }
  228. sndata.Add(data1[i]);
  229. }
  230. foreach (string[] content in dic)
  231. {
  232. string SN = "";
  233. string Machine = "";
  234. string EmName = "";
  235. string Day = "";
  236. string Time = "";
  237. string Result = "";
  238. string TotalPart = "";
  239. string NGPart = "";
  240. int MissTest = 0;
  241. string[] data = content;
  242. for (int i = 0; i < data.Length; i++)
  243. {
  244. switch (i.ToString())
  245. {
  246. case "0":
  247. SoftVersion = data[i];
  248. break;
  249. case "1":
  250. SN = data[i];
  251. break;
  252. case "2":
  253. Machine = data[i];
  254. break;
  255. case "4":
  256. EmName = data[i];
  257. break;
  258. case "6":
  259. Day = data[i];
  260. break;
  261. case "7":
  262. Time = data[i];
  263. break;
  264. case "8":
  265. Result += data[i];
  266. break;
  267. case "9":
  268. Result += data[i];
  269. break;
  270. case "10":
  271. Result += data[i];
  272. break;
  273. case "11":
  274. Result += data[i];
  275. break;
  276. case "13":
  277. TotalPart = data[i];
  278. break;
  279. case "14":
  280. NGPart = data[i];
  281. break;
  282. default:
  283. break;
  284. }
  285. }
  286. if (SN == "")
  287. {
  288. tipform.startthread("序列号不能为空", "NG");
  289. continue;
  290. }
  291. if (data.Length > 15)
  292. {
  293. for (int i = 15; i < data.Length; i++)
  294. {
  295. switch ((i - 15) % 6)
  296. {
  297. case 0:
  298. badlocation.Add(data[i]);
  299. break;
  300. case 1:
  301. badprod.Add(data[i]);
  302. break;
  303. case 2:
  304. badcode.Add(data[i].ToUpper());
  305. break;
  306. default:
  307. break;
  308. }
  309. }
  310. }
  311. sR.Close();
  312. //如果没有不良位号等数据则是15个数据
  313. if (Result == "0001")
  314. {
  315. if (data.Length > 16)
  316. {
  317. MissTest = -1;
  318. Result = "NG";
  319. }
  320. else
  321. {
  322. Result = "OK";
  323. }
  324. }
  325. else
  326. {
  327. Result = "OK";
  328. }
  329. string makecode = "";
  330. string stepcode = "";
  331. string oErrMessage = "";
  332. string oMSID = "";
  333. if (LogicHandler.CheckStepSNAndMacode(ma_code.Text, isource, SN, iusercode, out makecode, out oMSID, out oErrMessage))
  334. {
  335. //获取序列号ID最大的工单
  336. dt = (DataTable)dh.ExecuteSql("select ms_makecode,ms_id,ms_stepcode from makeserial where ms_sncode='" + SN + "' order by ms_id desc", "select");
  337. if (dt.Rows.Count > 0)
  338. {
  339. ma_code.Text = dt.Rows[0]["ms_makecode"].ToString();
  340. makecode = dt.Rows[0]["ms_makecode"].ToString();
  341. stepcode = dt.Rows[0]["ms_stepcode"].ToString();
  342. if (stepcode == istepcode)
  343. {
  344. tipform.startthread("序列号 " + SN + "已执行过" + istepcode, "NG");
  345. OperateResult.AppendText("序列号 " + SN + "已执行过" + istepcode + "\n");
  346. continue;
  347. }
  348. switch (Result)
  349. {
  350. case "OK":
  351. if (!LogicHandler.SetStepResult(makecode, isource, SN, "自动过站采集", Result, iusercode, out oErrMessage))
  352. {
  353. OperateResult.AppendText(oErrMessage + "\n");
  354. tipform.startthread(oErrMessage, Result);
  355. continue;
  356. }
  357. tipform.startthread("序列号 " + SN + "通过检测", Result);
  358. break;
  359. case "NG":
  360. RecordBadInfo(SN, makecode, FileName, "", badcode.ToArray(), badlocation.ToArray(), badprod.ToArray());
  361. //所有的序列号均采集为良品
  362. if (!LogicHandler.SetTestNGDetail(SN, makecode, iusercode, isource, Result, out oErrMessage))
  363. {
  364. OperateResult.AppendText(oErrMessage + "\n");
  365. tipform.startthread(oErrMessage, Result);
  366. continue;
  367. }
  368. tipform.startthread("序列号 " + SN + "未通过检测", Result);
  369. break;
  370. default:
  371. break;
  372. }
  373. LogicHandler.AutoPassLog(SN, isource, makecode, Day + Time, istepcode, ilinecode, SoftVersion, Result == "NG" ? "-1" : "0", "0", MissTest, TotalPart, NGPart);
  374. FileName = FileName.Substring(FileName.LastIndexOf(@"\") + 1);
  375. FileInfo file = new FileInfo(FolderPath.Text + @"\" + FileName);
  376. //if (file.Exists)
  377. //{
  378. // try
  379. // {
  380. // for (int i = 1; i <= 20; i++)
  381. // {
  382. // if (!File.Exists(BackUpFolderPath.Text + @"\" + FileName))
  383. // {
  384. // file.MoveTo(BackUpFolderPath.Text + @"\" + FileName);
  385. // OperateResult.AppendText("成功解析文件" + DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss") + " " + FileName + "\n");
  386. // break;
  387. // }
  388. // else if (!File.Exists(BackUpFolderPath.Text + @"\" + FileName.Split('.')[0] + "(" + i + ")" + "." + FileName.Split('.')[1]))
  389. // {
  390. // file.MoveTo(BackUpFolderPath.Text + @"\" + FileName.Split('.')[0] + "(" + i + ")" + "." + FileName.Split('.')[1]);
  391. // OperateResult.AppendText("成功解析文件" + DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss") + " " + FileName + "\n");
  392. // break;
  393. // }
  394. // }
  395. // }
  396. // catch (Exception ex)
  397. // {
  398. // OperateResult.AppendText(FileName + ex.Message + "\n");
  399. // }
  400. //}
  401. }
  402. }
  403. else
  404. {
  405. OperateResult.AppendText(oErrMessage + "\n");
  406. tipform.startthread(oErrMessage, Result);
  407. continue;
  408. }
  409. }
  410. }
  411. /// <summary>
  412. /// 使用进程处理文件,避免界面假死
  413. /// </summary>
  414. private void XmlHandleProcess(string FileName)
  415. {
  416. string test_date = "";
  417. string test_result = "";
  418. string test_sn = "";
  419. string imageurl = "";
  420. string oMSID = "";
  421. string oErrMessage = "";
  422. XmlReader myReader = XmlReader.Create(FolderPath.Text + @"\" + FileName);
  423. OperateResult.AppendText("读取文件" + FileName + "\n");
  424. //获取采集的项目名称
  425. List<string> badcode = new List<string>();
  426. //获取采集项目的结果
  427. List<string> badlocation = new List<string>();
  428. while (myReader.Read())
  429. {
  430. if (myReader.NodeType == XmlNodeType.Element && myReader.Name == "test" && myReader.IsStartElement())
  431. {
  432. test_sn = myReader.GetAttribute("test_sn");
  433. test_result = myReader.GetAttribute("test_result");
  434. test_date = myReader.GetAttribute("test_date");
  435. imageurl = myReader.GetAttribute("imgurl");
  436. }
  437. //if (myReader.NodeType == XmlNodeType.Text)
  438. //{
  439. // if (code_or_location % 2 == 0)
  440. // {
  441. // badcode.Add(myReader.Value);
  442. // code_or_location++;
  443. // }
  444. // else
  445. // {
  446. // badlocation.Add(myReader.Value);
  447. // code_or_location++;
  448. // }
  449. //}
  450. if (myReader.NodeType == XmlNodeType.Element && myReader.Name == "err_reason" && myReader.IsStartElement())
  451. {
  452. badcode.Add(myReader.ReadInnerXml() + " ");
  453. }
  454. if (myReader.NodeType == XmlNodeType.Element && myReader.Name == "err_location" && myReader.IsStartElement())
  455. {
  456. badlocation.Add(myReader.ReadInnerXml() + " ");
  457. }
  458. }
  459. myReader.Close();
  460. //获取文件名的序列号,如SA123456.xml,如果开头为-表示无条码需要自动获取
  461. string sncode = FileName.Substring(0, 1) == "-" ? "" : FileName.Split('.')[0];
  462. string makecode = "";
  463. //获取序列号ID最大的工单号,所有序列号都测试为良品
  464. if (sncode == "")
  465. {
  466. //获取资源,线别,工序相等,并且状态在线的序列号进行分配
  467. sql.Clear();
  468. sql.Append("select ms_sncode,ms_makecode from makeserial left join source on sc_code=ms_sccode where ");
  469. sql.Append("sc_linecode='" + ilinecode + "' and ms_nextstepcode='" + istepcode + "' and ms_status=1 order by ms_id");
  470. dt = (DataTable)dh.ExecuteSql(sql.ToString(), "select");
  471. if (dt.Rows.Count > 0)
  472. {
  473. makecode = dt.Rows[0]["ms_makecode"].ToString();
  474. sncode = dt.Rows[0]["ms_sncode"].ToString();
  475. }
  476. else
  477. {
  478. OperateResult.AppendText("当前线别在" + istepcode + "无可分配序列号");
  479. tipform.startthread("当前线别在" + istepcode + "无可分配序列号", "NG");
  480. return;
  481. }
  482. //-2-NG2017/10/2514:46:29.xml取第二位版号
  483. string combinecode = FileName.Substring(1, 1);
  484. if (LogicHandler.CheckStepSNAndMacode(ma_code.Text == "" ? makecode : ma_code.Text, isource, sncode, iusercode, out makecode, out oMSID, out oErrMessage))
  485. {
  486. //插入日志
  487. LogicHandler.AutoPassLog(sncode, isource, makecode, test_date, istepcode, ilinecode, FileName, test_result == "NG" ? "-1" : "0", "-1", 0, "", "");
  488. //如果是不良品记录日志,用于测试采集判负
  489. if (test_result == "NG")
  490. {
  491. RecordBadInfo(sncode, makecode, FileName, combinecode, badcode.ToArray(), badlocation.ToArray(), null);
  492. }
  493. if (!LogicHandler.SetStepResult(makecode, isource, sncode, "自动过站采集", "OK", iusercode, out oErrMessage))
  494. {
  495. OperateResult.AppendText(oErrMessage + "\n");
  496. tipform.startthread(oErrMessage, "NG");
  497. return;
  498. }
  499. tipform.startthread("序列号 " + sncode + "通过检测", test_result);
  500. }
  501. else
  502. {
  503. OperateResult.AppendText(oErrMessage + "\n");
  504. tipform.startthread(oErrMessage, "NG");
  505. return;
  506. }
  507. }
  508. else
  509. {
  510. string status = "";
  511. string errmessage = "";
  512. bool NoteAlready = false;
  513. LogicHandler.GetMakeInfo(sncode, out makecode, out status, out errmessage);
  514. if (ma_code.Text != makecode && makecode != "" && ma_code.Text != "" && status != "2")
  515. {
  516. string ChangeMakeCode = MessageBox.Show(this.ParentForm, "序列号" + sncode + "所属工单不同,是否切换?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question).ToString();
  517. //如果选择不切换赋值当前界面工单
  518. if (ChangeMakeCode != "Yes")
  519. {
  520. makecode = ma_code.Text;
  521. }
  522. else
  523. {
  524. NoteAlready = true;
  525. }
  526. }
  527. if (LogicHandler.CheckStepSNAndMacode(ma_code.Text == "" ? makecode : ma_code.Text, isource, sncode, iusercode, out makecode, out oMSID, out oErrMessage))
  528. {
  529. if (!NoteAlready)
  530. {
  531. if (makecode != ma_code.Text && ma_code.Text != "")
  532. {
  533. string ChangeMakeCode = MessageBox.Show(this.ParentForm, "序列号所属工单不同,是否切换?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question).ToString();
  534. //如果选择不切换赋值当前界面工单
  535. if (ChangeMakeCode == "Yes")
  536. {
  537. ma_code.Text = makecode;
  538. }
  539. else
  540. {
  541. OperateResult.AppendText(">>请重新采集序列号\n");
  542. tipform.startthread("请重新采集序列号", test_result);
  543. return;
  544. }
  545. }
  546. }
  547. //获取序列号ID最大的工单
  548. dt = (DataTable)dh.ExecuteSql("select ms_makecode,ms_id from makeserial where ms_sncode='" + sncode + "' order by ms_id desc", "select");
  549. if (dt.Rows.Count > 0)
  550. {
  551. ma_code.Text = dt.Rows[0]["ms_makecode"].ToString();
  552. makecode = dt.Rows[0]["ms_makecode"].ToString();
  553. switch (test_result)
  554. {
  555. case "OK":
  556. if (!LogicHandler.SetStepResult(makecode, isource, sncode, "自动过站采集", test_result, iusercode, out oErrMessage))
  557. {
  558. OperateResult.AppendText(oErrMessage + "\n");
  559. tipform.startthread(oErrMessage, test_result);
  560. return;
  561. }
  562. tipform.startthread("序列号 " + sncode + "通过检测", test_result);
  563. break;
  564. case "NG":
  565. RecordBadInfo(sncode, makecode, FileName, "", badcode.ToArray(), badlocation.ToArray(), null);
  566. //所有的序列号均采集为良品
  567. if (!LogicHandler.SetTestNGDetail(sncode, makecode, iusercode, isource, test_result, out oErrMessage))
  568. {
  569. OperateResult.AppendText(oErrMessage + "\n");
  570. tipform.startthread(oErrMessage, test_result);
  571. return;
  572. }
  573. tipform.startthread("序列号 " + sncode + "未通过检测", test_result);
  574. break;
  575. default:
  576. break;
  577. }
  578. LogicHandler.AutoPassLog(sncode, isource, makecode, test_date, istepcode, ilinecode, FileName, test_result == "NG" ? "1" : "0", "0", 0, "", "");
  579. }
  580. }
  581. else
  582. {
  583. OperateResult.AppendText(oErrMessage + "\n");
  584. tipform.startthread(oErrMessage, "NG");
  585. return;
  586. }
  587. }
  588. FileInfo file = new FileInfo(FolderPath.Text + @"\" + FileName);
  589. if (file.Exists)
  590. {
  591. try
  592. {
  593. for (int i = 1; i <= 20; i++)
  594. {
  595. if (!File.Exists(BackUpFolderPath.Text + @"\" + FileName))
  596. {
  597. file.MoveTo(BackUpFolderPath.Text + @"\" + FileName);
  598. OperateResult.AppendText("成功解析文件" + FileName + "\n");
  599. break;
  600. }
  601. else if (!File.Exists(BackUpFolderPath.Text + @"\" + FileName.Split('.')[0] + "(" + i + ")" + "." + FileName.Split('.')[1]))
  602. {
  603. file.MoveTo(BackUpFolderPath.Text + @"\" + FileName.Split('.')[0] + "(" + i + ")" + "." + FileName.Split('.')[1]);
  604. OperateResult.AppendText("成功解析文件" + FileName + "\n");
  605. break;
  606. }
  607. }
  608. }
  609. catch (Exception ex)
  610. {
  611. OperateResult.AppendText(FileName + ex.Message + "\n");
  612. }
  613. }
  614. }
  615. private void RecordBadInfo(string sncode, string makecode, string filename, string combine, string[] badcode, string[] badlocation, string[] soncode)
  616. {
  617. string[] ID = dh.GetSEQ("makebad_seq", badcode.Length);
  618. sql.Clear();
  619. sql.Append("update makebad set mb_status=-1 where mb_sncode='" + sncode + "' and mb_stepcode='" + istepcode + "' and mb_makecode='" + makecode + "' and mb_status=0");
  620. dh.ExecuteSql(sql.ToString(), "update");
  621. sql.Clear();
  622. sql.Append("insert into makebad(mb_id,mb_makecode,mb_mscode,mb_sncode,mb_inman,mb_indate,mb_stepcode");
  623. sql.Append(",mb_sourcecode,mb_badcode,mb_bgcode,mb_badtable,mb_status,mb_badlocation,mb_soncode,mb_content) select :ID");
  624. sql.Append(",ma_code,ms_code,ms_sncode,'" + iusercode + "',sysdate,'" + istepcode + "','" + isource + "',");
  625. sql.Append(":bc_code,'" + ibadgroup + "','','0',:location,:soncode,'" + nextLine + "' from make left join makeSerial on ms_makecode=ma_code ");
  626. sql.Append("where ms_sncode='" + sncode + "' and ms_makecode='" + makecode + "'");
  627. dh.BatchInsert(sql.ToString(), new string[] { "ID", "bc_code", "location", "soncode" }, ID, badcode, badlocation, soncode);
  628. sql.Clear();
  629. 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) ");
  630. sql.Append("where mb_id=:ID");
  631. dh.BatchInsert(sql.ToString(), new string[] { "ID" }, ID);
  632. //记录判断日志
  633. if (dh.getFieldDataByCondition("source", "sc_scantype", "sc_code='" + isource + "'").ToString() == "REJUCE")
  634. {
  635. LogicHandler.AutoPassJudge(sncode, makecode, isource, filename, ilinecode, combine);
  636. }
  637. }
  638. private void StopWatch_Click(object sender, EventArgs e)
  639. {
  640. XmlWatcher.EnableRaisingEvents = false;
  641. StartWatch.Enabled = true;
  642. ma_code.Enabled = true;
  643. Master.Enabled = true;
  644. ChooseFolder.Enabled = true;
  645. ChooseBackUpFolder.Enabled = true;
  646. StopWatch.Enabled = false;
  647. OperateResult.AppendText("停止执行监控\n");
  648. }
  649. private void Clean_Click(object sender, EventArgs e)
  650. {
  651. OperateResult.Clear();
  652. }
  653. private void ChooseFolder_Click(object sender, EventArgs e)
  654. {
  655. FolderBrowserDialog folder = new FolderBrowserDialog();
  656. folder.Description = "选择监控文件夹";
  657. DialogResult result = folder.ShowDialog();
  658. if (result == DialogResult.OK)
  659. {
  660. FolderPath.Text = folder.SelectedPath;
  661. }
  662. }
  663. private void ChooseBackUpFolder_Click(object sender, EventArgs e)
  664. {
  665. FolderBrowserDialog folder = new FolderBrowserDialog();
  666. folder.Description = "选择备份文件夹";
  667. DialogResult result = folder.ShowDialog();
  668. if (result == DialogResult.OK)
  669. {
  670. BackUpFolderPath.Text = folder.SelectedPath;
  671. }
  672. }
  673. private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  674. {
  675. string ExitConfirm = MessageBox.Show(this, "确认退出?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question).ToString();
  676. if (ExitConfirm != "Yes")
  677. {
  678. WindowState = FormWindowState.Minimized;
  679. e.Cancel = true;
  680. }
  681. }
  682. private void AutoStart_CheckedChanged(object sender, EventArgs e)
  683. {
  684. SetAutoRun();
  685. }
  686. private void SetAutoRun()
  687. {
  688. if (AutoStart.Checked) //设置开机自启动
  689. {
  690. string path = Application.ExecutablePath;
  691. RegistryKey rk = Registry.LocalMachine;
  692. RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
  693. rk2.SetValue("UAS自动过站解析器.exe", path);
  694. rk2.Close();
  695. rk.Close();
  696. }
  697. else //取消开机自启动
  698. {
  699. string path = Application.ExecutablePath;
  700. RegistryKey rk = Registry.LocalMachine;
  701. RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
  702. rk2.DeleteValue("UAS自动过站解析器.exe", false);
  703. rk2.Close();
  704. rk.Close();
  705. }
  706. }
  707. private void BatchAnalysus_Click(object sender, EventArgs e)
  708. {
  709. var AllXmls = Directory.GetFiles(FolderPath.Text, "*.txt");
  710. foreach (var Xml in AllXmls)
  711. {
  712. TxtHandleProcess(Xml);
  713. }
  714. }
  715. private void Timer_Tick(object sender, EventArgs e)
  716. {
  717. dh.ExecuteSql("select sysdate from dual", "select");
  718. }
  719. }
  720. }