From 76d3d6f9e66e02f68ca35eb8ff80436ced4d8279 Mon Sep 17 00:00:00 2001 From: Samuele Locatelli Date: Sat, 11 Apr 2026 10:43:02 +0200 Subject: [PATCH] Aggiunta metodo getCurrODL --- MP.IOC/Controllers/IOBController.cs | 69 ++++++-------------------- MP.IOC/Data/MpDataService.cs | 76 ++++++++++++++++++++++++++++- 2 files changed, 89 insertions(+), 56 deletions(-) diff --git a/MP.IOC/Controllers/IOBController.cs b/MP.IOC/Controllers/IOBController.cs index 3f7552d1..f362f726 100644 --- a/MP.IOC/Controllers/IOBController.cs +++ b/MP.IOC/Controllers/IOBController.cs @@ -69,21 +69,6 @@ namespace MP.IOC.Controllers //return StatusCode(503, "NO"); return UnprocessableEntity("NO"); } -#if false - if (string.IsNullOrEmpty(id)) return Ok("KO"); - - try - { - var result = DService.IobInsEnab(id) ? "OK" : "NO"; - // Logga solo l'esito se necessario - return Ok(result); - } - catch (Exception exc) - { - Log.Error($"Errore in enabled {id}: {exc.Message}"); - return Ok("NO"); - } -#endif } /// @@ -127,8 +112,7 @@ namespace MP.IOC.Controllers { if (string.IsNullOrEmpty(id)) return BadRequest("Missing ID"); - // attenzione! poiché nell'URL il carattere "#" viene filtrato ci aspettiamo il - // carattere "|" che poi trasformiamo ora in "#" + // Multi: gestione carattere "|" trasformato in "#" id = id.Replace("|", "#"); try { @@ -138,61 +122,38 @@ namespace MP.IOC.Controllers catch (Exception exc) { Log.Error(exc, "Errore durante il recupero del counter TC per la macchina {MachineId}", id); + //return StatusCode(StatusCodes.Status500InternalServerError, "NO"); return StatusCode(StatusCodes.Status500InternalServerError, "Errore interno | GetCounterTCRec"); } } -#if false /// - /// Recupera COUNTER x macchina: - /// - /// GET: IOB/getCounter/5 + /// Recupera ODL corrente x macchina: + /// GET: IOB/getCurrODL/SIMUL_03 /// /// /// - [HttpGet("getCounter/{id}")] - public async Task getCounter(string id) + [HttpGet("getCurrODL/{id}")] + public async Task GetCurrODL(string id) { - string answ = ""; - try - { - var pzCount = await _tabDService.pzCounter(id); - answ = $"{pzCount}"; - } - catch (Exception exc) - { - Log.Error($"Errore in counter TC (get){Environment.NewLine}{exc}"); - answ = "NO"; - } - return answ; - } - /// - /// Recupera COUNTER x macchina dal CONTEGGIO dei TCRecorded: - /// - /// GET: IOB/getCounterTCRec/5 - /// - /// - /// - [HttpGet("getCounterTCRec/{id}")] - public async Task getCounterTCRec(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("|", "#"); string answ = ""; try { - var pzCountTC = await _tabDService.pzCounterTC(id); - answ = $"{pzCountTC}"; + var odl = await DService.GetCurrOdlAsync(id); + answ = $"{odl}"; + return Ok(answ); } catch (Exception exc) { - Log.Error($"Errore in counter TC (get){Environment.NewLine}{exc}"); - answ = "NO"; + Log.Error(exc, "Errore GetCurrODL | macchina {MachineId}", id); + return StatusCode(StatusCodes.Status500InternalServerError, "NO"); + //return StatusCode(StatusCodes.Status500InternalServerError, "Errore interno | GetCurrODL"); } - return answ; } -#endif /// /// Recupera TASK richiesto x macchina: diff --git a/MP.IOC/Data/MpDataService.cs b/MP.IOC/Data/MpDataService.cs index 9b3ae5a1..d6141826 100644 --- a/MP.IOC/Data/MpDataService.cs +++ b/MP.IOC/Data/MpDataService.cs @@ -761,6 +761,58 @@ namespace MP.IOC.Data return result; } + /// + /// Restituisce il valore dell'ODL corrente (ODL deve esserci per gestione contapezzi, senza + /// ODL NO invio/gestione ODL) + /// + /// + /// + public async Task GetCurrOdlAsync(string idxMacchina) + { + string result = ""; + string currKey = $"{Utils.redisOdlCurrByMac}:{idxMacchina}"; + // cerco in redis dato valore 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; + } + + /// + /// Restituisce il valore dell'ODL corrente (ODL deve esserci per gestione contapezzi, senza + /// ODL NO invio/gestione ODL) + /// + /// + /// indica se forzare lettura da db (true) o meno + /// + public async Task GetCurrOdlAsync(string idxMacchina, bool forceDb) + { + string answ = ""; + // se ho forceDB leggo dai dati prod... + if (forceDb) + { + var datiProd = await StatoProdMacchinaAsync(idxMacchina, DateTime.Now, true); + if (datiProd != null) + { + answ = datiProd.IdxOdl.ToString(); + } + } + else + { + answ = await GetCurrOdlAsync(idxMacchina); + } + return answ; + } + /// /// Init ricetta /// @@ -2194,6 +2246,26 @@ namespace MP.IOC.Data #region Private Methods + /// + /// 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; + } + private async Task POdlFlushCache() { bool answ = false; @@ -2223,7 +2295,7 @@ namespace MP.IOC.Data /// /// /// - private async Task StatoProdMacchinaAsync(string idxMacchina, DateTime dtReq) + private async Task StatoProdMacchinaAsync(string idxMacchina, DateTime dtReq, bool forceDb = false) { int rndWait = rnd.Next(0, 2); await Task.Delay(rndWait); @@ -2234,7 +2306,7 @@ namespace MP.IOC.Data string currKey = $"{Utils.redisStatoProd}:{idxMacchina}:{dtReq:HHmm}"; RedisValue rawData = await redisDb.StringGetAsync(currKey); //if (!string.IsNullOrEmpty($"{rawData}")) - if (rawData.HasValue) + if (rawData.HasValue && !forceDb) { result = JsonConvert.DeserializeObject($"{rawData}"); source = "REDIS";