Browse Source

Add bulk send to scenario

tznind 9 months ago
parent
commit
0ddc11c41b
1 changed files with 193 additions and 0 deletions
  1. 193 0
      UICatalog/Scenarios/AnsiEscapeSequenceRequests.cs

+ 193 - 0
UICatalog/Scenarios/AnsiEscapeSequenceRequests.cs

@@ -1,4 +1,7 @@
+using System;
 using System.Collections.Generic;
+using System.Linq;
+using System.Text;
 using Terminal.Gui;
 
 namespace UICatalog.Scenarios;
@@ -7,6 +10,18 @@ namespace UICatalog.Scenarios;
 [ScenarioCategory ("Ansi Escape Sequence")]
 public sealed class AnsiEscapeSequenceRequests : Scenario
 {
+    private GraphView _graphView;
+
+    private DateTime start = DateTime.Now;
+    private ScatterSeries _sentSeries;
+    private ScatterSeries _answeredSeries;
+
+    private List<DateTime> sends = new ();
+
+    private object lockAnswers = new object ();
+    private Dictionary<DateTime, string> answers = new ();
+    private Label _lblSummary;
+
     public override void Main ()
     {
         // Init
@@ -177,6 +192,184 @@ public sealed class AnsiEscapeSequenceRequests : Scenario
             Height = Dim.Fill ()
         };
 
+        var lbl = new Label ()
+        {
+            Text = "This scenario tests Ansi request/response processing. Use the TextView to ensure regular user interaction continues as normal during sends",
+            Height = 2,
+            Width = Dim.Fill ()
+        };
+
+        Application.AddTimeout (
+                                TimeSpan.FromMilliseconds (1000),
+                                () =>
+                                {
+                                    lock (lockAnswers)
+                                    {
+                                        UpdateGraph ();
+
+                                        UpdateResponses ();
+                                    }
+
+
+
+                                    return true;
+                                });
+
+        var tv = new TextView ()
+        {
+            Y = Pos.Bottom (lbl),
+            Width = Dim.Percent (50),
+            Height = Dim.Fill ()
+        };
+
+
+        var lblDar = new Label ()
+        {
+            Y = Pos.Bottom (lbl),
+            X = Pos.Right (tv) + 1,
+            Text = "DAR per second",
+        };
+        var cbDar = new NumericUpDown ()
+        {
+            X = Pos.Right (lblDar),
+            Y = Pos.Bottom (lbl),
+            Value = 0,
+        };
+
+        cbDar.ValueChanging += (s, e) =>
+        {
+            if (e.NewValue < 0 || e.NewValue > 20)
+            {
+                e.Cancel = true;
+            }
+        };
+        w.Add (cbDar);
+
+        int lastSendTime = Environment.TickCount;
+        object lockObj = new object ();
+        Application.AddTimeout (
+                                TimeSpan.FromMilliseconds (50),
+                                () =>
+                                {
+                                    lock (lockObj)
+                                    {
+                                        if (cbDar.Value > 0)
+                                        {
+                                            int interval = 1000 / cbDar.Value; // Calculate the desired interval in milliseconds
+                                            int currentTime = Environment.TickCount; // Current system time in milliseconds
+
+                                            // Check if the time elapsed since the last send is greater than the interval
+                                            if (currentTime - lastSendTime >= interval)
+                                            {
+                                                SendDar (); // Send the request
+                                                lastSendTime = currentTime; // Update the last send time
+                                            }
+                                        }
+                                    }
+
+                                    return true;
+                                });
+
+
+        _graphView = new GraphView ()
+        {
+            Y = Pos.Bottom (cbDar),
+            X = Pos.Right (tv),
+            Width = Dim.Fill (),
+            Height = Dim.Fill (1)
+        };
+
+        _lblSummary = new Label ()
+        {
+            Y = Pos.Bottom (_graphView),
+            X = Pos.Right (tv),
+            Width = Dim.Fill ()
+        };
+
+        SetupGraph ();
+
+        w.Add (lbl);
+        w.Add (lblDar);
+        w.Add (cbDar);
+        w.Add (tv);
+        w.Add (_graphView);
+        w.Add (_lblSummary);
+
         return w;
     }
+    private void UpdateResponses ()
+    {
+        _lblSummary.Text = GetSummary ();
+        _lblSummary.SetNeedsDisplay ();
+    }
+
+    private string GetSummary ()
+    {
+        if (answers.Count == 0)
+        {
+            return "No requests sent yet";
+        }
+
+        var last = answers.Last ().Value;
+
+        var unique = answers.Values.Distinct ().Count ();
+        var total = answers.Count;
+
+        return $"Last:{last} U:{unique} T:{total}";
+    }
+
+    private void SetupGraph ()
+    {
+
+        _graphView.Series.Add (_sentSeries = new ScatterSeries ());
+        _graphView.Series.Add (_answeredSeries = new ScatterSeries ());
+
+        _sentSeries.Fill = new GraphCellToRender (new Rune ('.'), new Attribute (ColorName16.BrightGreen, ColorName16.Black));
+        _answeredSeries.Fill = new GraphCellToRender (new Rune ('.'), new Attribute (ColorName16.BrightRed, ColorName16.Black));
+
+        // Todo:
+        // _graphView.Annotations.Add (_sentSeries new PathAnnotation {});
+
+        _graphView.CellSize = new PointF (1, 1);
+        _graphView.MarginBottom = 2;
+        _graphView.AxisX.Increment = 1;
+        _graphView.AxisX.Text = "Seconds";
+        _graphView.GraphColor = new Attribute (Color.Green, Color.Black);
+    }
+
+    private void UpdateGraph ()
+    {
+        _sentSeries.Points = sends
+                             .GroupBy (ToSeconds)
+                             .Select (g => new PointF (g.Key, g.Count ()))
+                             .ToList ();
+
+        _answeredSeries.Points = answers.Keys
+                                        .GroupBy (ToSeconds)
+                                        .Select (g => new PointF (g.Key, g.Count ()))
+                                        .ToList ();
+        //  _graphView.ScrollOffset  = new PointF(,0);
+        _graphView.SetNeedsDisplay ();
+
+    }
+
+    private int ToSeconds (DateTime t)
+    {
+        return (int)(DateTime.Now - t).TotalSeconds;
+    }
+
+    private void SendDar ()
+    {
+        sends.Add (DateTime.Now);
+        var result = Application.Driver.WriteAnsiRequest (EscSeqUtils.CSI_SendDeviceAttributes);
+        HandleResponse (result);
+    }
+
+    private void HandleResponse (string response)
+    {
+        lock (lockAnswers)
+        {
+            answers.Add (DateTime.Now, response);
+        }
+    }
 }