AutoAnalysisXmlByStep.cs 21 KB

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