|
|
@@ -0,0 +1,638 @@
|
|
|
+using DevExpress.Utils.CodedUISupport;
|
|
|
+using HslCommunication;
|
|
|
+using NPOI.SS.Formula.Functions;
|
|
|
+using System;
|
|
|
+using System.Collections.Generic;
|
|
|
+using System.ComponentModel;
|
|
|
+using System.Data;
|
|
|
+using System.Drawing;
|
|
|
+using System.IO;
|
|
|
+using System.Linq;
|
|
|
+using System.Net;
|
|
|
+using System.Text;
|
|
|
+using System.Web.Services.Description;
|
|
|
+using System.Windows.Forms;
|
|
|
+using UAS_MES_NEW.CustomControl.ButtonUtil;
|
|
|
+using UAS_MES_NEW.DataOperate;
|
|
|
+using UAS_MES_NEW.Entity;
|
|
|
+using UAS_MES_NEW.PublicMethod;
|
|
|
+
|
|
|
+namespace UAS_MES_NEW.Make
|
|
|
+{
|
|
|
+ public partial class Make_ParseLog : Form
|
|
|
+ {
|
|
|
+ public Make_ParseLog()
|
|
|
+ {
|
|
|
+ InitializeComponent();
|
|
|
+ }
|
|
|
+
|
|
|
+ FileSystemWatcher watcher;
|
|
|
+
|
|
|
+ LogStringBuilder sql = new LogStringBuilder();
|
|
|
+ DataTable Dbfind;
|
|
|
+ DataTable dt;
|
|
|
+
|
|
|
+ DataHelper dh;
|
|
|
+ string currFileType = "", equiType = "";
|
|
|
+
|
|
|
+ string SN, omakeCode, oMsid, oErrorMessage = "";
|
|
|
+ List<string> fileList = new List<string>();
|
|
|
+
|
|
|
+ Timer ChangeWoTimer;
|
|
|
+
|
|
|
+ private void Make_ParseLog_Load(object sender, EventArgs e)
|
|
|
+ {
|
|
|
+ dh = SystemInf.dh;
|
|
|
+ Choose.Enabled = false;
|
|
|
+ fileList.Add("C:\\Users\\MI\\Desktop");
|
|
|
+
|
|
|
+ ChangeWoTimer = new Timer();
|
|
|
+ ChangeWoTimer.Interval = 30000;
|
|
|
+ ChangeWoTimer.Tick += ChangeWoFunc;
|
|
|
+
|
|
|
+ watcher = new FileSystemWatcher();
|
|
|
+ watcher.NotifyFilter = NotifyFilters.FileName | NotifyFilters.LastWrite;
|
|
|
+ watcher.Created += OnFileCreated;
|
|
|
+ watcher.Changed += OnFileChanged;
|
|
|
+ /*watcher.Deleted += OnFileChanged;
|
|
|
+ watcher.Renamed += OnFileChanged;*/
|
|
|
+
|
|
|
+ //设置锁定工单
|
|
|
+ LockMakeCode.GetMakeCodeCtl(ma_code);
|
|
|
+ ma_code.SetLockCheckBox(LockMakeCode);
|
|
|
+ //工单号放大镜配置
|
|
|
+ ma_code.TableName = "make left join product on ma_prodcode=pr_code";
|
|
|
+ ma_code.SelectField = "ma_code # 工单号,pr_code # 产品编号,pr_detail # 产品名称";
|
|
|
+ ma_code.FormName = Name;
|
|
|
+ ma_code.SetValueField = new string[] { "ma_code", "pr_code", "pr_detail" };
|
|
|
+ ma_code.Condition = "ma_statuscode='STARTED'";
|
|
|
+ ma_code.DbChange += Ma_code_DbChange;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void Ma_code_DbChange(object sender, EventArgs e)
|
|
|
+ {
|
|
|
+ Dbfind = ma_code.ReturnData;
|
|
|
+ BaseUtil.SetFormValue(this.Controls, Dbfind);
|
|
|
+ //获取工单的其他信息
|
|
|
+ sql.Clear();
|
|
|
+ sql.Append("select ma_code,nvl(mcd_okqty,0) mcd_okqty,ma_prodcode as pr_code ,pr_detail,");
|
|
|
+ sql.Append("pr_spec,ma_qty - nvl(mcd_inqty, 0) mcd_remainqty from make left join makecraftdetail on ");
|
|
|
+ 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 + "'");
|
|
|
+ dt = (DataTable)dh.ExecuteSql(sql.GetString(), "select");
|
|
|
+ if (dt.Rows.Count > 0)
|
|
|
+ {
|
|
|
+ BaseUtil.SetFormValue(this.Controls, dt);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void Device_SelectedIndexChanged(object sender, EventArgs e)
|
|
|
+ {
|
|
|
+ if (Device.SelectedIndex == -1) return;
|
|
|
+
|
|
|
+ switch (Device.SelectedIndex)
|
|
|
+ {
|
|
|
+ case 0:
|
|
|
+ currFileType = "Txt";
|
|
|
+ equiType = "AOI";
|
|
|
+ ChangeWoTimer.Start();
|
|
|
+ break;
|
|
|
+ case 1:
|
|
|
+ currFileType = "jpg";
|
|
|
+ equiType = "CCD";
|
|
|
+ ChangeWoTimer.Stop();
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ if (string.IsNullOrEmpty(ma_code.Text))
|
|
|
+ {
|
|
|
+ Device.SelectedIndex = -1;
|
|
|
+ txtPath.Text = "";
|
|
|
+ MessageBox.Show(this.ParentForm, "请选择工单", "提示");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ txtPath.Text = fileList[Device.SelectedIndex];
|
|
|
+ txtPath.Focus();
|
|
|
+ txtPath.SelectAll();
|
|
|
+ ma_code.Enabled = true;
|
|
|
+ Choose.Enabled = true;
|
|
|
+ txtPath.Enabled = false;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void Choose_Click(object sender, EventArgs e)
|
|
|
+ {
|
|
|
+ using (var dialog = new FolderBrowserDialog())
|
|
|
+ {
|
|
|
+ if (dialog.ShowDialog() == DialogResult.OK)
|
|
|
+ {
|
|
|
+ txtPath.Text = dialog.SelectedPath;
|
|
|
+ txtPath.Enabled = false;
|
|
|
+ Device.Enabled = false;
|
|
|
+ watcher.EnableRaisingEvents = false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void claerBtn_Click(object sender, EventArgs e)
|
|
|
+ {
|
|
|
+ lstFiles.Items.Clear();
|
|
|
+ }
|
|
|
+
|
|
|
+ private void allParse_Click(object sender, EventArgs e)
|
|
|
+ {
|
|
|
+ if (Device.SelectedIndex == 0)
|
|
|
+ {
|
|
|
+ if (string.IsNullOrEmpty(ma_code.Text))
|
|
|
+ {
|
|
|
+ MessageBox.Show(this.ParentForm, "请选择工单", "提示");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (Device.SelectedIndex == -1)
|
|
|
+ {
|
|
|
+ Device.Focus();
|
|
|
+ Device.SelectAll();
|
|
|
+ MessageBox.Show(this.ParentForm, "请选择设备", "提示");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if (String.IsNullOrEmpty(txtPath.Text))
|
|
|
+ {
|
|
|
+ txtPath.Focus();
|
|
|
+ txtPath.SelectAll();
|
|
|
+ MessageBox.Show(this.ParentForm, "请选择解析路径", "提示");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ string[] txtFiles = Directory.GetFiles(txtPath.Text, $"*.{currFileType}");
|
|
|
+ if (txtFiles.Length == 0)
|
|
|
+ {
|
|
|
+ LogMessage(0, $"当前{txtPath.Text},没有{equiType}设备输出的{currFileType.ToLower()}文件");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ RefreshFileList();
|
|
|
+ }
|
|
|
+
|
|
|
+ private void onWatch_Click(object sender, EventArgs e)
|
|
|
+ {
|
|
|
+ if (Device.SelectedIndex == 0)
|
|
|
+ {
|
|
|
+ if (string.IsNullOrEmpty(ma_code.Text))
|
|
|
+ {
|
|
|
+ MessageBox.Show(this.ParentForm, "请选择工单", "提示");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (Device.SelectedIndex == -1)
|
|
|
+ {
|
|
|
+ Device.Focus();
|
|
|
+ Device.SelectAll();
|
|
|
+ MessageBox.Show(this.ParentForm, "请选择设备", "提示");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if (String.IsNullOrEmpty(txtPath.Text))
|
|
|
+ {
|
|
|
+ txtPath.Focus();
|
|
|
+ txtPath.SelectAll();
|
|
|
+ MessageBox.Show(this.ParentForm, "请选择解析路径", "提示");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ try
|
|
|
+ {
|
|
|
+ watcher.Path = txtPath.Text;
|
|
|
+ watcher.Filter = $"*.{currFileType}";
|
|
|
+ if (onWatch.Text == "开启解析")
|
|
|
+ {
|
|
|
+ watcher.EnableRaisingEvents = true;
|
|
|
+ onWatch.Text = "关闭解析";
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ watcher.EnableRaisingEvents = false;
|
|
|
+ onWatch.Text = "开启解析";
|
|
|
+ }
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ MessageBox.Show(this.ParentForm, ex.Message, "警告");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void OnFileCreated(object sender, FileSystemEventArgs e)
|
|
|
+ {
|
|
|
+ RefreshFileList();
|
|
|
+ }
|
|
|
+
|
|
|
+ private void OnFileChanged(object sender, FileSystemEventArgs e)
|
|
|
+ {
|
|
|
+ if (e.ChangeType == WatcherChangeTypes.Changed)
|
|
|
+ {
|
|
|
+ RefreshFileList();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void RefreshFileList()
|
|
|
+ {
|
|
|
+ if (lstFiles.InvokeRequired)
|
|
|
+ {
|
|
|
+ lstFiles.Invoke(new Action(RefreshFileList));
|
|
|
+ lstFiles.BeginInvoke(new Action(RefreshFileList));
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ try
|
|
|
+ {
|
|
|
+ if (!Directory.Exists(txtPath.Text))
|
|
|
+ {
|
|
|
+ LogMessage(0, $"目录不存在: {txtPath.Text}");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ string[] txtFiles = Directory.GetFiles(txtPath.Text, $"*.{currFileType}");
|
|
|
+ if (txtFiles.Length == 0)
|
|
|
+ {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ foreach (string file in txtFiles)
|
|
|
+ {
|
|
|
+ if (!File.Exists(file))
|
|
|
+ {
|
|
|
+ LogMessage(0, $"文件不存在: {file}");
|
|
|
+ }
|
|
|
+
|
|
|
+ string outFileMsg = CheckFileAccess(file);
|
|
|
+ if (outFileMsg != "OK")
|
|
|
+ {
|
|
|
+ LogMessage(0, outFileMsg);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (File.ReadAllText(file).Length == 0)
|
|
|
+ {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (equiType == "AOI")
|
|
|
+ {
|
|
|
+ StreamReader SR = File.OpenText(file);
|
|
|
+ string restOfStream = SR.ReadToEnd();
|
|
|
+ SR.Close();
|
|
|
+ SR.Dispose();
|
|
|
+
|
|
|
+ List<Log> logArr = new List<Log>() { };
|
|
|
+ string[] lines = restOfStream.Split(new string[] { "\r\n" }, StringSplitOptions.None);
|
|
|
+ int fileNum = string.IsNullOrEmpty(lines[lines.Length - 1]) ? lines.Length - 1 : lines.Length;
|
|
|
+
|
|
|
+ foreach (var item in lines)
|
|
|
+ {
|
|
|
+ if (string.IsNullOrEmpty(item)) break;
|
|
|
+
|
|
|
+ Log itemLog = new Log() { };
|
|
|
+
|
|
|
+ if (Array.IndexOf(lines, item) == 1) itemLog.SN = item.Split(':')[1].ToString().Trim();
|
|
|
+
|
|
|
+ if (Array.IndexOf(lines, item) == 2) itemLog.TestTime = item.Split(':')[1].Split('.')[0].ToString().Trim();
|
|
|
+
|
|
|
+ if (Array.IndexOf(lines, item) == 3) itemLog.Result = item.Split(':')[1].ToString().Trim();
|
|
|
+
|
|
|
+ if (Array.IndexOf(lines, item) == 4) itemLog.Side = item.Split(':')[1].ToString().Trim();
|
|
|
+
|
|
|
+ logArr.Add(itemLog);
|
|
|
+ }
|
|
|
+ if (logArr.Count == 0)
|
|
|
+ {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (InsertDb(logArr, file, fileNum))
|
|
|
+ {
|
|
|
+ if (ConsoleLog(restOfStream, file))
|
|
|
+ {
|
|
|
+ File.WriteAllText(file, string.Empty);
|
|
|
+ File.Delete(file);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else if(equiType == "CCD")
|
|
|
+ {
|
|
|
+ SN = Path.GetFileName(file).Split('_')[0].ToString();
|
|
|
+ if (UploadImageToFtp(file, SN))
|
|
|
+ {
|
|
|
+ dh.ExecuteSql($@"INSERT INTO steptestdetail( sd_id, sd_makecode, sd_sn, SD_MACHINECODE, SD_INDATE,SD_ACTVALUE)
|
|
|
+ VALUES( steptestdetail_seq.NEXTVAL, '{ma_code.Text}', '{SN}', 'CCD', sysdate,
|
|
|
+ 'http://192.168.1.92:8088/ftp/mes/Picture/{SN}/{Path.GetFileName(file)}')", "insert");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ LogMessage(0, $"Error: 解析文件列表失败: {ex.Message}");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private bool InsertDb(List<Log> logs, string PathName, int fileNum)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ StringBuilder sql = new StringBuilder();
|
|
|
+ List<string> param = new List<string>() { };
|
|
|
+ foreach (var item in logs)
|
|
|
+ {
|
|
|
+ string sqlStr = $@"select ma_code,nvl(mcd_okqty,0) mcd_okqty,ma_prodcode,pr_detail,ma_qty - nvl(mcd_okqty, 0) remain_qty
|
|
|
+ from make left join makecraftdetail on mcd_maid=ma_id left join product on pr_code = ma_prodcode
|
|
|
+ where ma_code='{ma_code.Text}' and mcd_stepcode='" + User.CurrentStepCode + "'";
|
|
|
+ dt = (DataTable)dh.ExecuteSql(sqlStr, "select");
|
|
|
+ BaseUtil.SetFormValue(Controls, dt);
|
|
|
+ //记录操作日志
|
|
|
+ LogicHandler.DoCommandLog(Tag.ToString(), User.UserCode, ma_code.Text, User.UserLineCode, User.UserSourceCode, "日志解析", "日志解析过站成功", item.SN, "");
|
|
|
+
|
|
|
+ string outMsg = "";
|
|
|
+ param.Add(item.SN);
|
|
|
+ param.Add(ma_code.Text);
|
|
|
+ param.Add(User.UserSourceCode);
|
|
|
+ param.Add(item.Result);
|
|
|
+ param.Add(item.TestTime);
|
|
|
+ param.Add(item.Side);
|
|
|
+ param.Add(outMsg);
|
|
|
+
|
|
|
+ string[] paramList = param.ToArray();
|
|
|
+ dh.CallProcedure("CS_INSERT_TESTDETAIL", ref paramList);
|
|
|
+ LogMessage(1, $"文件: {PathName},共{fileNum}条记录SN: {item.SN}解析已过站");
|
|
|
+ param.Clear();
|
|
|
+
|
|
|
+ if (logs.IndexOf(item) == logs.Count - 1)
|
|
|
+ {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ /* if (LogicHandler.CheckStepSNAndMacode(ma_code.Text, User.UserSourceCode, item.SN, User.UserCode, false, out omakeCode, out oMsid, out oErrorMessage))
|
|
|
+ {
|
|
|
+ if (LogicHandler.SetStepResult(omakeCode, User.UserSourceCode, item.SN, "设备日志解析", "OK", User.UserCode, false, out oErrorMessage))
|
|
|
+ {
|
|
|
+ string sqlStr = $@"select ma_code,nvl(mcd_okqty,0) mcd_okqty,ma_prodcode,pr_detail,ma_qty - nvl(mcd_okqty, 0) remain_qty
|
|
|
+ from make left join makecraftdetail on mcd_maid=ma_id left join product on pr_code = ma_prodcode
|
|
|
+ where ma_code='" + omakeCode + "' and mcd_stepcode='" + User.CurrentStepCode + "'";
|
|
|
+ dt = (DataTable)dh.ExecuteSql(sqlStr, "select");
|
|
|
+ BaseUtil.SetFormValue(Controls, dt);
|
|
|
+ //记录操作日志
|
|
|
+ LogicHandler.DoCommandLog(Tag.ToString(), User.UserCode, ma_code.Text, User.UserLineCode, User.UserSourceCode, "日志解析", "日志解析成功", item.SN, "");
|
|
|
+
|
|
|
+ string outMsg = "";
|
|
|
+ param.Add(item.SN);
|
|
|
+ param.Add(omakeCode);
|
|
|
+ param.Add(User.UserSourceCode);
|
|
|
+ param.Add(item.Result);
|
|
|
+ param.Add(outMsg);
|
|
|
+
|
|
|
+ string[] paramList = param.ToArray();
|
|
|
+ dh.CallProcedure("cs_insert_testdetail", ref paramList);
|
|
|
+ LogMessage($"文件: {PathName},共{fileNum}条记录SN: {item.SN}解析已过站");
|
|
|
+ param.Clear();
|
|
|
+
|
|
|
+ if (logs.IndexOf(item) == logs.Count - 1)
|
|
|
+ {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ LogMessage($"处理过站NG:{oErrorMessage}");
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ LogMessage($"过站核对NG:{oErrorMessage}");
|
|
|
+ break;
|
|
|
+ }*/
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ LogMessage(0, $"Error,处理解析写入: {ex.Message}");
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void ChangeWoFunc(object sender, EventArgs e)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ sql.Clear();
|
|
|
+ sql.Append($"SELECT dl_macode FROM deviceline WHERE dl_linecode = '{User.UserLineCode}'");
|
|
|
+ dt = (DataTable)dh.ExecuteSql(sql.GetString(), "select");
|
|
|
+ if (dt.Rows.Count > 0 && !String.IsNullOrEmpty(dt.Rows[0]["dl_macode"].ToString()))
|
|
|
+ {
|
|
|
+ ma_code.Text = dt.Rows[0]["dl_macode"].ToString().Trim();
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ LogMessage(0, $"NG,自动识别更新工单");
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ LogMessage(0, $"Error,自动切换工单: {ex.Message}");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private bool ConsoleLog(string Content, string PathName)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ string sourceDir = Path.GetDirectoryName(PathName);
|
|
|
+ string newFolderName = "Logs";
|
|
|
+ string newFolderPath = Path.Combine(sourceDir, newFolderName);
|
|
|
+
|
|
|
+ if (!Directory.Exists(newFolderPath))
|
|
|
+ {
|
|
|
+ Directory.CreateDirectory(newFolderPath);
|
|
|
+ }
|
|
|
+ string newFileName = "Log_" + Path.GetFileName(PathName);
|
|
|
+ string newFilePath = Path.Combine(newFolderPath, newFileName);
|
|
|
+
|
|
|
+ File.AppendAllText(newFilePath, Content + Environment.NewLine);
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ MessageBox.Show(this.ParentForm, ex.Message, "警告");
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void LogMessage(int type, string message)
|
|
|
+ {
|
|
|
+
|
|
|
+ if (type == 0)
|
|
|
+ {
|
|
|
+ if (lstFiles.InvokeRequired)
|
|
|
+ {
|
|
|
+ lstFiles.Invoke(new Action<int, string>(LogMessage), new object[] { type, message });
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ lstFiles.Items.Add($"[{DateTime.Now:yyyy-MM-dd HH:mm:ss}] {message}");
|
|
|
+ lstFiles.TopIndex = lstFiles.Items.Count - 1;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ if (lstOk.InvokeRequired)
|
|
|
+ {
|
|
|
+ lstOk.Invoke(new Action<int, string>(LogMessage), new object[] { type, message });
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ lstOk.Items.Add($"[{DateTime.Now:yyyy-MM-dd HH:mm:ss}] {message}");
|
|
|
+ lstOk.TopIndex = lstOk.Items.Count - 1;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ private void lstFiles_DrawItem(object sender, DrawItemEventArgs e)
|
|
|
+ {
|
|
|
+ if (e.Index < 0) return;
|
|
|
+
|
|
|
+ e.DrawBackground();
|
|
|
+ string txt = lstFiles.Items[e.Index].ToString().ToUpper();
|
|
|
+ Brush color = Brushes.Black;
|
|
|
+ if (txt.Contains("NG"))
|
|
|
+ {
|
|
|
+ color = Brushes.Red;
|
|
|
+ }
|
|
|
+ else if (txt.Contains("ERROR"))
|
|
|
+ {
|
|
|
+ color = Brushes.Red;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ color = Brushes.Green;
|
|
|
+ }
|
|
|
+ e.DrawFocusRectangle();
|
|
|
+ e.Graphics.DrawString(lstFiles.Items[e.Index].ToString(), e.Font, color, e.Bounds, StringFormat.GenericDefault);
|
|
|
+ }
|
|
|
+
|
|
|
+ public bool UploadImageToFtp(string localFilePath, string Sn)
|
|
|
+ {
|
|
|
+ //string ftpServer = "ftp://10.8.0.208:21/mes/Picture/";
|
|
|
+ string ftpServer = "ftp://192.168.1.92:21/mes/Picture/";
|
|
|
+ string username = "vsftpd";
|
|
|
+ string password = "vsftpd3cd79018fl";
|
|
|
+
|
|
|
+ string currentDate = DateTime.Now.ToString("yyyyMMdd");
|
|
|
+ string ftpFullPath = $"{ftpServer.TrimEnd('/')}/{currentDate}";
|
|
|
+ /*string ftpFullPath = $"{ftpServer.TrimEnd('/')}/{Sn}";*/
|
|
|
+ string outResult = CreateFtpDirectoryIfNotExists(ftpFullPath, username, password);
|
|
|
+ if (outResult.Substring(0, 2) == "NG")
|
|
|
+ {
|
|
|
+ LogMessage(0, outResult);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ string remoteFileName = Path.GetFileName(localFilePath);
|
|
|
+ string uri = $"{ftpFullPath}/{remoteFileName}";
|
|
|
+
|
|
|
+ try
|
|
|
+ {
|
|
|
+ var request = (FtpWebRequest)WebRequest.Create(uri);
|
|
|
+ request.Method = WebRequestMethods.Ftp.UploadFile;
|
|
|
+ request.Credentials = new NetworkCredential(username, password);
|
|
|
+ request.UsePassive = true;
|
|
|
+ request.UseBinary = true;
|
|
|
+ request.KeepAlive = false;
|
|
|
+
|
|
|
+ using (var fileStream = File.OpenRead(localFilePath))
|
|
|
+ using (var requestStream = request.GetRequestStream())
|
|
|
+ {
|
|
|
+ byte[] buffer = new byte[4096];
|
|
|
+ int bytesRead;
|
|
|
+ while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) > 0)
|
|
|
+ {
|
|
|
+ requestStream.Write(buffer, 0, bytesRead);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ using (var response = (FtpWebResponse)request.GetResponse())
|
|
|
+ {
|
|
|
+ LogMessage(1, $"文件: {localFilePath}上传成功,状态{response.StatusDescription}");
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ catch (WebException ex)
|
|
|
+ {
|
|
|
+ if (ex.Response is FtpWebResponse response)
|
|
|
+ {
|
|
|
+ LogMessage(0, $"NG,FTP 错误码: {(int)response.StatusCode} - {response.StatusDescription}");
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ LogMessage(0, $"NG,Web异常: {ex.Message}");
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ LogMessage(0, $"NG,上传失败: {ex.Message}");
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private string CreateFtpDirectoryIfNotExists(string ftpDirectoryPath, string username, string password)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpDirectoryPath);
|
|
|
+ request.Method = WebRequestMethods.Ftp.MakeDirectory;
|
|
|
+ request.Credentials = new NetworkCredential(username, password);
|
|
|
+ request.UsePassive = true;
|
|
|
+ request.UseBinary = true;
|
|
|
+ request.KeepAlive = false;
|
|
|
+
|
|
|
+ using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
|
|
|
+ {
|
|
|
+ response.Close();
|
|
|
+ return "OK,目录创建成功: " + response.StatusDescription;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ catch (WebException ex)
|
|
|
+ {
|
|
|
+ if (ex.Response is FtpWebResponse response && response.StatusCode == FtpStatusCode.ActionNotTakenFileUnavailable)
|
|
|
+ {
|
|
|
+ return "OK,目录已存在: " + response.StatusDescription;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ return "NG,创建目录时发生错误: " + ex.Message;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ public string CheckFileAccess(string filePath)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ using (FileStream fileStream = File.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.None))
|
|
|
+ {
|
|
|
+ return "OK";
|
|
|
+ }
|
|
|
+ }
|
|
|
+ catch (IOException ex)
|
|
|
+ {
|
|
|
+ return "文件被占用: " + ex.Message;
|
|
|
+ }
|
|
|
+ catch (UnauthorizedAccessException ex)
|
|
|
+ {
|
|
|
+ return "权限不足,无法访问文件: " + ex.Message;
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ return ex.Message;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private class Log
|
|
|
+ {
|
|
|
+ public string SN { set; get; }
|
|
|
+
|
|
|
+ public string Result { set; get; }
|
|
|
+
|
|
|
+ public string TestTime { set; get; }
|
|
|
+
|
|
|
+ public string Side { set; get; }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|