diff --git a/MP.Data/Controllers/MpIocController.cs b/MP.Data/Controllers/MpIocController.cs index cf4bf139..c1e8d1ca 100644 --- a/MP.Data/Controllers/MpIocController.cs +++ b/MP.Data/Controllers/MpIocController.cs @@ -313,6 +313,39 @@ namespace MP.Data.Controllers return fatto; } + public async Task KeepAliveUpsertAsync(string IdxMacc, DateTime OraServer, DateTime OraMacc) + { + bool fatto = false; + using (var dbCtx = new MoonProContext(_configuration)) + { + var currRec = await dbCtx + .DbSetKeepAlive + .Where(x => x.IdxMacchina == IdxMacc) + .FirstOrDefaultAsync(); + if (currRec != null) + { + currRec.DataOraServer = OraServer; + currRec.DataOraMacchina = OraMacc; + dbCtx.Entry(currRec).State = EntityState.Modified; + } + else + { + KeepAliveModel newRec = new KeepAliveModel() + { + IdxMacchina = IdxMacc, + DataOraMacchina = OraMacc, + DataOraServer = OraServer, + DataOraStart = DateTime.Now + }; + dbCtx + .DbSetKeepAlive + .Add(newRec); + } + fatto = await dbCtx.SaveChangesAsync() > 0; + } + return fatto; + } + public List ListLinkFilt(string tipoLink) { List dbResult = new List(); diff --git a/MP.IOC/Controllers/IOBController.cs b/MP.IOC/Controllers/IOBController.cs index 11ad3696..af325bda 100644 --- a/MP.IOC/Controllers/IOBController.cs +++ b/MP.IOC/Controllers/IOBController.cs @@ -44,6 +44,34 @@ namespace MP.IOC.Controllers } } + [HttpGet("addTask2Exe/{id}")] + public async Task AddTask2Exe(string id, string taskName, string taskVal) + { + if (string.IsNullOrEmpty(id)) return BadRequest("Missing ID"); + // Multi: gestione carattere "|" trasformato in "#" + id = id.Replace("|", "#"); + + string answ = ""; + try + { + await DService.ScriviKeepAliveAsync(id, DateTime.Now); + taskType tName = taskType.nihil; + bool isOk = Enum.TryParse(taskName, out tName); + if (isOk) + { + // elimino se trovato + rileggo da REDIS elenco task restanti x macchina... + var newList = DService.AddTask4Machine(id, tName, taskVal); + answ = JsonConvert.SerializeObject(newList); + } + } + catch (Exception exc) + { + Log.Error($"Errore in GetTask2Exe{Environment.NewLine}{exc}"); + return StatusCode(StatusCodes.Status500InternalServerError, "NO"); + } + return Ok(answ); + } + /// /// GET: IOB/ /// @@ -437,12 +465,19 @@ namespace MP.IOC.Controllers // Multi: gestione carattere "|" trasformato in "#" id = id.Replace("|", "#"); - string answ = ""; - DService.ScriviKeepAlive(id, DateTime.Now); - // leggo da REDIS eventuale elenco task x macchina... - Dictionary valori = await DService.GetTask2ExeMacchinaAsync(id); - answ = JsonConvert.SerializeObject(valori); + try + { + DService.ScriviKeepAlive(id, DateTime.Now); + // leggo da REDIS eventuale elenco task x macchina... + Dictionary valori = await DService.GetTask2ExeMacchinaAsync(id); + answ = JsonConvert.SerializeObject(valori); + } + catch (Exception exc) + { + Log.Error($"Errore in GetTask2Exe{Environment.NewLine}{exc}"); + return StatusCode(StatusCodes.Status500InternalServerError, "NO"); + } return Ok(answ); } @@ -480,6 +515,52 @@ namespace MP.IOC.Controllers } } + /// + /// AGGIUNGE TASK richiesto x macchina: + /// + /// GET: IOB/addTask2Exe/3010?taskName=startSetup&taskVal=T190406101512 + /// GET: IOB/addTask2Exe/3010?taskName=stopSetup&taskVal=T190406101512 + /// GET: IOB/addTask2Exe/SIMUL_03?taskName=setProg&taskVal=P00000001 + /// GET: IOB/addTask2Exe/SIMUL_03?taskName=setComm&taskVal=ODL_0000123 + /// GET: IOB/addTask2Exe/SIMUL_03?taskName=setArt&taskVal=ART_0000321 + /// + /// + /// + /// + /// + /// ELIMINA TASK richiesto x macchina: + /// + /// GET: IOB/remTask2Exe/SIMUL_03?taskName=T180326160502 + /// + /// + /// + [HttpGet("remTask2Exe/{id}")] + public async Task RemTask2Exe(string id, string taskName) + { + if (string.IsNullOrEmpty(id)) return BadRequest("Missing ID"); + // Multi: gestione carattere "|" trasformato in "#" + id = id.Replace("|", "#"); + string answ = ""; + try + { + await DService.ScriviKeepAliveAsync(id, DateTime.Now); + taskType tName = taskType.nihil; + bool isOk = Enum.TryParse(taskName, out tName); + if (isOk) + { + // elimino se trovato + rileggo da REDIS elenco task restanti x macchina... + var newList = await DService.RemTask2ExeMacchinaAsync(id, tName); + answ = JsonConvert.SerializeObject(newList); + } + } + catch (Exception exc) + { + Log.Error($"Errore in GetTask2Exe{Environment.NewLine}{exc}"); + return StatusCode(StatusCodes.Status500InternalServerError, "NO"); + } + return Ok(answ); + } + /// /// Processa una chiamata POST per l'invio di un array Json di oggetti plcMemConf /// PUT: IOB/saveConf/SIMUL_03 @@ -496,7 +577,6 @@ namespace MP.IOC.Controllers string answ = ""; - // procedo a deserializzare in blocco l'oggetto... PlcMemMapDto currMemMap = null; try @@ -653,7 +733,6 @@ namespace MP.IOC.Controllers return Ok(answ); } - /// /// Salva MAC adress + IP dopo il reboot /// GET: IOB/sendReboot/SIMUL_05?mac=18:C0:4D:37:3C:8C diff --git a/MP.IOC/Data/MpDataService.cs b/MP.IOC/Data/MpDataService.cs index 168df138..96983526 100644 --- a/MP.IOC/Data/MpDataService.cs +++ b/MP.IOC/Data/MpDataService.cs @@ -128,7 +128,7 @@ namespace MP.IOC.Data } /// - /// Verifica se sia da reinviare un task alla macchina dall'elenco di quelli salvati (in + /// Verifica se sia da reinviare un tName alla macchina dall'elenco di quelli salvati (in /// modalità upsert) se non scaduti /// /// @@ -148,9 +148,9 @@ namespace MP.IOC.Data // se ho un valore != "" --> rimetto in coda di invio... if (!string.IsNullOrEmpty(savedVal) && (savedVal != taskVal)) { - // leggo task attuali... + // leggo tName attuali... Dictionary currTask = mTaskMacchina(idxMacchina); - // rimetto in task da eseguire... + // rimetto in tName da eseguire... currTask[taskKey.ToString()] = savedVal; answ = RedisSetHashDict(currHash, currTask); Log.Info($"re-issued task4machine: idxMacchina: {idxMacchina} | taskKey: {taskKey} | savedVal: {savedVal}"); @@ -177,7 +177,7 @@ namespace MP.IOC.Data RedisKey currHash = Utils.OptParHash(idxMacchina); try { - // leggo task attuali... + // leggo tName attuali... var currVal = mOptParMacchina(idxMacchina); currVal[taskKey] = taskVal; answ = RedisSetHashDict(currHash, currVal); @@ -190,7 +190,7 @@ namespace MP.IOC.Data } /// - /// Aggiunge un task all'elenco di quelli salvati (in modalità upsert) + /// Aggiunge un tName all'elenco di quelli salvati (in modalità upsert) /// /// /// @@ -205,7 +205,7 @@ namespace MP.IOC.Data Dictionary savedTask = new Dictionary(); try { - // leggo task attuali... + // leggo tName attuali... currTask = mTaskMacchina(idxMacchina); if (currTask.ContainsKey($"{taskKey}")) { @@ -219,14 +219,14 @@ namespace MP.IOC.Data Log.Info($"Task ADD | hash: {currHash} | idxMacchina: {idxMacchina} | taskKey: {taskKey} | taskVal: {taskVal}"); } catch { } - // verifico in base al tipo di task se fare backup... + // verifico in base al tipo di tName se fare backup... switch (taskKey) { case Enums.taskType.setArt: case Enums.taskType.setComm: case Enums.taskType.setPzComm: case Enums.taskType.setProg: - // leggo task SALVATI attuali... + // leggo tName SALVATI attuali... savedTask = mSavedTaskMacchina(idxMacchina); savedTask[taskKey.ToString()] = taskVal; answ = RedisSetHashDict(currSavedParHash, savedTask); @@ -590,7 +590,7 @@ namespace MP.IOC.Data }; // salva e processa answ = scriviRigaEvento(newRecEv); - //answ = scriviRigaEvento(idxMacchina, idxTipoEv, codArticolo, valEsteso, 0, "-", dtEve, DateTime.Now); + //currTask = scriviRigaEvento(idxMacchina, idxTipoEv, codArticolo, valEsteso, 0, "-", dtEve, DateTime.Now); // forzo RESET dati macchina... ResetDatiMacchina(idxMacchina); } @@ -2584,7 +2584,8 @@ namespace MP.IOC.Data /// public bool RedisDelKey(string keyVal) { - bool answ = false; + bool answ = redisDb.KeyDelete((RedisKey)keyVal); +#if false var listEndpoints = redisConnAdmin.GetEndPoints(); foreach (var endPoint in listEndpoints) { @@ -2595,9 +2596,44 @@ namespace MP.IOC.Data answ = true; } } +#endif return answ; } + /// + /// Esegue eliminazione memoria redis keyVal + /// + /// + /// + public async Task RedisDelKeyAsync(string keyVal) + { + return await RedisDelKeyAsync((RedisKey)keyVal); +#if false + bool answ = false; + var listEndpoints = redisConnAdmin.GetEndPoints(); + foreach (var endPoint in listEndpoints) + { + var server = redisConnAdmin.GetServer(endPoint); + if (server != null) + { + await redisDb.KeyDeleteAsync((RedisKey)keyVal); + answ = true; + } + } + return answ; +#endif + } + + /// + /// Esegue eliminazione memoria redis keyVal + /// + /// + /// + public async Task RedisDelKeyAsync(RedisKey keyVal) + { + return await redisDb.KeyDeleteAsync(keyVal); + } + /// /// Esegue flush memoria redis dato keyVal /// @@ -2730,6 +2766,21 @@ namespace MP.IOC.Data return result; } + public async Task RedisKeyPresentAsync(RedisKey key) + { + bool result = false; + try + { + result = await redisDb.KeyExistsAsync(key); + } + catch (Exception arg) + { + Log.Error($"Errore in redKeyPresent per la currKey {key}:{Environment.NewLine}{arg}"); + } + + return result; + } + public bool RedisKeyPresentSz(string key) { bool result = false; @@ -2804,7 +2855,7 @@ namespace MP.IOC.Data // insert del record fatto = await IocDbController.RemRebootLogAddAsync(newRec); // pulizia record vecchi - int num2keep = 10; + int num2keep = 5; string confVal = await tryGetConfig("IO_NumReboot2Keep"); if (!string.IsNullOrEmpty(confVal)) { @@ -2888,6 +2939,35 @@ namespace MP.IOC.Data return result; } + /// + /// Elimina da elenco KVP il TASK per l'impianto indicato + /// + /// + /// + /// + public async Task> RemTask2ExeMacchinaAsync(string idxMacchina, taskType tName) + { + // hard coded dimensione vettore DatiMacchine + Dictionary answ = new Dictionary(); + // ORA recupero da memoria redis... + try + { + var currHash = Utils.RedKeyTask2ExeMacc(idxMacchina, MpIoNS); + answ = await RedisGetHashDictAsync(currHash); + + answ.Remove($"{tName}"); + // riscrivo! + await RedisDelKeyAsync(currHash); + await RedisSetHashDictAsync(currHash, answ); + Log.Info($"Task REM - idxMacchina: {idxMacchina} | taskKey: {tName.ToString()}"); + } + catch (Exception exc) + { + Log.Info(string.Format("Errore in RemTask2ExeMacchinaAsync | idxMacchina {2}:{0}{1}", Environment.NewLine, exc, idxMacchina)); + } + return answ; + } + /// /// Resetta (rileggendo) i dati della State Machine multi ingressi nel formato /// currKey: IdxMacchina @@ -2898,7 +2978,7 @@ namespace MP.IOC.Data public KeyValuePair[] resetMSMI(string idxMacchina) { var currHash = Utils.RedKeyMsmi(idxMacchina); - //answ = RedisGetHash(currHash); + //currTask = RedisGetHash(currHash); // recupero records var tabMSMI = IocDbController.VMSFDGetMultiByMacc(idxMacchina); @@ -3222,6 +3302,42 @@ namespace MP.IOC.Data } } + /// + /// scrive un evento di keepalive sulla tabella + /// + /// + /// + /// + public async Task ScriviKeepAliveAsync(string IdxMacchina, DateTime oraMacchina) + { + string nomeVar = string.Format("KeepAlive:{0}", IdxMacchina); + // cerco se ho keep alive in redis, + bool keyPresent = false; + DateTime adesso = DateTime.Now; + var currKey = Utils.RedKeyHash(nomeVar); + try + { + keyPresent = await RedisKeyPresentAsync(currKey); + } + catch + { } + // se NON presente salvo in REDIS con TTL 10 sec e sul DB... + if (!keyPresent) + { + await redisDb.StringSetAsync(currKey, adesso.ToString("s"), TimeSpan.FromSeconds(10)); + try + { + Log.Trace($"Scrittura keep alive! IdxMacchina: {IdxMacchina}"); + // effettuo scrittura sul DB + await IocDbController.KeepAliveUpsertAsync(IdxMacchina, DateTime.Now, oraMacchina); + } + catch (Exception exc) + { + Log.Error($"Errore in scrittura keep alive!{Environment.NewLine}oraMacchina: {oraMacchina} - IdxMacchina: {IdxMacchina}{Environment.NewLine}{exc}"); + } + } + } + /// /// Salvataggio YAML completo di configurazione dell'IOB /// diff --git a/MP.IOC/MP.IOC.csproj b/MP.IOC/MP.IOC.csproj index 698c0def..f6a5e0de 100644 --- a/MP.IOC/MP.IOC.csproj +++ b/MP.IOC/MP.IOC.csproj @@ -4,7 +4,7 @@ net8.0 enable enable - 6.16.2604.1515 + 6.16.2604.1517 diff --git a/MP.IOC/Resources/ChangeLog.html b/MP.IOC/Resources/ChangeLog.html index 65c3e33c..5b1014d6 100644 --- a/MP.IOC/Resources/ChangeLog.html +++ b/MP.IOC/Resources/ChangeLog.html @@ -1,6 +1,6 @@ Modulo MP-IOC -

Versione: 6.16.2604.1515

+

Versione: 6.16.2604.1517


Note di rilascio:
  • diff --git a/MP.IOC/Resources/VersNum.txt b/MP.IOC/Resources/VersNum.txt index 862c2ef3..9cef419a 100644 --- a/MP.IOC/Resources/VersNum.txt +++ b/MP.IOC/Resources/VersNum.txt @@ -1 +1 @@ -6.16.2604.1515 +6.16.2604.1517 diff --git a/MP.IOC/Resources/manifest.xml b/MP.IOC/Resources/manifest.xml index 87537642..71938dea 100644 --- a/MP.IOC/Resources/manifest.xml +++ b/MP.IOC/Resources/manifest.xml @@ -1,6 +1,6 @@ - 6.16.2604.1515 + 6.16.2604.1517 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