AutoAnalysisXmlByStep.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  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. using System.Linq;
  12. using NPOI.SS.Formula.Functions;
  13. using System.Windows.Media.TextFormatting;
  14. using UMESDLLService;
  15. namespace FileWatcher
  16. {
  17. public partial class AutoAnalysisXmlByStep : Form
  18. {
  19. public static DataHelper dh;
  20. DataTable dt;
  21. DataTable DB;
  22. /// <summary>
  23. /// 用户编号
  24. /// </summary>
  25. string iusercode;
  26. /// <summary>
  27. /// 岗位资源
  28. /// </summary>
  29. string isource;
  30. Tip tipform;
  31. Thread InitDB;
  32. /// <summary>
  33. /// 线别
  34. /// </summary>
  35. string ilinecode;
  36. /// <summary>
  37. /// 不良代码组
  38. /// </summary>
  39. string ibadgroup;
  40. /// <summary>
  41. /// 当前工序
  42. /// </summary>
  43. string istepcode;
  44. StringBuilder sql = new StringBuilder();
  45. /// <summary>
  46. /// 缓存的文件
  47. /// </summary>
  48. public static string CachePath = Environment.GetEnvironmentVariable("windir").Substring(0, 1) + @":/UAS_MES/XmlAnalysor/Cache.xml";
  49. /// <summary>
  50. /// 缓存的文件夹
  51. /// </summary>
  52. public static string CachePathFolder = Environment.GetEnvironmentVariable("windir").Substring(0, 1) + @":/UAS_MES/XmlAnalysor/";
  53. /// <summary>
  54. /// 需要解析的文件名
  55. /// </summary>
  56. List<string> _FileName = new List<string>();
  57. ftpOperater ftp;
  58. string imaster;
  59. protected override CreateParams CreateParams
  60. {
  61. get
  62. {
  63. CreateParams cp = base.CreateParams;
  64. const int WS_EX_APPWINDOW = 0x40000;
  65. const int WS_EX_TOOLWINDOW = 0x80;
  66. cp.ExStyle &= (~WS_EX_APPWINDOW);
  67. cp.ExStyle |= WS_EX_TOOLWINDOW;
  68. return cp;
  69. }
  70. }
  71. public AutoAnalysisXmlByStep()
  72. {
  73. tipform = new Tip();
  74. InitializeComponent();
  75. StartPosition = FormStartPosition.CenterScreen;
  76. }
  77. public AutoAnalysisXmlByStep(string iUserName, string iSource, string iMaster)
  78. {
  79. tipform = new Tip();
  80. InitializeComponent();
  81. iusercode = iUserName;
  82. isource = iSource.ToUpper();
  83. imaster = iMaster;
  84. StartPosition = FormStartPosition.CenterScreen;
  85. }
  86. string fieltype = "";
  87. string ftpulr = "";
  88. private void Form1_Load(object sender, EventArgs e)
  89. {
  90. tipform.Show();
  91. CheckForIllegalCrossThreadCalls = false;
  92. FormBorderStyle = FormBorderStyle.FixedSingle;
  93. InitDB = new Thread(ConnectDB);
  94. //添加监控事件
  95. SetLoadingWindow stw = new SetLoadingWindow(InitDB, "正在启动程序");
  96. stw.StartPosition = FormStartPosition.CenterScreen;
  97. stw.ShowDialog();
  98. List<string> CacheInf = new List<string>();
  99. StreamReader sR = new StreamReader(Application.StartupPath + @"\MES_SETTING.txt", Encoding.Default);
  100. while ((nextLine = sR.ReadLine()) != null)
  101. {
  102. string name = nextLine.ToString().Split('=')[0];
  103. string value = nextLine.ToString().Split('=')[1];
  104. if (name == "FTPURL")
  105. {
  106. ftpulr = value;
  107. }
  108. if (name == "FileType")
  109. {
  110. fieltype = value;
  111. }
  112. }
  113. string[] FTPInf = ftpulr.Split('#');
  114. ftp = new ftpOperater(FTPInf[0], FTPInf[1], FTPInf[2]);
  115. //获取缓存信息
  116. try
  117. {
  118. FolderPath.Text = BaseUtil.GetCacheData("FolderPath").ToString();
  119. BackUpFolderPath.Text = BaseUtil.GetCacheData("BackUpFolderPath").ToString();
  120. Master.Text = BaseUtil.GetCacheData("Master").ToString();
  121. li_code.Text = BaseUtil.GetCacheData("Line").ToString();
  122. MachineType.Text = BaseUtil.GetCacheData("MachineType").ToString();
  123. Device.Text = BaseUtil.GetCacheData("Device").ToString();
  124. }
  125. catch (Exception ex) { MessageBox.Show(ex.Message); }
  126. //StartWatch.PerformClick();
  127. //bool hide = true;
  128. //if (hide)
  129. //{
  130. // this.WindowState = FormWindowState.Minimized;
  131. // this.ShowInTaskbar = true;
  132. //}
  133. }
  134. private void ConnectDB()
  135. {
  136. dh = new DataHelper();
  137. }
  138. private void StartWatch_Click(object sender, EventArgs e)
  139. {
  140. if (FolderPath.Text == "")
  141. {
  142. OperateResult.AppendText("请选择监控文件夹\n");
  143. return;
  144. }
  145. else
  146. {
  147. if (!Directory.Exists(FolderPath.Text))
  148. {
  149. OperateResult.AppendText("监控文件夹不存在\n");
  150. return;
  151. }
  152. }
  153. if (!dh.CheckExist("line", "li_code='" + li_code.Text + "'"))
  154. {
  155. OperateResult.AppendText("线别不存在\n");
  156. return;
  157. }
  158. XmlWatcher.Path = FolderPath.Text;
  159. XmlWatcher.Filter = "*.*";
  160. XmlWatcher.EnableRaisingEvents = true;
  161. //设置缓存数据
  162. BaseUtil.SetCacheData("FolderPath", FolderPath.Text);
  163. BaseUtil.SetCacheData("BackUpFolderPath", BackUpFolderPath.Text);
  164. BaseUtil.SetCacheData("Master", Master.Text);
  165. BaseUtil.SetCacheData("Line", li_code.Text);
  166. BaseUtil.SetCacheData("MachineType", MachineType.Text);
  167. BaseUtil.SetCacheData("Device", Device.Text);
  168. //BaseUtil.SetCacheData("AutoStart", AutoStart.Checked);
  169. if (Device.Text != "SPI")
  170. {
  171. XmlWatcher.Created += new FileSystemEventHandler(XmlWatcher_Created);
  172. }
  173. else {
  174. Timer.Interval = 1000 * int.Parse(TimerInter.Text.ToString());
  175. Timer.Start();
  176. }
  177. //设置按钮不可点击
  178. StartWatch.Enabled = false;
  179. ChooseFolder.Enabled = false;
  180. Master.Enabled = false;
  181. StopWatch.Enabled = true;
  182. OperateResult.AppendText("开始执行监控\n");
  183. this.WindowState = FormWindowState.Minimized;
  184. this.ShowInTaskbar = false;
  185. }
  186. public void CreateNode(XmlDocument xmlDoc, XmlNode parentNode, string name, string value)
  187. {
  188. XmlNode node = xmlDoc.CreateNode(XmlNodeType.Element, name, null);
  189. node.InnerText = value;
  190. parentNode.AppendChild(node);
  191. }
  192. private void XmlWatcher_Created(object sender, FileSystemEventArgs e)
  193. {
  194. string[] str = fieltype.Split('#');
  195. for (int i = 0; i < str.Length; i++)
  196. {
  197. if (e.FullPath.ToUpper().EndsWith(str[i].ToUpper()))
  198. {
  199. TxtHandleProcess(e.FullPath);
  200. }
  201. }
  202. }
  203. string nextLine;
  204. private void TxtHandleProcess(string FileName)
  205. {
  206. List<string> badcode = new List<string>();
  207. List<string> badlocation = new List<string>();
  208. List<string> badprod = new List<string>();
  209. try
  210. {
  211. string filename = FileName;
  212. string folderpath = FileName.Substring(0, FileName.LastIndexOf(@"\"));
  213. string SN = FileName.Substring(FileName.LastIndexOf(@"\") + 1);
  214. string filetype = FileName.Substring(FileName.LastIndexOf(".") + 1);
  215. {
  216. Console.WriteLine(FileName);
  217. try
  218. {
  219. string sn1 = SN.Split('.')[0];
  220. //string makecode = dh.getFieldDataByCondition("makeserial", "ms_makecode", "ms_sncode='" + sn1 + "'").ToString();
  221. //MachineType.Text = makecode;
  222. string ftppath = "/" + MachineType.Text + "/";
  223. //string ErrMessage = ftp.UpLoadFile(folderpath, SN, ftppath, "");
  224. DirectoryInfo info = new DirectoryInfo(folderpath);
  225. MachineType.Text = info.Name;//获取当前路径最后一级文件夹名称
  226. if (Device.Text == "炉后AOI")
  227. {
  228. StreamReader sR = new StreamReader(FileName, Encoding.Default);
  229. string ASN = "";
  230. string Model = "";
  231. string Shift = "";
  232. string Rep_Date_Time = "";
  233. string OPID = "";
  234. string StationName = "";
  235. string Surface = "";
  236. string CycleTime = "";
  237. string Result = "";
  238. int Rowindex = 0;
  239. List<string> Location = new List<string>();
  240. List<string> PinNum = new List<string>();
  241. List<string> ErrCode = new List<string>();
  242. List<string> RepairStatus = new List<string>();
  243. while ((nextLine = sR.ReadLine()) != null)
  244. {
  245. string data = nextLine.ToString().Split('=')[1];
  246. string itemname = nextLine.ToString().Split('=')[0];
  247. switch (Rowindex)
  248. {
  249. case 0:
  250. ASN = data;
  251. break;
  252. case 1:
  253. Model = data;
  254. break;
  255. case 2:
  256. Shift = data;
  257. break;
  258. case 3:
  259. Rep_Date_Time = data;
  260. break;
  261. case 4:
  262. OPID = data;
  263. break;
  264. case 5:
  265. StationName = data;
  266. break;
  267. case 6:
  268. Surface = data;
  269. break;
  270. case 7:
  271. CycleTime = data;
  272. break;
  273. default:
  274. break;
  275. }
  276. if (itemname == "Result")
  277. {
  278. Result = data;
  279. continue;
  280. }
  281. if (Rowindex >= 8)
  282. {
  283. //Location
  284. if ((Rowindex - 8) % 4 == 0)
  285. {
  286. Location.Add(data);
  287. }
  288. //PinNum
  289. if ((Rowindex - 8) % 4 == 1)
  290. {
  291. PinNum.Add(data);
  292. }
  293. //ErrCode
  294. if ((Rowindex - 8) % 4 == 2)
  295. {
  296. ErrCode.Add(data);
  297. }
  298. //RepairStatus
  299. if ((Rowindex - 8) % 4 == 3)
  300. {
  301. RepairStatus.Add(data);
  302. }
  303. }
  304. Rowindex = Rowindex + 1;
  305. }
  306. sR.Close();
  307. Thread.Sleep(500);
  308. int num = int.Parse(dh.ExecuteSql("insert into STEPTESTDETAIL (std_id,std_sn,std_makecode,std_indate,std_class,STD_RESCODE,std_stepcode,std_linecode,std_machinetype, STD_TESTRESULT)select STEPTESTDETAIL_seq.nextval,'" + SN + "','',sysdate,'" + ftpulr.Split('#')[0] + imaster + "/" + ftppath + SN + "','" + isource + "','" + Device.Text + "','" + li_code.Text + "','" + MachineType.Text + "','" + Result + "' from dual", "insert").ToString());
  309. if (Result.ToUpper() == "FAIL")
  310. {
  311. sql.Clear();
  312. sql.Append("insert into makebad(mb_id,MB_MSCODE, MB_SNCODE, MB_INDATE, MB_STEPCODE, MB_SOURCECODE, MB_BADCODE, MB_YMD, MB_LINECODE,MB_BADNAME)");
  313. sql.Append("values(makebad_seq.nextval,'" + ASN + "','" + ASN + "',sysdate,'AOI-B','',:MB_BADCODE,to_char(sysdate,'yyyymmdd'),'" + li_code.Text + "',:MB_BADNAME)");
  314. dh.BatchInsert(sql.ToString(), new string[] { "mb_badcode", "mb_badname" }, ErrCode.ToArray(), Location.ToArray());
  315. }
  316. if (num > 0)
  317. {
  318. OperateResult.AppendText("序列号:" + SN + "上传成功\n");
  319. string Backupfilename = BackUpFolderPath.Text + @"\" + MachineType.Text + @"\" + SN;
  320. //if (!Directory.Exists(BackUpFolderPath.Text + @"\" + MachineType.Text))
  321. //{
  322. // Directory.CreateDirectory(BackUpFolderPath.Text + @"\" + MachineType.Text);
  323. //}
  324. //if (File.Exists(Backupfilename))
  325. //{
  326. // File.Delete(Backupfilename);
  327. //}
  328. if (deleteFile.Checked)
  329. {
  330. File.Delete(FileName);
  331. }
  332. }
  333. }
  334. else
  335. {
  336. int num = int.Parse(dh.ExecuteSql("insert into STEPTESTDETAIL (std_id,std_sn,std_makecode,std_indate,std_class,STD_RESCODE,std_stepcode,std_linecode,std_machinetype)select STEPTESTDETAIL_seq.nextval,'" + SN + "','',sysdate,'" + ftpulr.Split('#')[0] + imaster + "/" + ftppath + SN + "','" + isource + "','" + Device.Text + "','" + li_code.Text + "','" + MachineType.Text + "' from dual", "insert").ToString());
  337. if (num > 0)
  338. {
  339. OperateResult.AppendText("序列号:" + SN + "上传成功\n");
  340. if (deleteFile.Checked)
  341. {
  342. File.Delete(FileName);
  343. }
  344. //string Backupfilename = BackUpFolderPath.Text + @"\" + MachineType.Text + @"\" + SN;
  345. //if (!Directory.Exists(BackUpFolderPath.Text + @"\" + MachineType.Text))
  346. //{
  347. // Directory.CreateDirectory(BackUpFolderPath.Text + @"\" + MachineType.Text);
  348. //}
  349. //File.Move(FileName, Backupfilename);
  350. }
  351. }
  352. }
  353. catch (Exception ex)
  354. {
  355. OperateResult.AppendText(ex.Message + ex.StackTrace + "\n");
  356. }
  357. }
  358. }
  359. catch (Exception e)
  360. {
  361. OperateResult.AppendText("解析失败:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "\n" + FileName + "\n" + e.Message + "\n");
  362. }
  363. }
  364. private void StopWatch_Click(object sender, EventArgs e)
  365. {
  366. XmlWatcher.EnableRaisingEvents = false;
  367. StartWatch.Enabled = true;
  368. Master.Enabled = true;
  369. ChooseFolder.Enabled = true;
  370. StopWatch.Enabled = false;
  371. OperateResult.AppendText("停止执行监控\n");
  372. }
  373. private void Clean_Click(object sender, EventArgs e)
  374. {
  375. OperateResult.Clear();
  376. }
  377. private void ChooseFolder_Click(object sender, EventArgs e)
  378. {
  379. FolderBrowserDialog folder = new FolderBrowserDialog();
  380. folder.Description = "选择监控文件夹";
  381. DialogResult result = folder.ShowDialog();
  382. if (result == DialogResult.OK)
  383. {
  384. FolderPath.Text = folder.SelectedPath;
  385. }
  386. }
  387. private void ChooseBackUpFolder_Click(object sender, EventArgs e)
  388. {
  389. FolderBrowserDialog folder = new FolderBrowserDialog();
  390. folder.Description = "选择备份文件夹";
  391. DialogResult result = folder.ShowDialog();
  392. if (result == DialogResult.OK)
  393. {
  394. BackUpFolderPath.Text = folder.SelectedPath;
  395. }
  396. }
  397. private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  398. {
  399. string ExitConfirm = MessageBox.Show(this, "最小化托盘?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question).ToString();
  400. if (ExitConfirm == "Yes")
  401. {
  402. WindowState = FormWindowState.Minimized;
  403. e.Cancel = true;
  404. }
  405. else
  406. {
  407. e.Cancel = true;
  408. }
  409. }
  410. private void AutoStart_CheckedChanged(object sender, EventArgs e)
  411. {
  412. SetAutoRun();
  413. }
  414. private void SetAutoRun()
  415. {
  416. if (AutoStart.Checked) //设置开机自启动
  417. {
  418. string path = Application.ExecutablePath;
  419. RegistryKey rk = Registry.LocalMachine;
  420. RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
  421. rk2.SetValue("UAS自动过站解析器.exe", path);
  422. rk2.Close();
  423. rk.Close();
  424. }
  425. else //取消开机自启动
  426. {
  427. string path = Application.ExecutablePath;
  428. RegistryKey rk = Registry.LocalMachine;
  429. RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
  430. rk2.DeleteValue("UAS自动过站解析器.exe", false);
  431. rk2.Close();
  432. rk.Close();
  433. }
  434. }
  435. private void BatchAnalysus_Click(object sender, EventArgs e)
  436. {
  437. //var AllXmls = Directory.GetFiles(FolderPath.Text, "*.txt");
  438. //foreach (var Xml in AllXmls)
  439. //{
  440. // TxtHandleProcess(Xml);
  441. //}
  442. }
  443. private void Timer_Tick(object sender, EventArgs e)
  444. {
  445. if (deleteFile.Checked)
  446. {
  447. var AllXmls = Directory.GetFiles(FolderPath.Text, "*.*", SearchOption.AllDirectories).Where(s => s.ToUpper().EndsWith(".TXT") || s.ToUpper().EndsWith(".INI"));
  448. foreach (var Xml in AllXmls)
  449. {
  450. try
  451. {
  452. TxtHandleProcess(Xml.ToString());
  453. File.Delete(Xml.ToString());
  454. }
  455. catch (Exception ex)
  456. {
  457. }
  458. }
  459. }
  460. }
  461. private void CleanFolderPath_Click(object sender, EventArgs e)
  462. {
  463. FolderPath.Text = "";
  464. }
  465. private void CleanBackUpFolderPath_Click(object sender, EventArgs e)
  466. {
  467. BackUpFolderPath.Text = "";
  468. }
  469. private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
  470. {
  471. //if (WindowState == FormWindowState.Minimized)
  472. {
  473. Thread.Sleep(1000);
  474. WindowState = FormWindowState.Normal;
  475. Thread.Sleep(1000);
  476. ShowInTaskbar = true;
  477. }
  478. }
  479. private void AutoAnalysisXmlByStep_Deactivate(object sender, EventArgs e)
  480. {
  481. if (WindowState == FormWindowState.Minimized)
  482. {
  483. ShowInTaskbar = false;
  484. }
  485. }
  486. }
  487. }