using Microsoft.AspNetCore.Components; using MP.Data.Conf; using MP.Data.DatabaseModels; using MP.Mon.Data; using NLog; namespace MP.Mon.Pages { public partial class Index : IDisposable { #region Public Methods public void Dispose() { //fastTimer.Elapsed -= ElapsedFastTimer; fastTimer.Stop(); fastTimer.Dispose(); //slowTimer.Elapsed -= ElapsedSlowTimer; slowTimer.Stop(); slowTimer.Dispose(); } public void ElapsedFastTimer(object? source, System.Timers.ElapsedEventArgs e) { var pUpd = Task.Run(async () => { await ReloadData(); await Task.Delay(1); Log.Trace("Elapsed Fast Timer"); await InvokeAsync(StateHasChanged); }); pUpd.Wait(); } public async void ElapsedSlowTimer(object? source, System.Timers.ElapsedEventArgs e) { ListMSE = null; await Task.Delay(1); Log.Trace("Elapsed Slow Timer"); NavManager.NavigateTo(NavManager.Uri); } 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(); } #endregion Public Methods #region Protected Fields protected bool doAnimate = true; protected int fastRefreshSec = 10; protected int keepAliveMin = 1; protected List? ListMSE = null; protected int maxCol = 4; protected string showArt = ""; protected int slowRefreshSec = 300; #endregion Protected Fields #region Protected Properties protected int fastRefreshMs { get => 1000 * fastRefreshSec; } [Inject] protected MpDataService MMDataService { get; set; } = null!; [Inject] protected NavigationManager NavManager { get; set; } = null!; protected int slowRefreshMs { get => 1000 * slowRefreshSec; } #endregion Protected Properties #region Protected Methods /// /// Recupera il valore e se trovato aggiorna /// /// Valore da cercare /// String in cui salvare il valore se trovato /// protected bool getConfVal(string chiave, ref string 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) { varObj = risultato.Valore; answ = !string.IsNullOrEmpty(risultato.Valore); } } return answ; } /// /// Recupera il valore e se trovato aggiorna /// /// Valore da cercare /// Int 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; } /// /// Recupera da conf eventuale setup tag dell'IOB indicato /// /// /// protected List? getIobTag(string codIob) { List? answ = null; if (MMDataService.currTagConf != null) { // cerco x chiave IOB... if (MMDataService.currTagConf.ContainsKey(codIob)) { answ = MMDataService.currTagConf[codIob]; } } return answ; } /// /// Recupera da redis (in una chiamata soltanto) tutti i valori richiesti e compone un /// dizionario x ottimizzare visualizzazione /// /// /// protected Dictionary getTagVal(string codIob) { Dictionary answ = new Dictionary(); // recupero conf tags... var currTags = getIobTag(codIob); if (currTags != null && currTags.Count > 0) { // FIXME TODO !!!! FARE !!!! - da verificare answ = currTags.ToDictionary(x => x.TagLocation, x => MMDataService.getTagConf(x.TagLocation)); } return answ; } protected override async Task OnInitializedAsync() { await setupConf(); await ReloadData(); StartTimer(); } #endregion Protected Methods #region Private Fields private static System.Timers.Timer fastTimer = new System.Timers.Timer(4000); private static NLog.Logger Log = LogManager.GetCurrentClassLogger(); private static System.Timers.Timer slowTimer = new System.Timers.Timer(300000); private List? CurrConfig = null; #endregion Private Fields #region Private Methods private async Task ReloadData() { #if DEBUG // hack: legge 4 volte i dati x stressare sistema var singleData = await MMDataService.MseGetAll(); ListMSE = new List(); for (int i = 0; i < 4; i++) { ListMSE.AddRange(singleData); } #else ListMSE = await MMDataService.MseGetAll(); #endif } 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); getConfVal("sART", ref showArt); Log.Info($"Effettuato setup parametri | keepAlive: {keepAliveMin} | MaxCol: {maxCol} | doAnimate: {doAnimate} | slowRefreshSec: {slowRefreshSec} | fastRefreshSec: {fastRefreshSec}"); } } #endregion Private Methods } }