using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Components; using System.Net.Http; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Components.Authorization; using Microsoft.AspNetCore.Components.Forms; using Microsoft.AspNetCore.Components.Routing; using Microsoft.AspNetCore.Components.Web; using Microsoft.AspNetCore.Components.Web.Virtualization; using Microsoft.JSInterop; using MP.Mon; using MP.Mon.Shared; using MP.Mon.Components; using MP.Data.DatabaseModels; using MP.Mon.Components; using MP.Mon.Data; using NLog; namespace MP.Mon.Pages { public partial class Index : IDisposable { protected List? ListMSE = null; List? CurrConfig = null; private static NLog.Logger Log = LogManager.GetCurrentClassLogger(); protected int keepAliveMin = 1; protected int maxCol = 4; protected bool doAnimate = true; protected int slowRefreshSec = 300; protected int fastRefreshSec = 10; protected int slowRefreshMs { get => 1000 * slowRefreshSec; } protected int fastRefreshMs { get => 1000 * fastRefreshSec; } protected override async Task OnInitializedAsync() { await setupConf(); await ReloadData(); StartTimer(); } [Inject] protected MpDataService MMDataService { get; set; } = null!; [Inject] protected NavigationManager NavManager { get; set; } = null!; private async Task setupConf() { CurrConfig = await MMDataService.ConfigGetAll(); if (CurrConfig != null && CurrConfig.Count > 0) { // sistemo i parametri opzionali... getConfValInt("keepAliveMin", ref keepAliveMin); getConfValInt("MON_maxCol", ref maxCol); int intDoAnim = 0; getConfValInt("doAnimate", ref intDoAnim); doAnimate = intDoAnim == 1; getConfValInt("pageRefreshSec", ref slowRefreshSec); getConfValInt("MSE_cacheDuration", ref fastRefreshSec); Log.Info($"Effettuato setup parametri | keepAlive: {keepAliveMin} | MaxCol: {maxCol} | doAnimate: {doAnimate} | pageRefreshSec: {slowRefreshSec} | fastRefreshSec: {fastRefreshSec}"); } } /// /// Recupera il valore e se trovato aggiorna /// /// Valore da cercare /// Oggetto in cui salvare il valore se trovato /// protected bool getConfValInt(string chiave, ref int varObj) { bool answ = false; if (CurrConfig != null && CurrConfig.Count > 0) { // sistemo i parametri opzionali... ConfigModel? risultato = CurrConfig.FirstOrDefault(x => x.Chiave == chiave); if (risultato != null) { answ = int.TryParse(risultato.Valore, out varObj); } } return answ; } private async Task ReloadData() { ListMSE = await MMDataService.MseGetAll(); } public void Dispose() { fastTimer.Stop(); fastTimer.Dispose(); slowTimer.Stop(); slowTimer.Dispose(); } private static System.Timers.Timer fastTimer; private static System.Timers.Timer slowTimer; public void StartTimer() { // timer veloce fastTimer = new System.Timers.Timer(fastRefreshMs); fastTimer.Elapsed += ElapsedFastTimer; fastTimer.Enabled = true; fastTimer.Start(); // timer lento slowTimer = new System.Timers.Timer(slowRefreshMs); slowTimer.Elapsed += ElapsedSlowTimer; slowTimer.Enabled = true; slowTimer.Start(); } public void ElapsedFastTimer(Object source, System.Timers.ElapsedEventArgs e) { var pUpd = Task.Run(async () => { await ReloadData(); //await Task.Delay(1); await InvokeAsync(StateHasChanged); }); pUpd.Wait(); } public async void ElapsedSlowTimer(Object source, System.Timers.ElapsedEventArgs e) { ListMSE = null; await Task.Delay(200); NavManager.NavigateTo(NavManager.Uri); } } }