Files
mapo-core/MP.Mon/Pages/Index.razor.cs
T
2022-07-08 19:52:50 +02:00

231 lines
7.4 KiB
C#

using Microsoft.AspNetCore.Components;
using MP.Data.Conf;
using MP.Data.DatabaseModels;
using MP.Mon.Data;
using NLog;
namespace MP.Mon.Pages
{
public partial class Index : IDisposable
{
#region Public Methods
public void Dispose()
{
//fastTimer.Elapsed -= ElapsedFastTimer;
fastTimer.Stop();
fastTimer.Dispose();
//slowTimer.Elapsed -= ElapsedSlowTimer;
slowTimer.Stop();
slowTimer.Dispose();
}
public void ElapsedFastTimer(object? source, System.Timers.ElapsedEventArgs e)
{
var pUpd = Task.Run(async () =>
{
await ReloadData();
await Task.Delay(1);
Log.Trace("Elapsed Fast Timer");
await InvokeAsync(StateHasChanged);
});
pUpd.Wait();
}
public async void ElapsedSlowTimer(object? source, System.Timers.ElapsedEventArgs e)
{
ListMSE = null;
await Task.Delay(1);
Log.Trace("Elapsed Slow Timer");
NavManager.NavigateTo(NavManager.Uri);
}
public void StartTimer()
{
// timer veloce
fastTimer = new System.Timers.Timer(fastRefreshMs);
fastTimer.Elapsed += ElapsedFastTimer;
fastTimer.Enabled = true;
fastTimer.Start();
// timer lento
slowTimer = new System.Timers.Timer(slowRefreshMs);
slowTimer.Elapsed += ElapsedSlowTimer;
slowTimer.Enabled = true;
slowTimer.Start();
}
#endregion Public Methods
#region Protected Fields
protected bool doAnimate = true;
protected int fastRefreshSec = 10;
protected int keepAliveMin = 1;
protected List<MappaStatoExpl>? ListMSE = null;
protected int maxCol = 4;
protected string showArt = "";
protected int slowRefreshSec = 300;
#endregion Protected Fields
#region Protected Properties
protected int fastRefreshMs
{
get => 1000 * fastRefreshSec;
}
[Inject]
protected MpDataService MMDataService { get; set; } = null!;
[Inject]
protected NavigationManager NavManager { get; set; } = null!;
protected int slowRefreshMs
{
get => 1000 * slowRefreshSec;
}
#endregion Protected Properties
#region Protected Methods
/// <summary>
/// Recupera il valore e se trovato aggiorna
/// </summary>
/// <param name="chiave">Valore da cercare</param>
/// <param name="varObj">String in cui salvare il valore se trovato</param>
/// <returns></returns>
protected bool getConfVal(string chiave, ref string varObj)
{
bool answ = false;
if (CurrConfig != null && CurrConfig.Count > 0)
{
// sistemo i parametri opzionali...
ConfigModel? risultato = CurrConfig.FirstOrDefault(x => x.Chiave == chiave);
if (risultato != null)
{
varObj = risultato.Valore;
answ = !string.IsNullOrEmpty(risultato.Valore);
}
}
return answ;
}
/// <summary>
/// Recupera il valore e se trovato aggiorna
/// </summary>
/// <param name="chiave">Valore da cercare</param>
/// <param name="varObj">Int in cui salvare il valore se trovato</param>
/// <returns></returns>
protected bool getConfValInt(string chiave, ref int varObj)
{
bool answ = false;
if (CurrConfig != null && CurrConfig.Count > 0)
{
// sistemo i parametri opzionali...
ConfigModel? risultato = CurrConfig.FirstOrDefault(x => x.Chiave == chiave);
if (risultato != null)
{
answ = int.TryParse(risultato.Valore, out varObj);
}
}
return answ;
}
/// <summary>
/// Recupera da conf eventuale setup tag dell'IOB indicato
/// </summary>
/// <param name="codIob"></param>
/// <returns></returns>
protected List<TagData>? getIobTag(string codIob)
{
List<TagData>? answ = null;
if (MMDataService.currTagConf != null)
{
// cerco x chiave IOB...
if (MMDataService.currTagConf.ContainsKey(codIob))
{
answ = MMDataService.currTagConf[codIob];
}
}
return answ;
}
/// <summary>
/// Recupera da redis (in una chiamata soltanto) tutti i valori richiesti e compone un
/// dizionario x ottimizzare visualizzazione
/// </summary>
/// <param name="codIob"></param>
/// <returns></returns>
protected Dictionary<string, string> getTagVal(string codIob)
{
Dictionary<string, string> answ = new Dictionary<string, string>();
// recupero conf tags...
var currTags = getIobTag(codIob);
if (currTags != null && currTags.Count > 0)
{
// FIXME TODO !!!! FARE !!!! - da verificare
answ = currTags.ToDictionary(x => x.TagLocation, x => MMDataService.getTagConf(x.TagLocation));
}
return answ;
}
protected override async Task OnInitializedAsync()
{
await setupConf();
await ReloadData();
StartTimer();
}
#endregion Protected Methods
#region Private Fields
private static System.Timers.Timer fastTimer = new System.Timers.Timer(4000);
private static NLog.Logger Log = LogManager.GetCurrentClassLogger();
private static System.Timers.Timer slowTimer = new System.Timers.Timer(300000);
private List<ConfigModel>? CurrConfig = null;
#endregion Private Fields
#region Private Methods
private async Task ReloadData()
{
#if DEBUG
// hack: legge 4 volte i dati x stressare sistema
var singleData = await MMDataService.MseGetAll();
ListMSE = new List<MappaStatoExpl>();
for (int i = 0; i < 4; i++)
{
ListMSE.AddRange(singleData);
}
#else
ListMSE = await MMDataService.MseGetAll();
#endif
}
private async Task setupConf()
{
CurrConfig = await MMDataService.ConfigGetAll();
if (CurrConfig != null && CurrConfig.Count > 0)
{
// sistemo i parametri opzionali...
getConfValInt("keepAliveMin", ref keepAliveMin);
getConfValInt("MON_maxCol", ref maxCol);
int intDoAnim = 0;
getConfValInt("doAnimate", ref intDoAnim);
doAnimate = intDoAnim == 1;
getConfValInt("pageRefreshSec", ref slowRefreshSec);
getConfValInt("MSE_cacheDuration", ref fastRefreshSec);
getConfVal("sART", ref showArt);
Log.Info($"Effettuato setup parametri | keepAlive: {keepAliveMin} | MaxCol: {maxCol} | doAnimate: {doAnimate} | slowRefreshSec: {slowRefreshSec} | fastRefreshSec: {fastRefreshSec}");
}
}
#endregion Private Methods
}
}