AutoAnalysisXml.cs 37 KB

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