using Microsoft.AspNetCore.Components;
using MP.Data;
using MP.Core.Conf;
using MP.Data.DbModels;
using MP.Data.Services;
using Newtonsoft.Json;
using NLog;
using MP.Core.DTO;
using MP.Data.Translate;
namespace MP.MON.Components.Pages
{
public partial class Index : ComponentBase, IDisposable
{
#region Public Methods
public void Dispose()
{
MMDataService.dataPipe.EA_NewMessage -= DataPipe_EA_NewMessage;
MMDataService.blinkPipe.EA_NewMessage -= BlinkPipe_EA_NewMessage;
}
#endregion Public Methods
#region Protected Fields
protected bool doAnimate = true;
protected bool scrollText = false;
protected bool showTC = false;
protected int fastRefreshMs = 1000;
protected int keepAliveMin = 1;
protected int maxCol = 6;
protected bool newDisplay = true;
protected string showArt = "";
#endregion Protected Fields
#region Protected Properties
[Inject]
protected IConfiguration config { get; set; } = null!;
///
/// Numero righe mappa: calcolato da num elementi e numMax x riga...
///
protected int mapNRow
{
get
{
int answ = 1;
int numElems = ListMSE != null ? ListMSE.Count : 1;
answ = (int)Math.Ceiling((double)numElems / maxCol);
return answ;
}
}
[Inject]
protected MonDataFeeder MMDataService { get; set; } = null!;
[Inject]
protected NavigationManager NavMan { get; set; } = null!;
[Inject]
protected NavigationManager NavManager { get; set; } = null!;
#endregion Protected Properties
#region Protected Methods
///
/// Recupera il valore e se trovato aggiorna
///
/// Valore da cercare
/// String in cui salvare il valore se trovato
///
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;
}
///
/// Recupera il valore e se trovato aggiorna
///
/// Valore da cercare
///
protected bool getConfValBool(string chiave)
{
bool answ = false;
if (CurrConfig != null && CurrConfig.Count > 0)
{
// sistemo i parametri opzionali...
ConfigModel? risultato = CurrConfig.FirstOrDefault(x => x.Chiave == chiave);
if (risultato != null)
{
bool.TryParse(risultato.Valore, out answ);
}
}
return answ;
}
///
/// Recupera il valore e se trovato aggiorna
///
/// Valore da cercare
/// Int in cui salvare il valore se trovato
///
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;
}
///
/// Recupera da conf eventuale setup tag dell'IOB indicato
///
///
///
protected List? getIobTag(string codIob)
{
List? answ = null;
if (MMDataService.currTagConf != null)
{
// cerco x chiave IOB...
if (MMDataService.currTagConf.ContainsKey(codIob))
{
answ = MMDataService.currTagConf[codIob];
}
}
return answ;
}
///
/// Recupera da redis (in una chiamata soltanto) tutti i valori richiesti e compone un
/// dizionario x ottimizzare visualizzazione
///
///
///
protected Dictionary getTagVal(string codIob)
{
Dictionary answ = new Dictionary();
// 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;
}
///
/// restituisce MSE dato indice
///
/// Indice MSE richiesto
///
protected MappaStatoExplDTO? MseById(int mseIdx)
{
MappaStatoExplDTO? answ = null;
try
{
if (ListMSE != null && ListMSE.Count > mseIdx)
{
answ = DbDto.Mse2DTO(ListMSE[mseIdx]);
}
}
catch (Exception exc)
{
Log.Error($"Eccezione in recupero MSE: richiesto idx {mseIdx} | trovati {listMacchine.Count}{Environment.NewLine}{exc}");
}
return answ;
}
protected override async Task OnInitializedAsync()
{
await setupConf();
MMDataService.dataPipe.EA_NewMessage += DataPipe_EA_NewMessage;
MMDataService.blinkPipe.EA_NewMessage += BlinkPipe_EA_NewMessage;
cssOverlayOff = config.GetValue("ServerConf:cssOverlayOff") ?? "";
}
#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 string CodGruppo = "";
private string cssOverlayOff = "bg-dark text-light opacity-100";
private List? CurrConfig = null;
private bool doBlink = false;
private List listMacchine = new List();
private List? ListMSE = null;
///
/// num max caratteri prima di abilitare scrolling testo
///
private int maxChar4Scroll = 10;
///
/// Moltiplicatore di base dei caratteri espresso in rem
///
private string charMult = "1rem";
///
/// Moltiplicatore dimensione titolo scheda detail
///
private string titleMult = "2.5rem";
///
/// css per impostare una luminosiità custom x andare a aumentare la brillantezza dei colori a livello di singolo blocco DetailMSE
///
private string brightCss = "";
private Random rnd = new Random();
#endregion Private Fields
#region Private Properties
#endregion Private Properties
#region Private Methods
///
/// Ricezione evento blink
///
///
///
private void BlinkPipe_EA_NewMessage(object? sender, EventArgs e)
{
PubSubEventArgs currArgs = (PubSubEventArgs)e;
// conversione on-the-fly List --> allarmi
if (!string.IsNullOrEmpty(currArgs.newMessage))
{
try
{
var dataRaw = JsonConvert.DeserializeObject(currArgs.newMessage);
if (dataRaw != null)
{
bool.TryParse($"{dataRaw}", out doBlink);
}
}
catch
{ }
InvokeAsync(() =>
{
StateHasChanged();
});
}
}
///
/// Ricevuto nuovi dati da mostrare!
///
///
///
///
private void DataPipe_EA_NewMessage(object? sender, EventArgs e)
{
PubSubEventArgs currArgs = (PubSubEventArgs)e;
// conversione on-the-fly List --> allarmi
if (!string.IsNullOrEmpty(currArgs.newMessage))
{
try
{
List? dataList = JsonConvert.DeserializeObject>(currArgs.newMessage);
if (dataList != null)
{
// se ho filtro macchine --> tengo solo quelle che mi tornano...
if (listMacchine != null && listMacchine.Count > 0)
{
List listaFilt = new List();
foreach (var item in listMacchine)
{
var rigaFilt = dataList.FirstOrDefault(x => x.IdxMacchina == item.IdxMacchina);
if (rigaFilt != null)
{
listaFilt.Add(rigaFilt);
}
}
dataList = listaFilt;
}
#if DEBUG
// hack: legge 3 volte i dati x stressare sistema
var singleData = dataList;
ListMSE = new List();
for (int i = 0; i < 3; i++)
{
ListMSE.AddRange(singleData);
}
#else
ListMSE = dataList;
#endif
}
}
catch
{ }
}
InvokeAsync(() =>
{
StateHasChanged();
});
}
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);
newDisplay = getConfValBool("MON_newDisplay");
string rawData = "";
getConfVal("doAnimate", ref rawData);
doAnimate = rawData == "1";
getConfVal("MON_scrollText", ref rawData);
scrollText = rawData.ToLower() == "true";
getConfVal("MON_showTC", ref rawData);
showTC = rawData.ToLower() == "true";
getConfVal("sART", ref showArt);
// con luminosità, dimensione carattere e num char x andare a capo x gestione specifica in caso di "allargamento componenti"
getConfValInt("MON_maxChar4Scroll", ref maxChar4Scroll);
getConfVal("MON_CharMult", ref charMult);
getConfVal("MON_NameMult", ref titleMult);
getConfVal("MON_BrightCss", ref brightCss);
Log.Info($"setupConf | Effettuato setup parametri | keepAlive: {keepAliveMin} | MaxCol: {maxCol} | doAnimate: {doAnimate} | fastRefreshMs: {fastRefreshMs}");
}
// verifico se ho richiesta filtro x codGruppo...
CodGruppo = NavMan.ExtractQueryStringByKey("CodGruppo");
if (!string.IsNullOrEmpty(CodGruppo))
{
listMacchine = await MMDataService.MacchineGetByGruppo(CodGruppo);
}
}
#endregion Private Methods
}
}