AutoAnalysisXmlByStep.cs 34 KB

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