From 231bf245ae709eef3b53e15f03a2d15c00f604d8 Mon Sep 17 00:00:00 2001 From: Samuele Locatelli Date: Tue, 8 Oct 2024 16:16:29 +0200 Subject: [PATCH 1/2] EgwProxy.FTP - aggiunta eliminazione elenco file in singolo step connessione --- EgwProxy.Ftp.Test/Program.cs | 12 +++-- EgwProxy.Ftp.Test/TestSetup.cs | 2 +- EgwProxy.Ftp.Test/conf/testSetup.json | 4 +- EgwProxy.Ftp/Manager.cs | 35 +++++++++++++- IOB-WIN-NEXT/IOB-WIN-NEXT.csproj | 2 +- IOB-WIN-NEXT/IobNet/Ftp.cs | 68 ++++++++++++++++++++++++++- IOB-WIN-NEXT/packages.config | 2 +- 7 files changed, 112 insertions(+), 13 deletions(-) diff --git a/EgwProxy.Ftp.Test/Program.cs b/EgwProxy.Ftp.Test/Program.cs index 13207ec9..440e5265 100644 --- a/EgwProxy.Ftp.Test/Program.cs +++ b/EgwProxy.Ftp.Test/Program.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using System.Configuration; using System.Diagnostics; using System.IO; +using System.Linq; using System.Text; using static System.Net.Mime.MediaTypeNames; @@ -375,7 +376,7 @@ namespace EgwProxy.Ftp.Test esitoStep = $"Completato mirror R2L: {sw.ElapsedMilliseconds:N1} ms"; break; - case stepType.getRemoteList: + case stepType.delFileList: if (item.paramList != null && item.paramList.Count > 1) { sw.Restart(); @@ -386,15 +387,18 @@ namespace EgwProxy.Ftp.Test List resListFilt = new List(); foreach (var flItem in resList) { - if(flItem.Type == FluentFTP.FtpObjectType.File && flItem.Name.Contains(fileSearch)) + if(flItem.Type == FluentFTP.FtpObjectType.File && flItem.Name.EndsWith(fileSearch)) { resListFilt.Add(flItem); } } - + // elimino quelli trovati if (resListFilt.Count>0) { - esitoStep = $"Trovati {resListFilt.Count} file"; + var fList = resListFilt.Select(x => x.FullName).ToList(); + var numDel = ftpClient.DeleteFileList(fList); + // indico esito eliminazione + esitoStep = $"Eliminati {numDel}/{resListFilt.Count} file"; } else { diff --git a/EgwProxy.Ftp.Test/TestSetup.cs b/EgwProxy.Ftp.Test/TestSetup.cs index d3f7e480..fc7f167d 100644 --- a/EgwProxy.Ftp.Test/TestSetup.cs +++ b/EgwProxy.Ftp.Test/TestSetup.cs @@ -32,10 +32,10 @@ namespace EgwProxy.Ftp.Test createDir, delDir, delFile, + delFileList, downloadDir, downloadFile, genRandomDir, - getRemoteList, listContent, mirrorDirL2R, mirrorDirR2L, diff --git a/EgwProxy.Ftp.Test/conf/testSetup.json b/EgwProxy.Ftp.Test/conf/testSetup.json index d5c003ad..b70e0936 100644 --- a/EgwProxy.Ftp.Test/conf/testSetup.json +++ b/EgwProxy.Ftp.Test/conf/testSetup.json @@ -8,10 +8,10 @@ { "id": "01", "description": "Generate Local Folder Data", - "action": "getRemoteList", + "action": "delFileList", "paramList": [ "ftpdata/syncfolder/", - ".txt" + "File_000.txt" ] }, //{ diff --git a/EgwProxy.Ftp/Manager.cs b/EgwProxy.Ftp/Manager.cs index 8141752a..1f208036 100644 --- a/EgwProxy.Ftp/Manager.cs +++ b/EgwProxy.Ftp/Manager.cs @@ -102,7 +102,7 @@ namespace EgwProxy.Ftp } /// - /// Eliminazionedirectory remota + /// Eliminazione file remoto /// /// Nome file remoto da eliminare public bool DeleteFile(string remoteFile) @@ -124,6 +124,37 @@ namespace EgwProxy.Ftp return answ; } + /// + /// Eliminazione lista di files remoti + /// + /// Lista file remoti da eliminare + public int DeleteFileList(List remFileList) + { + int numDone = 0; + tryConnect(); + try + { + foreach (var remoteFile in remFileList) + { + // Elimina folder + client.DeleteFile(remoteFile); + // verifico se sia presente... + bool fExist = client.FileExists(remoteFile); + if (!fExist) + { + numDone++; + } + } + } + catch (Exception exc) + { + Log.Error($"Eccezione in DeleteFileList{Environment.NewLine}{exc}"); + } + // chiudo! + client.Disconnect(); + return numDone; + } + /// /// Verifica esistenza directory su server FTP remoto /// @@ -455,7 +486,7 @@ namespace EgwProxy.Ftp #region Private Properties - private FtpClient client { get; set; } + private FtpClient client { get; set; } = new FtpClient(); #endregion Private Properties diff --git a/IOB-WIN-NEXT/IOB-WIN-NEXT.csproj b/IOB-WIN-NEXT/IOB-WIN-NEXT.csproj index 005db73c..6805a039 100644 --- a/IOB-WIN-NEXT/IOB-WIN-NEXT.csproj +++ b/IOB-WIN-NEXT/IOB-WIN-NEXT.csproj @@ -111,7 +111,7 @@ ..\packages\EasyModbusTCP.5.6.0\lib\net40\EasyModbus.dll - ..\packages\EgwProxy.Ftp.3.6.2410.809\lib\EgwProxy.Ftp.dll + ..\packages\EgwProxy.Ftp.3.6.2410.815\lib\EgwProxy.Ftp.dll ..\packages\EgwProxy.Gomba.3.6.2309.1416\lib\EgwProxy.Gomba.dll diff --git a/IOB-WIN-NEXT/IobNet/Ftp.cs b/IOB-WIN-NEXT/IobNet/Ftp.cs index 2e747c15..1afd1416 100644 --- a/IOB-WIN-NEXT/IobNet/Ftp.cs +++ b/IOB-WIN-NEXT/IobNet/Ftp.cs @@ -8,6 +8,7 @@ using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; +using System.IO.Packaging; using System.Linq; using System.Net.NetworkInformation; using System.Text; @@ -190,7 +191,7 @@ namespace IOB_WIN_NEXT.IobNet //string remFile = $"{RemoteBaseDir}/{odlFolder}/{fName}"; // creo file locale File.WriteAllText(locFile, item.Value); - // creo file temp x invio + // invio file x ODL attivo currActParam.Add("LocalFile", locFile); currActParam.Add("RemoteFile", remFile); currAct = new ActionConfig() @@ -214,7 +215,25 @@ namespace IOB_WIN_NEXT.IobNet break; case taskType.endProd: - // cerco tutti i file dell'odl arttivo e li elimino + currActParam.Add("RemoteDir", RemoteBaseDir); + currActParam.Add("FileName2Del", fNameOdl); + currAct = new ActionConfig() + { + Id = "01", + Description = "Clean Curr ODL Files", + Action = ActType.RemoveFileByName, + ParamList = currActParam + }; + // eseguo step... + taskOk = doStep(currAct); + if (taskOk) + { + taskVal = $"Cleaned CurrOdlFile: {item.Key} --> {item.Value} | remDir: {RemoteBaseDir} | fName: {fNameOdl}"; + } + else + { + lgError($"Error in CurrOdlFile | remDir: {RemoteBaseDir} | fName: {fNameOdl}"); + } break; @@ -666,6 +685,51 @@ namespace IOB_WIN_NEXT.IobNet break; case ActType.PingServer: + break; + + case ActType.RemoveFileByName: + string fName2Del = ""; + int numRem = 0; + if (step.ParamList != null && step.ParamList.Count > 1) + { + sw.Restart(); + remoteVal = step.ParamList["RemoteDir"] ?? ""; + fName2Del = step.ParamList["FileName2Del"] ?? ""; + //verifico dir remota + fatto = ftpClient.DirExists(remoteVal); + if (fatto) + { + // recupero elenco di TUTTI i file presenti + List resList = ftpClient.GetRemoteList(remoteVal, true); + List list2del = new List(); + // cerco tutti i file che indicano ODL attivo e li elimino + foreach (var flItem in resList) + { + if (flItem.Type == FluentFTP.FtpObjectType.File && flItem.Name.EndsWith(fName2Del)) + { + list2del.Add(flItem); + } + } + // ciclo sui file da eliminare... + foreach (var file2del in list2del) + { + bool delOk= ftpClient.DeleteFile(file2del.FullName); + numRem += delOk ? 1 : 0; + } + } + } + else + { + lgError("Error: missing parameters!"); + } + sw.Stop(); + if (fatto) + { + lgInfo($"RemoveFileByName | path: {remoteVal} | name: {fName2Del} | # del: {numRem} | {sw.ElapsedMilliseconds:N1} ms"); + } + + + break; case ActType.UploadDir: diff --git a/IOB-WIN-NEXT/packages.config b/IOB-WIN-NEXT/packages.config index 1813249c..552c5993 100644 --- a/IOB-WIN-NEXT/packages.config +++ b/IOB-WIN-NEXT/packages.config @@ -3,7 +3,7 @@ - + From 74690d9b1e80af74e33673c9e1a55fd83949a272 Mon Sep 17 00:00:00 2001 From: Samuele Locatelli Date: Tue, 8 Oct 2024 18:34:15 +0200 Subject: [PATCH 2/2] IOB-FTP - gestione cancellazione file non necessari in altri ODL - verifica casi gestione file txt x ODL gestito - update con test TAB3 nuovo... OK! --- IOB-UT-NEXT/FtpActConf.cs | 2 + IOB-WIN-NEXT/DATA/CONF/FTP_SONATEST.ini | 4 +- IOB-WIN-NEXT/IOB-WIN-NEXT.csproj | 2 +- IOB-WIN-NEXT/IobNet/Ftp.cs | 108 ++++++++++++++++-------- IOB-WIN-NEXT/packages.config | 2 +- 5 files changed, 77 insertions(+), 41 deletions(-) diff --git a/IOB-UT-NEXT/FtpActConf.cs b/IOB-UT-NEXT/FtpActConf.cs index 0a0e2243..7ee333b0 100644 --- a/IOB-UT-NEXT/FtpActConf.cs +++ b/IOB-UT-NEXT/FtpActConf.cs @@ -96,6 +96,7 @@ namespace IOB_UT_NEXT CreateDir, DelDir, DelFile, + //DelFileList, DownloadDir, DownloadFile, GenRandomDir, @@ -103,6 +104,7 @@ namespace IOB_UT_NEXT MirrorDirL2R, MirrorDirR2L, PingServer, + RemoveFileByName, UploadDir, UploadFile } diff --git a/IOB-WIN-NEXT/DATA/CONF/FTP_SONATEST.ini b/IOB-WIN-NEXT/DATA/CONF/FTP_SONATEST.ini index f67ff483..e93bb486 100644 --- a/IOB-WIN-NEXT/DATA/CONF/FTP_SONATEST.ini +++ b/IOB-WIN-NEXT/DATA/CONF/FTP_SONATEST.ini @@ -50,8 +50,8 @@ DISABLE_SEND_WDST=TRUE ; gestioni PING MAX_TRY_PING=3 -VETO_QUEUE_IN=5 -VETO_PING_SEC=5 +VETO_QUEUE_IN=2 +VETO_PING_SEC=2 MAX_ELAPSED_TIME_SEC=60 ;VETO_SEND_SNAPSHOT=3 diff --git a/IOB-WIN-NEXT/IOB-WIN-NEXT.csproj b/IOB-WIN-NEXT/IOB-WIN-NEXT.csproj index 6805a039..6de9eea9 100644 --- a/IOB-WIN-NEXT/IOB-WIN-NEXT.csproj +++ b/IOB-WIN-NEXT/IOB-WIN-NEXT.csproj @@ -111,7 +111,7 @@ ..\packages\EasyModbusTCP.5.6.0\lib\net40\EasyModbus.dll - ..\packages\EgwProxy.Ftp.3.6.2410.815\lib\EgwProxy.Ftp.dll + ..\packages\EgwProxy.Ftp.3.6.2410.816\lib\EgwProxy.Ftp.dll ..\packages\EgwProxy.Gomba.3.6.2309.1416\lib\EgwProxy.Gomba.dll diff --git a/IOB-WIN-NEXT/IobNet/Ftp.cs b/IOB-WIN-NEXT/IobNet/Ftp.cs index 1afd1416..7ff78754 100644 --- a/IOB-WIN-NEXT/IobNet/Ftp.cs +++ b/IOB-WIN-NEXT/IobNet/Ftp.cs @@ -61,7 +61,7 @@ namespace IOB_WIN_NEXT.IobNet if (!string.IsNullOrEmpty(ftpConfFile)) { loadFtpConfFile(ftpConfFile); - // mi calcolo ed imposto la ftpClient + Remote BaseDir... + // mi calcolo ed imposto la ftpClientMan + Remote BaseDir... string actKey = "RemoteDir"; if (currFtpTaskList != null && currFtpTaskList.ListTask.Count > 0) { @@ -80,10 +80,10 @@ namespace IOB_WIN_NEXT.IobNet } } } - if (!ftpClient.IsConfigured) + if (!ftpClientMan.IsConfigured) { - // setup ftpClient! - ftpClient = new Manager(fTask.ServerAddr, fTask.ConnUser, fTask.ConnPasswd, fTask.RawCert, fTask.SkipCert); + // setup ftpClientMan! + ftpClientMan = new Manager(fTask.ServerAddr, fTask.ConnUser, fTask.ConnPasswd, fTask.RawCert, fTask.SkipCert); } } } @@ -173,6 +173,14 @@ namespace IOB_WIN_NEXT.IobNet case taskType.startSetup: case taskType.stopSetup: + //verifico odl x prima cosa... + pzCntReload(true); + // faccio pulizia preliminare della commessa corrente... + bool cleanDone = RemPlaceholder(RemoteBaseDir, fNameOdl); + if (cleanDone) + { + lgInfo($"Cleaned {fNameOdl} during startSetup | remDir: {RemoteBaseDir}"); + } // verifico di avere giĆ  un ODL corrente... if (currIdxODL == 0) { @@ -182,10 +190,9 @@ namespace IOB_WIN_NEXT.IobNet { string odlFolder = $"ODL{currIdxODL:00000000}"; // scrivo file ultima richiesta setup - string fName = $"{item.Key}.txt"; string basePath = System.Windows.Forms.Application.StartupPath; string tempDir = Path.Combine(basePath, "temp"); - string locFile = Path.Combine(tempDir, fName); + string locFile = Path.Combine(tempDir, fNameOdl); // salva col nome FISSO di ODL_CORRENTE string remFile = $"{RemoteBaseDir}/{odlFolder}/{fNameOdl}"; //string remFile = $"{RemoteBaseDir}/{odlFolder}/{fName}"; @@ -212,21 +219,12 @@ namespace IOB_WIN_NEXT.IobNet lgError($"Error in startSetup | fileUpload | locFile: {locFile} | remFile: {remFile}"); } } + break; case taskType.endProd: - currActParam.Add("RemoteDir", RemoteBaseDir); - currActParam.Add("FileName2Del", fNameOdl); - currAct = new ActionConfig() - { - Id = "01", - Description = "Clean Curr ODL Files", - Action = ActType.RemoveFileByName, - ParamList = currActParam - }; - // eseguo step... - taskOk = doStep(currAct); - if (taskOk) + bool okRemove = RemPlaceholder(RemoteBaseDir, fNameOdl); + if (okRemove) { taskVal = $"Cleaned CurrOdlFile: {item.Key} --> {item.Value} | remDir: {RemoteBaseDir} | fName: {fNameOdl}"; } @@ -234,7 +232,6 @@ namespace IOB_WIN_NEXT.IobNet { lgError($"Error in CurrOdlFile | remDir: {RemoteBaseDir} | fName: {fNameOdl}"); } - break; default: @@ -251,6 +248,36 @@ namespace IOB_WIN_NEXT.IobNet return taskDone; } + /// + /// Rimuove eventuali file placeholder di ODL corrente preesistenti + /// + /// + /// + private bool RemPlaceholder(string remDir, string fNameOdl) + { + bool fatto = false; + try + { + Dictionary currActParam = new Dictionary(); + currActParam.Add("RemoteDir", remDir); + currActParam.Add("FileName2Del", fNameOdl); + ActionConfig currAct = new ActionConfig() + { + Id = "01", + Description = "Clean Curr ODL Files", + Action = ActType.RemoveFileByName, + ParamList = currActParam + }; + // eseguo step... + fatto = doStep(currAct); + } + catch (Exception exc) + { + lgError($"Eccezione in RemPlaceholder{Environment.NewLine}{exc}"); + } + return fatto; + } + /// /// Effettua processing CUSTOM x FTP: /// - prende ogni conf specifica @@ -272,12 +299,12 @@ namespace IOB_WIN_NEXT.IobNet // imposto nuovo veto... ActionResetVeto(srvFtp); // ora setup server FTP x item... - ftpClient = new Manager(srvFtp.ServerAddr, srvFtp.ConnUser, srvFtp.ConnPasswd, srvFtp.RawCert, srvFtp.SkipCert); + ftpClientMan = new Manager(srvFtp.ServerAddr, srvFtp.ConnUser, srvFtp.ConnPasswd, srvFtp.RawCert, srvFtp.SkipCert); // test server ok... - if (ftpClient.ServerOk()) + if (ftpClientMan.ServerOk()) { int stepDone = 0; - string srvType = ftpClient.ServerType(); + string srvType = ftpClientMan.ServerType(); lgTrace($"Connesso a server {srvType} | {srvFtp.ServerAddr} | inizio processing {srvFtp.StepsList.Count} steps"); // ciclo tra i vari steps! foreach (var step in srvFtp.StepsList) @@ -469,7 +496,7 @@ namespace IOB_WIN_NEXT.IobNet /// /// CLient connessioni FTP /// - private Manager ftpClient { get; set; } = new Manager("", "", "", "", false); + private Manager ftpClientMan { get; set; } = new Manager("", "", "", "", false); /// /// Directpry remota di abse @@ -557,7 +584,7 @@ namespace IOB_WIN_NEXT.IobNet sw.Restart(); remoteVal = step.ParamList["RemoteDir"] ?? ""; //verifico dir remota - fatto = ftpClient.DirExists(remoteVal); + fatto = ftpClientMan.DirExists(remoteVal); } else { @@ -579,14 +606,14 @@ namespace IOB_WIN_NEXT.IobNet sw.Restart(); remoteVal = step.ParamList["RemoteDir"] ?? ""; //verifico dir remota - bool dirExist = ftpClient.DirExists(remoteVal); + bool dirExist = ftpClientMan.DirExists(remoteVal); if (dirExist) { lgTrace("Error: Folder already exists!"); } else { - fatto = ftpClient.CreateDir(remoteVal); + fatto = ftpClientMan.CreateDir(remoteVal); } } else @@ -637,12 +664,12 @@ namespace IOB_WIN_NEXT.IobNet Directory.CreateDirectory(localVal); } //verifico dir remota - var preTest = ftpClient.DirExists(remoteVal); + var preTest = ftpClientMan.DirExists(remoteVal); if (preTest) { // chiamo metodo MIRROR x calcolare esattamente se ci siano stati // download di sync... - var mirResult = ftpClient.MirrorRemoteDir(localVal, remoteVal, FluentFTP.FtpFolderSyncMode.Mirror); + var mirResult = ftpClientMan.MirrorRemoteDir(localVal, remoteVal, FluentFTP.FtpFolderSyncMode.Mirror); // ciclo cercando eventuali info da emttere in DynData... foreach (var result in mirResult) { @@ -696,11 +723,11 @@ namespace IOB_WIN_NEXT.IobNet remoteVal = step.ParamList["RemoteDir"] ?? ""; fName2Del = step.ParamList["FileName2Del"] ?? ""; //verifico dir remota - fatto = ftpClient.DirExists(remoteVal); + fatto = ftpClientMan.DirExists(remoteVal); if (fatto) { // recupero elenco di TUTTI i file presenti - List resList = ftpClient.GetRemoteList(remoteVal, true); + List resList = ftpClientMan.GetRemoteList(remoteVal, true); List list2del = new List(); // cerco tutti i file che indicano ODL attivo e li elimino foreach (var flItem in resList) @@ -710,11 +737,11 @@ namespace IOB_WIN_NEXT.IobNet list2del.Add(flItem); } } - // ciclo sui file da eliminare... - foreach (var file2del in list2del) + // elimino quelli trovati + if (list2del.Count > 0) { - bool delOk= ftpClient.DeleteFile(file2del.FullName); - numRem += delOk ? 1 : 0; + var fList = list2del.Select(x => x.FullName).ToList(); + numRem = ftpClientMan.DeleteFileList(fList); } } } @@ -728,8 +755,6 @@ namespace IOB_WIN_NEXT.IobNet lgInfo($"RemoveFileByName | path: {remoteVal} | name: {fName2Del} | # del: {numRem} | {sw.ElapsedMilliseconds:N1} ms"); } - - break; case ActType.UploadDir: @@ -741,7 +766,16 @@ namespace IOB_WIN_NEXT.IobNet sw.Restart(); localVal = step.ParamList["LocalFile"] ?? ""; remoteVal = step.ParamList["RemoteFile"] ?? ""; - fatto = ftpClient.SendFile(localVal, remoteVal); + + //verifico dir remota + string remoteDir = remoteVal.Substring(0, remoteVal.LastIndexOf("/")); + bool dirExist = ftpClientMan.DirExists(remoteDir); + if (!dirExist) + { + fatto = ftpClientMan.CreateDir(remoteDir); + } + + fatto = ftpClientMan.SendFile(localVal, remoteVal); if (!fatto) { lgError($"Error: {localVal} NOT uploaded!"); diff --git a/IOB-WIN-NEXT/packages.config b/IOB-WIN-NEXT/packages.config index 552c5993..0df37a61 100644 --- a/IOB-WIN-NEXT/packages.config +++ b/IOB-WIN-NEXT/packages.config @@ -3,7 +3,7 @@ - +