diff --git a/IOB-WIN-NEXT/IobSqlServPama.cs b/IOB-WIN-NEXT/IobSqlServPama.cs index 6bea2057..fa665f77 100644 --- a/IOB-WIN-NEXT/IobSqlServPama.cs +++ b/IOB-WIN-NEXT/IobSqlServPama.cs @@ -1,13 +1,13 @@ -using EgwProxy.SqlDb.DbModels; -using EgwProxy.SqlDb.Controllers; +using EgwProxy.SqlDb.Controllers; +using EgwProxy.SqlDb.DbModels; using IOB_UT_NEXT; using MapoSDK; +using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Diagnostics; -using System.Net.NetworkInformation; using System.Linq; -using Newtonsoft.Json; +using System.Net.NetworkInformation; using static IOB_UT_NEXT.CustomObj; namespace IOB_WIN_NEXT @@ -23,8 +23,6 @@ namespace IOB_WIN_NEXT { #region Public Constructors - protected int vetoReadDbSec = 3; - protected int VetoSyncDbSec = 20; /// /// Costruttore dell'IOB DB PAMA /// @@ -165,29 +163,13 @@ namespace IOB_WIN_NEXT /// public override Dictionary getDynData() { - // valore non presente in vers default... se gestito fare override + DateTime adesso = DateTime.Now; + + // dizionario vuoto / faccio direttamente accodamento in FluxLog Dictionary outVal = new Dictionary(); - // totalmente da fare e testare sulal falsariga del signLog - -#if false - // recupero da DB locale stato sync attuale - var elencoSyncStateCurr = dbProxy.DataController.SyncStateGetAll(); - foreach (var item in elencoSyncStateCurr) - { - saveValue(ref outVal, item.NumRec, $"{item.TableName}_NumRec"); - saveValue(ref outVal, item.NumRecIn, $"{item.TableName}_NumRecIn"); - saveValue(ref outVal, item.LastIdx, $"{item.TableName}_LastIdx"); - saveValue(ref outVal, item.LastIdxIn, $"{item.TableName}_LastIdxIn"); - } - - // aggiungo anche i campi currData - var currData = dbProxy.DataController.CurrDataGetAll(); - foreach (var item in currData) - { - saveValue(ref outVal, (double)item.CurrVal, item.Topic); - } -#endif + // processo ed accodo! + processFluxLogTable(adesso); lastReadPLC = DateTime.Now; return outVal; @@ -228,70 +210,12 @@ namespace IOB_WIN_NEXT // semaforo currDispData.semIn = Semaforo.SV; - - // verifico ultimo idxLetto x SignLog e processo + // recupero syncState elencoSyncState = dbProxy.SyncStateGetAll(); - // cerco info sui SignLog (ToMes e letti) - var currSignLogRead = elencoSyncState.FirstOrDefault(x => x.TableName == "SignLog"); - // se nullo inizializzo - if (currSignLogRead == null) - { - currSignLogRead = new SyncStateModel() - { - LastIdx = 0, - LastUpdate = adesso, - TableName = "SignLog", - Note = "Init" - }; - } - var currSignLogSent = elencoSyncState.FirstOrDefault(x => x.TableName == "SignLogToMes"); - // se nullo inizializzo - if (currSignLogSent == null) - { - currSignLogSent = new SyncStateModel() - { - LastIdx = 0, - LastUpdate = adesso, - TableName = "SignLogToMes", - Note = "Init" - }; - } - // verifica se ci siano dati da trasmettere (sui valori LastIdx) - if (currSignLogRead.LastIdx > currSignLogSent.LastIdx) - { - // recupero i dati dal DB... - var data2send = dbProxy.MachSigLogGetNew(currSignLogSent.LastIdx); - // se ho dati preparo invio - if (data2send != null && data2send.Count > 0) - { - foreach (var sLog2send in data2send) - { - string currVal = getEncodSigLog(sLog2send.DtEve, sLog2send.ValInt, counterSigIN); - // --> accodo (valore già formattato)! - QueueIN.Enqueue(currVal); - // loggo! - lgTrace(string.Format("[QUEUE-IN] {0}", currVal)); - counterSigIN++; - if (counterSigIN > 9999) - { - counterSigIN = 0; - } - } - } - - // aggiorno idx inviato... - currSignLogSent.LastIdx = currSignLogRead.LastIdx; - var lastRec = data2send.LastOrDefault(); - if (lastRec != null) - { - // salvo in B_input ultimo valore letto... - B_input = lastRec.ValInt; - } - } - - - // alla fine aggiorno i dati inviati! - dbProxy.SyncStateUpsert(currSignLogSent); + // verifico SignLog e processo + bool sentSignLog = processSignLogTable(adesso); + // verifico ProdData e processo + bool sentProdData = processProdDataTable(adesso); } } else @@ -381,6 +305,14 @@ namespace IOB_WIN_NEXT #endregion Public Methods + #region Protected Fields + + protected int vetoReadDbSec = 3; + + protected int VetoSyncDbSec = 20; + + #endregion Protected Fields + #region Protected Properties protected DbController dbProxy { get; set; } = null; @@ -415,8 +347,7 @@ namespace IOB_WIN_NEXT } } - /// Esegue task IMPORT (MES PODL to MACHINE) - /// + /// Esegue task IMPORT (MES PODL to MACHINE) private void execImportAll() { Stopwatch sw = new Stopwatch(); @@ -435,6 +366,223 @@ namespace IOB_WIN_NEXT } } + /// + /// Esegue processing + invio dati tab SignLog + /// + /// + /// + private bool processFluxLogTable(DateTime adesso) + { + bool fatto = false; + string tabNameIn = "FluxLog"; + string tabNameOut = "FluxLogToMes"; + // cerco info sui SignLog (ToMes e letti) + var currSignLogRead = elencoSyncState.FirstOrDefault(x => x.TableName == tabNameIn); + // se nullo inizializzo + if (currSignLogRead == null) + { + currSignLogRead = new SyncStateModel() + { + LastIdx = 0, + LastUpdate = adesso, + TableName = tabNameIn, + Note = "Init" + }; + } + var currSignLogSent = elencoSyncState.FirstOrDefault(x => x.TableName == tabNameOut); + // se nullo inizializzo + if (currSignLogSent == null) + { + currSignLogSent = new SyncStateModel() + { + LastIdx = 0, + LastUpdate = adesso, + TableName = tabNameOut, + Note = "Init" + }; + } + // verifica se ci siano dati da trasmettere (sui valori LastIdx) + if (currSignLogRead.LastIdx > currSignLogSent.LastIdx) + { + // recupero i dati dal DB... + var data2send = dbProxy.MachFluxLogGetNew(currSignLogSent.LastIdx); + // se ho dati preparo invio + if (data2send != null && data2send.Count > 0) + { + string sVal = ""; + foreach (var fLog2send in data2send) + { + sVal = $"[DYNDATA] |{fLog2send.DtEvento:yyyy-MM-dd HH:mm:ss}|{fLog2send.CodFlux}|{fLog2send.Valore}"; + // chiamo accodamento con dataora corretta... + accodaFLog(sVal, qEncodeFLog(fLog2send.DtEvento, fLog2send.CodFlux, fLog2send.Valore)); + } + fatto = true; + } + + // aggiorno idx inviato... + currSignLogSent.LastIdx = currSignLogRead.LastIdx; + } + + // alla fine aggiorno i dati inviati! + dbProxy.SyncStateUpsert(currSignLogSent); + + return fatto; + } + + /// + /// Esegue processing + invio dati tab ProdData + /// + /// + /// + private bool processProdDataTable(DateTime adesso) + { + bool fatto = false; + string tabNameIn = "ProdData"; + string tabNameOut = "ProdDataToMes"; + // cerco info sui SignLog (ToMes e letti) + var currSignLogRead = elencoSyncState.FirstOrDefault(x => x.TableName == tabNameIn); + // se nullo inizializzo + if (currSignLogRead == null) + { + currSignLogRead = new SyncStateModel() + { + LastIdx = 0, + LastUpdate = adesso, + TableName = tabNameIn, + Note = "Init" + }; + } + var currSignLogSent = elencoSyncState.FirstOrDefault(x => x.TableName == tabNameOut); + // se nullo inizializzo + if (currSignLogSent == null) + { + currSignLogSent = new SyncStateModel() + { + LastIdx = 0, + LastUpdate = adesso, + TableName = tabNameOut, + Note = "Init" + }; + } + // verifica se ci siano dati da trasmettere (sui valori LastIdx) + if (currSignLogRead.LastIdx > currSignLogSent.LastIdx) + { + // recupero i dati dal DB... + var data2send = dbProxy.MachProdDataGetNew(currSignLogSent.LastIdx); + // se ho dati preparo invio + if (data2send != null && data2send.Count > 0) + { + foreach (var sLog2send in data2send) + { +#if false + string currVal = getEncodSigLog(sLog2send.DtEve, sLog2send.ValInt, counterSigIN); + // --> accodo (valore già formattato)! + QueueIN.Enqueue(currVal); + // loggo! + lgTrace(string.Format("[QUEUE-IN] {0}", currVal)); + counterSigIN++; + if (counterSigIN > 9999) + { + counterSigIN = 0; + } +#endif + } + } + + // aggiorno idx inviato... + currSignLogSent.LastIdx = currSignLogRead.LastIdx; +#if false + var lastRec = data2send.LastOrDefault(); + if (lastRec != null) + { + // salvo in B_input ultimo valore letto... + B_input = lastRec.ValInt; + } +#endif + fatto = true; + } + + // alla fine aggiorno i dati inviati! + dbProxy.SyncStateUpsert(currSignLogSent); + + return fatto; + } + + /// + /// Esegue processing + invio dati tab SignLog + /// + /// + /// + private bool processSignLogTable(DateTime adesso) + { + bool fatto = false; + string tabNameIn = "SignLog"; + string tabNameOut = "SignLogToMes"; + // cerco info sui SignLog (ToMes e letti) + var currSignLogRead = elencoSyncState.FirstOrDefault(x => x.TableName == tabNameIn); + // se nullo inizializzo + if (currSignLogRead == null) + { + currSignLogRead = new SyncStateModel() + { + LastIdx = 0, + LastUpdate = adesso, + TableName = tabNameIn, + Note = "Init" + }; + } + var currSignLogSent = elencoSyncState.FirstOrDefault(x => x.TableName == tabNameOut); + // se nullo inizializzo + if (currSignLogSent == null) + { + currSignLogSent = new SyncStateModel() + { + LastIdx = 0, + LastUpdate = adesso, + TableName = tabNameOut, + Note = "Init" + }; + } + // verifica se ci siano dati da trasmettere (sui valori LastIdx) + if (currSignLogRead.LastIdx > currSignLogSent.LastIdx) + { + // recupero i dati dal DB... + var data2send = dbProxy.MachSigLogGetNew(currSignLogSent.LastIdx); + // se ho dati preparo invio + if (data2send != null && data2send.Count > 0) + { + foreach (var sLog2send in data2send) + { + string currVal = getEncodSigLog(sLog2send.DtEve, sLog2send.ValInt, counterSigIN); + // --> accodo (valore già formattato)! + QueueIN.Enqueue(currVal); + // loggo! + lgTrace(string.Format("[QUEUE-IN] {0}", currVal)); + counterSigIN++; + if (counterSigIN > 9999) + { + counterSigIN = 0; + } + } + } + + // aggiorno idx inviato... + currSignLogSent.LastIdx = currSignLogRead.LastIdx; + var lastRec = data2send.LastOrDefault(); + if (lastRec != null) + { + // salvo in B_input ultimo valore letto... + B_input = lastRec.ValInt; + } + fatto = true; + } + + // alla fine aggiorno i dati inviati! + dbProxy.SyncStateUpsert(currSignLogSent); + + return fatto; + } + #endregion Private Methods } } \ No newline at end of file