using Core.Models; using LiMan.DB; using LiMan.DB.DBModels; using LiMan.DB.DTO; using LiMan.UI.Data; using Microsoft.AspNetCore.Components; using Microsoft.JSInterop; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using static Core.Enum; namespace LiMan.UI.Components { public partial class InstallInfoStats : IDisposable { #region Public Methods public void Dispose() { LMDService.EnrollMessPipe.EA_NewMessage -= async (sender, e) => await EnrollMessPipe_EA_NewMessage(sender, e); LMDService.TaskMessPipe.EA_NewMessage -= async (sender, e) => await TaskMessPipe_EA_NewMessage(sender, e); MServ.EA_SelCodImp -= async () => await MServ_EA_SelCodImp(); MServ.EA_SelCodInst -= async () => await MServ_EA_SelCodInst(); } #endregion Public Methods #region Protected Properties /// /// Codice installazione selezionata /// protected string CodInstSel { get => codInstSel; set { if (codInstSel != value) { codInstSel = value; isLoading = true; var pUpd = Task.Run(async () => { await ReloadData(); }); pUpd.Wait(); isLoading = false; } } } /// /// Tipo app selezionato /// protected string CodTipoApp { get => codTipoApp; set { if (codTipoApp != value) { codTipoApp = value; isLoading = true; var pUpd = Task.Run(async () => { await ReloadData(); }); pUpd.Wait(); isLoading = false; } } } [Inject] protected LiManDataService LMDService { get; set; } = null!; [Inject] protected MessageService MServ { get; set; } = null!; #endregion Protected Properties #region Protected Methods protected override async Task OnInitializedAsync() { LMDService.EnrollMessPipe.EA_NewMessage += async (sender, e) => await EnrollMessPipe_EA_NewMessage(sender, e); LMDService.TaskMessPipe.EA_NewMessage += async (sender, e) => await TaskMessPipe_EA_NewMessage(sender, e); MServ.EA_SelCodImp += async () => await MServ_EA_SelCodImp(); MServ.EA_SelCodInst += async () => await MServ_EA_SelCodInst(); await ReloadLicData(); await ReloadData(); } #endregion Protected Methods #region Private Fields private string CodDeviceSel = ""; private string codInstSel = ""; private Dictionary DetailFilt = new Dictionary(); /// /// Ebilitazione editing parametri configurazione x impiango selezionata /// private bool DoEditConfig = false; private bool HasFiltClienti = false; private bool HasFiltImpiego = false; private List ListInstall; /// /// Lista Task in stato DONE /// private Dictionary TaskDone = new Dictionary(); /// /// Lista Task in stato REQUESTED /// private Dictionary TaskReq = new Dictionary(); /// /// Lista Task in stato RUNNING /// private Dictionary TaskRun = new Dictionary(); /// /// Versione raw serializzata della conf impianto (immutabile x modifica) /// private string TgtListRaw = ""; #endregion Private Fields #region Private Properties /// /// Impianto selezionato /// private string CodImpSel { get; set; } = ""; private string DevName { get; set; } = ""; private string codTipoApp { get; set; } = ""; private string cssBtnCli { get => HasFiltClienti ? "btn-info" : "btn-primary"; } private DateTime DtFine { get; set; } = DateTime.Today.AddHours(DateTime.Now.Hour); private DateTime DtInizio { get; set; } = DateTime.Today.AddMonths(-1); private InstallStatusDTO InfoStats { get; set; } = new InstallStatusDTO(); private bool isLoading { get; set; } = false; private Dictionary ListDevices { get; set; } = new Dictionary(); #endregion Private Properties #region Private Methods private string BtnState(int numRec) { return numRec > 0 ? "text-bg-dark" : "text-bg-secondary"; } private async Task CloseDetail() { isLoading = true; await InvokeAsync(StateHasChanged); HasFiltImpiego = false; CodImpSel = ""; DetailFilt.Clear(); isLoading = false; await InvokeAsync(StateHasChanged); } /// /// Evento refresh legato a ricezione evento da MessagePipe /// /// /// private async Task EnrollMessPipe_EA_NewMessage(object sender, EventArgs e) { PubSubEventArgs currArgs = (PubSubEventArgs)e; // qualsiasi messaggio fa scattare reload data if (!string.IsNullOrEmpty(currArgs.newMessage)) { isLoading = true; await InvokeAsync(StateHasChanged); await ReloadLicData(); await ReloadData(); isLoading = false; await InvokeAsync(StateHasChanged); } } /// /// Selezionato CodImp (PC) /// /// private async Task MServ_EA_SelCodImp() { isLoading = true; // se trovo salvate in preferenze utente le info necessarie... CodImpSel = MServ.UsrParamGet("CodImp"); HasFiltImpiego = !string.IsNullOrEmpty(CodImpSel); DetailFilt.Clear(); DetailFilt.Add("Cliente", MServ.UsrParamGet("Cliente")); DevName = MServ.UsrParamGet("PcInst"); DetailFilt.Add("PC", DevName); DetailFilt.Add("SW ver.", MServ.UsrParamGet("Version")); DetailFilt.Add("IP", MServ.UsrParamGet("PcIP")); DetailFilt.Add("Started", MServ.UsrParamGet("PcRestart")); ReloadTaskStatus(); isLoading = false; await InvokeAsync(StateHasChanged); } [Inject] protected IJSRuntime JSRuntime { get; set; } /// /// Force reboot applicazione EgwACC /// /// private async Task SendReboot() { if (!await JSRuntime.InvokeAsync("confirm", "Sicuro di voler forzare il riavvio di EgwAppControlCenter sul Deivice? In caso di update disponibile questo sarà applicato in modo automatico senza conferma utente.")) return; SendTask(Core.Enum.EgwAccTask.ForceUpdate); } /// /// Selezionato CodInstall (Cliente) /// /// private async Task MServ_EA_SelCodInst() { isLoading = true; CodInstSel = MServ.UsrParamGet("CodInst"); HasFiltClienti = !string.IsNullOrEmpty(CodInstSel); ReloadDevices(); isLoading = false; await InvokeAsync(StateHasChanged); } private async Task ReloadData() { isLoading = true; InfoStats = await LMDService.InstallStatusGetInfo(DtInizio, DtFine, CodInstSel, CodTipoApp); ReloadDevices(); isLoading = false; } private void ReloadDevices() { ListDevices = new Dictionary(); if (InfoStats != null) { // se necessario filtro... if (HasFiltClienti) { ListDevices = InfoStats .InstDevices .Where(x => x.Value.CodInst == CodInstSel) .OrderBy(x => x.Value.DevName) .ToDictionary(x => x.Key, x => x.Value); } else { ListDevices = InfoStats .InstDevices .OrderBy(x => x.Value.DevName) .ToDictionary(x => x.Key, x => x.Value); } } } private async Task ReloadLicData() { ListInstall = await LMDService.InstallazioniNextGetAll(); } /// /// Esegue update stato task /// private void ReloadTaskStatus() { TaskReq = new Dictionary(); TaskRun = new Dictionary(); TaskDone = new Dictionary(); TgtListRaw = ""; if (!string.IsNullOrEmpty(CodImpSel)) { TaskReq = LMDService.TaskReqGet(CodImpSel); TaskRun = LMDService.TaskRunGet(CodImpSel); TaskDone = LMDService.TaskDoneGet(CodImpSel); // è uno dei task done l'elenco target... if (TaskDone.ContainsKey("TargetList")) { TgtListRaw = TaskDone["TargetList"]; } } } /// /// Resetta le richeiste di task x impiango corrente /// private async Task ResetTaskReq() { if (!string.IsNullOrEmpty(CodImpSel)) { // registro la richiesta LMDService.TaskReqReset(CodImpSel); await Task.Delay(5); // mando notifica con messageservice... MServ.ReportTaskChange(); } } /// /// Invia una richiesta esecuzione task /// /// /// Parametri opzionali x svolgiimento private void SendTask(EgwAccTask reqTask, string optPar = "") { if (!string.IsNullOrEmpty(CodImpSel)) { // se parametro vuoto metto dataora... optPar = string.IsNullOrEmpty(optPar) ? $"{DateTime.Now:yyyy-MM-dd HH:mm:ss}" : optPar; // registro la richiesta LMDService.TaskReqAdd(CodImpSel, reqTask, optPar); // aggiorno stato... ReloadTaskStatus(); // mando notifica con messageservice... MServ.ReportTaskChange(); } } /// /// Evento refresh legato a ricezione evento da MessagePipe /// /// /// private async Task TaskMessPipe_EA_NewMessage(object sender, EventArgs e) { PubSubEventArgs currArgs = (PubSubEventArgs)e; // qualsiasi messaggio fa scattare reload data if (!string.IsNullOrEmpty(currArgs.newMessage)) { // verifico se sia il cod impianto selezionato, nel faso forzo refreesh... if (!string.IsNullOrEmpty(CodImpSel) && CodImpSel.Equals(currArgs.newMessage)) { isLoading = true; ReloadTaskStatus(); isLoading = false; await InvokeAsync(StateHasChanged); } } } private void TglFiltCli() { HasFiltClienti = !HasFiltClienti; if (!HasFiltClienti) { CodInstSel = ""; } } /// /// Toggle modalità editing parametri confguraziione app /// private void ToggleParamConfig() { if (!string.IsNullOrEmpty(CodImpSel)) { DoEditConfig = !DoEditConfig; } } /// /// Ricezione configurazione serializzata da inviare al device /// /// private void ConfigUpdated(string newData) { if(!string.IsNullOrEmpty(newData)) { // invio task update configurazione LMDService.TaskReqAdd(CodImpSel, EgwAccTask.TargetListUpsert, newData); } DoEditConfig = false; } #endregion Private Methods } }