AutoAnalysisXml.cs 37 KB

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