Miglioramento enabled (HOPE!!!)

This commit is contained in:
Samuele Locatelli
2026-04-16 08:40:44 +02:00
parent 875b0109f3
commit 8cf048fb4e
3 changed files with 184 additions and 7 deletions
+134 -1
View File
@@ -1281,6 +1281,30 @@ namespace MP.IOC.Data
return answ;
}
/// <summary>
/// Restituisce il valore booleano se la macchina sia abilitata all'input
/// </summary>
/// <param name="idxMacchina"></param>
/// <returns></returns>
public async Task<bool> 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");
}
/// <summary>
/// </summary>
/// <param name="IdxOdl">idxMacc odl da cercare</param>
@@ -1350,6 +1374,40 @@ namespace MP.IOC.Data
Log.Debug($"Macchine2SlaveGetAll | Read from {readType}: {ts.TotalMilliseconds}ms");
return result;
}
/// <summary>
/// Elenco completo valori Macchine 2 Slave
/// </summary>
/// <returns></returns>
public async Task<List<Macchine2SlaveModel>> Macchine2SlaveGetAllAsync()
{
List<Macchine2SlaveModel>? result = new List<Macchine2SlaveModel>();
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<List<Macchine2SlaveModel>>($"{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<Macchine2SlaveModel>();
}
stopWatch.Stop();
TimeSpan ts = stopWatch.Elapsed;
Log.Debug($"Macchine2SlaveGetAllAsync | Read from {readType}: {ts.TotalMilliseconds}ms");
return result;
}
/// <summary>
/// Elenco di tutte le macchine gestite
@@ -3017,11 +3075,15 @@ namespace MP.IOC.Data
return answ;
}
public async Task<bool> RedisSetHashDictAsync(RedisKey redKey, Dictionary<string, string> valori)
public async Task<bool> RedisSetHashDictAsync(RedisKey redKey, Dictionary<string, string> 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;
}
/// <summary>
/// Restitusice elenco KVP dei campi DatiMacchine + StatoMacchine per l'impianto indicato
/// </summary>
/// <param name="idxMacc"></param>
/// <returns></returns>
private async Task<Dictionary<string, string>> ResetDatiMacchinaAsync(string idxMacc)
{
var currHash = Utils.RedKeyDatiMacc(idxMacc, MpIoNS);
// inizio con un bel reset...
RedisFlushPattern($"{currHash}");
Dictionary<string, string>? result = new Dictionary<string, string>();
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<string, string>();
}
stopWatch.Stop();
TimeSpan ts = stopWatch.Elapsed;
Log.Debug($"GetCurrMSFDMacc | Read from {readType}: {ts.TotalMilliseconds}ms");
return result;
}
/// <summary>
/// Resetta (rileggendo) i dati della State Machine ingressi nel formato
/// currKey: cState_nVal (current MICRO-STATE + "_" + new Value)