AutoAnalysisXml.cs 36 KB

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