using global::Microsoft.AspNetCore.Components; using Microsoft.JSInterop; using MongoDB.Driver.Linq; using MP.Data; using MP.Data.DatabaseModels; using MP.Data.Services; using NLog; using System.Text; using static EgwCoreLib.Utils.DtUtils; namespace MP_TAB3.Components { public partial class AlarmsMan { #region Public Properties [Parameter] public MappaStatoExpl? RecMSE { get; set; } = null; #endregion Public Properties #region Public Methods /// /// Aggiorno valori produzione alla data richiesta... /// /// public async Task doUpdate() { isProcessing = true; await Task.Delay(1); if (!string.IsNullOrEmpty(IdxMaccSel)) { ListComplete = await TabDServ.AlarmLogListFilt(IdxMaccSel, CurrPeriodo.Inizio, CurrPeriodo.Fine, false); TotalCount = ListComplete.Count; // esegue paginazione UpdateTable(); } isProcessing = false; await Task.Delay(1); } #endregion Public Methods #region Protected Fields protected bool isProcessing = false; protected bool isSending = false; protected int NumRecPage = 10; protected int PageNum = 1; protected int TotalCount = 0; #endregion Protected Fields #region Protected Properties protected List AlarmsDest { get; set; } = new List(); [Inject] protected IConfiguration config { get; set; } = null!; [Inject] protected IJSRuntime JSRuntime { get; set; } = null!; protected List ListComplete { get; set; } = new List(); protected List ListPaged { get; set; } = new List(); [Inject] protected MailService MailServ { get; set; } = null!; [Inject] protected MessageService MServ { get; set; } = null!; [Inject] protected TabDataService TabDServ { get; set; } = null!; #endregion Protected Properties #region Protected Methods protected async Task DoAck(AlarmLogModel currAlarm) { if (!await JSRuntime.InvokeAsync("confirm", $"Sicuro di voler registrare ACK dell'allarme?{Environment.NewLine}[{currAlarm.ValDecoded}]")) return; isSending = true; await Task.Delay(1); TabDServ.AlarmLogSetAck(currAlarm.AlarmLogId, DateTime.Now, $"{MServ.MatrOpr} - {MServ.CognomeNome}"); isSending = false; await Task.Delay(1); await InvokeAsync(StateHasChanged); } protected override async Task OnInitializedAsync() { await Task.Delay(1); if (RecMSE != null) { IdxMaccSel = RecMSE.IdxMacchina; DateTime fine = DateTime.Today.AddDays(1); DateTime inizio = fine.AddDays(-8); CurrPeriodo = new Periodo(inizio, fine); } // gestione conf allarmi var rawDest = config.GetValue("OptConf:AlarmDest"); if (!string.IsNullOrEmpty(rawDest)) { AlarmsDest = rawDest.Split(",").ToList(); } else { AlarmsDest = new List() { "samuele@steamware.net" }; } alarmMinDuration = config.GetValue("OptConf:AlarmMinDuration"); } protected override async Task OnParametersSetAsync() { if (RecMSE != null && !RecMSE.MostlyEquals(lastRecMSE)) { lastRecMSE = RecMSE; await doUpdate(); } } protected void SaveNumRec(int newNum) { NumRecPage = newNum; UpdateTable(); } protected void SavePage(int newNum) { PageNum = newNum; UpdateTable(); } protected async Task SendNotify(AlarmLogModel currAlarm) { if (!await JSRuntime.InvokeAsync("confirm", $"Sicuro di voler inviare notifica allarme?{Environment.NewLine}[{currAlarm.ValDecoded}]")) return; isSending = true; await Task.Delay(1); await InvokeAsync(StateHasChanged); //List dest = new List(); //dest.Add("samuele@steamware.net"); string oggetto = $"Notifica allarme impianto {currAlarm.MachineId}"; StringBuilder sb = new StringBuilder(); sb.AppendLine("Attenzione: allarme presente per un periodo superiore alla soglia di controllo."); sb.AppendLine(); sb.AppendLine($"{currAlarm.ValDecoded}"); sb.AppendLine(); sb.AppendLine($"Info Area: {currAlarm.MemAddress}.{currAlarm.MemIndex} | Status {currAlarm.StatusVal}"); sb.AppendLine(); string corpo = sb.ToString().Replace($"{Environment.NewLine}", "
"); MailKitMailData mData = new MailKitMailData() { To = AlarmsDest, Subject = oggetto, Body = corpo }; bool done = await MailServ.SendAsync(mData); if (done) { // salvo invio Notifica allarme TabDServ.AlarmLogSetNotify(currAlarm.AlarmLogId, DateTime.Now); Log.Info($"Notifica allarme {currAlarm.AlarmLogId} inviato a {string.Join(",", AlarmsDest)}"); } else { Log.Error($"Errore in invio email allarmi attivi per {currAlarm.AlarmLogId} | tentato invio a {string.Join(",", AlarmsDest)}"); } isSending = false; await Task.Delay(1); await InvokeAsync(StateHasChanged); } protected async Task SetMacc(string selIdxMacc) { isProcessing = true; await Task.Delay(10); IdxMaccSel = selIdxMacc; await doUpdate(); isProcessing = false; await Task.Delay(10); } protected async Task SetPeriodo(Periodo newPeriodo) { CurrPeriodo = newPeriodo; await doUpdate(); } protected void UpdateTable() { // esegue paginazione if (TotalCount > NumRecPage) { ListPaged = ListComplete.Skip((PageNum - 1) * NumRecPage).Take(NumRecPage).ToList(); } else { ListPaged = ListComplete; } } #endregion Protected Methods #region Private Fields private static Logger Log = LogManager.GetCurrentClassLogger(); /// /// durata minima allarmi x abilitare invio email di notifica /// private decimal alarmMinDuration = 1; #endregion Private Fields #region Private Properties private Periodo CurrPeriodo { get; set; } = new Periodo(); private string IdxMaccSel { get; set; } = ""; private MappaStatoExpl? lastRecMSE { get; set; } = null; #endregion Private Properties } }