using global::Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components.Routing; using Microsoft.JSInterop; using MP.Data.DatabaseModels; using MP.Data.Services; using MP_TAB3.Components; using NLog; namespace MP_TAB3.Shared { public partial class MainLayout : IDisposable { #region Public Methods public void Dispose() { NavMan.LocationChanged -= HandleLocationChanged; } #endregion Public Methods #region Protected Fields protected string ResetClass = "btn-primary"; #endregion Protected Fields #region Protected Properties /// /// Livello corrente del menu /// protected string CurrLevel { get; set; } = ""; /// /// Elenco items da men� per pagina corrente... /// protected List CurrMenuItems { get; set; } = new List(); protected bool HideMenu { get => NavMan.Uri.Contains("reg-new-device"); } [Inject] protected ListSelectDataSrv MDataService { get; set; } = null!; [Inject] protected MessageService MsgServ { get; set; } = null!; [Inject] protected SharedMemService MStor { get; set; } = null!; [Inject] protected NavigationManager NavMan { get; set; } = null!; [Inject] protected TabDataService TDataService { get; set; } = null!; [Inject] protected IJSRuntime JSRuntime { get; set; } = null!; #endregion Protected Properties #region Protected Methods protected string bodyType { get => pageOk ? "mainBodyNoSide" : "mainBody"; } protected bool pageOk { get { string currPage = NavMan.Uri; return currPage.Contains("logout") || currPage.Contains("reg-new-device"); } } protected async Task handleBodyClick() { await Task.Delay(1); if (!pageOk) { await checkDtDiff2Logout(); } } private int MatrOpr { get => MsgServ.MatrOpr; } /// /// Tipo scadenza login, a -1 di default così da NON avere problemi in caso di setuop errato... /// protected int typeScadLogin { get; set; } = -1; protected int dtScadLogin { get; set; } = 0; protected Guid currDevGuid { get; set; } = new Guid(); protected async Task checkDtDiff2Logout() { TimeSpan tsDeltaAct = DateTime.Now.Subtract(MsgServ.dtLastAction); TimeSpan tsDeltaSave = DateTime.Now.Subtract(MsgServ.dtLastSave); switch (typeScadLogin) { case 1: if (tsDeltaAct.TotalMinutes >= dtScadLogin) { var userTkn = await TDataService.OperatoreGetRedis(MatrOpr, currDevGuid); if (!string.IsNullOrEmpty(userTkn)) { await MsgServ.DoLogIn(userTkn, true); MsgServ.dtLastAction = DateTime.Now; MsgServ.dtLastSave = DateTime.Now; } else { NavMan.NavigateTo("logout"); } } else { // se fosse oltre 1 minuto da ultimo save --> salvo! if (tsDeltaSave.TotalMinutes > 1) { var userTkn = await TDataService.OperatoreGetRedis(MatrOpr, currDevGuid); if (!string.IsNullOrEmpty(userTkn)) { await MsgServ.DoLogIn(userTkn, true); MsgServ.dtLastAction = DateTime.Now; MsgServ.dtLastSave = DateTime.Now; } } } break; case 2: if (tsDeltaAct.TotalMinutes >= dtScadLogin) { NavMan.NavigateTo("logout"); } break; case 0: case -1: await ReloadMemStor(); typeScadLogin = MStor.GetConfInt("TAB_TypeScadLogin"); break; } } /// /// Init struttura dati /// /// protected override async Task OnInitializedAsync() { typeScadLogin = MStor.GetConfInt("TAB_TypeScadLogin"); dtScadLogin = MStor.GetConfInt("TAB_dtTimerScadLogin"); NavMan.LocationChanged += HandleLocationChanged; currDevGuid = await MsgServ.GetCurrDevGuidLSAsync(); // verifica preliminare setup mdati if (!MStor.MenuOk) { await ReloadMemStor(); } CurrLevel = MStor.PageLevel(NavMan.Uri); // recupero men� if (MStor.DictMenu.ContainsKey(CurrLevel)) { CurrMenuItems = MStor.DictMenu[CurrLevel]; } await Task.Delay(1); } protected bool userIsOk { get; set; } = false; protected async Task checkIfUserOk(bool isOk) { await Task.Delay(1); userIsOk = isOk; } protected async Task ReloadMemStor() { // in primis svuoto... MStor.ClearCache(); // rileggo link var allData = await MDataService.ListLinkAll(); MStor.SetupMenu(allData); // fix config... var allConf = await MDataService.ConfigGetAll(); MStor.SetConfig(allConf); // fix MSFD... var allMSFD = await TDataService.VMSFDGetAll(); MStor.SetMsfd(allMSFD); // fix slave var macSlave = await TDataService.Macchine2Slave(); MStor.SetM2S(macSlave); // fix elenco eventi var allEvents = await TDataService.AnagEventiGetAll(); MStor.SetEventi(allEvents); // fix elenco stati var allStati = await TDataService.AnaStatiGetAll(); MStor.SetStati(allStati); // non da farsi globalmente // fix macchine var allMach = await // MDataService.MacchineByMatrOper(0); MStor.DictMacchine = allMach.ToDictionary(x => // x.IdxMacchina, x => $"{x.IdxMacchina} | {x.Nome}"); // fix vocabolario var allVoc = TDataService.VocabolarioGetAll(); MStor.SetVocab(allVoc); // resetto il tabDServ await TDataService.FlushCache(); // ricarica la config... TDataService.SetupConfig(); } #endregion Protected Methods #region Private Fields private static Logger Log = LogManager.GetCurrentClassLogger(); #endregion Private Fields #region Private Methods private void HandleLocationChanged(object? sender, LocationChangedEventArgs e) { // Logger.LogInformation("URL of new location: {Location}", e.Location); CurrLevel = MStor.PageLevel(NavMan.Uri); // recupero men� if (MStor.DictMenu.ContainsKey(CurrLevel)) { CurrMenuItems = MStor.DictMenu[CurrLevel]; InvokeAsync(StateHasChanged).ConfigureAwait(false); } } private void OnYes() { Console.WriteLine($"{DateTime.Now} | 'Yes' button selected."); } #endregion Private Methods } }