Browse Source

老化日志读取

callm 1 year ago
parent
commit
0109b0a50a

+ 27 - 12
FileWatcher/AutoAnalysisDevice.Designer.cs

@@ -47,13 +47,15 @@
             this.Timer = new System.Windows.Forms.Timer(this.components);
             this.Device = new System.Windows.Forms.ComboBox();
             this.label3 = new System.Windows.Forms.Label();
+            this.ATEFile = new System.IO.FileSystemWatcher();
             ((System.ComponentModel.ISupportInitialize)(this.XmlWatcher)).BeginInit();
+            ((System.ComponentModel.ISupportInitialize)(this.ATEFile)).BeginInit();
             this.SuspendLayout();
             // 
             // XmlWatcher
             // 
             this.XmlWatcher.EnableRaisingEvents = true;
-            this.XmlWatcher.Filter = "*.xml";
+            this.XmlWatcher.Filter = "*.log";
             this.XmlWatcher.SynchronizingObject = this;
             // 
             // StartWatch
@@ -85,7 +87,7 @@
             // 
             this.label1.AutoSize = true;
             this.label1.Font = new System.Drawing.Font("微软雅黑", 10.8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
-            this.label1.Location = new System.Drawing.Point(48, 400);
+            this.label1.Location = new System.Drawing.Point(48, 283);
             this.label1.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
             this.label1.Name = "label1";
             this.label1.Size = new System.Drawing.Size(162, 38);
@@ -95,7 +97,7 @@
             // FolderPath
             // 
             this.FolderPath.Enabled = false;
-            this.FolderPath.Location = new System.Drawing.Point(218, 400);
+            this.FolderPath.Location = new System.Drawing.Point(218, 283);
             this.FolderPath.Margin = new System.Windows.Forms.Padding(4, 3, 4, 3);
             this.FolderPath.Name = "FolderPath";
             this.FolderPath.Size = new System.Drawing.Size(436, 35);
@@ -104,7 +106,7 @@
             // ChooseFolder
             // 
             this.ChooseFolder.Font = new System.Drawing.Font("微软雅黑", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
-            this.ChooseFolder.Location = new System.Drawing.Point(686, 394);
+            this.ChooseFolder.Location = new System.Drawing.Point(686, 277);
             this.ChooseFolder.Margin = new System.Windows.Forms.Padding(4, 3, 4, 3);
             this.ChooseFolder.Name = "ChooseFolder";
             this.ChooseFolder.Size = new System.Drawing.Size(156, 48);
@@ -138,12 +140,13 @@
             // 
             this.MakeCode_lable.AutoSize = true;
             this.MakeCode_lable.Font = new System.Drawing.Font("微软雅黑", 10.8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
-            this.MakeCode_lable.Location = new System.Drawing.Point(48, 311);
+            this.MakeCode_lable.Location = new System.Drawing.Point(54, 105);
             this.MakeCode_lable.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
             this.MakeCode_lable.Name = "MakeCode_lable";
             this.MakeCode_lable.Size = new System.Drawing.Size(104, 38);
             this.MakeCode_lable.TabIndex = 12;
             this.MakeCode_lable.Text = "工单号";
+            this.MakeCode_lable.Visible = false;
             // 
             // label4
             // 
@@ -166,38 +169,42 @@
             this.Master.Name = "Master";
             this.Master.Size = new System.Drawing.Size(436, 32);
             this.Master.TabIndex = 15;
+            this.Master.Visible = false;
             // 
             // AutoStart
             // 
             this.AutoStart.AutoSize = true;
-            this.AutoStart.Location = new System.Drawing.Point(686, 51);
+            this.AutoStart.Checked = true;
+            this.AutoStart.CheckState = System.Windows.Forms.CheckState.Checked;
+            this.AutoStart.Location = new System.Drawing.Point(686, 201);
             this.AutoStart.Margin = new System.Windows.Forms.Padding(6);
             this.AutoStart.Name = "AutoStart";
             this.AutoStart.Size = new System.Drawing.Size(186, 28);
             this.AutoStart.TabIndex = 16;
             this.AutoStart.Text = "开机自动启动";
             this.AutoStart.UseVisualStyleBackColor = true;
-            this.AutoStart.Visible = false;
             this.AutoStart.CheckedChanged += new System.EventHandler(this.AutoStart_CheckedChanged);
             // 
             // ma_code
             // 
-            this.ma_code.Location = new System.Drawing.Point(218, 311);
+            this.ma_code.Location = new System.Drawing.Point(224, 105);
             this.ma_code.Margin = new System.Windows.Forms.Padding(4, 3, 4, 3);
             this.ma_code.Name = "ma_code";
             this.ma_code.Size = new System.Drawing.Size(436, 35);
             this.ma_code.TabIndex = 17;
+            this.ma_code.Visible = false;
             // 
             // BatchAnalysus
             // 
             this.BatchAnalysus.Font = new System.Drawing.Font("微软雅黑", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
-            this.BatchAnalysus.Location = new System.Drawing.Point(686, 308);
+            this.BatchAnalysus.Location = new System.Drawing.Point(686, 95);
             this.BatchAnalysus.Margin = new System.Windows.Forms.Padding(4, 3, 4, 3);
             this.BatchAnalysus.Name = "BatchAnalysus";
             this.BatchAnalysus.Size = new System.Drawing.Size(156, 48);
             this.BatchAnalysus.TabIndex = 18;
             this.BatchAnalysus.Text = "批量解析";
             this.BatchAnalysus.UseVisualStyleBackColor = true;
+            this.BatchAnalysus.Visible = false;
             this.BatchAnalysus.Click += new System.EventHandler(this.BatchAnalysus_Click);
             // 
             // Timer
@@ -212,7 +219,7 @@
             this.Device.Items.AddRange(new object[] {
             "ATE设备",
             "老化设备"});
-            this.Device.Location = new System.Drawing.Point(218, 222);
+            this.Device.Location = new System.Drawing.Point(224, 201);
             this.Device.Margin = new System.Windows.Forms.Padding(6);
             this.Device.Name = "Device";
             this.Device.Size = new System.Drawing.Size(436, 32);
@@ -222,13 +229,19 @@
             // 
             this.label3.AutoSize = true;
             this.label3.Font = new System.Drawing.Font("微软雅黑", 10.8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
-            this.label3.Location = new System.Drawing.Point(48, 222);
+            this.label3.Location = new System.Drawing.Point(54, 201);
             this.label3.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
             this.label3.Name = "label3";
             this.label3.Size = new System.Drawing.Size(133, 38);
             this.label3.TabIndex = 19;
             this.label3.Text = "设备选择";
             // 
+            // ATEFile
+            // 
+            this.ATEFile.EnableRaisingEvents = true;
+            this.ATEFile.Filter = "*.ini";
+            this.ATEFile.SynchronizingObject = this;
+            // 
             // AutoAnalysisDevice
             // 
             this.AutoScaleDimensions = new System.Drawing.SizeF(12F, 24F);
@@ -252,10 +265,11 @@
             this.Margin = new System.Windows.Forms.Padding(4, 3, 4, 3);
             this.MaximizeBox = false;
             this.Name = "AutoAnalysisDevice";
-            this.Text = "UAS自动过站解析器 2022-07-20  15:05";
+            this.Text = "UAS设备数据解析 2023-06-29  10:05";
             this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form1_FormClosing);
             this.Load += new System.EventHandler(this.Form1_Load);
             ((System.ComponentModel.ISupportInitialize)(this.XmlWatcher)).EndInit();
+            ((System.ComponentModel.ISupportInitialize)(this.ATEFile)).EndInit();
             this.ResumeLayout(false);
             this.PerformLayout();
 
@@ -281,6 +295,7 @@
         private System.Windows.Forms.Timer Timer;
         private System.Windows.Forms.ComboBox Device;
         private System.Windows.Forms.Label label3;
+        private System.IO.FileSystemWatcher ATEFile;
     }
 }
 

+ 135 - 25
FileWatcher/AutoAnalysisDevice.cs

@@ -89,8 +89,8 @@ namespace FileWatcher
             FormBorderStyle = FormBorderStyle.FixedSingle;
             InitDB = new Thread(ConnectDB);
             //添加监控事件
-            XmlWatcher.Created += new FileSystemEventHandler(XmlWatcher_Created);
             XmlWatcher.Changed += new FileSystemEventHandler(XmlWatcher_Created);
+            ATEFile.Created += new FileSystemEventHandler(XmlWatcher_Created);
             SetLoadingWindow stw = new SetLoadingWindow(InitDB, "正在启动程序");
             stw.StartPosition = FormStartPosition.CenterScreen;
             stw.ShowDialog();
@@ -105,7 +105,8 @@ namespace FileWatcher
             //获取缓存信息
             try
             {
-                //FolderPath.Text = BaseUtil.GetCacheData("FolderPath").ToString();
+                FolderPath.Text = BaseUtil.GetCacheData("FolderPath").ToString();
+                Device.Text = BaseUtil.GetCacheData("Device").ToString();
                 //BackUpFolderPath.Text = BaseUtil.GetCacheData("BackUpFolderPath").ToString();
                 //Master.Text = BaseUtil.GetCacheData("Master").ToString();
                 //AutoStart.Checked = (bool)BaseUtil.GetCacheData("AutoStart");
@@ -123,7 +124,7 @@ namespace FileWatcher
 
         private void ConnectDB()
         {
-            DataHelper.DBConnectionString = "Connection Timeout=0;Pooling=false;Password=select!#%*(;User ID=N_MES;Pooling=false;Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.101.253)(PORT=11678)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=orcl)));";
+            DataHelper.DBConnectionString = "Connection Timeout=0;Pooling=false;Password=select!#%*(;User ID=N_MES;Pooling=false;Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.6.253)(PORT=1521)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=orcl)));";
             dh = new DataHelper();
         }
 
@@ -143,10 +144,15 @@ namespace FileWatcher
                 }
             }
             XmlWatcher.Path = FolderPath.Text;
-            XmlWatcher.Filter = "*.log|*.ini";
             XmlWatcher.EnableRaisingEvents = true;
+            XmlWatcher.NotifyFilter = NotifyFilters.FileName | NotifyFilters.LastWrite;
+            ATEFile.Path = FolderPath.Text;
+            ATEFile.EnableRaisingEvents = true;
+            ATEFile.NotifyFilter = NotifyFilters.FileName | NotifyFilters.LastWrite;
             //设置缓存数据
-            //BaseUtil.SetCacheData("FolderPath", FolderPath.Text);
+            BaseUtil.SetCacheData("FolderPath", FolderPath.Text);
+            BaseUtil.SetCacheData("Device", Device.Text);
+
             //BaseUtil.SetCacheData("BackUpFolderPath", BackUpFolderPath.Text);
             //BaseUtil.SetCacheData("Master", Master.Text);
             //BaseUtil.SetCacheData("AutoStart", AutoStart.Checked);
@@ -161,17 +167,12 @@ namespace FileWatcher
             OperateResult.AppendText("开始执行监控\n");
         }
 
