using global::System; using global::System.Collections.Generic; using global::System.Linq; using global::System.Threading.Tasks; using global::Microsoft.AspNetCore.Components; using System.Net.Http; using System.Net.Http.Json; using Microsoft.AspNetCore.Components.Forms; using Microsoft.AspNetCore.Components.Routing; using Microsoft.AspNetCore.Components.Sections; using Microsoft.AspNetCore.Components.Web; using Microsoft.AspNetCore.Components.Web.Virtualization; using Microsoft.JSInterop; using MP.Data; using MP.Data.DatabaseModels; using MP.Data.DTO; using MP.Data.Services; using MP_TAB; using MP_TAB.Components; using NLog; using MP.Data.Conf; using Newtonsoft.Json; using static System.Runtime.InteropServices.JavaScript.JSType; namespace MP_TAB.Components.Pages { public partial class StatusMap { #region Protected Fields protected bool doAnimate = true; protected int keepAliveMin = 1; protected int maxCol = 6; protected string showArt = ""; protected int slowRefreshSec = 300; #endregion Protected Fields #region Protected Properties [Inject] protected StatusData MDataService { get; set; } = null!; [Inject] protected MessageService MsgServ { 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 (MDataService.currTagConf != null) { // cerco x chiave IOB... if (MDataService.currTagConf.ContainsKey(codIob)) { answ = MDataService.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 => MDataService.getTagConf(x.TagLocation)); } return answ; } protected override async Task OnInitializedAsync() { CurrListMSE = null; await setupConf(); //await InvokeAsync(StateHasChanged); //await Task.Delay(500); await ReloadData(); } #endregion Protected Methods #region Private Fields private static NLog.Logger Log = LogManager.GetCurrentClassLogger(); private static System.Timers.Timer slowTimer = new System.Timers.Timer(300000); private List? CurrConfig = null; private Random rnd = new Random(); #endregion Private Fields #region Private Properties private List? CurrListMSE { get; set; } = null; #endregion Private Properties #region Private Methods private async Task ReloadData() { CurrListMSE = await MDataService.MseGetAll(); //try //{ // // salvo in LocalStorage... // await MsgServ.SaveMse(CurrListMSE); //} //catch (Exception exc) //{ // Log.Error($"Eccezione in ReloadData{Environment.NewLine}{exc}"); //} } protected override async Task OnAfterRenderAsync(bool firstRender) { if (CurrListMSE != null) { // salvo in LocalStorage... await MsgServ.SaveMse(CurrListMSE); //return base.OnAfterRenderAsync(firstRender); } } private async Task setupConf() { CurrConfig = await MDataService.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}"); } } #endregion Private Methods } }