diff --git a/MP.Data/Controllers/MpIocController.cs b/MP.Data/Controllers/MpIocController.cs
index 73a081d3..41483f33 100644
--- a/MP.Data/Controllers/MpIocController.cs
+++ b/MP.Data/Controllers/MpIocController.cs
@@ -525,6 +525,24 @@ namespace MP.Data.Controllers
return dbResult;
}
+ ///
+ /// Intera tabella relazione master/slave in machine (gestione setup master --> slave)
+ ///
+ ///
+ public async Task> Macchine2SlaveAsync()
+ {
+ List dbResult = new List();
+ using (var dbCtx = new MoonProContext(_configuration))
+ {
+ dbResult = await dbCtx
+ .DbSetM2S
+ .AsNoTracking()
+ .OrderBy(x => x.IdxMacchina)
+ .ToListAsync();
+ }
+ return dbResult;
+ }
+
///
/// Elenco Record Macchine
///
@@ -1419,6 +1437,27 @@ namespace MP.Data.Controllers
return dbResult;
}
+ ///
+ /// Vista v_MSFD x singola macchina (da stored) - singolo record
+ ///
+ ///
+ ///
+ public async Task> VMSFDGetByMaccAsync(string idxMacc)
+ {
+ List dbResult = new List();
+ using (var dbCtx = new MoonProContext(_configuration))
+ {
+ var IdxMacchina = new SqlParameter("@pIdxMacchina", idxMacc);
+
+ dbResult = await dbCtx
+ .DbSetMSFD
+ .FromSqlRaw("exec dbo.stp_MSFD_getMacc @pIdxMacchina", IdxMacchina)
+ .AsNoTracking()
+ .ToListAsync();
+ }
+ return dbResult;
+ }
+
///
/// Vista v_MSFD delle machine MULTI filtrato x macchina (da stored)
///
diff --git a/MP.IOC/Controllers/IOBController.cs b/MP.IOC/Controllers/IOBController.cs
index f283a8d7..5333055f 100644
--- a/MP.IOC/Controllers/IOBController.cs
+++ b/MP.IOC/Controllers/IOBController.cs
@@ -88,18 +88,23 @@ namespace MP.IOC.Controllers
///
///
[HttpGet("enabled/{id}")]
- public IActionResult Enabled(string id)
+ public async Task Enabled(string id)
{
if (string.IsNullOrEmpty(id)) return BadRequest("Missing ID");
- if (DService.IobInsEnab(id))
+ try
{
- return Ok("OK");
+ // Il metodo ora restituisce direttamente il booleano logico
+ bool isEnabled = await DService.IobInsEnabAsync(id);
+
+ return isEnabled
+ ? Ok("OK")
+ : UnprocessableEntity("NO");
}
- else
+ catch (Exception ex)
{
- //return StatusCode(503, "NO");
- return UnprocessableEntity("NO");
+ Log.Error(ex, "Errore durante la verifica abilitazione per {Id}", id);
+ return StatusCode(500, "Errore interno del server");
}
}
diff --git a/MP.IOC/Data/MpDataService.cs b/MP.IOC/Data/MpDataService.cs
index 9de837b8..7ffbd6ab 100644
--- a/MP.IOC/Data/MpDataService.cs
+++ b/MP.IOC/Data/MpDataService.cs
@@ -1281,6 +1281,30 @@ namespace MP.IOC.Data
return answ;
}
+ ///
+ /// Restituisce il valore booleano se la macchina sia abilitata all'input
+ ///
+ ///
+ ///
+ public async Task IobInsEnabAsync(string idxMacchina)
+ {
+ var key = Utils.RedKeyDatiMacc(idxMacchina, MpIoNS);
+
+ // 1. Tentativo ottimizzato: leggiamo solo il campo che ci serve
+ // Supponendo che tu usi StackExchange.Redis direttamente o un wrapper
+ string? val = await redisDb.HashGetAsync(key, "insEnabled");
+
+ // 2. Se non c'รจ in cache, carichiamo/resettiamo tutto
+ if (val == null)
+ {
+ var data = await ResetDatiMacchinaAsync(idxMacchina);
+ data.TryGetValue("insEnabled", out val);
+ }
+
+ // 3. Parsing sicuro
+ return val != null && (val == "1" || val.ToLower() == "true");
+ }
+
///
///
/// idxMacc odl da cercare
@@ -1350,6 +1374,40 @@ namespace MP.IOC.Data
Log.Debug($"Macchine2SlaveGetAll | Read from {readType}: {ts.TotalMilliseconds}ms");
return result;
}
+ ///
+ /// Elenco completo valori Macchine 2 Slave
+ ///
+ ///
+ public async Task> Macchine2SlaveGetAllAsync()
+ {
+ List? result = new List();
+ Stopwatch stopWatch = new Stopwatch();
+ stopWatch.Start();
+ string readType = "DB";
+ string currKey = $"{Utils.redisBaseAddr}:M2STab";
+ // cerco in redis dato valore sel macchina...
+ RedisValue rawData = await redisDb.StringGetAsync(currKey);
+ if (rawData.HasValue)
+ {
+ result = JsonConvert.DeserializeObject>($"{rawData}");
+ readType = "REDIS";
+ }
+ else
+ {
+ result = await IocDbController.Macchine2SlaveAsync();
+ // serializzo e salvo...
+ rawData = JsonConvert.SerializeObject(result);
+ await redisDb.StringSetAsync(currKey, rawData, getRandTOut(redisLongTimeCache));
+ }
+ if (result == null)
+ {
+ result = new List();
+ }
+ stopWatch.Stop();
+ TimeSpan ts = stopWatch.Elapsed;
+ Log.Debug($"Macchine2SlaveGetAllAsync | Read from {readType}: {ts.TotalMilliseconds}ms");
+ return result;
+ }
///
/// Elenco di tutte le macchine gestite
@@ -3017,11 +3075,15 @@ namespace MP.IOC.Data
return answ;
}
- public async Task RedisSetHashDictAsync(RedisKey redKey, Dictionary valori)
+ public async Task RedisSetHashDictAsync(RedisKey redKey, Dictionary valori, double expireSeconds = -1.0)
{
bool answ = false;
HashEntry[] redHash = valori.Select(x => new HashEntry(x.Key, x.Value)).ToArray();
await redisDb.HashSetAsync(redKey, redHash);
+ if (expireSeconds > 0.0)
+ {
+ redisDb.KeyExpire(redKey, DateTime.Now.AddSeconds(expireSeconds));
+ }
answ = true;
return answ;
}
@@ -4271,6 +4333,77 @@ namespace MP.IOC.Data
return result;
}
+ ///
+ /// Restitusice elenco KVP dei campi DatiMacchine + StatoMacchine per l'impianto indicato
+ ///
+ ///
+ ///
+ private async Task> ResetDatiMacchinaAsync(string idxMacc)
+ {
+ var currHash = Utils.RedKeyDatiMacc(idxMacc, MpIoNS);
+ // inizio con un bel reset...
+ RedisFlushPattern($"{currHash}");
+ Dictionary? result = new Dictionary();
+ Stopwatch stopWatch = new Stopwatch();
+ stopWatch.Start();
+ string readType = "DB";
+ var dbResults = await IocDbController.VMSFDGetByMaccAsync(idxMacc);
+ // converto in formato dizionario...
+ if (dbResults != null && dbResults.Count > 0)
+ {
+ var rowResult = dbResults[0];
+ // salvo 1:1 i valori... STATO
+ result.Add("IdxMicroStato", $"{rowResult.IdxMicroStato}");
+ result.Add("IdxStato", $"{rowResult.IdxStato}");
+ result.Add("CodArticolo", $"{rowResult.CodArticolo}");
+ result.Add("insEnabled", $"{rowResult.InsEnabled}");
+ result.Add("sLogEnabled", $"{rowResult.SLogEnabled}");
+ result.Add("pallet", $"{rowResult.Pallet}");
+ result.Add("CodArticolo_A", $"{rowResult.CodArticoloA}");
+ result.Add("CodArticolo_B", $"{rowResult.CodArticoloB}");
+ result.Add("TempoCicloBase", $"{rowResult.TempoCicloBase}");
+ result.Add("PzPalletProd", $"{rowResult.PzPalletProd}");
+ result.Add("MatrOpr", $"{rowResult.MatrOpr}");
+ result.Add("lastVal", $"{rowResult.LastVal}");
+ result.Add("TCBase", $"{rowResult.TempoCicloBase}");
+
+ //...e SETUP
+ result.Add("CodMacc", $"{rowResult.Codmacchina}");
+ result.Add("IdxFamIn", $"{rowResult.IdxFamigliaIngresso}");
+ result.Add("Multi", $"{rowResult.Multi}");
+ result.Add("BitFilt", $"{rowResult.BitFilt}");
+ result.Add("MaxVal", $"{rowResult.MaxVal}");
+ result.Add("BSR", $"{rowResult.Bsr}");
+ result.Add("ExplodeBit", $"{rowResult.ExplodeBit}");
+ result.Add("NumBit", $"{rowResult.NumBit}");
+ result.Add("IdxFamMacc", $"{rowResult.IdxFamiglia}");
+ result.Add("simplePallet", $"{rowResult.SimplePallet}");
+ result.Add("palletChange", $"{rowResult.PalletChange}");
+ }
+ // cerco info Master/slave...
+ var m2sTab = await Macchine2SlaveGetAllAsync();
+ string isMaster = m2sTab.Where(x => x.IdxMacchina == idxMacc).Count() > 0 ? "1" : "0";
+ string isSlave = m2sTab.Where(x => x.IdxMacchinaSlave == idxMacc).Count() > 0 ? "1" : "0";
+ result.Add("Master", isMaster);
+ result.Add("Slave", isSlave);
+
+ // durata cache in secondi dal valore insEnabled...
+ double numSecCache = 60 * ((result["insEnabled"].ToLower() == "true") ? redisShortTimeCache / 4 : redisShortTimeCache);
+ // ...e salvo...
+ await RedisSetHashDictAsync(currHash, result, numSecCache);
+
+ if (result == null)
+ {
+ result = new Dictionary();
+ }
+ stopWatch.Stop();
+ TimeSpan ts = stopWatch.Elapsed;
+ Log.Debug($"GetCurrMSFDMacc | Read from {readType}: {ts.TotalMilliseconds}ms");
+ return result;
+ }
+
+
+
///
/// Resetta (rileggendo) i dati della State Machine ingressi nel formato
/// currKey: cState_nVal (current MICRO-STATE + "_" + new Value)