using Microsoft.AspNetCore.Components; using MP.MONO.Core.DTO; using MP.MONO.Data; using Newtonsoft.Json; namespace MP.MONO.UI.Components { public partial class ParamPlot { #region Private Fields private int maxRecord = 120; private bool showParam = false; #endregion Private Fields #region Protected Fields protected DateTime lastRec = DateTime.Now.AddMinutes(-1); protected Dictionary> plotData = new Dictionary>(); protected List LevelVal { get => plotData[SelectedParam] ?? new List(); } protected double sampleSecMin = 1; #endregion Protected Fields #region Private Properties private List? ListRecords { get; set; } = null; #endregion Private Properties #region Protected Properties protected string _selParam { get; set; } = ""; protected string? MaxVal { get; set; } = null; protected string? MinVal { get; set; } = null; protected Dictionary listMinVal = new Dictionary(); protected Dictionary listMaxVal = new Dictionary(); #endregion Protected Properties #region Public Properties [Parameter] public string SelectedParam { get { return _selParam; } set { // controllo se è variato if (_selParam != value) { // salvo _selParam = value; // cerco se ho in dictionary... if (!plotData.ContainsKey(value)) { plotData.Add(value, new List()); } } } } #endregion Public Properties #region Private Methods //private int inputValue = 123; private void ParametersPipe_EA_NewMessage(object? sender, EventArgs e) { DateTime adesso = DateTime.Now; if (lastRec.AddSeconds(sampleSecMin) < adesso) { PubSubEventArgs currArgs = (PubSubEventArgs)e; if (!string.IsNullOrEmpty(currArgs.newMessage)) { try { var ListRecords = JsonConvert.DeserializeObject>(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()); } // 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); } } } #if false var newData = ListRecords.Where(x => x.Title == SelectedParam).FirstOrDefault(); // lo accodo if (newData != null) { if (string.IsNullOrEmpty(MinVal) || string.IsNullOrEmpty(MaxVal)) { MinVal = $"{newData.MinVal:N0}"; MaxVal = $"{newData.MaxVal:N0}"; } LevelVal.Add(new chartJsData.chartJsTSerie() { x = adesso, y = newData.ValueNum }); if (LevelVal.Count > maxRecord) { LevelVal.RemoveRange(0, LevelVal.Count - maxRecord); } } #endif } } catch { } } lastRec = adesso; } InvokeAsync(() => { StateHasChanged(); }); } private void toggleParam() { showParam = !showParam; } #endregion Private Methods #region Protected Methods protected List getFillColors(string alpha) { List answ = new List(); answ.Add($"rgba(108, 164, 254, {alpha})"); return answ; } protected List getLineColors(string alpha) { List answ = new List(); answ.Add($"rgba(54, 82, 254, {alpha})"); return answ; } protected List getPointColors(string alpha) { List answ = new List(); answ.Add($"rgba(108, 118, 158, {alpha})"); return answ; } protected override async Task OnInitializedAsync() { //await ReloadData(); await Task.Delay(1); MMDataService.parametersPipe.EA_NewMessage += ParametersPipe_EA_NewMessage; } #endregion Protected Methods } }