Files
2025-01-18 10:45:40 +01:00

237 lines
6.6 KiB
C#

using EgwControlCenter.Core.Models;
using EgwControlCenter.Core;
using Microsoft.AspNetCore.Components;
using EgwCoreLib.Razor;
using EgwCoreLib.Utils;
namespace EgwControlCenter.App.Components.Compo
{
public partial class AuthChallengeMan : IDisposable
{
#region Public Properties
[Parameter]
public EventCallback<bool> EC_GotoSetup { get; set; }
[Parameter]
public EventCallback<bool> EC_Updated { get; set; }
#endregion Public Properties
#region Public Methods
public void Dispose()
{
periodicTimer?.Dispose();
ACService.EA_ConfigUpdated -= ACService_EA_ConfigUpdated;
ACService.EA_RemoteCalling -= ACService_EA_RemoteCalling;
}
#endregion Public Methods
#region Protected Fields
protected int tCounter = 0;
#endregion Protected Fields
#region Protected Properties
[Inject]
protected AppControlService ACService { get; set; } = null!;
protected string AppKey
{
get => ACService.AppKey;
}
protected string MainKey
{
get => ACService.MainKey;
set => ACService.MainKey = value;
}
protected int RefreshPeriod
{
get => ACService.RefreshPeriod;
set => ACService.RefreshPeriod = value;
}
protected bool RemoteCallActive { get; set; } = false;
#endregion Protected Properties
#region Protected Methods
protected void DoCancel()
{
ACService.DoReloadConfig();
}
protected void DoReset()
{
ACService.ResetConf();
}
protected void DoSave()
{
ACService.DoSaveConfig();
ACService.DoReloadConfig();
}
protected override async Task OnInitializedAsync()
{
CodImpiego = SLicManager.CodImpiego();
// mi faccio staccare un codice di auth da remoto
passcode = await ACService.GetAuthPassocde();
tCounter = startCounter;
RunTimer();
// aggancio gestione eventi
ACService.EA_ConfigUpdated += ACService_EA_ConfigUpdated;
ACService.EA_RemoteCalling += ACService_EA_RemoteCalling;
}
protected override void OnParametersSet()
{
ForceReload();
}
protected void SetNumRec(int newNum)
{
numRecord = newNum;
currPage = 1;
ForceReload();
}
protected void SetPage(int newNum)
{
currPage = newNum;
ForceReload();
}
protected async void ToggleSetup()
{
ReqSetup = !ReqSetup;
await EC_GotoSetup.InvokeAsync(ReqSetup);
}
#endregion Protected Methods
#region Private Fields
private string bandColor = "13FD13";
private int passcode = 0;
private PeriodicTimer periodicTimer = new(TimeSpan.FromSeconds(1));
private bool remoteCalling = false;
private bool ReqSetup = true;
private Random rnd = new Random();
private int startCounter = 600;
/// <summary>
/// Veto controllo remoto stato approvazione licenza
/// </summary>
private DateTime vetoRemCheck = DateTime.Now;
#endregion Private Fields
#region Private Properties
private string CodImpiego { get; set; } = "";
private int currPage { get; set; } = 1;
private bool isLoading { get; set; } = false;
private List<ControlTarget> ListRecords { get; set; } = new List<ControlTarget>();
private int numRecord { get; set; } = 5;
private bool PwdOk
{
get => !string.IsNullOrEmpty(SetupPwd) && SetupPwd == EgwControlCenter.Core.Const.EditPwd;
}
private List<ControlTarget> SearchRecords
{
get => ACService.TargetList;
set => ACService.TargetList = value;
}
private string SetupPwd { get; set; } = "";
private SubLicManager SLicManager { get; set; } = new SubLicManager();
private int totalCount { get; set; } = 0;
private int VetoCheck
{
get => ACService.VetoCheck;
set => ACService.VetoCheck = value;
}
#endregion Private Properties
#region Private Methods
private void ACService_EA_ConfigUpdated()
{
ForceReload();
}
/// <summary>
/// Gestione evento chiamata verso server
/// </summary>
private async void ACService_EA_RemoteCalling()
{
RemoteCallActive = ACService.CloudCallActive;
await InvokeAsync(StateHasChanged);
}
private void ForceReload()
{
totalCount = SearchRecords.Count;
ListRecords = SearchRecords
.Skip(numRecord * (currPage - 1))
.Take(numRecord)
.ToList();
}
private async void RunTimer()
{
while (await periodicTimer.WaitForNextTickAsync())
{
// controllo veto check online...
if (DateTime.Now.Subtract(vetoRemCheck).TotalSeconds > 0)
{
// controllo stato chiamata...
string mainKey = await ACService.CheckAssignIdxLic();
vetoRemCheck = DateTime.Now.AddMilliseconds(rnd.Next(800, 1200));
// se la auth key fosse valida ritorno evento x uscire da fase validazione...
if (!string.IsNullOrEmpty(mainKey))
{
await EC_Updated.InvokeAsync(true);
}
}
tCounter--;
if (tCounter < 0)
{
tCounter = startCounter;
}
// aggiungo colore secondo valore...
if (tCounter > startCounter * 0.6)
{
bandColor = "#00FF00";
}
else if (tCounter > startCounter * 0.3)
{
bandColor = "#26ED26";
}
else if (tCounter > startCounter * 0.1)
{
bandColor = "#DCFD15";
}
else
{
bandColor = "#DC2615";
}
await InvokeAsync(StateHasChanged);
}
}
#endregion Private Methods
}
}