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(); 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 listMaxVal = new Dictionary(); protected Dictionary listMinVal = new Dictionary(); protected Dictionary> plotData = new Dictionary>(); #endregion Protected Fields #region Protected Properties protected string _selTool { get; set; } = ""; protected List LevelVal { get { List answ = new List(); 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 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.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>(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()); // creo un oggetto della lunghezza desiderata... var emptyData = new List(); 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 } }