Files
mapo-core/MP-TAB2/MP-TAB/Components/Pages/StatusMap.razor.cs
T
2023-12-18 11:36:24 +01:00

215 lines
6.9 KiB
C#

using global::System;
using global::System.Collections.Generic;
using global::System.Linq;
using global::System.Threading.Tasks;
using global::Microsoft.AspNetCore.Components;
using System.Net.Http;
using System.Net.Http.Json;
using Microsoft.AspNetCore.Components.Forms;
using Microsoft.AspNetCore.Components.Routing;
using Microsoft.AspNetCore.Components.Sections;
using Microsoft.AspNetCore.Components.Web;
using Microsoft.AspNetCore.Components.Web.Virtualization;
using Microsoft.JSInterop;
using MP.Data;
using MP.Data.DatabaseModels;
using MP.Data.DTO;
using MP.Data.Services;
using MP_TAB;
using MP_TAB.Components;
using NLog;
using MP.Data.Conf;
using Newtonsoft.Json;
using static System.Runtime.InteropServices.JavaScript.JSType;
namespace MP_TAB.Components.Pages
{
public partial class StatusMap
{
#region Protected Fields
protected bool doAnimate = true;
protected int keepAliveMin = 1;
protected int maxCol = 6;
protected string showArt = "";
protected int slowRefreshSec = 300;
#endregion Protected Fields
#region Protected Properties
[Inject]
protected StatusData MDataService { get; set; } = null!;
[Inject]
protected MessageService MsgServ { get; set; } = null!;
protected int slowRefreshMs
{
get
{
// tempo variabile tra +/- 10% del target
int answ = rnd.Next(900, 1100) * slowRefreshSec;
return answ;
}
}
#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 (MDataService.currTagConf != null)
{
// cerco x chiave IOB...
if (MDataService.currTagConf.ContainsKey(codIob))
{
answ = MDataService.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 => MDataService.getTagConf(x.TagLocation));
}
return answ;
}
protected override async Task OnInitializedAsync()
{
CurrListMSE = null;
await setupConf();
//await InvokeAsync(StateHasChanged);
//await Task.Delay(500);
await ReloadData();
}
#endregion Protected Methods
#region Private Fields
private static NLog.Logger Log = LogManager.GetCurrentClassLogger();
private static System.Timers.Timer slowTimer = new System.Timers.Timer(300000);
private List<ConfigModel>? CurrConfig = null;
private Random rnd = new Random();
#endregion Private Fields
#region Private Properties
private List<MappaStatoExpl>? CurrListMSE { get; set; } = null;
#endregion Private Properties
#region Private Methods
private async Task ReloadData()
{
CurrListMSE = await MDataService.MseGetAll();
//try
//{
// // salvo in LocalStorage...
// await MsgServ.SaveMse(CurrListMSE);
//}
//catch (Exception exc)
//{
// Log.Error($"Eccezione in ReloadData{Environment.NewLine}{exc}");
//}
}
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (CurrListMSE != null)
{
// salvo in LocalStorage...
await MsgServ.SaveMse(CurrListMSE);
//return base.OnAfterRenderAsync(firstRender);
}
}
private async Task setupConf()
{
CurrConfig = await MDataService.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($"setupConf | Effettuato setup parametri | keepAlive: {keepAliveMin} | MaxCol: {maxCol} | doAnimate: {doAnimate} | slowRefreshSec: {slowRefreshSec}");
}
}
#endregion Private Methods
}
}