From a0eeac59f04e2f23c1d29cc960c5a13544ee040e Mon Sep 17 00:00:00 2001 From: Samuele Locatelli Date: Wed, 15 Apr 2026 18:41:22 +0200 Subject: [PATCH] Aggiunta metodo GetIdlePeriod --- MP.Core/Utils.cs | 1 + MP.Data/Controllers/MpIocController.cs | 28 +++++++++--- MP.Data/Services/StatusData.cs | 8 +++- MP.IOC/Controllers/IOBController.cs | 40 ++++++++-------- MP.IOC/Data/MpDataService.cs | 63 +++++++++++++++++++++++++- 5 files changed, 113 insertions(+), 27 deletions(-) diff --git a/MP.Core/Utils.cs b/MP.Core/Utils.cs index e1c13b99..445dd7d7 100644 --- a/MP.Core/Utils.cs +++ b/MP.Core/Utils.cs @@ -11,6 +11,7 @@ namespace MP.Core public const string redisAKVKey = redisBaseAddr + "Cache:AKV"; public const string redisAnagGruppi = redisBaseAddr + "Cache:AnagGruppi"; + public const string redisAnagStati = redisBaseAddr + "Cache:AnagStati"; public const string redisArtByDossier = redisBaseAddr + "Cache:ArtByDossier"; diff --git a/MP.Data/Controllers/MpIocController.cs b/MP.Data/Controllers/MpIocController.cs index f0d5556b..a70f7c09 100644 --- a/MP.Data/Controllers/MpIocController.cs +++ b/MP.Data/Controllers/MpIocController.cs @@ -55,6 +55,22 @@ namespace MP.Data.Controllers } return fatto; } + /// + /// Restituisce l'anagrafica STATI per intero + /// + /// + public async Task> AnagStatiGetAllAsync() + { + List dbResult = new List(); + using (var dbCtx = new MoonProContext(_configuration)) + { + dbResult = await dbCtx + .DbSetAnagStati + .AsNoTracking() + .ToListAsync(); + } + return dbResult; + } /// /// Elenco tabella decodifica articoli / codice decimale @@ -612,18 +628,18 @@ namespace MP.Data.Controllers /// Elenco da tabella MappaStatoExplModel /// /// - public List MseGetAll(int maxAge = 2000) + public async Task> MseGetAllAsync(int maxAge = 2000) { List dbResult = new List(); using (var dbCtx = new MoonProContext(_configuration)) { var maxAgeSec = new SqlParameter("@maxAgeSec", maxAge); - dbResult = dbCtx - .DbSetMSE - .FromSqlRaw("EXEC stp_MSE_getData @maxAgeSec", maxAgeSec) - .AsNoTracking() - .ToList(); + dbResult = await dbCtx + .DbSetMSE + .FromSqlRaw("EXEC stp_MSE_getData @maxAgeSec", maxAgeSec) + .AsNoTracking() + .ToListAsync(); } return dbResult; } diff --git a/MP.Data/Services/StatusData.cs b/MP.Data/Services/StatusData.cs index 7c86ac9c..02b0b848 100644 --- a/MP.Data/Services/StatusData.cs +++ b/MP.Data/Services/StatusData.cs @@ -320,6 +320,10 @@ namespace MP.Data.Services return answ; } + /// + /// Elenco da tabella MappaStatoExplModel + /// + /// public async Task> MseGetAll(bool forceDb = false) { Stopwatch sw = new Stopwatch(); @@ -327,10 +331,10 @@ namespace MP.Data.Services sw.Start(); List? result = new List(); // cerco in _redisConn... - RedisValue rawData = redisDb.StringGet(Constants.redisMseKey); + RedisValue rawData = await redisDb.StringGetAsync(Constants.redisMseKey); if (rawData.HasValue && !forceDb) { - result = JsonConvert.DeserializeObject>($"{rawData}"); + result = JsonConvert.DeserializeObject>($"{rawData}") ?? new(); source = "REDIS"; } else diff --git a/MP.IOC/Controllers/IOBController.cs b/MP.IOC/Controllers/IOBController.cs index 723bd34e..6130ef73 100644 --- a/MP.IOC/Controllers/IOBController.cs +++ b/MP.IOC/Controllers/IOBController.cs @@ -483,7 +483,6 @@ namespace MP.IOC.Controllers return Ok(answ); } -#if false /// /// Restituisce il valore dello stato di IDLE della macchina, quindi SOLO SE NON é in lavoro /// e già convertito in minuti... @@ -492,38 +491,43 @@ namespace MP.IOC.Controllers /// /// [HttpGet("getIdlePeriod/{id}")] - public int GetIdlePeriod(string id) + public async Task GetIdlePeriod(string id) { - // attenzione! poiché nell'URL il carattere "#" viene filtrato ci aspettiamo il - // carattere "|" che poi trasformiamo ora in "#" + if (string.IsNullOrEmpty(id)) return BadRequest("Missing ID"); + // Multi: gestione carattere "|" trasformato in "#" id = id.Replace("|", "#"); int answ = 0; - DataLayer DataLayerObj = new DataLayer(); - DS_applicazione.StatoMacchineDataTable currData = null; - // chiamo metodo redis/db... + + // chiamo metodo x avere stato macchina... try { - currData = DataLayerObj.currSMTab(id); - if (currData.Count > 0) + var mseData = await DService.MseGetAllAsync(); + if (mseData.Count > 0) { - // recupero da redis elenco stati - DS_applicazione.AnagraficaStatiDataTable anagStati = DataLayerObj.AnagraficaStati(); - DS_applicazione.AnagraficaStatiRow currStato = anagStati.FindByIdxStato(currData[0].IdxStato); - // calcolo SE sia idle... OVVERO SEMAFORO NON VERDE!!! - if (currStato.Semaforo != "sVe") + var currRec = mseData.FirstOrDefault(x => x.IdxMacchina == id); + if (currRec != null) { - // calcolo durata... - answ = (int)DateTime.Now.Subtract(currData[0].InizioStato).TotalMinutes; + // recupero da redis elenco stati + var anagStati = await DService.AnagStatiGetAllAsync(); + var currStato = anagStati.FirstOrDefault(x => x.IdxStato == currRec.IdxStato); + // calcolo SE sia idle... OVVERO SEMAFORO NON VERDE!!! + if (currStato != null && currStato.Semaforo != "sVe") + { + // calcolo durata... + answ = (int)(currRec.Durata ?? 0); + } } } } catch (Exception exc) { - logger.lg.scriviLog($"Eccezione in recupero getIdlePeriod{Environment.NewLine}{exc}", tipoLog.EXCEPTION); + Log.Error($"Errore in GetIdlePeriod{Environment.NewLine}{exc}"); + return StatusCode(StatusCodes.Status500InternalServerError, "NO"); } - return answ; + return Ok(answ); } +#if false /// /// Recupera codice numerico ODL/PODL dato CodXdl, in pratica la parte finale SENZA ODL/PODL /// Funzione per impianti che accettano solo INT in scrittura: diff --git a/MP.IOC/Data/MpDataService.cs b/MP.IOC/Data/MpDataService.cs index e1d014d8..2fd83ab9 100644 --- a/MP.IOC/Data/MpDataService.cs +++ b/MP.IOC/Data/MpDataService.cs @@ -1,4 +1,5 @@ -using MP.Core.Conf; +using Microsoft.EntityFrameworkCore; +using MP.Core.Conf; using MP.Core.DTO; using MP.Core.Objects; using MP.Data; @@ -263,6 +264,66 @@ namespace MP.IOC.Data return answ; } + /// + /// Restituisce l'anagrafica STATI per intero + /// + /// + public async Task> AnagStatiGetAllAsync() + { + List dbResult = new List(); + // cerco in redis... + var currKey = Utils.redisAnagStati; + RedisValue rawData = await redisDb.StringGetAsync(currKey); + if (rawData.HasValue) + { + dbResult = JsonConvert.DeserializeObject>($"{rawData}") ?? new(); + } + else + { + dbResult = await IocDbController.AnagStatiGetAllAsync(); + // serializzo e salvo... + rawData = JsonConvert.SerializeObject(dbResult); + await redisDb.StringSetAsync(currKey, rawData, getRandTOut(redisLongTimeCache)); + } + return dbResult; + } + /// + /// MS max age x dato MSE + /// + private int maxAge = 2000; + /// + /// Elenco da tabella MappaStatoExplModel + /// + /// + public async Task> MseGetAllAsync(bool forceDb = false) + { + Stopwatch sw = new Stopwatch(); + string source = "DB"; + sw.Start(); + List? result = new List(); + // cerco in _redisConn... + RedisValue rawData = await redisDb.StringGetAsync(Constants.redisMseKey); + if (rawData.HasValue && !forceDb) + { + result = JsonConvert.DeserializeObject>($"{rawData}") ?? new(); + source = "REDIS"; + } + else + { + result = await IocDbController.MseGetAllAsync(maxAge); + // serializzp e salvo... + rawData = JsonConvert.SerializeObject(result); + await redisDb.StringSetAsync(Constants.redisMseKey, rawData, getRandTOut(redisShortTimeCache / 2)); + } + if (result == null) + { + result = new List(); + } + sw.Stop(); + Log.Debug($"MseGetAllAsync | {source} | {sw.Elapsed.TotalMilliseconds}ms"); + return result; + } + public async Task> AnagStatiComm() { Stopwatch stopWatch = new Stopwatch();