Files
Samuele Locatelli 78361306e8 Fix pagina parametri x invoke multiplo
- sistemato aprametro filtro max refresh
- fix max parametri selezioanbili
2022-10-27 15:28:48 +02:00

224 lines
7.4 KiB
C#

using Microsoft.AspNetCore.Components;
using MP.MONO.Core.DTO;
using MP.MONO.Data;
using Newtonsoft.Json;
using NLog;
namespace MP.MONO.UI.Components
{
public partial class ToolsPlotRT
{
#region Public Properties
[Parameter]
public int maxRecord { get; set; } = 120;
[Parameter]
public double sampleSecMin { get; set; } = 1;
[Parameter]
public string SelectedTool
{
get
{
return _selTool;
}
set
{
// controllo se è variato
if (_selTool != value)
{
// salvo
_selTool = value;
// cerco se ho in dictionary...
if (!plotData.ContainsKey(value))
{
DateTime adesso = DateTime.Now;
// creo un oggetto della lunghezza desiderata...
var emptyData = new List<chartJsData.chartJsTSerie>();
for (int i = -maxRecord; i < 0; i++)
{
emptyData.Add(new chartJsData.chartJsTSerie() { x = adesso.AddSeconds(i), y = 0 });
}
plotData.Add(value, emptyData);
}
}
}
}
public string ToolId
{
get => _selTool.Replace(" ", "_");
}
#endregion Public Properties
#region Protected Fields
protected DateTime lastRec = DateTime.Now.AddMinutes(-1);
protected Dictionary<string, string> listMaxVal = new Dictionary<string, string>();
protected Dictionary<string, string> listMinVal = new Dictionary<string, string>();
protected Dictionary<string, List<chartJsData.chartJsTSerie>> plotData = new Dictionary<string, List<chartJsData.chartJsTSerie>>();
#endregion Protected Fields
#region Protected Properties
protected string _selTool { get; set; } = "";
protected List<chartJsData.chartJsTSerie> LevelVal
{
get
{
List<chartJsData.chartJsTSerie> answ = new List<chartJsData.chartJsTSerie>();
if (plotData.ContainsKey(SelectedTool))
{
answ = plotData[SelectedTool];
}
return answ;
}
}
protected string? MaxVal
{
get
{
string answ = "0";
if (listMaxVal != null && listMaxVal.Count > 0)
{
answ = listMaxVal[SelectedTool] ?? "0";
}
return answ;
}
}
protected string? MinVal
{
get
{
string answ = "0";
if (listMinVal != null && listMinVal.Count > 0)
{
answ = listMinVal[SelectedTool] ?? "0";
}
return answ;
}
}
#endregion Protected Properties
#region Protected Methods
protected List<string> getFillColors(string alpha)
{
List<string> answ = new List<string>();
answ.Add($"rgba(108, 164, 254, {alpha})");
return answ;
}
protected List<string> getLineColors(string alpha)
{
List<string> answ = new List<string>();
answ.Add($"rgba(54, 82, 254, {alpha})");
return answ;
}
protected List<string> getPointColors(string alpha)
{
List<string> answ = new List<string>();
answ.Add($"rgba(108, 118, 158, {alpha})");
return answ;
}
protected override async Task OnInitializedAsync()
{
//await ReloadData();
await Task.Delay(1);
MMDataService.toolsPipe.EA_NewMessage += ToolsPipe_EA_NewMessage;
}
#endregion Protected Methods
#region Private Fields
private static NLog.Logger Log = LogManager.GetCurrentClassLogger();
#endregion Private Fields
#region Private Methods
//private int inputValue = 123;
private void ToolsPipe_EA_NewMessage(object? sender, EventArgs e)
{
DateTime adesso = DateTime.Now;
if (lastRec.AddSeconds(sampleSecMin) < adesso)
{
PubSubEventArgs currArgs = (PubSubEventArgs)e;
if (!string.IsNullOrEmpty(currArgs.newMessage))
{
lastRec = adesso;
try
{
var ListRecords = JsonConvert.DeserializeObject<List<DisplayDataDTO>>(currArgs.newMessage);
// prendo SOLO il dato della TS richiesta
if (ListRecords != null)
{
// accodo in blocco tutti i valori...
foreach (var item in ListRecords)
{
// cerco se ci sono min/max del valore indicato...
if (!listMinVal.ContainsKey(item.Title))
{
listMinVal.Add(item.Title, $"{item.MinVal:N0}");
}
if (!listMaxVal.ContainsKey(item.Title))
{
listMaxVal.Add(item.Title, $"{item.MaxVal:N0}");
}
// verifico dictionary se mancasse...
if (!plotData.ContainsKey(item.Title))
{
//plotData.Add(item.Title, new List<chartJsData.chartJsTSerie>());
// creo un oggetto della lunghezza desiderata...
var emptyData = new List<chartJsData.chartJsTSerie>();
for (int i = -maxRecord; i < 0; i++)
{
emptyData.Add(new chartJsData.chartJsTSerie() { x = adesso.AddSeconds(i), y = 0 });
}
plotData.Add(item.Title, emptyData);
}
// ora accodo il valore
if (plotData.ContainsKey(item.Title))
{
plotData[item.Title].Add(new chartJsData.chartJsTSerie() { x = adesso, y = item.ValueNum });
// verifico limite visualizzazione
if (plotData[item.Title].Count > maxRecord)
{
plotData[item.Title].RemoveRange(0, plotData[item.Title].Count - maxRecord);
}
}
}
}
}
catch
{ }
}
InvokeAsync(() =>
{
StateHasChanged();
});
}
else
{
Log.Trace("scartato valore");
}
}
#endregion Private Methods
}
}