Files
samuele e71b873a4c - nuget update
- pulizia codice da warning
2023-08-21 18:06:54 +02:00

433 lines
12 KiB
C#

using Microsoft.AspNetCore.Components.Server.ProtectedBrowserStorage;
using MP.MONO.Data.DbModels;
using MP.MONO.UI.Data;
using NLog;
using System.Diagnostics;
namespace MP.MONO.UI.Pages
{
public partial class Alarms : IDisposable
{
#region Public Properties
public bool isActive
{
get => MMDataService.AlarmLogActive;
}
public string lastUpdate
{
get => actFilter.lastUpdate;
set=> actFilter.lastUpdate = value;
}
public bool liveUpdate { get; set; } = true;
public bool setLogRec
{
get => currFilter.isActive;
set => currFilter.isActive = value;
}
#endregion Public Properties
#region Public Methods
public void Dispose()
{
SearchRecords = null;
SearchRecordsREC = null;
ListRecords = null;
ListRecordsREC = null;
try
{
aTimer.Elapsed -= ElapsedTimer;
aTimer.Stop();
aTimer.Dispose();
}
catch
{ }
GC.Collect();
}
public void ElapsedTimer(object? source, System.Timers.ElapsedEventArgs e)
{
if (!isLoading && liveUpdate)
{
try
{
aTimer.Stop();
}
catch
{ }
// 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;
int deltaTime = RefreshPeriod - (int)ts.TotalMilliseconds;
aTimer.Interval = deltaTime > 100 ? deltaTime : 100;
try
{
aTimer.Start();
}
catch
{ }
Log.Debug("ElapsedTimer: Timer Restarted");
GC.Collect();
}
}
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 bool doSetup = false;
protected bool reqPassw = false;
protected string SHCSSmode = "fw-bold";
protected string SRCSSmode = "text-secondary";
#endregion Protected Fields
#region Protected Properties
protected string currMode
{
get => setLogRec ? "Status History mode" : "Single Rec mode";
}
protected int RefreshPeriod { get; set; } = 1000;
#endregion Protected Properties
#region Protected Methods
protected async Task CheckAuth()
{
await Task.Delay(1);
// se non sono già in edit mostro auth...
if (reqPassw || doSetup)
{
reqPassw = false;
}
else
{
reqPassw = !reqPassw;
}
doSetup = false;
await Task.Delay(1);
}
protected void ForceReload(int newNum)
{
numRecord = newNum;
}
protected void ForceReloadPage(int newNum)
{
currPage = newNum;
liveUpdate = !liveUpdate;
DateTime adesso = DateTime.Now.AddSeconds(1);
liveUpdate = (currPage == 1);
if (!liveUpdate)
{
lastUpdate = $"{adesso:yyyy/MM/dd HH:mm:ss}";
maxRecordStatus = true;
}
else
{
maxRecordStatus = false;
MaxRecord = 200;
}
StateHasChanged();
}
protected bool isPageReady { get; set; } = false;
protected override async Task OnInitializedAsync()
{
StartTimer();
isPageReady = true;
await Task.Delay(1);
}
protected async Task ReloadData(bool setChanged)
{
// solo se NON sto già aspettando...
if (!isWaitingReload)
{
isWaitingReload = true;
DateTime adesso = DateTime.Now;
Random rand = new Random();
while (isLoading)
{
await Task.Delay(rand.Next(20, 40));
}
TimeSpan ts = DateTime.Now.Subtract(adesso);
if (ts.TotalMilliseconds >= 20)
{
Log.Debug($"Waited for ReloadData | total wait {ts.TotalMilliseconds:N3}ms");
}
isWaitingReload = false;
isLoading = true;
SearchRecords = null;
SearchRecordsREC = null;
ListRecords = null;
ListRecordsREC = null;
await Task.Delay(1);
if (liveUpdate)
{
if (setLogRec)
{
SearchRecordsREC = await MMDataService.AlarmRecGetFilt(MachineId, 0, MaxRecord);
ListRecordsREC = SearchRecordsREC.Skip(numRecord * (currPage - 1)).Take(numRecord).ToList();
totalCount = SearchRecordsREC.Count;
await Task.Delay(1);
}
else
{
SearchRecords = await MMDataService.AlarmLogGetFilt(MachineId, 0, MaxRecord);
ListRecords = SearchRecords.Skip(numRecord * (currPage - 1)).Take(numRecord).ToList();
totalCount = SearchRecords.Count;
await Task.Delay(1);
}
}
else
{
if (setLogRec)
{
SearchRecordsREC = await MMDataService.AlarmRecGetFiltByDate(MachineId, dtMax, 0, MaxRecord);
ListRecordsREC = SearchRecordsREC.Skip(numRecord * (currPage - 1)).Take(numRecord).ToList();
totalCount = SearchRecordsREC.Count;
await Task.Delay(1);
}
else
{
SearchRecords = await MMDataService.AlarmLogGetFiltByDate(MachineId, dtMax, 0, MaxRecord);
ListRecords = SearchRecords.Skip(numRecord * (currPage - 1)).Take(numRecord).ToList();
totalCount = SearchRecords.Count;
await Task.Delay(1);
}
}
await Task.Delay(1);
isLoading = false;
await InvokeAsync(() =>
{
StateHasChanged();
});
}
else
{
Log.Debug("Already waiting, ReloadData discarded");
}
}
protected async Task reloadFromMessage()
{
if (isPageReady == true)
{
try
{
aTimer.Stop();
}
catch
{ }
await ReloadData(true);
try
{
aTimer.Start();
}
catch
{ }
}
}
#if false
protected void resetDate()
{
dtRif = DateTime.Now;
}
protected void resetMaxRecord()
{
MaxRecord = 200;
}
#endif
protected async Task ShowSetup()
{
await Task.Delay(1);
doSetup = !doSetup;
reqPassw = false;
await Task.Delay(1);
}
protected async Task toggleUpdate()
{
liveUpdate = !liveUpdate;
currPage = 1;
await Task.Delay(1);
if (!liveUpdate)
{
lastUpdate = $"{DateTime.Now:yyyy/MM/dd HH:mm:ss}";
maxRecordStatus = true;
}
else
{
//MaxRecord = 200;
maxRecordStatus = false;
dtMax = DateTime.Now;
}
await ReloadData(true);
}
#endregion Protected Methods
#region Private Fields
private static System.Timers.Timer aTimer = null!;
private static NLog.Logger Log = LogManager.GetCurrentClassLogger();
private DateTime _dtRif = DateTime.Now;
private List<AlarmLogModel>? ListRecords = null;
private List<AlarmRecModel>? ListRecordsREC = null;
// FARE!!! filtro macchina
private int MachineId = 1;
private List<AlarmLogModel>? SearchRecords = null;
private List<AlarmRecModel>? SearchRecordsREC = null;
private bool showParams = false;
#endregion Private Fields
#region Private Properties
private selectGlobalToggle currFilter { get; set; } = new selectGlobalToggle();
private int currPage
{
get => actFilter.CurrPage;
set => actFilter.CurrPage = value;
}
private DateTime dtMax
{
get => actFilter.dtMax;
set => actFilter.dtMax = value;
}
#if false
private DateTime dtRif
{
get
{
return _dtRif;
}
set
{
if (_dtRif != value)
{
_dtRif = value;
DateTime adesso = DateTime.Now.AddSeconds(1);
if (_dtRif.Date != adesso.Date)
{
liveUpdate = false;
if (!liveUpdate)
{
lastUpdate = $"{adesso:yyyy/MM/dd HH:mm:ss}";
maxRecordStatus = true;
}
else
{
maxRecordStatus = false;
//MaxRecord = 200;
}
}
}
}
}
#endif
private static bool isLoading { get; set; } = false;
private static bool isWaitingReload { get; set; } = false;
protected override async Task OnParametersSetAsync()
{
await ReloadData(true);
}
private bool maxRecordStatus
{
get=>actFilter.maxRecordStatus;
set=>actFilter.maxRecordStatus = value;
}
private int numAlarm
{
get => currFilter.numAlarms;
}
private int numRecord
{
get => actFilter.NumRec;
set => actFilter.NumRec = value;
}
protected selectAlarmsFilt actFilter = new selectAlarmsFilt();
protected int MaxRecord
{
get => actFilter.maxRec;
set => actFilter.maxRec = value;
}
private int totalCount { get; set; } = 0;
#endregion Private Properties
#region Private Methods
private void toggleShowParams()
{
showParams = !showParams;
}
private async Task updateFilterToggle(selectGlobalToggle newParams)
{
await Task.Delay(1);
newParams.rightString = "SINGLE REC";
newParams.leftString = "STATUS HISTORY";
await ReloadData(true);
currFilter = newParams;
}
private async Task updateFilterAlarms(selectAlarmsFilt newParams)
{
await Task.Delay(1);
currPage = 1;
actFilter = newParams;
await ReloadData(true);
}
protected void UpdateTotCount(int newTotCount)
{
totalCount = newTotCount;
}
#endregion Private Methods
}
}