using Microsoft.AspNetCore.Components; using MP.Data; using MP.Data.Conf; using MP.Data.DatabaseModels; using MP.Mon.Data; using Newtonsoft.Json; using NLog; namespace MP.Mon.Pages { public partial class Index : IDisposable { #region Public Methods public void Dispose() { disposeTimers(); } public async void ElapsedSlowTimer(object? source, System.Timers.ElapsedEventArgs e) { listMSE = null; await Task.Delay(10); Log.Info("Elapsed Slow Timer --> full page reload"); // dispongo i vari timers... disposeTimers(); await Task.Delay(10); // reload pagina NavManager.NavigateTo(NavManager.Uri); } public void StartTimer() { // 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 [Inject] protected MpDataService MMDataService { get; set; } = null!; [Inject] protected NavigationManager NavManager { get; set; } = null!; protected int slowRefreshMs { get { // tempo variabile tra +/- 10% del target int answ = rnd.Next(900, 1100) * slowRefreshSec; return answ; } } #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(); MMDataService.dataPipe.EA_NewMessage += DataPipe_EA_NewMessage; MMDataService.blinkPipe.EA_NewMessage += BlinkPipe_EA_NewMessage; Random rnd = new Random(); await Task.Delay(rnd.Next(1000, 1200)); 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; private bool doBlink = false; private List? listMSE = null; private Random rnd = new Random(); #endregion Private Fields #region Private Methods private void BlinkPipe_EA_NewMessage(object? sender, EventArgs e) { PubSubEventArgs currArgs = (PubSubEventArgs)e; // conversione on-the-fly List --> allarmi if (!string.IsNullOrEmpty(currArgs.newMessage)) { try { var dataRaw = JsonConvert.DeserializeObject(currArgs.newMessage); if (dataRaw != null) { bool.TryParse($"{dataRaw}", out doBlink); } } catch { } InvokeAsync(() => { StateHasChanged(); }); } } /// /// Ricevuto nuovi dati da mostrare! /// /// /// /// private void DataPipe_EA_NewMessage(object? sender, EventArgs e) { PubSubEventArgs currArgs = (PubSubEventArgs)e; // conversione on-the-fly List --> allarmi if (!string.IsNullOrEmpty(currArgs.newMessage)) { try { var dataList = JsonConvert.DeserializeObject>(currArgs.newMessage); if (dataList != null) { #if DEBUG // hack: legge 4 volte i dati x stressare sistema var singleData = dataList; listMSE = new List(); for (int i = 0; i < 4; i++) { listMSE.AddRange(singleData); } #else listMSE = dataList; #endif } } catch { } } InvokeAsync(() => { #if false // attesa random 0-50ms... Random rnd = new Random(); Task.Delay(rnd.Next(5, 50)); #endif StateHasChanged(); }); } private void disposeTimers() { slowTimer.Elapsed -= ElapsedSlowTimer; slowTimer.Stop(); slowTimer.Dispose(); } 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); getConfVal("sART", ref showArt); Log.Info($"setupConf | Effettuato setup parametri | keepAlive: {keepAliveMin} | MaxCol: {maxCol} | doAnimate: {doAnimate} | slowRefreshSec: {slowRefreshSec} | fastRefreshMs: {fastRefreshMs}"); } } #endregion Private Methods } }