diff --git a/MP.Core/Utils.cs b/MP.Core/Utils.cs index 67fbed19..82b63f8d 100644 --- a/MP.Core/Utils.cs +++ b/MP.Core/Utils.cs @@ -60,6 +60,7 @@ namespace MP.Core public const string redisPOdlByPOdl = redisXdlData + "POdlByPOdl"; public const string redisPOdlList = redisXdlData + "POdlList"; public const string redisRecipeConf = redisBaseAddr + "Cache:Recipe:Conf"; + public const string redisRemRebLog = redisBaseAddr + "Cache:RemReb"; public const string redisStatoCom = redisBaseAddr + "Cache:StatoCom"; public const string redisStatoMacch = redisBaseAddr + "Cache:StatoMacch"; public const string redisStatoProd = redisBaseAddr + "Cache:StatoProd"; @@ -290,7 +291,7 @@ namespace MP.Core public static RedisKey RedKeyPzCount(string idxMacchina, string baseAddr = null) { var prefix = (baseAddr ?? redisBaseAddr).TrimEnd(':'); - return (RedisKey)$"{prefix}:Cache:PzCount:{idxMacchina}"; + return (RedisKey)$"{prefix}:PzCount:{idxMacchina}"; } /// diff --git a/MP.Data/Controllers/MpIocController.cs b/MP.Data/Controllers/MpIocController.cs index 7e132630..cf4bf139 100644 --- a/MP.Data/Controllers/MpIocController.cs +++ b/MP.Data/Controllers/MpIocController.cs @@ -941,6 +941,78 @@ namespace MP.Data.Controllers return fatto; } + /// + /// Aggiunta record RemoteRebootLog + /// + /// + /// + public async Task RemRebootLogAddAsync(RemoteRebootLogModel newRec) + { + bool fatto = false; + using (var dbCtx = new MoonProContext(_configuration)) + { + var dbResult = dbCtx + .DbSetRemRebLog + .Add(newRec); + fatto = await dbCtx.SaveChangesAsync() > 0; + } + return fatto; + } + + /// + /// Recupera tutti i record di RemoteRebootLog + /// + /// + public async Task> RemRebootLogGetAllAsync() + { + List dbResult = new List(); + using (var dbCtx = new MoonProContext(_configuration)) + { + dbResult = await dbCtx + .DbSetRemRebLog + .AsNoTracking() + .OrderByDescending(x => x.IdxReboot) + .ToListAsync(); + } + return dbResult; + } + + /// + /// Recupera ultimo record x ogni IdxMacchina x avere ultimo attivo + /// + /// + public async Task> RemRebootLogGetLastAsync() + { + List dbResult = new List(); + using (var dbCtx = new MoonProContext(_configuration)) + { + dbResult = await dbCtx + .DbSetRemRebLog + .FromSqlRaw("EXEC stp_RRL_getLast") + .AsNoTracking() + .ToListAsync(); + } + return dbResult; + } + + /// + /// Recupera ultimo record x ogni IdxMacchina x avere ultimo attivo + /// + /// + public async Task RemRebootLogKeepLastAsync(int num2keep) + { + bool answ = false; + using (var dbCtx = new MoonProContext(_configuration)) + { + var Num2keep = new SqlParameter("@num2keep", num2keep); + var dbResult = await dbCtx + .Database + .ExecuteSqlRawAsync("exec dbo.stp_RRL_KeepLatest @num2keep", Num2keep); + answ = dbResult > 0; + } + return answ; + } + /// /// Annulla modifiche su una specifica entity (cancel update) /// diff --git a/MP.IOC/Controllers/IOBController.cs b/MP.IOC/Controllers/IOBController.cs index e74a6b59..11ad3696 100644 --- a/MP.IOC/Controllers/IOBController.cs +++ b/MP.IOC/Controllers/IOBController.cs @@ -1,6 +1,7 @@ using Microsoft.AspNetCore.Mvc; using MP.Core.DTO; using MP.Core.Objects; +using MP.Data.DbModels; using MP.IOC.Data; using Newtonsoft.Json; using NLog; @@ -652,6 +653,62 @@ 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 + /// + /// ID macchina + /// MAC address macchina + /// + [HttpGet("sendReboot/{id}")] + public async Task SendReboot(string id, string mac) + { + if (string.IsNullOrEmpty(id)) return BadRequest("Missing ID"); + + // Multi: gestione carattere "|" trasformato in "#" + id = id.Replace("|", "#"); + + string answ = "NO"; + try + { + // recupero IP del client remoto + var agent = Request.Headers["User-Agent"].ToString(); + var iPv4 = HttpContext.Connection.RemoteIpAddress?.ToString(); + + // chiamo registrazione reboot + RemoteRebootLogModel newRec = new() + { + Agent = agent, + DataOraBoot = DateTime.Now, + IdxMacchina = id, + IPv4 = iPv4, + MacAddr = mac + }; + bool fatto = await DService.RemRebootLogAddAsync(newRec); + answ = fatto ? "OK" : "KO"; + } + catch (Exception exc) + { + Log.Error($"Errore in sendReboot{Environment.NewLine}{exc}"); + return StatusCode(StatusCodes.Status500InternalServerError, "NO"); + } + return Ok(answ); + } + + /// + /// Salva IP del gateway dopo il reboot + /// + /// IP del Gateway + /// + [HttpGet("sendRebootGateway/{id}")] + public async Task SendRebootGateway(string GWIP) + { + string answ = "OK"; + // !!!FARE!!! deve salvare il riavvio dell'applicazione GATEWAY multiclient + return Ok(answ); + } + /// /// SALVA Counter x macchina restituendo il valore appena inviato o, se mancasse chiave /// redis, del valore da DB @@ -666,8 +723,6 @@ namespace MP.IOC.Controllers { if (string.IsNullOrEmpty(id)) return BadRequest("Missing ID"); - //Stopwatch sw = new Stopwatch(); - //sw.Start(); // Multi: gestione carattere "|" trasformato in "#" id = id.Replace("|", "#"); @@ -683,8 +738,6 @@ namespace MP.IOC.Controllers Log.Error($"Errore in SetCounter{Environment.NewLine}{exc}"); return StatusCode(StatusCodes.Status500InternalServerError, "NO"); } - //sw.Stop(); - //Log.Trace($"SetCounter | Elapsed: {sw.Elapsed.TotalMilliseconds:N2}"); return Ok(answ); } diff --git a/MP.IOC/Data/MpDataService.cs b/MP.IOC/Data/MpDataService.cs index 60820464..168df138 100644 --- a/MP.IOC/Data/MpDataService.cs +++ b/MP.IOC/Data/MpDataService.cs @@ -2794,6 +2794,100 @@ namespace MP.IOC.Data return answ; } + /// + /// Inserisce record RRL + fa pulizia vecchi record + /// + /// + public async Task RemRebootLogAddAsync(RemoteRebootLogModel newRec) + { + bool fatto = false; + // insert del record + fatto = await IocDbController.RemRebootLogAddAsync(newRec); + // pulizia record vecchi + int num2keep = 10; + string confVal = await tryGetConfig("IO_NumReboot2Keep"); + if (!string.IsNullOrEmpty(confVal)) + { + int.TryParse(confVal, out num2keep); + } + fatto = await IocDbController.RemRebootLogKeepLastAsync(num2keep); + // svuota cache + var currKey = $"{Utils.redisRemRebLog}:*"; + await RedisFlushPatternAsync(currKey); + return fatto; + } + + /// + /// Recupera tutti i record di RemoteRebootLog + /// + /// + public async Task> RemRebootLogGetAllAsync() + { + // setup parametri costanti + string source = "DB"; + Stopwatch sw = new Stopwatch(); + sw.Start(); + List result = new List(); + // cerco in _redisConn... + var currKey = $"{Utils.redisRemRebLog}:ALL"; + RedisValue rawData = await redisDb.StringGetAsync(currKey); + //if (!string.IsNullOrEmpty($"{rawData}")) + if (rawData.HasValue) + { + result = JsonConvert.DeserializeObject>($"{rawData}") ?? new(); + source = "REDIS"; + } + else + { + result = await IocDbController.RemRebootLogGetAllAsync(); + // serializzo e salvo... + rawData = JsonConvert.SerializeObject(result); + await redisDb.StringSetAsync(currKey, rawData, TimeSpan.FromSeconds(30)); + } + if (result == null) + { + result = new List(); + } + sw.Stop(); + Log.Debug($"RemRebootLogGetAll | {source} | {sw.Elapsed.TotalMilliseconds}ms"); + return result; + } + + /// + /// Recupera ultimo record x ogni IdxMacchina x avere ultimo attivo + /// + /// + public async Task> RemRebootLogGetLast() + { + // setup parametri costanti + string source = "DB"; + Stopwatch sw = new Stopwatch(); + sw.Start(); + List result = new List(); + // cerco in _redisConn... + var currKey = $"{Utils.redisRemRebLog}:LAST"; + RedisValue rawData = await redisDb.StringGetAsync(currKey); + if (rawData.HasValue) + { + result = JsonConvert.DeserializeObject>($"{rawData}") ?? new(); + source = "REDIS"; + } + else + { + result = await IocDbController.RemRebootLogGetLastAsync(); + // serializzo e salvo... + rawData = JsonConvert.SerializeObject(result); + await redisDb.StringSetAsync(currKey, rawData, TimeSpan.FromSeconds(30)); + } + if (result == null) + { + result = new List(); + } + sw.Stop(); + Log.Debug($"RemRebootLogGetLast | {source} | {sw.Elapsed.TotalMilliseconds}ms"); + return result; + } + /// /// Resetta (rileggendo) i dati della State Machine multi ingressi nel formato /// currKey: IdxMacchina diff --git a/MP.IOC/MP.IOC.csproj b/MP.IOC/MP.IOC.csproj index cf4655f4..698c0def 100644 --- a/MP.IOC/MP.IOC.csproj +++ b/MP.IOC/MP.IOC.csproj @@ -4,7 +4,7 @@ net8.0 enable enable - 6.16.2604.1512 + 6.16.2604.1515 diff --git a/MP.IOC/Resources/ChangeLog.html b/MP.IOC/Resources/ChangeLog.html index 258cd488..65c3e33c 100644 --- a/MP.IOC/Resources/ChangeLog.html +++ b/MP.IOC/Resources/ChangeLog.html @@ -1,6 +1,6 @@ Modulo MP-IOC -

Versione: 6.16.2604.1512

+

Versione: 6.16.2604.1515


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