130 lines
3.4 KiB
C#
130 lines
3.4 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)
|
|
{
|
|
aTimer.Elapsed -= ElapsedTimer;
|
|
aTimer.Stop();
|
|
aTimer.Dispose();
|
|
Log.Info("MseSampler Timer Disposed!");
|
|
}
|
|
}
|
|
|
|
#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 StatusData SDService { get; set; } = null!;
|
|
|
|
[Inject]
|
|
protected MessageService MServ { get; set; } = null!;
|
|
|
|
#endregion Protected Properties
|
|
|
|
#region Protected Methods
|
|
|
|
protected void ElapsedTimer(object? source, System.Timers.ElapsedEventArgs e)
|
|
{
|
|
try
|
|
{
|
|
var pUpd = Task.Run(async () =>
|
|
{
|
|
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()
|
|
{
|
|
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!;
|
|
|
|
private Random rnd = new Random();
|
|
|
|
#endregion Private Fields
|
|
|
|
#region Private Methods
|
|
|
|
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");
|
|
Log.Trace($"MseSampler setupConf | Effettuato setup parametri | fastRefreshMSec: {fastTimerMSec}");
|
|
}
|
|
|
|
#endregion Private Methods
|
|
}
|
|
} |