Files
mapo-core/MP.SPEC/Components/ListPARAMS.razor.cs
T
2023-11-17 18:49:55 +01:00

293 lines
7.8 KiB
C#

using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;
using MP.Data.DatabaseModels;
using MP.SPEC.Data;
using System.Diagnostics;
namespace MP.SPEC.Components
{
public partial class ListPARAMS : IDisposable
{
#region Public Fields
public SelectFluxParams? LastFilter = null;
#endregion Public Fields
#region Public Properties
[Parameter]
public EventCallback<bool> PagerResetReq { get; set; }
[Parameter]
public EventCallback<FluxLog> RecordSel { get; set; }
[Parameter]
public SelectFluxParams SelFilter { get; set; } = null!;
[Parameter]
public EventCallback<int> TotRecordChanged { get; set; }
#endregion Public Properties
#region Public Methods
public string checkSelect(FluxLog selRecord)
{
string answ = "";
if (currRecord != null)
{
try
{
answ = (currRecord.IdxMacchina == selRecord.IdxMacchina && currRecord.dtEvento == selRecord.dtEvento && currRecord.CodFlux == selRecord.CodFlux) ? "table-info" : "";
}
catch
{ }
}
return answ;
}
public void Dispose()
{
aTimer.Elapsed -= ElapsedTimer;
aTimer.Stop();
aTimer.Close();
aTimer.Dispose();
currRecord = null;
SearchRecords = null;
ListRecords = null;
GC.Collect();
}
public void ElapsedTimer(object? source, System.Timers.ElapsedEventArgs e)
{
// controlo se URL diverso da apgina params...
if (!NavManager.Uri.Contains("PARAMS"))
{
aTimer.Enabled = false;
}
else
{
aTimer.Enabled = true;
if (!isLoading && LiveUpdate)
{
// inizio misura esecuzione
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
var pUpd = Task.Run(async () =>
{
await Task.Delay(1);
await InvokeAsync(() => reloadData(true));
});
pUpd.Wait();
// misuro tempo esecuzione
stopWatch.Stop();
TimeSpan ts = stopWatch.Elapsed;
double deltaTime = RefreshPeriod - ts.TotalMilliseconds;
// aggiungo fattore di disturbo random...
double mFact = 0.01 * random.Next(80, 120);
aTimer.Interval = mFact * (deltaTime > 100 ? deltaTime : 100);
//aTimer.Start();
}
}
}
public async Task reloadData(bool setChanged)
{
isLoading = true;
SearchRecords = null;
ListRecords = null;
DateTime dataFrom = DateTime.Today.AddMonths(-1);
DateTime dataTo = DateTime.Now.AddDays(1);
if (SelFilter != null && SelFilter.dtMin != null)
{
dataFrom = (DateTime)SelFilter.dtMin;
}
if (SelDtMax != null)
{
dataTo = (DateTime)SelDtMax;
}
SearchRecords = await MDService.FluxLogGetLastFilt(dataTo, dataFrom, SelMacchina, SelFlux, MaxRecord, RefreshPeriod / 1000);
totalCount = SearchRecords.Count;
ListRecords = SearchRecords.Skip(numRecord * (currPage - 1)).Take(numRecord).ToList();
await Task.Delay(1);
if (setChanged)
{
await InvokeAsync(() => StateHasChanged());
}
isLoading = false;
}
public void StartTimer()
{
aTimer = new System.Timers.Timer(RefreshPeriod);
aTimer.Elapsed += ElapsedTimer;
aTimer.Enabled = true;
aTimer.AutoReset = true;
aTimer.Start();
}
#endregion Public Methods
#region Protected Fields
protected Random random = new Random();
#endregion Protected Fields
#region Protected Properties
[Inject]
protected IJSRuntime JSRuntime { get; set; } = null!;
[Inject]
protected MpDataService MDService { get; set; } = null!;
protected int RefreshPeriod
{
get => SelFilter.TempoAgg;
}
#endregion Protected Properties
#region Protected Methods
protected override void OnInitialized()
{
StartTimer();
}
protected override async Task OnParametersSetAsync()
{
await Task.Delay(1);
// se sono cambiati --> rileggo...
if (!lastFilter.Equals(SelFilter))
{
lastFilter = SelFilter.clone();
await reloadData(true);
}
}
protected async void OnSeachUpdated()
{
await InvokeAsync(() =>
{
PagerResetReq.InvokeAsync(false);
//currPage = 1;
Task task = UpdateData();
StateHasChanged();
});
}
protected async Task resetSel()
{
currRecord = null;
await RecordSel.InvokeAsync(null);
}
protected async Task selRecord(FluxLog selRec)
{
currRecord = selRec;
SelFilter.IdxMacchina = selRec.IdxMacchina;
SelDtMax = selRec.dtEvento;
// imposto pag 1 filtro
SelFilter.CurrPage = 1;
await reloadData(false);
await RecordSel.InvokeAsync(selRec);
await PagerResetReq.InvokeAsync(true);
}
protected async Task UpdateData()
{
currRecord = null;
await reloadData(true);
}
#endregion Protected Methods
#region Private Fields
private int _totalCount = 0;
private System.Timers.Timer aTimer = null!;
private FluxLog? currRecord = null;
private List<FluxLog>? ListRecords;
private List<FluxLog>? SearchRecords;
#endregion Private Fields
#region Private Properties
private int currPage
{
get => SelFilter.CurrPage;
set => SelFilter.CurrPage = value;
}
private bool isLoading { get; set; } = false;
private SelectFluxParams lastFilter { get; set; } = new SelectFluxParams() { CurrPage = -1 };
private bool LiveUpdate
{
get => SelFilter.LiveUpdate;
}
private int MaxRecord
{
get => SelFilter.MaxRecord;
}
[Inject]
private NavigationManager NavManager { get; set; } = null!;
private int numRecord
{
get => SelFilter.NumRec;
set => SelFilter.NumRec = value;
}
private DateTime? SelDtMax
{
get => SelFilter.dtMax;
set => SelFilter.dtMax = value;
}
private string SelFlux
{
get => SelFilter.CodFlux;
}
private string SelMacchina
{
get => SelFilter.IdxMacchina;
}
private int totalCount
{
get => _totalCount;
set
{
if (_totalCount != value)
{
_totalCount = value;
TotRecordChanged.InvokeAsync(value);
}
}
}
#endregion Private Properties
#region Private Methods
private string traduci(string lemma)
{
var answ = MDService.Traduci(lemma, "IT");
return answ;
}
#endregion Private Methods
}
}