Make_ParseLog.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  1. using DevExpress.Utils.CodedUISupport;
  2. using DevExpress.Xpo.Logger;
  3. using HslCommunication;
  4. using NPOI.POIFS.NIO;
  5. using NPOI.SS.Formula.Functions;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.ComponentModel;
  9. using System.Data;
  10. using System.Drawing;
  11. using System.IO;
  12. using System.Linq;
  13. using System.Net;
  14. using System.Text;
  15. using System.Web.Services.Description;
  16. using System.Web.UI.WebControls;
  17. using System.Windows.Forms;
  18. using UAS_MES_NEW.CustomControl.ButtonUtil;
  19. using UAS_MES_NEW.DataOperate;
  20. using UAS_MES_NEW.Entity;
  21. using UAS_MES_NEW.PublicMethod;
  22. namespace UAS_MES_NEW.Make
  23. {
  24. public partial class Make_ParseLog : Form
  25. {
  26. public Make_ParseLog()
  27. {
  28. InitializeComponent();
  29. }
  30. FileSystemWatcher watcher;
  31. LogStringBuilder sql = new LogStringBuilder();
  32. DataTable Dbfind;
  33. DataTable dt;
  34. DataHelper dh;
  35. string currFileType = "", equiType = "";
  36. string SN, omakeCode, oMsid, oErrorMessage = "";
  37. List<string> fileList = new List<string>();
  38. Timer ChangeWoTimer;
  39. private void Make_ParseLog_Load(object sender, EventArgs e)
  40. {
  41. dh = SystemInf.dh;
  42. Choose.Enabled = false;
  43. fileList.Add("C:\\Users\\MI\\Desktop");
  44. ChangeWoTimer = new Timer();
  45. ChangeWoTimer.Interval = 30000;
  46. ChangeWoTimer.Tick += ChangeWoFunc;
  47. watcher = new FileSystemWatcher();
  48. watcher.NotifyFilter = NotifyFilters.FileName | NotifyFilters.LastWrite;
  49. watcher.Created += OnFileCreated;
  50. watcher.Changed += OnFileChanged;
  51. /*watcher.Deleted += OnFileChanged;
  52. watcher.Renamed += OnFileChanged;*/
  53. //设置锁定工单
  54. LockMakeCode.GetMakeCodeCtl(ma_code);
  55. ma_code.SetLockCheckBox(LockMakeCode);
  56. //工单号放大镜配置
  57. ma_code.TableName = "make left join product on ma_prodcode=pr_code";
  58. ma_code.SelectField = "ma_code # 工单号,pr_code # 产品编号,pr_detail # 产品名称";
  59. ma_code.FormName = Name;
  60. ma_code.SetValueField = new string[] { "ma_code", "pr_code", "pr_detail" };
  61. ma_code.Condition = "ma_statuscode='STARTED'";
  62. ma_code.DbChange += Ma_code_DbChange;
  63. }
  64. private void Ma_code_DbChange(object sender, EventArgs e)
  65. {
  66. Dbfind = ma_code.ReturnData;
  67. BaseUtil.SetFormValue(this.Controls, Dbfind);
  68. //获取工单的其他信息
  69. sql.Clear();
  70. sql.Append("select ma_code,nvl(mcd_okqty,0) mcd_okqty,ma_prodcode as pr_code ,pr_detail,");
  71. sql.Append("pr_spec,ma_qty - nvl(mcd_inqty, 0) mcd_remainqty from make left join makecraftdetail on ");
  72. 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 + "'");
  73. dt = (DataTable)dh.ExecuteSql(sql.GetString(), "select");
  74. if (dt.Rows.Count > 0)
  75. {
  76. BaseUtil.SetFormValue(this.Controls, dt);
  77. }
  78. }
  79. private void Device_SelectedIndexChanged(object sender, EventArgs e)
  80. {
  81. if (Device.SelectedIndex == -1) return;
  82. switch (Device.SelectedIndex)
  83. {
  84. case 0:
  85. currFileType = "Txt";
  86. equiType = "AOI";
  87. //ChangeWoTimer.Start();
  88. break;
  89. case 1:
  90. currFileType = "jpg";
  91. equiType = "Xray";
  92. //ChangeWoTimer.Stop();
  93. break;
  94. case 2:
  95. currFileType = "jpg";
  96. equiType = "CCD";
  97. //ChangeWoTimer.Stop();
  98. break;
  99. }
  100. if (string.IsNullOrEmpty(ma_code.Text))
  101. {
  102. Device.SelectedIndex = -1;
  103. txtPath.Text = "";
  104. MessageBox.Show(this.ParentForm, "请选择工单", "提示");
  105. return;
  106. }
  107. txtPath.Text = fileList[Device.SelectedIndex];
  108. txtPath.Focus();
  109. txtPath.SelectAll();
  110. ma_code.Enabled = true;
  111. Choose.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. if (dialog.ShowDialog() == DialogResult.OK)
  119. {
  120. txtPath.Text = dialog.SelectedPath;
  121. txtPath.Enabled = false;
  122. Device.Enabled = false;
  123. watcher.EnableRaisingEvents = false;
  124. }
  125. }
  126. }
  127. private void claerBtn_Click(object sender, EventArgs e)
  128. {
  129. lstFiles.Items.Clear();
  130. }
  131. private void allParse_Click(object sender, EventArgs e)
  132. {
  133. if (Device.SelectedIndex == 0)
  134. {
  135. if (string.IsNullOrEmpty(ma_code.Text))
  136. {
  137. MessageBox.Show(this.ParentForm, "请选择工单", "提示");
  138. return;
  139. }
  140. }
  141. if (Device.SelectedIndex == -1)
  142. {
  143. Device.Focus();
  144. Device.SelectAll();
  145. MessageBox.Show(this.ParentForm, "请选择设备", "提示");
  146. return;
  147. }
  148. if (String.IsNullOrEmpty(txtPath.Text))
  149. {
  150. txtPath.Focus();
  151. txtPath.SelectAll();
  152. MessageBox.Show(this.ParentForm, "请选择解析路径", "提示");
  153. return;
  154. }
  155. string[] txtFiles = Directory.GetFiles(txtPath.Text, $"*.{currFileType}");
  156. if (txtFiles.Length == 0)
  157. {
  158. LogMessage(0, $"当前{txtPath.Text},没有{equiType}设备输出的{currFileType.ToLower()}文件");
  159. return;
  160. }
  161. RefreshFileList();
  162. }
  163. private void onWatch_Click(object sender, EventArgs e)
  164. {
  165. if (Device.SelectedIndex == 0)
  166. {
  167. if (string.IsNullOrEmpty(ma_code.Text))
  168. {
  169. MessageBox.Show(this.ParentForm, "请选择工单", "提示");
  170. return;
  171. }
  172. }
  173. if (Device.SelectedIndex == -1)
  174. {
  175. Device.Focus();
  176. Device.SelectAll();
  177. MessageBox.Show(this.ParentForm, "请选择设备", "提示");
  178. return;
  179. }
  180. if (String.IsNullOrEmpty(txtPath.Text))
  181. {
  182. txtPath.Focus();
  183. txtPath.SelectAll();
  184. MessageBox.Show(this.ParentForm, "请选择解析路径", "提示");
  185. return;
  186. }
  187. try
  188. {
  189. watcher.Path = txtPath.Text;
  190. watcher.Filter = $"*.{currFileType}";
  191. if (onWatch.Text == "开启解析")
  192. {
  193. watcher.EnableRaisingEvents = true;
  194. onWatch.Text = "关闭解析";
  195. }
  196. else
  197. {
  198. watcher.EnableRaisingEvents = false;
  199. onWatch.Text = "开启解析";
  200. }
  201. }
  202. catch (Exception ex)
  203. {
  204. MessageBox.Show(this.ParentForm, ex.Message, "警告");
  205. }
  206. }
  207. private void OnFileCreated(object sender, FileSystemEventArgs e)
  208. {
  209. RefreshFileList();
  210. }
  211. private void OnFileChanged(object sender, FileSystemEventArgs e)
  212. {
  213. if (e.ChangeType == WatcherChangeTypes.Changed)
  214. {
  215. RefreshFileList();
  216. }
  217. }
  218. private void RefreshFileList()
  219. {
  220. if (lstFiles.InvokeRequired)
  221. {
  222. lstFiles.Invoke(new Action(RefreshFileList));
  223. lstFiles.BeginInvoke(new Action(RefreshFileList));
  224. return;
  225. }
  226. try
  227. {
  228. if (!Directory.Exists(txtPath.Text))
  229. {
  230. LogMessage(0, $"目录不存在: {txtPath.Text}");
  231. return;
  232. }
  233. string[] txtFiles = Directory.GetFiles(txtPath.Text, $"*.{currFileType}");
  234. if (txtFiles.Length == 0)
  235. {
  236. return;
  237. }
  238. foreach (string file in txtFiles)
  239. {
  240. if (!File.Exists(file))
  241. {
  242. LogMessage(0, $"文件不存在: {file}");
  243. }
  244. string outFileMsg = CheckFileAccess(file);
  245. if (outFileMsg != "OK")
  246. {
  247. LogMessage(0, outFileMsg);
  248. continue;
  249. }
  250. if (File.ReadAllText(file).Length == 0)
  251. {
  252. continue;
  253. }
  254. if (equiType == "AOI")
  255. {
  256. using (StreamReader SR = new StreamReader(file, Encoding.GetEncoding("GBK")))
  257. {
  258. string Content = SR.ReadToEnd();
  259. SR.Close();
  260. SR.Dispose();
  261. List<Log> logArr = new List<Log>() { };
  262. string[] lines = Content.Split(new string[] { "\r\n" }, StringSplitOptions.None);
  263. int fileNum = string.IsNullOrEmpty(lines[lines.Length - 1]) ? lines.Length - 1 : lines.Length;
  264. Log itemLog = new Log() { };
  265. foreach (var item in lines)
  266. {
  267. if (string.IsNullOrEmpty(item)) continue;
  268. if (Array.IndexOf(lines, item) == 1) itemLog.SN = item.Split(':')[1].ToString().Trim();
  269. if (Array.IndexOf(lines, item) == 2) itemLog.TestTime = item.Split(':')[1].Split('.')[0].ToString().Trim();
  270. if (Array.IndexOf(lines, item) == 3) itemLog.Result = item.Split(':')[1].ToString().Trim();
  271. if (Array.IndexOf(lines, item) == 4) itemLog.Side = item.Split(':')[1].ToString().Trim();
  272. if (item.IndexOf("元件位置") > -1)
  273. {
  274. LogItem list = new LogItem();
  275. int ind = Array.IndexOf(lines, item);
  276. list.Location = lines[ind].Split(':')[1].ToString().Trim();
  277. list.ReelNo = lines[ind + 1].Split(':')[1].ToString().Trim();
  278. list.FirstType = lines[ind + 2].Split(':')[1].ToString().Trim();
  279. list.SceondType = lines[ind + 3].Split(':')[1].ToString().Trim();
  280. list.Name = lines[ind + 4].Split(':')[1].ToString().Trim();
  281. itemLog.LogItemList.Add(list);
  282. }
  283. }
  284. logArr.Add(itemLog);
  285. if (logArr.Count == 0)
  286. {
  287. break;
  288. }
  289. if (InsertDb(logArr, file, fileNum))
  290. {
  291. if (ConsoleLog(Content, file, logArr[0].SN))
  292. {
  293. File.WriteAllText(file, string.Empty);
  294. File.Delete(file);
  295. }
  296. }
  297. }
  298. }
  299. else if (equiType == "Xray")
  300. {
  301. }
  302. else if(equiType == "CCD")
  303. {
  304. SN = Path.GetFileName(file).Split('_')[0].ToString();
  305. if (UploadImageToFtp(file, SN))
  306. {
  307. dh.ExecuteSql($@"INSERT INTO steptestdetail( sd_id, sd_makecode, sd_sn, SD_MACHINECODE, SD_INDATE,SD_ACTVALUE)
  308. VALUES( steptestdetail_seq.NEXTVAL, '{ma_code.Text}', '{SN}', 'CCD', sysdate,
  309. 'http://192.168.1.92:8088/ftp/mes/Picture/{SN}/{Path.GetFileName(file)}')", "insert");
  310. }
  311. }
  312. }
  313. }
  314. catch (Exception ex)
  315. {
  316. LogMessage(0, $"Error: 解析文件列表失败: {ex.Message}");
  317. }
  318. }
  319. private bool InsertDb(List<Log> logs, string PathName, int fileNum)
  320. {
  321. try
  322. {
  323. StringBuilder sql = new StringBuilder();
  324. StringBuilder details = new StringBuilder();
  325. List<string> param = new List<string>() { };
  326. foreach (var item in logs)
  327. {
  328. //string sqlStr = $@"select ma_code,nvl(mcd_okqty,0) mcd_okqty,ma_prodcode,pr_detail,ma_qty - nvl(mcd_okqty, 0) remain_qty
  329. // from make left join makecraftdetail on mcd_maid=ma_id left join product on pr_code = ma_prodcode
  330. // where ma_code='{ma_code.Text}' and mcd_stepcode='" + User.CurrentStepCode + "'";
  331. //dt = (DataTable)dh.ExecuteSql(sqlStr, "select");
  332. //BaseUtil.SetFormValue(Controls, dt);
  333. string outMsg = "";
  334. param.Add(ma_code.Text);
  335. param.Add(item.SN);
  336. param.Add(User.UserSourceCode);
  337. param.Add(item.Result.ToUpper());
  338. param.Add(item.TestTime);
  339. param.Add(item.Side);
  340. param.Add(equiType);
  341. details.Clear();
  342. foreach (LogItem LI in item.LogItemList)
  343. {
  344. details.Append($"{LI.Location}/{LI.ReelNo}/{LI.FirstType}/{LI.SceondType}/{LI.Name};");
  345. }
  346. param.Add(details.ToString());
  347. param.Add(outMsg);
  348. string[] paramList = param.ToArray();
  349. dh.CallProcedure("cs_insert_testrejects", ref paramList);
  350. if(paramList[8].Substring(0, 2) == "OK")
  351. {
  352. LogMessage(1, $"文件: {item.SN} 采集成功, 测试结果为{item.Result},共{item.LogItemList.Count}条信息");
  353. }
  354. else
  355. {
  356. LogMessage(1, paramList[8]);
  357. }
  358. param.Clear();
  359. if (logs.IndexOf(item) == logs.Count - 1)
  360. {
  361. LogicHandler.DoCommandLog(Tag.ToString(), User.UserCode, ma_code.Text, User.UserLineCode, User.UserSourceCode, "日志解析", "日志解析过站成功", item.SN, "");
  362. return true;
  363. }
  364. /* if (LogicHandler.CheckStepSNAndMacode(ma_code.Text, User.UserSourceCode, item.SN, User.UserCode, false, out omakeCode, out oMsid, out oErrorMessage))
  365. {
  366. if (LogicHandler.SetStepResult(omakeCode, User.UserSourceCode, item.SN, "设备日志解析", "OK", User.UserCode, false, out oErrorMessage))
  367. {
  368. string sqlStr = $@"select ma_code,nvl(mcd_okqty,0) mcd_okqty,ma_prodcode,pr_detail,ma_qty - nvl(mcd_okqty, 0) remain_qty
  369. from make left join makecraftdetail on mcd_maid=ma_id left join product on pr_code = ma_prodcode
  370. where ma_code='" + omakeCode + "' and mcd_stepcode='" + User.CurrentStepCode + "'";
  371. dt = (DataTable)dh.ExecuteSql(sqlStr, "select");
  372. BaseUtil.SetFormValue(Controls, dt);
  373. //记录操作日志
  374. LogicHandler.DoCommandLog(Tag.ToString(), User.UserCode, ma_code.Text, User.UserLineCode, User.UserSourceCode, "日志解析", "日志解析成功", item.SN, "");
  375. string outMsg = "";
  376. param.Add(item.SN);
  377. param.Add(omakeCode);
  378. param.Add(User.UserSourceCode);
  379. param.Add(item.Result);
  380. param.Add(outMsg);
  381. string[] paramList = param.ToArray();
  382. dh.CallProcedure("cs_insert_testdetail", ref paramList);
  383. LogMessage($"文件: {PathName},共{fileNum}条记录SN: {item.SN}解析已过站");
  384. param.Clear();
  385. if (logs.IndexOf(item) == logs.Count - 1)
  386. {
  387. return true;
  388. }
  389. }
  390. else
  391. {
  392. LogMessage($"处理过站NG:{oErrorMessage}");
  393. break;
  394. }
  395. }
  396. else
  397. {
  398. LogMessage($"过站核对NG:{oErrorMessage}");
  399. break;
  400. }*/
  401. }
  402. return false;
  403. }
  404. catch (Exception ex)
  405. {
  406. LogMessage(0, $"Error,处理解析写入: {ex.Message}");
  407. return false;
  408. }
  409. }
  410. private void ChangeWoFunc(object sender, EventArgs e)
  411. {
  412. try
  413. {
  414. sql.Clear();
  415. sql.Append($"SELECT dl_macode FROM deviceline WHERE dl_linecode = '{User.UserLineCode}'");
  416. dt = (DataTable)dh.ExecuteSql(sql.GetString(), "select");
  417. if (dt.Rows.Count > 0 && !String.IsNullOrEmpty(dt.Rows[0]["dl_macode"].ToString()))
  418. {
  419. ma_code.Text = dt.Rows[0]["dl_macode"].ToString().Trim();
  420. return;
  421. }
  422. LogMessage(0, $"NG,自动识别更新工单");
  423. }
  424. catch (Exception ex)
  425. {
  426. LogMessage(0, $"Error,自动切换工单: {ex.Message}");
  427. }
  428. }
  429. private bool ConsoleLog(string Content, string PathName,string SN)
  430. {
  431. try
  432. {
  433. string sourcePaht = Path.GetDirectoryName(PathName);
  434. string changeName = Path.Combine(sourcePaht, $"{SN}.{currFileType}");
  435. string newFolderName = "Logs";
  436. string newFolderPath = Path.Combine(sourcePaht, newFolderName);
  437. if (!Directory.Exists(newFolderPath))
  438. {
  439. Directory.CreateDirectory(newFolderPath);
  440. }
  441. string newFileName = "Log_" + Path.GetFileName(changeName);
  442. string newFilePath = Path.Combine(newFolderPath, newFileName);
  443. File.AppendAllText(newFilePath, Content + Environment.NewLine);
  444. return true;
  445. }
  446. catch (Exception ex)
  447. {
  448. MessageBox.Show(this.ParentForm, ex.Message, "警告");
  449. return false;
  450. }
  451. }
  452. private void LogMessage(int type, string message)
  453. {
  454. if (type == 0)
  455. {
  456. if (lstFiles.InvokeRequired)
  457. {
  458. lstFiles.Invoke(new Action<int, string>(LogMessage), new object[] { type, message });
  459. return;
  460. }
  461. lstFiles.Items.Add($"[{DateTime.Now:yyyy-MM-dd HH:mm:ss}] {message}");
  462. lstFiles.TopIndex = lstFiles.Items.Count - 1;
  463. }
  464. else
  465. {
  466. if (lstOk.InvokeRequired)
  467. {
  468. lstOk.Invoke(new Action<int, string>(LogMessage), new object[] { type, message });
  469. return;
  470. }
  471. lstOk.Items.Add($"[{DateTime.Now:yyyy-MM-dd HH:mm:ss}] {message}");
  472. lstOk.TopIndex = lstOk.Items.Count - 1;
  473. }
  474. }
  475. private void lstFiles_DrawItem(object sender, DrawItemEventArgs e)
  476. {
  477. if (e.Index < 0) return;
  478. e.DrawBackground();
  479. string txt = lstFiles.Items[e.Index].ToString().ToUpper();
  480. Brush color = Brushes.Black;
  481. if (txt.Contains("NG"))
  482. {
  483. color = Brushes.Red;
  484. }
  485. else if (txt.Contains("ERROR"))
  486. {
  487. color = Brushes.Red;
  488. }
  489. else
  490. {
  491. color = Brushes.Green;
  492. }
  493. e.DrawFocusRectangle();
  494. e.Graphics.DrawString(lstFiles.Items[e.Index].ToString(), e.Font, color, e.Bounds, StringFormat.GenericDefault);
  495. }
  496. public bool UploadImageToFtp(string localFilePath, string Sn)
  497. {
  498. //string ftpServer = "ftp://10.8.0.208:21/mes/Picture/";
  499. string ftpServer = "ftp://192.168.1.92:21/mes/Picture/";
  500. string username = "vsftpd";
  501. string password = "vsftpd3cd79018fl";
  502. string currentDate = DateTime.Now.ToString("yyyyMMdd");
  503. string ftpFullPath = $"{ftpServer.TrimEnd('/')}/{currentDate}";
  504. /*string ftpFullPath = $"{ftpServer.TrimEnd('/')}/{Sn}";*/
  505. string outResult = CreateFtpDirectoryIfNotExists(ftpFullPath, username, password);
  506. if (outResult.Substring(0, 2) == "NG")
  507. {
  508. LogMessage(0, outResult);
  509. return false;
  510. }
  511. string remoteFileName = Path.GetFileName(localFilePath);
  512. string uri = $"{ftpFullPath}/{remoteFileName}";
  513. try
  514. {
  515. var request = (FtpWebRequest)WebRequest.Create(uri);
  516. request.Method = WebRequestMethods.Ftp.UploadFile;
  517. request.Credentials = new NetworkCredential(username, password);
  518. request.UsePassive = true;
  519. request.UseBinary = true;
  520. request.KeepAlive = false;
  521. using (var fileStream = File.OpenRead(localFilePath))
  522. using (var requestStream = request.GetRequestStream())
  523. {
  524. byte[] buffer = new byte[4096];
  525. int bytesRead;
  526. while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) > 0)
  527. {
  528. requestStream.Write(buffer, 0, bytesRead);
  529. }
  530. }
  531. using (var response = (FtpWebResponse)request.GetResponse())
  532. {
  533. LogMessage(1, $"文件: {localFilePath}上传成功,状态{response.StatusDescription}");
  534. return true;
  535. }
  536. }
  537. catch (WebException ex)
  538. {
  539. if (ex.Response is FtpWebResponse response)
  540. {
  541. LogMessage(0, $"NG,FTP 错误码: {(int)response.StatusCode} - {response.StatusDescription}");
  542. }
  543. else
  544. {
  545. LogMessage(0, $"NG,Web异常: {ex.Message}");
  546. }
  547. return false;
  548. }
  549. catch (Exception ex)
  550. {
  551. LogMessage(0, $"NG,上传失败: {ex.Message}");
  552. return false;
  553. }
  554. }
  555. private string CreateFtpDirectoryIfNotExists(string ftpDirectoryPath, string username, string password)
  556. {
  557. try
  558. {
  559. FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpDirectoryPath);
  560. request.Method = WebRequestMethods.Ftp.MakeDirectory;
  561. request.Credentials = new NetworkCredential(username, password);
  562. request.UsePassive = true;
  563. request.UseBinary = true;
  564. request.KeepAlive = false;
  565. using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
  566. {
  567. response.Close();
  568. return "OK,目录创建成功: " + response.StatusDescription;
  569. }
  570. }
  571. catch (WebException ex)
  572. {
  573. if (ex.Response is FtpWebResponse response && response.StatusCode == FtpStatusCode.ActionNotTakenFileUnavailable)
  574. {
  575. return "OK,目录已存在: " + response.StatusDescription;
  576. }
  577. else
  578. {
  579. return "NG,创建目录时发生错误: " + ex.Message;
  580. }
  581. }
  582. }
  583. public string CheckFileAccess(string filePath)
  584. {
  585. try
  586. {
  587. using (FileStream fileStream = File.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.None))
  588. {
  589. return "OK";
  590. }
  591. }
  592. catch (IOException ex)
  593. {
  594. return "文件被占用: " + ex.Message;
  595. }
  596. catch (UnauthorizedAccessException ex)
  597. {
  598. return "权限不足,无法访问文件: " + ex.Message;
  599. }
  600. catch (Exception ex)
  601. {
  602. return ex.Message;
  603. }
  604. }
  605. public class Log
  606. {
  607. public string SN { set; get; }
  608. public string Result { set; get; }
  609. public string TestTime { set; get; }
  610. public string Side { set; get; }
  611. public List<LogItem> LogItemList { set; get; } = new List<LogItem>();
  612. }
  613. public class LogItem
  614. {
  615. public string Location { set; get; }
  616. public string ReelNo { set; get; }
  617. public string FirstType { set; get; }
  618. public string SceondType { set; get; }
  619. public string Name { set; get; }
  620. }
  621. }
  622. }