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-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 005db73c..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.809\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 2e747c15..7ff78754 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; @@ -60,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) { @@ -79,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); } } } @@ -172,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) { @@ -181,16 +190,15 @@ 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}"; // 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() @@ -211,11 +219,19 @@ namespace IOB_WIN_NEXT.IobNet lgError($"Error in startSetup | fileUpload | locFile: {locFile} | remFile: {remFile}"); } } + break; case taskType.endProd: - // cerco tutti i file dell'odl arttivo e li elimino - + bool okRemove = RemPlaceholder(RemoteBaseDir, fNameOdl); + if (okRemove) + { + taskVal = $"Cleaned CurrOdlFile: {item.Key} --> {item.Value} | remDir: {RemoteBaseDir} | fName: {fNameOdl}"; + } + else + { + lgError($"Error in CurrOdlFile | remDir: {RemoteBaseDir} | fName: {fNameOdl}"); + } break; default: @@ -232,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 @@ -253,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) @@ -450,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 @@ -538,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 { @@ -560,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 @@ -618,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) { @@ -668,6 +714,49 @@ namespace IOB_WIN_NEXT.IobNet 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 = ftpClientMan.DirExists(remoteVal); + if (fatto) + { + // recupero elenco di TUTTI i file presenti + 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) + { + if (flItem.Type == FluentFTP.FtpObjectType.File && flItem.Name.EndsWith(fName2Del)) + { + list2del.Add(flItem); + } + } + // elimino quelli trovati + if (list2del.Count > 0) + { + var fList = list2del.Select(x => x.FullName).ToList(); + numRem = ftpClientMan.DeleteFileList(fList); + } + } + } + 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: break; @@ -677,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 1813249c..0df37a61 100644 --- a/IOB-WIN-NEXT/packages.config +++ b/IOB-WIN-NEXT/packages.config @@ -3,7 +3,7 @@ - +