AutoAnalysisXmlByStep.cs 20 KB

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