AutoAnalysisXml.cs 37 KB

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