Files
mapo-core/MP-TAB3/Components/MseSampler.razor.cs
T
2024-02-19 09:19:36 +01:00

162 lines
4.3 KiB
C#

using global::Microsoft.AspNetCore.Components;
using MP.Data.DatabaseModels;
using MP.Data.Services;
using NLog;
namespace MP_TAB3.Components
{
public partial class MseSampler : IDisposable
{
#region Public Properties
[Parameter]
public EventCallback<List<MappaStatoExpl>> E_Updated { get; set; }
/// <summary> Moltiplicatore campionamento:
/// HF: se > 1 (mappa)
/// LF: se < 1 (dettaglio) </summary>
[Parameter]
public double SampleMult { get; set; } = 1;
#endregion Public Properties
#region Public Methods
public void Dispose()
{
if (aTimer != null)
{
disposeTimer();
}
}
#endregion Public Methods
#region Protected Fields
protected int fastTimerMSec = 3000;
#endregion Protected Fields
#region Protected Properties
[Inject]
protected IConfiguration config { get; set; } = null!;
protected int fastRefreshMs
{
get
{
// tempo variabile tra +/- 10% del target (SampleMult) della freq standard di update MSE
int answ = ((int)(fastTimerMSec / SampleMult * rnd.Next(900, 1100))) / 1000;
return answ;
}
}
[Inject]
protected MessageService MServ { get; set; } = null!;
[Inject]
protected StatusData SDService { get; set; } = null!;
#endregion Protected Properties
#region Protected Methods
protected void ElapsedTimer(object? source, System.Timers.ElapsedEventArgs e)
{
try
{
var pUpd = Task.Run(async () =>
{
numCall++;
// se supero limite --> resetto
if (numCall >= maxCall)
{
StartTimer();
numCall = 0;
}
else
{
aTimer.Interval = fastRefreshMs;
await InvokeAsync(RefreshData);
}
});
pUpd.Wait();
}
catch (Exception exc)
{
Log.Error($"Eccezione durante MseSampler.ElapsedTimer{Environment.NewLine}{exc}");
}
}
protected override void OnInitialized()
{
SetupConf();
StartTimer();
}
protected void StartTimer()
{
if (aTimer != null)
{
disposeTimer();
}
aTimer = new System.Timers.Timer(fastRefreshMs);
aTimer.Elapsed += ElapsedTimer;
aTimer.Enabled = true;
aTimer.Start();
Log.Info("MseSampler Timer started!");
}
#endregion Protected Methods
#region Private Fields
private static NLog.Logger Log = LogManager.GetCurrentClassLogger();
private System.Timers.Timer aTimer = null!;
/// <summary>
/// max chimate prima di fare dispose/GC
/// </summary>
private int maxCall = 10;
/// <summary>
/// Numero chiamate al sampler
/// </summary>
private int numCall = 0;
private Random rnd = new Random();
#endregion Private Fields
#region Private Methods
private void disposeTimer()
{
aTimer.Elapsed -= ElapsedTimer;
aTimer.Stop();
aTimer.Dispose();
GC.Collect();
Log.Info("MseSampler Timer Disposed!");
}
private async Task RefreshData()
{
List<MappaStatoExpl> ListMSE = await SDService.MseGetAll();
await MServ.SaveMse(ListMSE);
await E_Updated.InvokeAsync(ListMSE);
}
private void SetupConf()
{
// sistemo i parametri opzionali...
fastTimerMSec = config.GetValue<int>("OptConf:msRefresh");
maxCall = config.GetValue<int>("OptConf:samplerMaxCall");
Log.Trace($"MseSampler setupConf | Effettuato setup parametri | fastRefreshMSec: {fastTimerMSec} | maxCall: {maxCall}");
}
#endregion Private Methods
}
}