Files
mapo-core/MP-TAB3/Pages/RegNewDevice.razor.cs
T
2025-04-09 08:41:19 +02:00

221 lines
6.9 KiB
C#

using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.WebUtilities;
//using MongoDB.Bson.IO;
using MP.Data.DbModels;
using MP.Data.DTO;
using MP.Data.Services;
using Newtonsoft.Json;
namespace MP_TAB3.Pages
{
public partial class RegNewDevice : IDisposable
{
#region Public Methods
public void Dispose()
{
barcodeReaderCustom.Stop();
barcodeReaderCustom.CloseScan();
ShowScanBarcode = false;
txtError = "";
matrOpr = 0;
oprList = new List<AnagOperatoriModel>();
GC.Collect();
}
#endregion Public Methods
#region Protected Fields
protected int expDays = 1;
protected bool recordLogin = true;
protected bool vetoRecord
{
get => MsgServ.VetoRecordLogin;
set => MsgServ.VetoRecordLogin = value;
}
protected string txtError = "";
protected DateTime vetoScan = DateTime.Now;
#endregion Protected Fields
#region Protected Properties
protected string _authKey { get; set; } = "";
protected int _matrOpr { get; set; } = 0;
protected string authKey
{
get
{
return _authKey;
}
set
{
if (_authKey != value)
{
_authKey = value;
FirstLogIn().ConfigureAwait(false);
}
}
}
protected EgwCoreLib.Razor.BarcodeReader barcodeReaderCustom { get; set; } = null!;
protected string CurrOprTknLS { get; set; } = null!;
protected string CurrOprTknRedis { get; set; } = null!;
protected int matrOpr
{
get { return _matrOpr; }
set => _matrOpr = value;
}
[Inject]
protected MessageService MsgServ { get; set; } = null!;
[Inject]
protected NavigationManager NavMan { get; set; } = null!;
protected List<AnagOperatoriModel> oprList { get; set; } = new List<AnagOperatoriModel>();
protected AnagOperatoriModel rigaOpr { get; set; } = null!;
[Inject]
protected SharedMemService SMServ { get; set; } = null!;
[Inject]
protected TabDataService TDService { get; set; } = null!;
#endregion Protected Properties
#region Protected Methods
protected async Task FirstLogIn()
{
rigaOpr = await TDService.OperatoreSearch(matrOpr, authKey);
var devGuid = await MsgServ.GetCurrDevGuidLSAsync();
if (rigaOpr == null)
{
var deHash = TDService.DecryptData(authKey);
rigaOpr = await TDService.OperatoreSearch(matrOpr, deHash);
}
await Task.Delay(50);
if (rigaOpr != null && rigaOpr.MatrOpr > 0)
{
userTknDTO newUserTkn = new userTknDTO()
{
currOpr = rigaOpr,
DevGuid = devGuid
};
var jsonTkn = JsonConvert.SerializeObject(newUserTkn);
string hash = TDService.EncryptData(jsonTkn);
MsgServ.RigaOper = rigaOpr;
await Task.Delay(50);
await MsgServ.SetCurrOperDtoLSAsync(hash);
await MsgServ.SetLastMatrOprAsync(rigaOpr.MatrOpr);
await TDService.OperatoreSetRedis(matrOpr, hash, devGuid);
// se abilitato in config...
if (recordLogin && !vetoRecord)
{
// imposto veto (TTL: 60sec hard-coded)
vetoRecord = true;
// salvo registrazione login utente
string generOpr = $"{rigaOpr.Cognome} {rigaOpr.Nome}";
RegistroDichiarazioniModel ulRec = new RegistroDichiarazioniModel()
{
TagCode = "UserLogin",
IdxMacchina = "",
DtRec = DateTime.Now,
MatrOpr = matrOpr,
ValString = $"Login: {generOpr}"
};
await TDService.RegDichiarInsert(ulRec);
}
// nascondo scanner...
ShowScanBarcode = false;
// passo a status map
await Task.Delay(50);
NavMan.NavigateTo("status-map", true);
}
else
{
txtError = Traduci("UserPwdMismatch");
authKey = "";
}
}
protected override async Task OnInitializedAsync()
{
txtError = "";
matrOpr = await MsgServ.GetLastMatrOprAsync();
TDService.ConfigGetVal("cookieDayExpire", ref expDays);
TDService.ConfigGetVal("TAB_UL_Record", ref recordLogin);
ShowScanBarcode = !SMServ.GetConfBool("TAB_WebCamHide");
oprList = await TDService.ElencoOperatori();
// filtro solo abilitati + ordino x nome...
oprList = oprList
.Where(x => x.isEnabled)
.OrderBy(x => x.Cognome)
.ThenBy(x => x.Nome)
.ToList();
}
protected async Task ScanDoneHandler(string value)
{
DateTime adesso = DateTime.Now;
// non nullo
if (!string.IsNullOrEmpty(value))
{
// non veto...
if (adesso.Subtract(vetoScan).TotalMinutes > 0)
{
vetoScan = adesso.AddSeconds(4);
//NavManager.NavigateTo($"CodeProcess/{value}");
var uri = NavMan.ToAbsoluteUri(value);
if (QueryHelpers.ParseQuery(uri.Query).TryGetValue("MatrOpr", out var Uri_matrOpr))
{
if (!string.IsNullOrEmpty(Uri_matrOpr))
{
matrOpr = int.Parse(Uri_matrOpr);
}
}
if (QueryHelpers.ParseQuery(uri.Query).TryGetValue("UserAuthKey", out var Uri_authKey))
{
if (!string.IsNullOrEmpty(Uri_authKey))
{
authKey = Uri_authKey;
}
}
await FirstLogIn();
}
}
}
protected async Task ToggleCodeScan()
{
await Task.Delay(1);
ShowScanBarcode = !ShowScanBarcode;
}
protected string Traduci(string lemma)
{
return SMServ.Traduci($"EN_{lemma}".ToUpper());
}
#endregion Protected Methods
#region Private Properties
private bool ShowScanBarcode { get; set; } = false;
#endregion Private Properties
}
}