-        public void CreateNode(XmlDocument xmlDoc, XmlNode parentNode, string name, string value)
-        {
-            XmlNode node = xmlDoc.CreateNode(XmlNodeType.Element, name, null);
-            node.InnerText = value;
-            parentNode.AppendChild(node);
-        }
-
         private void XmlWatcher_Created(object sender, FileSystemEventArgs e)
         {
-
+            OperateResult.AppendText("文件修改:" + e.FullPath + "\n");
+            TxtHandleProcess(e.FullPath);
         }
+
         string nextLine;
         private void TxtHandleProcess(string FileName)
         {
@@ -180,7 +181,8 @@ namespace FileWatcher
             List<string> badprod = new List<string>();
             if (Device.Text == "老化设备")
             {
-                StreamReader sR = File.OpenText(FileName);
+                StreamReader sR = new StreamReader(FileName, Encoding.Default);
+                Console.WriteLine(sR.CurrentEncoding);
                 DataTable filedt = new DataTable();
                 //文件内的行,用一个DataTable存储
                 int Rowindex = 0;
@@ -229,13 +231,27 @@ namespace FileWatcher
                     Rowindex = Rowindex + 1;
                 }
                 sR.Close();
+                OperateResult.AppendText("文件修改" + filedt.Rows.Count + "\n");
                 for (int i = 0; i < filedt.Rows.Count; i++)
                 {
-                    string filename = filedt.Rows[i]["文件路径"].ToString();
-                    string ftppath = "/" + DateTime.Now.ToString("yyyy-MM-dd") + "/";
-                    string folderpath = filename.Substring(0, filename.LastIndexOf(@"\"));
-                    ftp.UpLoadFile(folderpath, filename, ftppath, "");
-                    dh.ExecuteSql("insert into Devicetestinfo(dti_id,dti_prodcode,dti_area,dti_uptime,dti_downtime,dti_upqty,dti_okqty,dti_ngqty,dti_ipaddress,dti_filepath,dti_indate)values(Devicetestinfo_seq.nextval,'" + filedt.Rows[i]["产品代码"].ToString() + "','" + filedt.Rows[i]["区域"].ToString() + "',to_date('" + filedt.Rows[i]["上架时间"].ToString() + "','yyyy/mm/dd hh24:mi:ss'),to_date('" + filedt.Rows[i]["下架时间"].ToString() + "','yyyy/mm/dd hh24:mi:ss'),'" + filedt.Rows[i]["上架数量"].ToString() + "','" + filedt.Rows[i]["合格数量"].ToString() + "','" + filedt.Rows[i]["不良数量"].ToString() + "','" + IPAddress + "','http://192.168.6.253:8099/ftp" + ftppath + filename + "',sysdate)", "insert");
+                    if (!dh.CheckExist("Devicetestinfo", "(dti_uptime)=to_date('" + filedt.Rows[i]["上架时间"].ToString() + "','yyyy/mm/dd hh24:mi:ss') and dti_ipaddress='" + IPAddress + "'"))
+                    {
+                        string filename = filedt.Rows[i]["文件路径"].ToString();
+                        string ftppath = "/AGING/" + DateTime.Now.ToString("yyyy") + "/" + DateTime.Now.ToString("yyyy-MM") + "/" + DateTime.Now.ToString("MM-dd") + "/";
+                        string folderpath = filename.Substring(0, filename.LastIndexOf(@"\"));
+                        //ftp.UpLoadFile(folderpath, filename.Substring(filename.LastIndexOf(@"\")), ftppath, "");
+                        Dictionary<string, object> dic = new Dictionary<string, object>();
+                        dic.Add("em_name", "管理员");
+                        dic.Add("em_code", "ADMIN");
+                        dic.Add("caller", "AGING");
+                        string fp_id;
+                        string fp_path;
+                        UploadFilesToRemoteUrl("http://192.168.199.173:8080/mes/MEScommon/uploadFiles.action?_noc=1", filename, dic,out fp_path, out fp_id);
+                        fp_path = "http://192.168.199.173:8080/" + fp_path.Replace("/app/uas/webapps/", "");
+                        fp_path= fp_path.Replace("\"","");
+                        dh.ExecuteSql("insert into Devicetestinfo(dti_id,dti_prodcode,dti_area,dti_uptime,dti_downtime,dti_upqty,dti_okqty,dti_ngqty,dti_ipaddress,dti_filepath,dti_indate,dti_fpid)values(Devicetestinfo_seq.nextval,'" + filedt.Rows[i]["产品代码"].ToString() + "','" + filedt.Rows[i]["区域"].ToString() + "',to_date('" + filedt.Rows[i]["上架时间"].ToString() + "','yyyy/mm/dd hh24:mi:ss'),to_date('" + filedt.Rows[i]["下架时间"].ToString() + "','yyyy/mm/dd hh24:mi:ss'),'" + filedt.Rows[i]["上架数量"].ToString() + "','" + filedt.Rows[i]["合格数量"].ToString() + "','" + filedt.Rows[i]["不良数量"].ToString() + "','" + IPAddress + "','"+ fp_path + "',sysdate,'"+ fp_id + "')", "insert");
+                        OperateResult.AppendText("上传文件" + filename + "\n");
+                    }
                 }
             }
             else if (Device.Text == "ATE设备")
@@ -352,23 +368,117 @@ namespace FileWatcher
         }
 
         private void BatchAnalysus_Click(object sender, EventArgs e)
+        {
+            TxtHandleProcess(@"C:\Users\callm\Desktop\导入数据\客户资料\睿德电子\新建文本文档.log");
+            //TxtHandleProcess(@"C:\Users\callm\Desktop\导入数据\客户资料\睿德电子\PowerTestTj.ini");
+        }
+
+        private void Timer_Tick(object sender, EventArgs e)
         {
             //var AllXmls = Directory.GetFiles(FolderPath.Text, "*.txt");
             //foreach (var Xml in AllXmls)
             //{
             //    TxtHandleProcess(Xml);
             //}
-            //TxtHandleProcess(@"C:\Users\callm\Desktop\客户资料\睿德电子\新建文本文档.log");
-            TxtHandleProcess(@"C:\Users\callm\Desktop\客户资料\睿德电子\PowerTestTj.ini");
         }
 
-        private void Timer_Tick(object sender, EventArgs e)
+        /// <summary>
+        /// 请求上传图片到阿里云
+        /// </summary>
+        /// <param name="url">上传地址</param>
+        /// <param name="filepath">本地文件路径</param>
+        /// <param name="dic">上传的数据信息</param>
+        /// <returns></returns>
+        public string UploadFilesToRemoteUrl(string url1, string filepath, Dictionary<string, object> dic,out string fp_path,out string fp_id)
         {
-            var AllXmls = Directory.GetFiles(FolderPath.Text, "*.txt");
-            foreach (var Xml in AllXmls)
+            fp_id = "";
+            fp_path = "";
+            try
+            {
+                ServicePointManager.DefaultConnectionLimit = 50;
+
+                string boundary = DateTime.Now.Ticks.ToString("x");
+
+                byte[] boundarybytes = System.Text.Encoding.UTF8.GetBytes("--" + boundary + "\r\n");
+
+                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url1);
+                request.Method = "POST";
+                request.Timeout = 10 * 10000;
+                request.ContentType = "multipart/form-data; boundary=" + boundary;
+
+                Stream rs = request.GetRequestStream();
+
+                var endBoundaryBytes = Encoding.UTF8.GetBytes("--" + boundary + "--\r\n");
+
+                string formdataTemplate = "Content-Disposition: form-data; name=\"{0}\"\r\n" + "\r\n" + "{1}" + "\r\n";
+                if (dic != null)
+                {
+                    foreach (string key in dic.Keys)
+                    {
+                        rs.Write(boundarybytes, 0, boundarybytes.Length);
+
+                        string formitem = string.Format(formdataTemplate, key, dic[key]);
+
+                        byte[] formitembytes = System.Text.Encoding.UTF8.GetBytes(formitem);
+
+                        rs.Write(formitembytes, 0, formitembytes.Length);
+                    }
+
+                }
+
+                string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\n\r\n";
+                {
+                    rs.Write(boundarybytes, 0, boundarybytes.Length);
+
+                    var header = string.Format(headerTemplate, "file", Path.GetFileName(filepath));
+
+                    var headerbytes = System.Text.Encoding.UTF8.GetBytes(header);
+
+                    rs.Write(headerbytes, 0, headerbytes.Length);
+
+                    using (var fileStream = new FileStream(filepath, FileMode.Open, FileAccess.Read))
+                    {
+                        var buffer = new byte[1024];
+
+                        var bytesRead = 0;
+
+                        while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
+                        {
+                            rs.Write(buffer, 0, bytesRead);
+                        }
+                    }
+                    var cr = Encoding.UTF8.GetBytes("\r\n");
+
+                    rs.Write(cr, 0, cr.Length);
+                }
+
+                rs.Write(endBoundaryBytes, 0, endBoundaryBytes.Length);
+
+                var response = request.GetResponse() as HttpWebResponse;
+
+                StreamReader newReader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
+                string Content = newReader.ReadToEnd();
+                Dictionary<string, object> dic1 = new Dictionary<string, object>();
+                List<Dictionary<string, object>> dic2 = null;
+                dic1 = BaseUtil.ToDictionary(Content);
+                dic2 = dic1["data"] as List<Dictionary<string, object>>;
+            
+                if (dic2[0]["filepath"] != null)
+                {
+                    fp_id = dic2[0]["filepath"].ToString();
+                    fp_path= dic2[0]["path"].ToString();
+                }
+                if (response.StatusCode == HttpStatusCode.OK)
+                {
+                    Console.WriteLine(fp_id);
+                    return fp_id;
+                }
+            }
+            catch (Exception e)
             {
-                TxtHandleProcess(Xml);
+                Console.WriteLine(e.Message + e.StackTrace);
             }
+            return "";
         }
     }
 }

+ 3 - 0
FileWatcher/AutoAnalysisDevice.resx

@@ -126,4 +126,7 @@
   <metadata name="Timer.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
     <value>296, 17</value>
   </metadata>
+  <metadata name="ATEFile.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
+    <value>435, 17</value>
+  </metadata>
 </root>