From 3066432c949256b65aeb393c1934a21734144dc3 Mon Sep 17 00:00:00 2001 From: "Samuele E. Locatelli" Date: Tue, 2 Mar 2021 11:02:37 +0100 Subject: [PATCH] fix controlllo path pdf x UNC + timeout ping --- AppData/ComLib.cs | 38 ++- NKC_SDK/NKC.cs | 396 ++++++++++++---------- NKC_WF/Controllers/BatchProcController.cs | 17 +- 3 files changed, 263 insertions(+), 188 deletions(-) diff --git a/AppData/ComLib.cs b/AppData/ComLib.cs index 9344799..97aa50e 100644 --- a/AppData/ComLib.cs +++ b/AppData/ComLib.cs @@ -7,6 +7,8 @@ using System; using System.Collections.Generic; using System.IO; using System.Web; +using System.Net; +using System.Net.NetworkInformation; namespace AppData { @@ -804,7 +806,25 @@ namespace AppData { try { - answ = File.Exists(pdfPath); + string parentDir = Path.GetDirectoryName(pdfPath); + bool shareOk = true; + bool dirOk = true; + if (IsUnc(parentDir)) + { + string pathRoot = Path.GetPathRoot(parentDir); + Uri newUri = new Uri(pathRoot); + // tento PING + PingReply pingResult = NKC.testPingDevice(newUri.Host); + shareOk = (pingResult.Status == IPStatus.Success); + } + if (shareOk) + { + dirOk = Directory.Exists(parentDir); + if (dirOk) + { + answ = File.Exists(pdfPath); + } + } } catch { } @@ -1242,6 +1262,22 @@ namespace AppData return answ; } + public static bool IsUnc(string path) + { + string root = Path.GetPathRoot(path); + + // Check if root starts with "\\", clearly an UNC + if (root.StartsWith(@"\\")) + return true; + + // Check if the drive is a network drive + DriveInfo drive = new DriveInfo(root); + if (drive.DriveType == DriveType.Network) + return true; + + return false; + } + /// /// restitusice ultima chiamata REST registrata su REDIS /// diff --git a/NKC_SDK/NKC.cs b/NKC_SDK/NKC.cs index aa22ab2..c34c778 100644 --- a/NKC_SDK/NKC.cs +++ b/NKC_SDK/NKC.cs @@ -8,8 +8,212 @@ namespace NKC_SDK { public class NKC { - #region utils comunicazione HTTP + #region Protected Fields + /// + /// file locale per persistenza BUNK + /// + protected string persistFileName = "data/persistFile.json"; + + #endregion Protected Fields + + #region Public Constructors + + /// + /// Classe per effettuare comunicazioni con NKC + /// + /// IP di base x ping + /// URL di abse x chiamate REST + /// Codice posstazione/macchina x cui si fa chiamata + public NKC(string baseIp, string baseUrl, string codPost) + { + _baseIp = baseIp; + _baseUrl = baseUrl; + _codPost = codPost; + } + + #endregion Public Constructors + + #region Protected Properties + + /// + /// DnsName/IP di base x chaimate + /// + protected string _baseIp { get; set; } = "seriate.steamware.net"; + + /// + /// URL di base per la comunicazione + /// + /// PROD: http://seriate.steamware.net:8083/NKC/ + /// DEV: https://localhost:44388/ + /// + protected string _baseUrl { get; set; } = @"http://seriate.steamware.net:8083/NKC/"; + + /// + /// COD macchina x cui si effettua chiamata + /// + protected string _codPost { get; set; } = ""; + + protected string urlAlive + { + get + { + return $"{_baseUrl}api/Alive"; + } + } + + protected string urlAliveClock + { + get + { + return $"{_baseUrl}api/Alive/1"; + } + } + + protected string urlCurrBunk + { + get + { + return $"{_baseUrl}api/Bunk"; + } + } + + protected string urlCurrSheet4Mac + { + get + { + return $"{_baseUrl}api/Sheet/{_codPost}"; + } + } + + /// + /// URL x salvataggio dati SHEET + /// + /// + protected string urlPutSheetList + { + get + { + return $"{_baseUrl}api/Sheet/{_codPost}"; + } + } + + #endregion Protected Properties + + #region Public Properties + + /// + /// Oggetto che contiene l'oggetto SHEET WorkList corrente salvato LOCALMENTE + /// + public SheetWorkList persistedSheetList + { + get + { + SheetWorkList answ = new SheetWorkList(); + try + { + string rawdata = File.ReadAllText(persistFileName); + answ = JsonConvert.DeserializeObject(rawdata); + } + catch (Exception exc) + { + Log.Instance.Error($"Eccezione durante recupero locale della SheetList{Environment.NewLine}{exc}"); + } + return answ; + } + set + { + try + { + // serializzo oggetto + string rawdata = JsonConvert.SerializeObject(value); + // salvo in locale + File.WriteAllText(persistFileName, rawdata); + } + catch (Exception exc) + { + Log.Instance.Error($"Eccezione durante salvataggio locale della SheetList{Environment.NewLine}{exc}"); + } + } + } + + /// + /// Effettua test alive all'indirizzo del server + /// + public bool testAlive + { + get + { + bool answ = false; + string returnData = callUrl(urlAlive); + returnData = JsonConvert.DeserializeObject(returnData); + answ = returnData == "OK"; + // rendo! + return answ; + } + } + + /// + /// Effettua test ping all'indirizzo del server + /// + public DateTime testClock + { + get + { + DateTime oggi = DateTime.Today; + DateTime answ = oggi.AddYears(-oggi.Year + 1900).AddMonths(-oggi.Month).AddDays(-oggi.Day + 1); + // recupero! + string returnData = callUrl(urlAliveClock); + try + { + DateTime.TryParse(JsonConvert.DeserializeObject(returnData), out answ); + } + catch (Exception exc) + { + Log.Instance.Error($"Eccezione durante testClock, ricevuto {returnData}{Environment.NewLine}{exc}"); + } + // rendo! + return answ; + } + } + + /// + /// Effettua test ping all'indirizzo del server + /// + public PingReply testPing + { + get + { + Ping myPing = new Ping(); + // timeout a 1 sec! + PingReply answ = myPing.Send(_baseIp, 1000); + // rendo! + return answ; + } + } + + #endregion Public Properties + + #region Protected Methods + + protected string urlGetBunk(int currBunkId) + { + return $"{_baseUrl}api/Bunk/{currBunkId}?showNext=false"; + } + + protected string urlNextBunk(int currBunkId) + { + return $"{_baseUrl}api/Bunk/{currBunkId}?showNext=true"; + } + + protected string urlPutBunk(int currBunkId) + { + return $"{_baseUrl}api/Bunk/{currBunkId}"; + } + + #endregion Protected Methods + + #region Public Methods /// /// Effettua chiamata URL e restituisce risultato @@ -34,6 +238,7 @@ namespace NKC_SDK // restituisco valore! return answ; } + /// /// Effettua chiamata URL e restituisce risultato /// @@ -86,151 +291,14 @@ namespace NKC_SDK return answ; } - - #endregion - - #region URL di base - - protected string urlAlive + public static PingReply testPingDevice(string address) { - get - { - return $"{_baseUrl}api/Alive"; - } + Ping myPing = new Ping(); + // timeout a 1 sec! + PingReply answ = myPing.Send(address, 2000); + // rendo! + return answ; } - protected string urlAliveClock - { - get - { - return $"{_baseUrl}api/Alive/1"; - } - } - protected string urlCurrBunk - { - get - { - return $"{_baseUrl}api/Bunk"; - } - } - protected string urlCurrSheet4Mac - { - get - { - return $"{_baseUrl}api/Sheet/{_codPost}"; - } - } - protected string urlGetBunk(int currBunkId) - { - return $"{_baseUrl}api/Bunk/{currBunkId}?showNext=false"; - } - protected string urlNextBunk(int currBunkId) - { - return $"{_baseUrl}api/Bunk/{currBunkId}?showNext=true"; - } - protected string urlPutBunk(int currBunkId) - { - return $"{_baseUrl}api/Bunk/{currBunkId}"; - } - /// - /// URL x salvataggio dati SHEET - /// - /// - protected string urlPutSheetList - { - get - { - return $"{_baseUrl}api/Sheet/{_codPost}"; - } - } - /// - /// file locale per persistenza BUNK - /// - protected string persistFileName = "data/persistFile.json"; - - #endregion - - /// - /// URL di base per la comunicazione - /// - /// PROD: http://seriate.steamware.net:8083/NKC/ - /// DEV: https://localhost:44388/ - /// - protected string _baseUrl { get; set; } = @"http://seriate.steamware.net:8083/NKC/"; - /// - /// DnsName/IP di base x chaimate - /// - protected string _baseIp { get; set; } = "seriate.steamware.net"; - /// - /// COD macchina x cui si effettua chiamata - /// - protected string _codPost { get; set; } = ""; - - /// - /// Classe per effettuare comunicazioni con NKC - /// - /// IP di base x ping - /// URL di abse x chiamate REST - /// Codice posstazione/macchina x cui si fa chiamata - public NKC(string baseIp, string baseUrl, string codPost) - { - _baseIp = baseIp; - _baseUrl = baseUrl; - _codPost = codPost; - } - /// - /// Effettua test ping all'indirizzo del server - /// - public PingReply testPing - { - get - { - Ping myPing = new Ping(); - // timeout a 1 sec! - PingReply answ = myPing.Send(_baseIp, 1000); - // rendo! - return answ; - } - } - /// - /// Effettua test alive all'indirizzo del server - /// - public bool testAlive - { - get - { - bool answ = false; - string returnData = callUrl(urlAlive); - returnData = JsonConvert.DeserializeObject(returnData); - answ = returnData == "OK"; - // rendo! - return answ; - } - } - /// - /// Effettua test ping all'indirizzo del server - /// - public DateTime testClock - { - get - { - DateTime oggi = DateTime.Today; - DateTime answ = oggi.AddYears(-oggi.Year + 1900).AddMonths(-oggi.Month).AddDays(-oggi.Day + 1); - // recupero! - string returnData = callUrl(urlAliveClock); - try - { - DateTime.TryParse(JsonConvert.DeserializeObject(returnData), out answ); - } - catch (Exception exc) - { - Log.Instance.Error($"Eccezione durante testClock, ricevuto {returnData}{Environment.NewLine}{exc}"); - } - // rendo! - return answ; - } - } - - #region metodi per SheetWorklist /// /// Recupera elenco dei fogli ATTIVI @@ -254,6 +322,7 @@ namespace NKC_SDK } return answ; } + /// /// Effettua salvataggio dell'elenco dei fogli (1..n) su NKC /// @@ -276,42 +345,7 @@ namespace NKC_SDK } return answ; } - /// - /// Oggetto che contiene l'oggetto SHEET WorkList corrente salvato LOCALMENTE - /// - public SheetWorkList persistedSheetList - { - get - { - SheetWorkList answ = new SheetWorkList(); - try - { - string rawdata = File.ReadAllText(persistFileName); - answ = JsonConvert.DeserializeObject(rawdata); - } - catch (Exception exc) - { - Log.Instance.Error($"Eccezione durante recupero locale della SheetList{Environment.NewLine}{exc}"); - } - return answ; - } - set - { - try - { - // serializzo oggetto - string rawdata = JsonConvert.SerializeObject(value); - // salvo in locale - File.WriteAllText(persistFileName, rawdata); - } - catch (Exception exc) - { - Log.Instance.Error($"Eccezione durante salvataggio locale della SheetList{Environment.NewLine}{exc}"); - } - } - } - - #endregion + #endregion Public Methods } -} +} \ No newline at end of file diff --git a/NKC_WF/Controllers/BatchProcController.cs b/NKC_WF/Controllers/BatchProcController.cs index f404848..3269deb 100644 --- a/NKC_WF/Controllers/BatchProcController.cs +++ b/NKC_WF/Controllers/BatchProcController.cs @@ -167,15 +167,20 @@ namespace NKC_WF.Controllers case procStatus.completed: if (isValidation || isTesting) { - // verifico PDF, se NON OK --> errore bool pdfOk = true; - string pdfPath = ""; - foreach (var item in rispStima.PartList) + // se richiesto CheckPDF + if (memLayer.ML.CRB("checkPdfPathTV")) { - pdfOk = pdfOk && ComLib.checkPdfExistAccessible(item, out pdfPath); - if (!pdfOk) + // verifico PDF, se NON OK --> errore + string pdfPath = ""; + foreach (var item in rispStima.PartList) { - DLMan.taEL.insertQuery(DateTime.Now, "Check PDF path", $"{rispStima.BatchID}", $"{rispStima.BatchID}.{item.PartExtCode}", $"Error: PDF file not found: {pdfPath}"); + pdfOk = pdfOk && ComLib.checkPdfExistAccessible(item, out pdfPath); + if (!pdfOk) + { + // codice รจ B.xxx dove xxx = BatchID + DLMan.taEL.insertQuery(DateTime.Now, "Check PDF path", $"B.{rispStima.BatchID}", $"{rispStima.BatchID}.{item.PartExtCode}", $"Error: PDF file not found: {pdfPath}"); + } } }