From 5dab9100da31d97056d9aa25c4eb1efc49b846ca Mon Sep 17 00:00:00 2001 From: Samuele Locatelli Date: Wed, 6 May 2026 10:08:09 +0200 Subject: [PATCH] Ulteriore ottimizzazione codice IOC x caching in memoria e obj scoped (da testare...) --- MP.Data/Repository/IOC/IIocRepository.cs | 8 ++ MP.Data/Repository/IOC/IocRepository.cs | 17 +++ MP.Data/Services/IOC/IIocService.cs | 31 +++++ MP.Data/Services/IOC/IocService.cs | 146 ++++++++++++++++++++++- MP.IOC/Controllers/IOBController.cs | 15 ++- MP.IOC/Data/MpDataService.cs | 8 +- MP.IOC/MP.IOC.csproj | 2 +- MP.IOC/Resources/ChangeLog.html | 2 +- MP.IOC/Resources/VersNum.txt | 2 +- MP.IOC/Resources/manifest.xml | 2 +- 10 files changed, 217 insertions(+), 16 deletions(-) diff --git a/MP.Data/Repository/IOC/IIocRepository.cs b/MP.Data/Repository/IOC/IIocRepository.cs index cfa5824f..19ad3f4a 100644 --- a/MP.Data/Repository/IOC/IIocRepository.cs +++ b/MP.Data/Repository/IOC/IIocRepository.cs @@ -102,6 +102,14 @@ namespace MP.Data.Repository.IOC /// Task> StateMachineIngressiAsync(int idxFam); + /// + /// Stato prod macchina (completo) Async + /// + /// + /// + /// + Task StatoProdMacchinaAsync(string idxMacchina, DateTime dtReq); + /// /// Vista v_MSFD x singola macchina (da stored) - singolo record /// diff --git a/MP.Data/Repository/IOC/IocRepository.cs b/MP.Data/Repository/IOC/IocRepository.cs index de11638e..2a13858d 100644 --- a/MP.Data/Repository/IOC/IocRepository.cs +++ b/MP.Data/Repository/IOC/IocRepository.cs @@ -316,6 +316,23 @@ namespace MP.Data.Repository.IOC return dbResult; } + /// + public async Task StatoProdMacchinaAsync(string idxMacchina, DateTime dtReq) + { + await using var dbCtx = await CreateContextAsync(); + + var IdxMacchina = new SqlParameter("@IdxMacchina", idxMacchina); + var DataOra = new SqlParameter("@DataOra ", dtReq); + var dbResult = (await dbCtx + .DbSetStatoProd + .FromSqlRaw("EXEC stp_StatoProd_getByMacchina @IdxMacchina, @DataOra ", IdxMacchina, DataOra) + .AsNoTracking() + .ToListAsync()) + .FirstOrDefault(); + + return dbResult; + } + /// public async Task VMSFDGetByMaccAsync(string idxMacc) { diff --git a/MP.Data/Services/IOC/IIocService.cs b/MP.Data/Services/IOC/IIocService.cs index 3f3b48a0..8e7f6a76 100644 --- a/MP.Data/Services/IOC/IIocService.cs +++ b/MP.Data/Services/IOC/IIocService.cs @@ -1,5 +1,6 @@ using MP.Data.DbModels; using System; +using System.Collections.Generic; using System.Threading.Tasks; using static MP.Core.Objects.Enums; @@ -31,6 +32,21 @@ namespace MP.Data.Services.IOC /// Task EvListMicroStatoInsertAsync(MicroStatoMacchinaModel newRecMsm, EventListModel newRecEv); + /// + /// Restituisce il valOut dell'ODL corrente (ODL deve esserci per gestione contapezzi, senza + /// ODL NO invio/gestione ODL) + /// + /// + /// + Task GetCurrOdlAsync(string idxMacchina); + + /// + /// Restitusice elenco KVP dei TASK (da passare a IOB-WIN) per l'impianto indicato + /// + /// + /// + Task> GetTask2ExeMacchinaAsync(string idxMacchina); + /// /// Restituisce il valOut booleano se la macchina sia abilitata all'input /// @@ -56,6 +72,21 @@ namespace MP.Data.Services.IOC /// Task ProcessInputAsync(string idxMacchina, string valore, string dtEve, string dtCurr, string contatore); + /// + /// Restituisce il contapezzi come CONTEGGIO da TCRilevati per la macchina - ASYNC + /// + /// + /// + Task PzCounterTcAsync(string idxMacchina); + + /// + /// Processa registrazione di un counter x una data macchina IOB + /// + /// + /// contapezzi + /// + Task SaveCounterAsync(string idxMacchina, string counter); + /// /// scrive un evento di keepalive sulla tabella /// diff --git a/MP.Data/Services/IOC/IocService.cs b/MP.Data/Services/IOC/IocService.cs index 7d9fed11..d4041c5f 100644 --- a/MP.Data/Services/IOC/IocService.cs +++ b/MP.Data/Services/IOC/IocService.cs @@ -53,6 +53,34 @@ namespace MP.Data.Services.IOC return success; } + /// + public async Task GetCurrOdlAsync(string idxMacchina) + { + string result = ""; + string currKey = $"{MP.Data.Utils.redisOdlCurrByMac}:{idxMacchina}"; + // cerco in redis dato valOut sel macchina... + RedisValue rawData = _redisDb.StringGet(currKey); + if (rawData.HasValue) + { + result = $"{rawData}"; + } + else + { + result = await GetCurrOdlByProdAsync(idxMacchina); + // serializzo e salvo... + rawData = JsonConvert.SerializeObject(result); + _redisDb.StringSet(currKey, rawData, getRandTOut(redisLongTimeCache)); + } + return result; + } + + /// + public async Task> GetTask2ExeMacchinaAsync(string idxMacchina) + { + var currHash = MP.Data.Utils.RedKeyTask2ExeMacc(idxMacchina, MpIoNS); + return await RedisGetHashDictAsync(currHash); + } + /// public async Task IobInsEnabAsync(string idxMacchina) { @@ -84,7 +112,7 @@ namespace MP.Data.Services.IOC public async Task ProcessInputAsync(string idxMacchina, string valore, string dtEve, string dtCurr, string contatore) { string answ = ""; - if (ValidateinputParams(idxMacchina, valore, dtEve, dtCurr)) + if (ValidateInputParams(idxMacchina, valore, dtEve, dtCurr)) { DateTime dataOraEvento = ParseEventTime(dtEve, dtCurr); @@ -137,6 +165,66 @@ namespace MP.Data.Services.IOC return answ; } + /// + public async Task PzCounterTcAsync(string idxMacchina) + { + int answ = -1; + DateTime dataRif = DateTime.Now; + var datiProd = await StatoProdMacchinaAsync(idxMacchina, dataRif); + if (datiProd != null) + { + answ = datiProd.PzTotODL; + } + return answ; + } + + /// + public async Task SaveCounterAsync(string idxMacchina, string counter) + { + string answ = "0"; + // inizio processing vero e proprio INPUT... + if (string.IsNullOrEmpty(idxMacchina)) + { + string errore = "Errore: parametro macchina vuoto"; + Log.Warn(errore); + answ = errore; + } + else + { + if (string.IsNullOrEmpty(counter)) + { + string errore = "Errore: parametro counter vuoto"; + Log.Warn(errore); + answ = errore; + } + else + { + int newCounter = -1; + int.TryParse(counter, out newCounter); + // se il conteggio è >= 0 SALVO come nuovo conteggio... + if (newCounter >= 0) + { + var currKey = MP.Data.Utils.RedKeyPzCount(idxMacchina, MpIoNS); + RedisValue rawData = await _redisDb.StringGetAsync(currKey); + if (rawData.HasValue) + { + // salvo per + tempo... + await _redisDb.StringSetAsync(currKey, answ.ToString()); + answ = counter; + } + else + { + int currCount = await PzCounterTcAsync(idxMacchina); + answ = currCount.ToString(); + // salvo per meno tempo... + await _redisDb.StringSetAsync(currKey, answ); + } + } + } + } + return answ; + } + /// public async Task ScriviKeepAliveAsync(string IdxMacchina, DateTime oraMacchina) { @@ -342,6 +430,26 @@ namespace MP.Data.Services.IOC return s?.Length > 17 ? s[..17] : s?.PadRight(17, '0') ?? string.Empty; } + /// + /// Recupero info ODL corrente da dati prod macchina + /// + /// + /// + private async Task GetCurrOdlByProdAsync(string idxMacchina) + { + string answ = ""; + // recupero stato... + var datiProd = await StatoProdMacchinaAsync(idxMacchina, DateTime.Now); + if (datiProd != null) + { + answ = datiProd.IdxOdl.ToString(); + } + // ultimo controllo su idxOdl... + answ = answ == "" ? "0" : answ; + // restituisco! + return answ; + } + /// /// Implementa gestione recupero cache da memoria o da obj esterno + cache memoria /// @@ -897,6 +1005,40 @@ namespace MP.Data.Services.IOC return answ; } + /// + /// Stato prod macchina (completo) + /// + /// + /// + /// + private async Task StatoProdMacchinaAsync(string idxMacchina, DateTime dtReq, bool forceDb = false) + { + string cacheKey = $"IOC_StatoProd_{idxMacchina}"; + return await GetOrFetchAsync(cacheKey, async () => + { + StatoProdModel? result = new StatoProdModel(); + // cerco in _redisConn... + string currKey = $"{MP.Data.Utils.redisStatoProd}:{idxMacchina}:{dtReq:HHmm}"; + RedisValue rawData = await _redisDb.StringGetAsync(currKey); + if (rawData.HasValue && !forceDb) + { + result = JsonConvert.DeserializeObject($"{rawData}"); + } + else + { + result = await _repo.StatoProdMacchinaAsync(idxMacchina, dtReq); + // serializzo e salvo... + rawData = JsonConvert.SerializeObject(result); + await _redisDb.StringSetAsync(currKey, rawData, TimeSpan.FromSeconds(30)); + } + if (result == null) + { + result = new StatoProdModel(); + } + return result; + }, TimeSpan.FromSeconds(10)); + } + /// /// Restituisce valOut della stringa (SE disponibile) /// @@ -923,7 +1065,7 @@ namespace MP.Data.Services.IOC /// /// /// - private bool ValidateinputParams(string idxMacchina, string valore, string dtEve, string dtCurr) + private bool ValidateInputParams(string idxMacchina, string valore, string dtEve, string dtCurr) { bool isValid = false; // preparo stringa valori correnti diff --git a/MP.IOC/Controllers/IOBController.cs b/MP.IOC/Controllers/IOBController.cs index b30d13ce..4a486ff9 100644 --- a/MP.IOC/Controllers/IOBController.cs +++ b/MP.IOC/Controllers/IOBController.cs @@ -429,7 +429,8 @@ namespace MP.IOC.Controllers id = id.Replace("|", "#"); try { - var pzCount = await DService.pzCounterTcAsync(id); + var pzCount = await IOCService.PzCounterTcAsync(id); + //var pzCount = await DService.PzCounterTcAsync(id); return Ok(pzCount); } catch (Exception exc) @@ -478,7 +479,8 @@ namespace MP.IOC.Controllers id = id.Replace("|", "#"); try { - var odl = await DService.GetCurrOdlAsync(id); + var odl = await IOCService.GetCurrOdlAsync(id); + //var odl = await DService.GetCurrOdlAsync(id); return Ok($"{odl}"); } catch (Exception exc) @@ -815,7 +817,8 @@ namespace MP.IOC.Controllers try { await DService.ScriviKeepAliveAsync(id, DateTime.UtcNow); - var valori = await DService.GetTask2ExeMacchinaAsync(id); + var valori = await IOCService.GetTask2ExeMacchinaAsync(id); + //var valori = await DService.GetTask2ExeMacchinaAsync(id); if (valori == null || valori.Count == 0) return Ok(new Dictionary()); @@ -1206,8 +1209,7 @@ namespace MP.IOC.Controllers /// /// SALVA Counter x macchina restituendo il valore appena inviato o, se mancasse chiave - /// redis, del valore da DB - /// + /// redis, valore da DB /// /// GET: IOB/setCounter/5?counter=10 /// /// cod macchina @@ -1226,7 +1228,8 @@ namespace MP.IOC.Controllers Log.Debug($"Salvataggio counter | id: {id} | pzCount: {counter}"); try { - answ = await DService.saveCounterAsync(id, counter); + answ = await IOCService.SaveCounterAsync(id, counter); + //answ = await DService.SaveCounterAsync(id, counter); } catch (Exception exc) { diff --git a/MP.IOC/Data/MpDataService.cs b/MP.IOC/Data/MpDataService.cs index 6dc5d273..abce85b4 100644 --- a/MP.IOC/Data/MpDataService.cs +++ b/MP.IOC/Data/MpDataService.cs @@ -3147,7 +3147,7 @@ namespace MP.IOC.Data } else { - answ = await pzCounterTcAsync(idxMacchina); + answ = await PzCounterTcAsync(idxMacchina); // salvo in _redisConn... await redisDb.StringSetAsync(currKey, answ.ToString(), TimeSpan.FromSeconds(1)); } @@ -3164,7 +3164,7 @@ namespace MP.IOC.Data /// /// /// - public async Task pzCounterTcAsync(string idxMacchina) + public async Task PzCounterTcAsync(string idxMacchina) { int answ = -1; DateTime dataRif = DateTime.Now; @@ -3741,7 +3741,7 @@ namespace MP.IOC.Data /// /// contapezzi /// - public async Task saveCounterAsync(string idxMacchina, string counter) + public async Task SaveCounterAsync(string idxMacchina, string counter) { string answ = "0"; // inizio processing vero e proprio INPUT... @@ -3776,7 +3776,7 @@ namespace MP.IOC.Data } else { - int currCount = await pzCounterTcAsync(idxMacchina); + int currCount = await PzCounterTcAsync(idxMacchina); answ = currCount.ToString(); // salvo per meno tempo... await redisDb.StringSetAsync(currKey, answ); diff --git a/MP.IOC/MP.IOC.csproj b/MP.IOC/MP.IOC.csproj index f2baf173..216a00d5 100644 --- a/MP.IOC/MP.IOC.csproj +++ b/MP.IOC/MP.IOC.csproj @@ -4,7 +4,7 @@ net8.0 enable enable - 8.16.2605.609 + 8.16.2605.610 diff --git a/MP.IOC/Resources/ChangeLog.html b/MP.IOC/Resources/ChangeLog.html index 8e8d8134..e707de72 100644 --- a/MP.IOC/Resources/ChangeLog.html +++ b/MP.IOC/Resources/ChangeLog.html @@ -1,6 +1,6 @@ Modulo MP-IOC -

Versione: 8.16.2605.609

+

Versione: 8.16.2605.610


Note di rilascio:
  • diff --git a/MP.IOC/Resources/VersNum.txt b/MP.IOC/Resources/VersNum.txt index 0a727783..311dfe39 100644 --- a/MP.IOC/Resources/VersNum.txt +++ b/MP.IOC/Resources/VersNum.txt @@ -1 +1 @@ -8.16.2605.609 +8.16.2605.610 diff --git a/MP.IOC/Resources/manifest.xml b/MP.IOC/Resources/manifest.xml index 0498bfc7..3dbadca7 100644 --- a/MP.IOC/Resources/manifest.xml +++ b/MP.IOC/Resources/manifest.xml @@ -1,6 +1,6 @@ - 8.16.2605.609 + 8.16.2605.610 https://nexus.steamware.net/repository/SWS/MP-IOC/stable/LAST/MP.IOC.zip https://nexus.steamware.net/repository/SWS/MP-IOC/stable/LAST/ChangeLog.html false