Files
mapo-mono/MP.MONO.UI/Components/AlarmsOverview.razor.cs
2022-03-18 08:13:05 +01:00

78 lines
2.6 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Components;
using System.Net.Http;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.AspNetCore.Components.Forms;
using Microsoft.AspNetCore.Components.Routing;
using Microsoft.AspNetCore.Components.Web;
using Microsoft.AspNetCore.Components.Web.Virtualization;
using Microsoft.JSInterop;
using MP.MONO.Core.DTO;
using MP.MONO.Data;
using MP.MONO.UI;
using MP.MONO.UI.Shared;
using MP.MONO.UI.Components;
using MP.MONO.UI.Data;
using Newtonsoft.Json;
namespace MP.MONO.UI.Components
{
public partial class AlarmsOverview
{
[Parameter]
public EventCallback<int> messageReceived { get; set; }
private List<DisplayDataDTO>? ListRecords { get; set; } = new List<DisplayDataDTO>();
protected override async Task OnInitializedAsync()
{
await ReloadData();
MMDataService.alarmPipe.EA_NewMessage += AlarmPipe_EA_NewMessage;
}
private void AlarmPipe_EA_NewMessage(object? sender, EventArgs e)
{
PubSubEventArgs currArgs = (PubSubEventArgs)e;
// conversione on-the-fly List<string> --> allarmi
if (!string.IsNullOrEmpty(currArgs.newMessage))
{
try
{
var alarmList = JsonConvert.DeserializeObject<List<string>>(currArgs.newMessage);
if (alarmList != null)
{
ListRecords = alarmList.Select(x => new DisplayDataDTO()
{
IsNumeric = false,
Title = $"{DateTime.Now:yyyy-MM-dd HH:mm:ss}",
Value = x,
Order = ListRecords == null ? 0 : ListRecords.Count
}).ToList();
messageReceived.InvokeAsync(alarmList.Count);
}
}
catch
{ }
}
InvokeAsync(() =>
{
StateHasChanged();
});
}
protected async Task ReloadData()
{
var alarmList = await MMDataService.getAlarms();
ListRecords = alarmList.Select(x => new DisplayDataDTO()
{
IsNumeric = false,
Title = $"{DateTime.Now:yyyy-MM-dd HH:mm:ss}",
Value = x,
Order = ListRecords == null ? 0 : ListRecords.Count
}).ToList();
}
}
}