From 3e64170d1b01f0686912bb1cdecefd23a20fad47 Mon Sep 17 00:00:00 2001 From: "Samuele E. Locatelli" Date: Fri, 27 Jun 2025 14:20:21 +0200 Subject: [PATCH] Aggiunto parametro disabilitazione KeepAlive per salvataggio FluxLog (singolo o json) da IOB --- MP-IO/Controllers/IOBController.cs | 222 +++++++++++++++++++---------- MP-IO/MP-IO.csproj.user | 2 +- MP-Site/MP-Site.csproj.user | 2 +- MapoDb/DataLayer.cs | 11 +- 4 files changed, 160 insertions(+), 77 deletions(-) diff --git a/MP-IO/Controllers/IOBController.cs b/MP-IO/Controllers/IOBController.cs index 1c08f3fd..9e85f18e 100644 --- a/MP-IO/Controllers/IOBController.cs +++ b/MP-IO/Controllers/IOBController.cs @@ -15,6 +15,7 @@ namespace MP_IO.Controllers { public class IOBController : Controller { + #region Public Methods /// SALVA x macchina KVP parametro/valore: @@ -450,8 +451,35 @@ namespace MP_IO.Controllers return answ; } - // GET: IOB/flog/SIMUL_03?flux=PROG&valore=P0001&dtEve=20161223180600000&dtCurr=20161223180600000&cnt=999 + /// + /// Invio record flog secco + /// GET: IOB/flog/SIMUL_03?flux=PROG&valore=P0001&dtEve=20161223180600000&dtCurr=20161223180600000&cnt=999 + /// + /// IdxMacchina + /// CodFlux + /// Valore da registrare + /// DataOra evento (IOB time) + /// DataOra corrente (IOB time) + /// Contatore eventi (IOB) + /// public string flog(string id, string flux, string valore, string dtEve, string dtCurr, string cnt) + { + return flog(id, flux, valore, dtEve, dtCurr, cnt, "false"); + } + + /// + /// Invio record flog secco + /// GET: IOB/flog/SIMUL_03?flux=PROG&valore=P0001&dtEve=20161223180600000&dtCurr=20161223180600000&cnt=999&disabKA=false + /// + /// IdxMacchina + /// CodFlux + /// Valore da registrare + /// DataOra evento (IOB time) + /// DataOra corrente (IOB time) + /// Contatore eventi (IOB) + /// Disabilitazione scrittura record KeepAlive + /// + public string flog(string id, string flux, string valore, string dtEve, string dtCurr, string cnt, string disabKA) { string answ = ""; // formato yyyymmddHHMMSSnnn ovvero da anno a millisecondi @@ -470,7 +498,9 @@ namespace MP_IO.Controllers DataLayer DataLayerObj = new DataLayer(); int count = 0; Int32.TryParse(cnt, out count); - answ = DataLayerObj.processFluxLog(id, flux, valore, dtEve, dtCurr, count); + int disKa = false; + bool.TryParse(disabKA, out disKa); + answ = DataLayerObj.processFluxLog(id, flux, valore, dtEve, dtCurr, count, disKa); } catch (Exception exc) { @@ -504,92 +534,140 @@ namespace MP_IO.Controllers // se ho dati... if (content != "") { - DataLayer DataLayerObj = new DataLayer(); - // procedo a deserializzare in blocco l'oggetto... - flogJsonPayload receivedData = new flogJsonPayload(); + answ = processFLogJson(id, false, insDone, answ, content); + } + return answ; + } + + /// + /// Processa una chiamata POST per l'invio di un array Json di oggetti fluxLog + /// PUT: IOB/flogJson/SIMUL_03 + /// + /// ID dell'IOB + /// Disabilitazione della scrittura del record KeepAlive + /// + [HttpPost] + public string flogJson(string id, bool disabKA) + { + int insDone = 0; + int disKa = false; + bool.TryParse(disabKA, out disKa); + string answ = "-"; + // questa classe è derivata da Controller.Response... x cui recupero lo stream in altro modo... + string content = ""; + System.Web.HttpContext.Current.Request.InputStream.Position = 0; + using (var reader = new StreamReader( + Request.InputStream, System.Text.Encoding.UTF8, true, 4096, true)) + { + content = reader.ReadToEnd(); + } + //Rest + System.Web.HttpContext.Current.Request.InputStream.Position = 0; + // se ho dati... + if (content != "") + { + answ = processFLogJson(id, disabKA, insDone, answ, content); + } + return answ; + } + + /// + /// Effettivo processing FLogJson + /// + /// + /// + /// + /// + /// + /// + private string processFLogJson(stringid, bool disabKA, int insDone, string answ, string content) + { + DataLayer DataLayerObj = new DataLayer(); + // procedo a deserializzare in blocco l'oggetto... + flogJsonPayload receivedData = new flogJsonPayload(); + try + { + // deserializzo. + receivedData = JsonConvert.DeserializeObject(content); + } + catch (Exception exc) + { + logger.lg.scriviLog($"Errore in fase deserializzazione flogJson{Environment.NewLine}{exc}"); + answ = "NO"; + } + // se ho qualcosa da processare... + if (receivedData != null) + { + // per ogni valore --> salvo! try { - // deserializzo. - receivedData = JsonConvert.DeserializeObject(content); + foreach (var item in receivedData.fluxData) + { + // formato datetime come yyyyMMddHHmmssfff -->es: 20181223180600000 + answ = DataLayerObj.processFluxLog(id, item.flux, item.valore, item.dtEve.ToString("yyyyMMddHHmmssfff"), item.dtCurr.ToString("yyyyMMddHHmmssfff"), item.cnt, disabKA); + } + // se vuoto --> OK! + if (string.IsNullOrEmpty(answ)) + { + answ = $"OK {insDone} processed"; + } } catch (Exception exc) { - logger.lg.scriviLog($"Errore in fase deserializzazione flogJson{Environment.NewLine}{exc}"); + logger.lg.scriviLog($"Errore in fase invio valori flogJson{Environment.NewLine}{exc}"); answ = "NO"; } - // se ho qualcosa da processare... - if (receivedData != null) + // leggo parametri correnti... + try { - // per ogni valore --> salvo! - try + List currParams = DataLayerObj.getCurrObjItems(id); + // ora per ogni valore RICEVUTO costruisco un oggetto in innovazioni da + // inviare...x salvare in stato parametri... + List innovazioni = new List(); + foreach (var item in receivedData.fluxData) { - foreach (var item in receivedData.fluxData) + // flux = uuid del parametro + objItem trovato = currParams.Find(obj => obj.uid == item.flux); + // se lo trovo aggiorno... + if (trovato != null) { - // formato datetime come yyyyMMddHHmmssfff -->es: 20181223180600000 - answ = DataLayerObj.processFluxLog(id, item.flux, item.valore, item.dtEve.ToString("yyyyMMddHHmmssfff"), item.dtCurr.ToString("yyyyMMddHHmmssfff"), item.cnt); - } - // se vuoto --> OK! - if (string.IsNullOrEmpty(answ)) - { - answ = $"OK {insDone} processed"; - } - } - catch (Exception exc) - { - logger.lg.scriviLog($"Errore in fase invio valori flogJson{Environment.NewLine}{exc}"); - answ = "NO"; - } - // leggo parametri correnti... - try - { - List currParams = DataLayerObj.getCurrObjItems(id); - // ora per ogni valore RICEVUTO costruisco un oggetto in innovazioni da - // inviare...x salvare in stato parametri... - List innovazioni = new List(); - foreach (var item in receivedData.fluxData) - { - // flux = uuid del parametro - objItem trovato = currParams.Find(obj => obj.uid == item.flux); - // se lo trovo aggiorno... - if (trovato != null) + // aggiorno valore e data + trovato.value = item.valore; + trovato.lastRead = DateTime.Now; + // se fosse un valore WRITE e mi ha dato un valore vuoto --> mando + // un fix x riscrittura + if (trovato.writable && string.IsNullOrEmpty(item.valore)) { - // aggiorno valore e data - trovato.value = item.valore; - trovato.lastRead = DateTime.Now; - // se fosse un valore WRITE e mi ha dato un valore vuoto --> mando - // un fix x riscrittura - if (trovato.writable && string.IsNullOrEmpty(item.valore)) - { - logger.lg.scriviLog($"flogJson | verifica parametri | {trovato.uid} | reqVal: {trovato.reqValue}"); - taskType currTask = (taskType)Enum.Parse(typeof(taskType), trovato.uid); - DataLayerObj.addCheckTask4Machine(id, currTask, item.valore); - } + logger.lg.scriviLog($"flogJson | verifica parametri | {trovato.uid} | reqVal: {trovato.reqValue}"); + taskType currTask = (taskType)Enum.Parse(typeof(taskType), trovato.uid); + DataLayerObj.addCheckTask4Machine(id, currTask, item.valore); } - // altrimenti AGGIUNGO (READ ONLY)... - else - { - trovato = new objItem - { - uid = item.flux, - name = item.flux, - value = item.valore, - lastRead = DateTime.Now, - writable = false - }; - } - // lo carico in innovation - innovazioni.Add(trovato); } - // faccio upsert innovations! - DataLayerObj.upsertCurrObjItems(id, innovazioni); - } - catch (Exception exc) - { - logger.lg.scriviLog($"Errore in fase salvataggio innovazioni parametri correnti da flogJson{Environment.NewLine}{exc}"); - answ = "NO"; + // altrimenti AGGIUNGO (READ ONLY)... + else + { + trovato = new objItem + { + uid = item.flux, + name = item.flux, + value = item.valore, + lastRead = DateTime.Now, + writable = false + }; + } + // lo carico in innovation + innovazioni.Add(trovato); } + // faccio upsert innovations! + DataLayerObj.upsertCurrObjItems(id, innovazioni); + } + catch (Exception exc) + { + logger.lg.scriviLog($"Errore in fase salvataggio innovazioni parametri correnti da flogJson{Environment.NewLine}{exc}"); + answ = "NO"; } } + return answ; } diff --git a/MP-IO/MP-IO.csproj.user b/MP-IO/MP-IO.csproj.user index 9e08f9d6..517918d6 100644 --- a/MP-IO/MP-IO.csproj.user +++ b/MP-IO/MP-IO.csproj.user @@ -10,7 +10,7 @@ - Release|Any CPU + Debug|Any CPU MvcControllerEmptyScaffolder root/Common/MVC/Controller 600 diff --git a/MP-Site/MP-Site.csproj.user b/MP-Site/MP-Site.csproj.user index 95a2660d..a8b53886 100644 --- a/MP-Site/MP-Site.csproj.user +++ b/MP-Site/MP-Site.csproj.user @@ -8,7 +8,7 @@ - Release|Any CPU + Debug|Any CPU ShowAllFiles C:\Users\samuele.steamw\source\MoonPro.net\MP-SITE\Properties\PublishProfiles\IIS03.pubxml diff --git a/MapoDb/DataLayer.cs b/MapoDb/DataLayer.cs index 10c28ea0..86e13e06 100644 --- a/MapoDb/DataLayer.cs +++ b/MapoDb/DataLayer.cs @@ -2553,13 +2553,18 @@ namespace MapoDb /// /// /// + /// /// - public string processFluxLog(string idxMacchina, string flux, string valore, string dtEve, string dtCurr, int contatore) + public string processFluxLog(string idxMacchina, string flux, string valore, string dtEve, string dtCurr, int contatore, bool disabKA) { // instanzio un nuovo oggetto MapoDb MapoDb connDb = new MapoDb(); - // scrivo keep alive!!! (se necessario, altrimenti è in cache...) - connDb.scriviKeepAlive(idxMacchina, DateTime.Now); + // se non vietato... + if (!disabKA) + { + // scrivo keep alive!!! (se necessario, altrimenti è in cache...) + connDb.scriviKeepAlive(idxMacchina, DateTime.Now); + } // registro conteggio impiego chiamate REDIS if (memLayer.ML.CRB("IOB_RedEnab")) {