Files
mapo-core/MP-TAB3/Components/AlarmsMan.razor.cs
T
Samuele Locatelli c8bb1065c7 Fix naming (maybe)
2023-12-18 11:40:46 +01:00

214 lines
6.7 KiB
C#

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
/// <summary>
/// Aggiorno valori produzione alla data richiesta...
/// </summary>
/// <param name="newDate"></param>
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<string> AlarmsDest { get; set; } = new List<string>();
[Inject]
protected IConfiguration config { get; set; } = null!;
[Inject]
protected IJSRuntime JSRuntime { get; set; } = null!;
protected List<AlarmLogModel> ListComplete { get; set; } = new List<AlarmLogModel>();
protected List<AlarmLogModel> ListPaged { get; set; } = new List<AlarmLogModel>();
[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<bool>("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);
await doUpdate();
}
var rawDest = config.GetValue<string>("AlarmDest");
if (!string.IsNullOrEmpty(rawDest))
{
AlarmsDest = rawDest.Split(",").ToList();
}
else
{
AlarmsDest = new List<string>() { "samuele@steamware.net" };
}
}
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<bool>("confirm", $"Sicuro di voler inviare notifica allarme?{Environment.NewLine}[{currAlarm.ValDecoded}]"))
return;
isSending = true;
await Task.Delay(1);
await InvokeAsync(StateHasChanged);
//List<string> dest = new List<string>();
//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($"<b>{currAlarm.ValDecoded}</b>");
sb.AppendLine();
sb.AppendLine($"Info Area: {currAlarm.MemAddress}.{currAlarm.MemIndex} | Status {currAlarm.StatusVal}");
sb.AppendLine();
string corpo = sb.ToString().Replace($"{Environment.NewLine}", "<br/>");
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();
#endregion Private Fields
#region Private Properties
private Periodo CurrPeriodo { get; set; } = new Periodo();
private string IdxMaccSel { get; set; } = "";
#endregion Private Properties
}
}