using MP.Data.Conf; using MP.Data.DatabaseModels; using NLog; using System.Net.Http.Json; namespace MP.WASM.Mon.Client.Pages { public partial class Index : IDisposable { #region Public Methods public void Dispose() { disposeTimers(); } public void ElapsedFastTimer(object? source, System.Timers.ElapsedEventArgs e) { var pUpd = Task.Run(async () => { // secondi pari --> blink, secondi dispari --> ricarica DateTime adesso = DateTime.Now; int resto = 0; Math.DivRem(adesso.Second, 2, out resto); if (resto == 0) { doBlink = true; Log.Trace("Elapsed Fast Timer Blink"); } else { doBlink = false; await ReloadData(); Log.Trace("Elapsed Fast Timer reload"); } await Task.Delay(1); await InvokeAsync(StateHasChanged); }); pUpd.Wait(); } public async void ElapsedSlowTimer(object? source, System.Timers.ElapsedEventArgs e) { listMSE = null; await Task.Delay(1); Log.Info("Elapsed Slow Timer --> full page reload"); // dispongo i vari timers... disposeTimers(); // reload pagina 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 fastRefreshMs = 1000; protected int keepAliveMin = 1; protected int maxCol = 6; protected string showArt = ""; protected int slowRefreshSec = 300; #endregion Protected Fields #region Protected Properties 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 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); } } #endif 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 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); } } #endif return answ; } /// /// Recupera da conf eventuale setup tag dell'IOB indicato /// /// /// protected List? getIobTag(string codIob) { List? answ = null; #if false if (MMDataService.currTagConf != null) { // cerco x chiave IOB... if (MMDataService.currTagConf.ContainsKey(codIob)) { answ = MMDataService.currTagConf[codIob]; } } #endif 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(); #if false // 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)); } #endif 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 bool doBlink = false; private List? listMSE = null; #endregion Private Fields #region Private Methods private void disposeTimers() { fastTimer.Elapsed -= ElapsedFastTimer; fastTimer.Stop(); fastTimer.Dispose(); slowTimer.Elapsed -= ElapsedSlowTimer; slowTimer.Stop(); slowTimer.Dispose(); } private async Task ReloadData() { // leggo stato server... fastTimer.Interval = fastRefreshMs * 3; var res = await Http.GetAsync("api/MSE/checkAlive"); Console.WriteLine("GetAsync end"); if (res.IsSuccessStatusCode) { #if DEBUG // hack: legge 4 volte i dati x stressare sistema var singleData = await Http.GetFromJsonAsync>("api/MSE"); listMSE = new List(); for (int i = 0; i < 4; i++) { listMSE.AddRange(singleData); } #else listMSE = await Http.GetFromJsonAsync>("api/MSE"); #endif fastTimer.Interval = fastRefreshMs; } else { Console.WriteLine("Error in request"); listMSE = null; await Task.Delay(100); await InvokeAsync(() => StateHasChanged()); } Console.WriteLine("ReloadData end"); } private async Task setupConf() { #if false 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); getConfVal("sART", ref showArt); Log.Info($"Effettuato setup parametri | keepAlive: {keepAliveMin} | MaxCol: {maxCol} | doAnimate: {doAnimate} | slowRefreshSec: {slowRefreshSec} | fastRefreshMs: {fastRefreshMs}"); } #endif } #endregion Private Methods } }