Browse Source

数据对接xray和ccd

yhluo 1 month ago
parent
commit
0411355c1f
1 changed files with 140 additions and 24 deletions
  1. 140 24
      UAS_MES_HYSX/FunctionCode/Make/Make_ParseLog.cs

+ 140 - 24
UAS_MES_HYSX/FunctionCode/Make/Make_ParseLog.cs

@@ -11,6 +11,8 @@ using System.Drawing;
 using System.IO;
 using System.Linq;
 using System.Net;
+using System.Security.AccessControl;
+using System.Security.Principal;
 using System.Text;
 using System.Web.Services.Description;
 using System.Web.UI.WebControls;
@@ -43,11 +45,15 @@ namespace UAS_MES_NEW.Make
 
         Timer ChangeWoTimer;
 
+        string outFileMsg;
+
         private void Make_ParseLog_Load(object sender, EventArgs e)
         {
             dh = SystemInf.dh;
             Choose.Enabled = false;
             fileList.Add("C:\\Users\\MI\\Desktop");
+            fileList.Add("C:\\Users\\MI\\Desktop");
+            fileList.Add("C:\\Users\\MI\\Desktop");
 
             ChangeWoTimer = new Timer();
             ChangeWoTimer.Interval = 30000;
@@ -169,7 +175,15 @@ namespace UAS_MES_NEW.Make
                 MessageBox.Show(this.ParentForm, "请选择解析路径", "提示");
                 return;
             }
-            string[] txtFiles = Directory.GetFiles(txtPath.Text, $"*.{currFileType}");
+            string[] txtFiles;
+            if (equiType == "Xray")
+            {
+                txtFiles = Directory.GetDirectories(txtPath.Text);
+            }
+            else
+            {
+                txtFiles = Directory.GetFiles(txtPath.Text, $"*.{currFileType}");
+            }
             if (txtFiles.Length == 0)
             {
                 LogMessage(0, $"当前{txtPath.Text},没有{equiType}设备输出的{currFileType.ToLower()}文件");
@@ -253,28 +267,61 @@ namespace UAS_MES_NEW.Make
                     return;
                 }
 
-                string[] txtFiles = Directory.GetFiles(txtPath.Text, $"*.{currFileType}");
+                string[] txtFiles;
+                if (equiType == "Xray")
+                {
+                    txtFiles = Directory.GetDirectories(txtPath.Text);
+                }
+                else
+                {
+                    txtFiles = Directory.GetFiles(txtPath.Text, $"*.{currFileType}");
+                }
                 if (txtFiles.Length == 0)
                 {
                     return;
                 }
+
                 foreach (string file in txtFiles)
                 {
-                    if (!File.Exists(file))
+                    if (equiType == "Xray")
                     {
-                        LogMessage(0, $"文件不存在: {file}");
-                    }
+                        if (Path.GetFileName(file).Contains("Log_"))
+                        {
+                            continue;
+                        }
 
-                    string outFileMsg = CheckFileAccess(file);
-                    if (outFileMsg != "OK")
-                    {
-                        LogMessage(0, outFileMsg);
-                        continue;
-                    }
+                        if (!Directory.Exists(file))
+                        {
+                            LogMessage(0, $"文件夹不存在: {file}");
+                        }
 
-                    if (File.ReadAllText(file).Length == 0)
+                        outFileMsg = CheckFolderPermissions(file);
+
+                        if (outFileMsg != "OK")
+                        {
+                            LogMessage(0, outFileMsg);
+                            continue;
+                        }
+                    }
+                    else
                     {
-                        continue;
+                        if (!File.Exists(file))
+                        {
+                            LogMessage(0, $"文件不存在: {file}");
+                        }
+
+                        outFileMsg = CheckFileAccess(file);
+
+                        if (outFileMsg != "OK")
+                        {
+                            LogMessage(0, outFileMsg);
+                            continue;
+                        }
+
+                        if (File.ReadAllText(file).Length == 0)
+                        {
+                            continue;
+                        }
                     }
 
                     if (equiType == "AOI")
