300 lines
9.5 KiB
C#
300 lines
9.5 KiB
C#
using global::Microsoft.AspNetCore.Components;
|
|
using Microsoft.AspNetCore.Components.Routing;
|
|
using Microsoft.JSInterop;
|
|
using MP.Data.DbModels;
|
|
using MP.Data.Services;
|
|
using NLog;
|
|
using static Org.BouncyCastle.Math.EC.ECCurve;
|
|
|
|
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
|
|
|
|
protected string bodyType
|
|
{
|
|
get => pageOk ? "mainBodyNoSide" : "mainBody";
|
|
}
|
|
|
|
[Inject]
|
|
protected IConfiguration config { get; set; } = null!;
|
|
|
|
protected Guid currDevGuid { get; set; } = new Guid();
|
|
|
|
/// <summary>
|
|
/// Livello corrente del menu
|
|
/// </summary>
|
|
protected string CurrLevel { get; set; } = "";
|
|
|
|
/// <summary>
|
|
/// Elenco items da men� per pagina corrente...
|
|
/// </summary>
|
|
protected List<LinkMenuModel> CurrMenuItems { get; set; } = new List<LinkMenuModel>();
|
|
|
|
/// <summary>
|
|
/// Scadenza del login
|
|
/// </summary>
|
|
protected int dtScadLogin { get; set; } = 0;
|
|
|
|
protected bool HideMenu
|
|
{
|
|
get => NavMan.Uri.Contains("reg-new-device");
|
|
}
|
|
|
|
[Inject]
|
|
protected IJSRuntime JSRuntime { get; set; } = null!;
|
|
|
|
[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!;
|
|
|
|
protected bool pageOk
|
|
{
|
|
get
|
|
{
|
|
string currPage = NavMan.Uri;
|
|
return currPage.Contains("logout") || currPage.Contains("reg-new-device");
|
|
}
|
|
}
|
|
|
|
[Inject]
|
|
protected TabDataService TDataService { get; set; } = null!;
|
|
|
|
/// <summary>
|
|
/// Tipo scadenza login Postoa -1 di default così da NON avere problemi in caso di setuop errato...
|
|
/// </summary>
|
|
protected int typeScadLogin { get; set; } = -1;
|
|
|
|
protected bool userIsOk { get; set; } = false;
|
|
|
|
#endregion Protected Properties
|
|
|
|
#region Protected Methods
|
|
|
|
protected async Task checkDtDiff2Logout()
|
|
{
|
|
DateTime adesso = DateTime.Now;
|
|
TimeSpan tsDeltaAct = adesso.Subtract(MsgServ.dtLastAction);
|
|
TimeSpan tsDeltaSave = adesso.Subtract(MsgServ.dtLastSave);
|
|
string userTkn = "";
|
|
switch (typeScadLogin)
|
|
{
|
|
case 1:
|
|
if (tsDeltaAct.TotalMinutes >= dtScadLogin)
|
|
{
|
|
userTkn = await TDataService.OperatoreGetRedis(MatrOpr, currDevGuid);
|
|
if (string.IsNullOrEmpty(userTkn))
|
|
{
|
|
NavMan.NavigateTo("logout");
|
|
}
|
|
else
|
|
{
|
|
await MsgServ.DoLogIn(userTkn, true);
|
|
MsgServ.dtLastAction = adesso;
|
|
MsgServ.dtLastSave = adesso;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
// se fosse oltre 1 minuto da ultimo save --> salvo!
|
|
if (tsDeltaSave.TotalMinutes > 1)
|
|
{
|
|
userTkn = await TDataService.OperatoreGetRedis(MatrOpr, currDevGuid);
|
|
if (!string.IsNullOrEmpty(userTkn))
|
|
{
|
|
await MsgServ.DoLogIn(userTkn, true);
|
|
MsgServ.dtLastAction = adesso;
|
|
MsgServ.dtLastSave = adesso;
|
|
}
|
|
}
|
|
}
|
|
break;
|
|
|
|
case 2:
|
|
// se fosse oltre 1 minuto da ultimo save --> salvo!
|
|
if (tsDeltaSave.TotalMinutes > 1)
|
|
{
|
|
// mitigazione controlli: solo ogni 1 minuto...
|
|
if (tsDeltaSave.TotalMinutes > 1)
|
|
{
|
|
MsgServ.dtLastSave = adesso;
|
|
userTkn = await TDataService.OperatoreGetRedis(MatrOpr, currDevGuid);
|
|
if (string.IsNullOrEmpty(userTkn))
|
|
{
|
|
NavMan.NavigateTo("logout");
|
|
}
|
|
if (tsDeltaAct.TotalMinutes >= dtScadLogin)
|
|
{
|
|
NavMan.NavigateTo("logout");
|
|
}
|
|
}
|
|
}
|
|
break;
|
|
|
|
case 0:
|
|
case -1:
|
|
await ReloadMemStor();
|
|
typeScadLogin = MStor.GetConfInt("TAB_TypeScadLogin");
|
|
dtScadLogin = MStor.GetConfInt("TAB_dtTimerScadLogin");
|
|
break;
|
|
}
|
|
}
|
|
|
|
protected async Task ForceReloadMStor(bool isOk)
|
|
{
|
|
await ReloadMemStor();
|
|
}
|
|
|
|
protected async Task HandleBodyClick()
|
|
{
|
|
await Task.Delay(1);
|
|
if (!pageOk)
|
|
{
|
|
await checkDtDiff2Logout();
|
|
}
|
|
}
|
|
|
|
protected override async Task OnAfterRenderAsync(bool firstRender)
|
|
{
|
|
currDevGuid = await MsgServ.GetCurrDevGuidLSAsync();
|
|
// rileggo scadenza ultima...
|
|
DateTime adesso = DateTime.Now;
|
|
TimeSpan tsScadenza = TDataService.OperatoreGetGuidTTL(MatrOpr, currDevGuid);
|
|
MsgServ.dtLastSave = adesso.Subtract(TimeSpan.FromMinutes(dtScadLogin)).Add(tsScadenza);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Init struttura dati
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
protected override async Task OnInitializedAsync()
|
|
{
|
|
NavMan.LocationChanged += HandleLocationChanged;
|
|
|
|
// verifica preliminare setup mdati
|
|
if (!MStor.MenuOk)
|
|
{
|
|
await ReloadMemStor();
|
|
}
|
|
// leggo resto della configurazione
|
|
typeScadLogin = MStor.GetConfInt("TAB_TypeScadLogin");
|
|
dtScadLogin = MStor.GetConfInt("TAB_dtTimerScadLogin");
|
|
CurrLevel = MStor.PageLevel(NavMan.Uri);
|
|
// recupero menu corrente
|
|
if (MStor.DictMenu.ContainsKey(CurrLevel))
|
|
{
|
|
CurrMenuItems = MStor.DictMenu[CurrLevel];
|
|
}
|
|
}
|
|
|
|
protected async Task ReloadMemStor()
|
|
{
|
|
// in primis svuoto...
|
|
MStor.ClearCache();
|
|
|
|
// fix url di base...
|
|
string baseUrl = config.GetValue<string>("SpecialConf:AppUrl") ?? (config.GetValue<string>("OptConf:AppUrl") ?? "");
|
|
MStor.baseUrl = baseUrl;
|
|
if (baseUrl.EndsWith(@"/"))
|
|
{
|
|
MStor.baseUrlImages = $"{baseUrl}macchine";
|
|
}
|
|
else
|
|
{
|
|
MStor.baseUrlImages = $"{baseUrl}/macchine";
|
|
}
|
|
|
|
// 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);
|
|
|
|
// fix vocabolario
|
|
var allVoc = TDataService.VocabolarioGetAll();
|
|
MStor.SetVocab(allVoc);
|
|
|
|
// resetto il tabDServ
|
|
await TDataService.FlushCache();
|
|
// ricarica la config...
|
|
TDataService.SetupConfig();
|
|
}
|
|
|
|
protected async Task SetUserOk(bool isOk)
|
|
{
|
|
await Task.Delay(1);
|
|
userIsOk = isOk;
|
|
}
|
|
|
|
#endregion Protected Methods
|
|
|
|
#region Private Fields
|
|
|
|
private static Logger Log = LogManager.GetCurrentClassLogger();
|
|
|
|
#endregion Private Fields
|
|
|
|
#region Private Properties
|
|
|
|
private int MatrOpr
|
|
{
|
|
get => MsgServ.MatrOpr;
|
|
}
|
|
|
|
#endregion Private Properties
|
|
|
|
#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 menu
|
|
if (MStor.DictMenu.ContainsKey(CurrLevel))
|
|
{
|
|
CurrMenuItems = MStor.DictMenu[CurrLevel];
|
|
InvokeAsync(StateHasChanged).ConfigureAwait(false);
|
|
}
|
|
}
|
|
|
|
#endregion Private Methods
|
|
}
|
|
} |