Make_ParseLog.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. using BarTender;
  2. using DevExpress.Utils.OAuth.Provider;
  3. using DevExpress.Xpo.Logger;
  4. using DevExpress.XtraEditors.Controls;
  5. using DevExpress.XtraExport;
  6. using DevExpress.XtraPrinting;
  7. using NPOI.SS.Formula.Functions;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.ComponentModel;
  11. using System.Data;
  12. using System.Drawing;
  13. using System.IO;
  14. using System.Linq;
  15. using System.Net;
  16. using System.Security;
  17. using System.Security.AccessControl;
  18. using System.Security.Principal;
  19. using System.Text;
  20. using System.Threading.Tasks;
  21. using System.Web.Services.Description;
  22. using System.Web.UI.WebControls;
  23. using System.Windows.Forms;
  24. using System.Xml;
  25. using UAS_MES_NEW.CustomControl.ButtonUtil;
  26. using UAS_MES_NEW.CustomControl.TextBoxWithIcon;
  27. using UAS_MES_NEW.DataOperate;
  28. using UAS_MES_NEW.Entity;
  29. using UAS_MES_NEW.PublicMethod;
  30. using static DevExpress.Xpo.DB.DataStoreLongrunnersWatch;
  31. using static System.Windows.Forms.VisualStyles.VisualStyleElement.Tab;
  32. namespace UAS_MES_NEW.Make
  33. {
  34. public partial class Make_ParseLog : Form
  35. {
  36. public Make_ParseLog()
  37. {
  38. InitializeComponent();
  39. }
  40. FileSystemWatcher watcher;
  41. LogStringBuilder sql = new LogStringBuilder();
  42. DataTable Dbfind;
  43. DataTable dt;
  44. DataHelper dh;
  45. string currFileType = "",equiType = "";
  46. string SN, omakeCode, oMsid, oErrorMessage = "";
  47. List<string> fileList = new List<string>();
  48. Timer formsTimer;
  49. string Board, Module;
  50. private void Make_ParseLog_Load(object sender, EventArgs e)
  51. {
  52. dh = SystemInf.dh;
  53. fileList.Add("C:\\Users\\MI\\Desktop");
  54. formsTimer = new Timer();
  55. formsTimer.Interval = 10000;
  56. formsTimer.Tick += timer1_Tick;
  57. //formsTimer.Start();
  58. //formsTimer.Stop();
  59. watcher = new FileSystemWatcher();
  60. watcher.NotifyFilter = NotifyFilters.FileName | NotifyFilters.LastWrite;
  61. watcher.Created += OnFileCreated;
  62. watcher.Changed += OnFileChanged;
  63. /*watcher.Deleted += OnFileChanged;
  64. watcher.Renamed += OnFileChanged;*/
  65. //设置锁定工单
  66. LockMakeCode.GetMakeCodeCtl(ma_code);
  67. ma_code.SetLockCheckBox(LockMakeCode);
  68. //工单号放大镜配置
  69. ma_code.TableName = "make left join product on ma_prodcode=pr_code";
  70. ma_code.SelectField = "ma_code # 工单号,pr_code # 产品编号,pr_detail # 产品名称";
  71. ma_code.FormName = Name;
  72. ma_code.SetValueField = new string[] { "ma_code", "pr_code", "pr_detail" };
  73. ma_code.Condition = "ma_statuscode='STARTED'";
  74. ma_code.DbChange += Ma_code_DbChange;
  75. }
  76. private void Ma_code_DbChange(object sender, EventArgs e)
  77. {
  78. Dbfind = ma_code.ReturnData;
  79. BaseUtil.SetFormValue(this.Controls, Dbfind);
  80. //获取工单的其他信息
  81. sql.Clear();
  82. sql.Append("select ma_code,nvl(mcd_okqty,0) mcd_okqty,ma_prodcode as pr_code ,pr_detail,");
  83. sql.Append("pr_spec,ma_qty - nvl(mcd_inqty, 0) mcd_remainqty from make left join makecraftdetail on ");
  84. sql.Append("mcd_maid=ma_id left join product on pr_code=ma_prodcode where ma_code='" + ma_code.Text + "' and mcd_stepcode='" + User.CurrentStepCode + "'");
  85. dt = (DataTable)dh.ExecuteSql(sql.GetString(), "select");
  86. if (dt.Rows.Count > 0)
  87. {
  88. BaseUtil.SetFormValue(this.Controls, dt);
  89. }
  90. }
  91. private void Device_SelectedIndexChanged(object sender, EventArgs e)
  92. {
  93. if (Device.SelectedIndex == -1) return;
  94. switch (Device.SelectedIndex)
  95. {
  96. case 0:
  97. currFileType = "ini";
  98. equiType = "贴片设备";
  99. break;
  100. }
  101. /*if (string.IsNullOrEmpty(ma_code.Text))
  102. {
  103. Device.SelectedIndex = -1;
  104. txtPath.Text = "";
  105. MessageBox.Show(this.ParentForm, "请选择工单", "提示");
  106. return;
  107. }*/
  108. txtPath.Text = fileList[Device.SelectedIndex];
  109. txtPath.Focus();
  110. txtPath.SelectAll();
  111. //ma_code.Enabled = true;
  112. txtPath.Enabled = false;
  113. }
  114. private void Choose_Click(object sender, EventArgs e)
  115. {
  116. using (var dialog = new FolderBrowserDialog())
  117. {
  118. switch (Device.SelectedIndex)
  119. {
  120. case 0:
  121. dialog.Description = "请选择读取设备文件夹";
  122. currFileType = "ini";
  123. equiType = "贴片设备";
  124. break;
  125. }
  126. if (dialog.ShowDialog() == DialogResult.OK)
  127. {
  128. txtPath.Text = dialog.SelectedPath;
  129. txtPath.Enabled = false;
  130. Device.Enabled = false;
  131. watcher.EnableRaisingEvents = false;
  132. }
  133. }
  134. }
  135. private void claerBtn_Click(object sender, EventArgs e)
  136. {
  137. lstFiles.Items.Clear();
  138. }
  139. private void allParse_Click(object sender, EventArgs e)
  140. {
  141. /*if (Device.SelectedIndex == 0)
  142. {
  143. if (string.IsNullOrEmpty(ma_code.Text))
  144. {
  145. MessageBox.Show(this.ParentForm, "请选择工单", "提示");
  146. return;
  147. }
  148. }*/
  149. if (Device.SelectedIndex == -1)
  150. {
  151. Device.Focus();
  152. Device.SelectAll();
  153. MessageBox.Show(this.ParentForm, "请选择设备", "提示");
  154. return;
  155. }
  156. if (String.IsNullOrEmpty(txtPath.Text))
  157. {
  158. txtPath.Focus();
  159. txtPath.SelectAll();
  160. return;
  161. }
  162. string[] txtFiles = Directory.GetFiles(txtPath.Text, $"*.{currFileType}");
  163. if (txtFiles.Length == 0)
  164. {
  165. LogMessage($"当前{txtPath.Text},没有{equiType}设备输出的{currFileType.ToLower()}文件");
  166. return;
  167. }
  168. RefreshFileList();
  169. }
  170. private void onWatch_Click(object sender, EventArgs e)
  171. {
  172. /*if (Device.SelectedIndex == 0)
  173. {
  174. if (string.IsNullOrEmpty(ma_code.Text))
  175. {
  176. MessageBox.Show(this.ParentForm, "请选择工单", "提示");
  177. return;
  178. }
  179. }*/
  180. if (Device.SelectedIndex == -1)
  181. {
  182. Device.Focus();
  183. Device.SelectAll();
  184. MessageBox.Show(this.ParentForm, "请选择设备", "提示");
  185. return;
  186. }
  187. if (String.IsNullOrEmpty(txtPath.Text))
  188. {
  189. txtPath.Focus();
  190. txtPath.SelectAll();
  191. return;
  192. }
  193. try
  194. {
  195. watcher.Path = txtPath.Text;
  196. watcher.Filter = $"*.{currFileType}";
  197. if (onWatch.Text == "开启解析")
  198. {
  199. watcher.EnableRaisingEvents = true;
  200. onWatch.Text = "关闭解析";
  201. }
  202. else
  203. {
  204. watcher.EnableRaisingEvents = false;
  205. onWatch.Text = "开启解析";
  206. }
  207. }
  208. catch (Exception ex)
  209. {
  210. MessageBox.Show(this.ParentForm, ex.Message, "警告");
  211. }
  212. }
  213. private void OnFileCreated(object sender, FileSystemEventArgs e)
  214. {
  215. RefreshFileList();
  216. }
  217. private void OnFileChanged(object sender, FileSystemEventArgs e)
  218. {
  219. if (e.ChangeType == WatcherChangeTypes.Changed)
  220. {
  221. RefreshFileList();
  222. }
  223. }
  224. private void RefreshFileList()
  225. {
  226. if (lstFiles.InvokeRequired)
  227. {
  228. lstFiles.Invoke(new Action(RefreshFileList));
  229. lstFiles.BeginInvoke(new Action(RefreshFileList));
  230. return;
  231. }
  232. try
  233. {
  234. if (!Directory.Exists(txtPath.Text))
  235. {
  236. LogMessage($"目录不存在: {txtPath.Text}");
  237. return;
  238. }
  239. string[] txtFiles = Directory.GetFiles(txtPath.Text, $"*.{currFileType}");
  240. if (txtFiles.Length == 0)
  241. {
  242. return;
  243. }
  244. else
  245. {
  246. foreach (string file in txtFiles)
  247. {
  248. if (!File.Exists(file))
  249. {
  250. LogMessage($"文件不存在: {file}");
  251. }
  252. string outFileMsg = CheckFileAccess(file);
  253. if (outFileMsg != "OK")
  254. {
  255. LogMessage(outFileMsg);
  256. continue;
  257. }
  258. if(File.ReadAllText(file).Length == 0)
  259. {
  260. continue;
  261. }
  262. if (Device.SelectedIndex == 0)
  263. {
  264. ParseLogInsert(file);
  265. }
  266. }
  267. }
  268. }
  269. catch (Exception ex)
  270. {
  271. LogMessage($"Error: 解析文件列表失败: {ex.Message}");
  272. }
  273. }
  274. private void ParseLogInsert(string PathName)
  275. {
  276. try
  277. {
  278. StreamReader SR = File.OpenText(PathName);
  279. string restOfStream = SR.ReadToEnd();
  280. SR.Close();
  281. SR.Dispose();
  282. string[] lines = restOfStream.Split(new string[] { "\r\n" }, StringSplitOptions.None);
  283. for (int i = 0; i < lines.Length; i++)
  284. {
  285. if (string.IsNullOrEmpty(lines[i])) continue;
  286. if (equiType == "贴片设备")
  287. {
  288. if (lines[i].Trim() == "[General.Count]")
  289. {
  290. if(string.IsNullOrEmpty(lines[i + 1]))
  291. {
  292. LogMessage($"文件: {PathName},更新[General.Count]下无Board数据NG");
  293. }
  294. if(lines[i + 1].Split('=')[0].ToUpper() != "BOARD")
  295. {
  296. LogMessage($"文件: {PathName},更新[General.Count]下无Board数据NG");
  297. }
  298. Board = lines[i + 1].Split('=')[1];
  299. break;
  300. }
  301. }
  302. }
  303. if (string.IsNullOrEmpty(Board))
  304. {
  305. LogMessage($"文件: {PathName},更新[General.Count]下Board数据NG: 未找到");
  306. return;
  307. }
  308. else
  309. {
  310. LogMessage($"文件: {PathName},[General.Count]下Board已更新: {Board}");
  311. }
  312. if (ConsoleLog(restOfStream, PathName))
  313. {
  314. File.WriteAllText(PathName, string.Empty);
  315. File.Delete(PathName);
  316. }
  317. }
  318. catch (Exception ex)
  319. {
  320. LogMessage($"Error: 文件 {PathName}, {ex.Message}");
  321. }
  322. }
  323. private void timer1_Tick(object sender, EventArgs e)
  324. {
  325. try
  326. {
  327. }
  328. catch (Exception ex)
  329. {
  330. LogMessage($"定时器异常: {ex.Message}");
  331. }
  332. }
  333. private bool ConsoleLog(string Content, string PathName)
  334. {
  335. try
  336. {
  337. string sourceDir = Path.GetDirectoryName(PathName);
  338. string newFolderName = "Logs";
  339. string newFolderPath = Path.Combine(sourceDir, newFolderName);
  340. if (!Directory.Exists(newFolderPath))
  341. {
  342. Directory.CreateDirectory(newFolderPath);
  343. }
  344. string newFileName = "Log_" + Path.GetFileName(PathName);
  345. string newFilePath = Path.Combine(newFolderPath, newFileName);
  346. File.AppendAllText(newFilePath, Content + Environment.NewLine);
  347. return true;
  348. }
  349. catch (Exception ex)
  350. {
  351. MessageBox.Show(this.ParentForm, ex.Message, "警告");
  352. return false;
  353. }
  354. }
  355. private void LogMessage(string message)
  356. {
  357. bool isNg;
  358. if (message.ToUpper().Contains("NG"))
  359. {
  360. isNg = true;
  361. }
  362. else if (message.ToUpper().Contains("ERROR"))
  363. {
  364. isNg = true;
  365. }
  366. else
  367. {
  368. isNg = false;
  369. }
  370. if (isNg)
  371. {
  372. if (lstOk.InvokeRequired)
  373. {
  374. lstOk.Invoke(new Action<string>(LogMessage), message);
  375. return;
  376. }
  377. lstOk.Items.Add($"[{DateTime.Now:yyyy-MM-dd HH:mm:ss}] {message}");
  378. lstOk.TopIndex = lstFiles.Items.Count - 1;
  379. }
  380. else
  381. {
  382. if (lstFiles.InvokeRequired)
  383. {
  384. lstFiles.Invoke(new Action<string>(LogMessage), message);
  385. return;
  386. }
  387. lstFiles.Items.Add($"[{DateTime.Now:yyyy-MM-dd HH:mm:ss}] {message}");
  388. lstFiles.TopIndex = lstFiles.Items.Count - 1;
  389. lstFiles.DrawMode = DrawMode.OwnerDrawFixed;
  390. }
  391. }
  392. private void lstFiles_DrawItem_1(object sender, DrawItemEventArgs e)
  393. {
  394. e.DrawBackground();
  395. Brush mybsh = Brushes.Black;
  396. if (lstFiles.Items[e.Index].ToString().Contains("NG"))
  397. {
  398. mybsh = Brushes.Red;
  399. }
  400. else if (lstFiles.Items[e.Index].ToString().Contains("ERROR"))
  401. {
  402. mybsh = Brushes.Red;
  403. }
  404. e.DrawFocusRectangle();
  405. e.Graphics.DrawString(lstFiles.Items[e.Index].ToString(), e.Font, mybsh, e.Bounds, StringFormat.GenericDefault);
  406. }
  407. public string CheckFileAccess(string filePath)
  408. {
  409. try
  410. {
  411. using (FileStream fileStream = File.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.None))
  412. {
  413. return "OK";
  414. }
  415. }
  416. catch (IOException ex)
  417. {
  418. return "Error文件被占用: " + ex.Message;
  419. }
  420. catch (UnauthorizedAccessException ex)
  421. {
  422. return "Error权限不足,无法访问文件: " + ex.Message;
  423. }
  424. catch (Exception ex)
  425. {
  426. return ex.Message;
  427. }
  428. }
  429. private class Log
  430. {
  431. public string SN { set; get; }
  432. public string Result { set; get; }
  433. }
  434. }
  435. }