@@ -327,16 +374,46 @@ namespace UAS_MES_NEW.Make
                     }
                     else if (equiType == "Xray")
                     {
+                        string[] floderFile = Directory.GetFiles(file, $"*.{currFileType}");
+                        foreach(string floderFileItem in floderFile)
+                        {
+                            SN = Path.GetFileName(floderFileItem).Split('-')[0];
+                            if (UploadImageToFtp(floderFileItem, SN))
+                            {
+                                dh.ExecuteSql($@"INSERT INTO steptestmain (sm_id, sm_sn,sm_makecode,sm_stepcode,sm_indate,sm_machinecode,sm_result) 
+                                    VALUES ( steptestmain_seq.NEXTVAL,'{SN}','{ma_code.Text}','{User.UserSourceCode}', sysdate,'Xray', 'http://192.168.1.5:8088/ftp/xray/{DateTime.Now.ToString("yyyyMMdd")}/{Path.GetFileName(floderFileItem)}' )", "insert");
 
+                                if (Array.IndexOf(floderFile, floderFileItem) == floderFile.Length - 1)
+                                {
+                                    string newFloderName = Path.GetDirectoryName(file);
+                                    Directory.Move(file, Path.Combine(newFloderName, "Log_" + Path.GetFileName(file)));
+                                }
+                            }
+                        }
                     }
                     else if(equiType == "CCD")
                     {
-                        SN = Path.GetFileName(file).Split('_')[0].ToString();
-                        if (UploadImageToFtp(file, SN))
+                        string imageName = Path.GetFileName(file);
+                        SN = imageName.Split('_')[0].ToString();
+
+                        if (imageName.ToUpper().Contains("NG"))
+                        {
+                            if (UploadImageToFtp(file, SN))
+                            {
+                                dh.ExecuteSql($@"INSERT INTO steptestmain (sm_id, sm_sn,sm_makecode,sm_stepcode,sm_indate,sm_machinecode,sm_result) 
+                                    VALUES ( steptestmain_seq.NEXTVAL,'{SN}','{ma_code.Text}','{User.UserSourceCode}', sysdate,'CCD', 'http://192.168.1.5:8088/ftp/ccd/{SN}/{Path.GetFileName(file)}' )", "insert");
+                            }
+                        }
+                        if(Array.IndexOf(txtFiles, file) == 1)
                         {
-                            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");
+                            string parentPath = Path.GetDirectoryName(file);
+                            string changeName = Path.Combine(parentPath, SN);
+
+                            if (!Directory.Exists(changeName))
+                            {
+                                Directory.CreateDirectory(changeName);
+                            }
+                            File.Move(file, changeName);
                         }
                     }
 
@@ -543,10 +620,15 @@ namespace UAS_MES_NEW.Make
 
         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 ftpServer;
+            //string ftpServer = "ftp://10.8.0.215:21/xray/";
+            //string ftpServer = "ftp://192.168.1.5:21/xray/";
+
+            ftpServer = equiType == "Xray" ? "ftp://10.8.0.215:21/xray/" : "ftp://10.8.0.215:21/ccd/";
+            ftpServer = equiType == "Xray" ? "ftp://192.168.1.5:21/xray/" : "ftp://192.168.1.5:21/ccd/";
+
             string username = "vsftpd";
-            string password = "vsftpd3cd79018fl";
+            string password = "vsftpd3ef41637hy";
 
             string currentDate = DateTime.Now.ToString("yyyyMMdd");
             string ftpFullPath = $"{ftpServer.TrimEnd('/')}/{currentDate}";
@@ -646,18 +728,52 @@ namespace UAS_MES_NEW.Make
             }
             catch (IOException ex)
             {
-                return "文件被占用: " + ex.Message;
+                return "NG,文件被占用: " + ex.Message;
             }
             catch (UnauthorizedAccessException ex)
             {
-                return "权限不足,无法访问文件: " + ex.Message;
+                return "NG,权限不足,无法访问文件: " + ex.Message;
             }
             catch (Exception ex)
             {
-                return ex.Message;
+                return "NG," + ex.Message;
             }
         }
+        public string CheckFolderPermissions(string path)
+        {
+            try
+            {
+                DirectorySecurity security = Directory.GetAccessControl(path);
+                AuthorizationRuleCollection rules = security.GetAccessRules(true, true, typeof(NTAccount));
+
+                WindowsIdentity currentUser = WindowsIdentity.GetCurrent();
+
+                foreach (FileSystemAccessRule rule in rules)
+                {
+                    if (rule.IdentityReference.Value == currentUser.Name || currentUser.Groups.Contains(rule.IdentityReference))
+                    {
+                        Console.WriteLine($" {rule.IdentityReference.Value}");
+                        Console.WriteLine($"权限类型: {rule.AccessControlType}");
+                        Console.WriteLine($"具体权限: {rule.FileSystemRights}");
+                        Console.WriteLine($"是否继承: {rule.IsInherited}");
 
+                    }
+                }
+                return "OK";
+            }
+            catch (UnauthorizedAccessException ex)
+            {
+                return $"NG,无权限访问文件夹: {ex.Message}";
+            }
+            catch (DirectoryNotFoundException)
+            {
+                return "NG,文件夹不存在";
+            }
+            catch (Exception ex)
+            {
+                return "NG," + ex.Message;
+            }
+        }
         public class Log
         {
             public string SN { set; get; }