Files
mapo-core/MP.WASM.Mon/Client/Pages/Index.razor.cs
T
Samuele Locatelli 78cb17d8fc Cleanup MP/MON
2022-07-12 19:05:30 +02:00

268 lines
8.5 KiB
C#

using MP.Data.Conf;
using MP.Data.DatabaseModels;
using NLog;
using System.Net.Http.Json;
namespace MP.WASM.Mon.Client.Pages
{
public partial class Index : IDisposable
{
#region Public Methods
public void Dispose()
{
disposeTimers();
}
public void ElapsedFastTimer(object? source, System.Timers.ElapsedEventArgs e)
{
var pUpd = Task.Run(async () =>
{
// secondi pari --> blink, secondi dispari --> ricarica
DateTime adesso = DateTime.Now;
int resto = 0;
Math.DivRem(adesso.Second, 2, out resto);
if (resto == 0)
{
doBlink = true;
Log.Trace("Elapsed Fast Timer Blink");
}
else
{
doBlink = false;
await ReloadData();
Log.Trace("Elapsed Fast Timer reload");
}
await Task.Delay(1);
await InvokeAsync(StateHasChanged);
});
pUpd.Wait();
}
public async void ElapsedSlowTimer(object? source, System.Timers.ElapsedEventArgs e)
{
listMSE = null;
await Task.Delay(1);
Log.Info("Elapsed Slow Timer --> full page reload");
// dispongo i vari timers...
disposeTimers();
// reload pagina
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 fastRefreshMs = 1000;
protected int keepAliveMin = 1;
protected int maxCol = 6;
protected string showArt = "";
protected int slowRefreshSec = 300;
#endregion Protected Fields
#region Protected Properties
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 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);
}
}
#endif
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 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);
}
}
#endif
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 false
if (MMDataService.currTagConf != null)
{
// cerco x chiave IOB...
if (MMDataService.currTagConf.ContainsKey(codIob))
{
answ = MMDataService.currTagConf[codIob];
}
}
#endif
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>();
#if false
// 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));
}
#endif
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 bool doBlink = false;
private List<MappaStatoExpl>? listMSE = null;
#endregion Private Fields
#region Private Methods
private void disposeTimers()
{
fastTimer.Elapsed -= ElapsedFastTimer;
fastTimer.Stop();
fastTimer.Dispose();
slowTimer.Elapsed -= ElapsedSlowTimer;
slowTimer.Stop();
slowTimer.Dispose();
}
private async Task ReloadData()
{
// leggo stato server...
fastTimer.Interval = fastRefreshMs * 3;
var res = await Http.GetAsync("api/MSE/checkAlive");
Console.WriteLine("GetAsync end");
if (res.IsSuccessStatusCode)
{
#if DEBUG
// hack: legge 4 volte i dati x stressare sistema
var singleData = await Http.GetFromJsonAsync<List<MappaStatoExpl>>("api/MSE");
listMSE = new List<MappaStatoExpl>();
for (int i = 0; i < 4; i++)
{
listMSE.AddRange(singleData);
}
#else
listMSE = await Http.GetFromJsonAsync<List<MappaStatoExpl>>("api/MSE");
#endif
fastTimer.Interval = fastRefreshMs;
}
else
{
Console.WriteLine("Error in request");
listMSE = null;
await Task.Delay(100);
await InvokeAsync(() => StateHasChanged());
}
Console.WriteLine("ReloadData end");
}
private async Task setupConf()
{
#if false
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);
getConfVal("sART", ref showArt);
Log.Info($"Effettuato setup parametri | keepAlive: {keepAliveMin} | MaxCol: {maxCol} | doAnimate: {doAnimate} | slowRefreshSec: {slowRefreshSec} | fastRefreshMs: {fastRefreshMs}");
}
#endif
}
#endregion Private Methods
}
}