From 90e662458ea453d8551a5bfd14f73c37299418c5 Mon Sep 17 00:00:00 2001 From: BlubbFish Date: Thu, 10 Aug 2017 15:10:18 +0000 Subject: [PATCH] =?UTF-8?q?[NF]=20Neue=20Anzeige=20hinzugef=C3=BCgt=20[BF]?= =?UTF-8?q?=20Graph=20zeichnet=20nun=20nur=20noch=20die=20eingestelle=20An?= =?UTF-8?q?zahl=20an=20punkten?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Mqtt-Dashboard/Dashboard.csproj | 1 + Mqtt-Dashboard/Form1.cs | 1 + Mqtt-Dashboard/Tracings/ATracings.cs | 2 +- Mqtt-Dashboard/Tracings/Graph.cs | 42 +++++++++++-------- Mqtt-Dashboard/Tracings/Meter.cs | 61 ++++++++++++++++++++++++++++ Mqtt-Dashboard/tracings.ini | 9 +++- 6 files changed, 98 insertions(+), 18 deletions(-) create mode 100644 Mqtt-Dashboard/Tracings/Meter.cs diff --git a/Mqtt-Dashboard/Dashboard.csproj b/Mqtt-Dashboard/Dashboard.csproj index 0a91e0c..b0018c4 100644 --- a/Mqtt-Dashboard/Dashboard.csproj +++ b/Mqtt-Dashboard/Dashboard.csproj @@ -62,6 +62,7 @@ + Form1.cs diff --git a/Mqtt-Dashboard/Form1.cs b/Mqtt-Dashboard/Form1.cs index ca0cf7d..4edc86b 100644 --- a/Mqtt-Dashboard/Form1.cs +++ b/Mqtt-Dashboard/Form1.cs @@ -48,6 +48,7 @@ namespace Dashboard { ATracings t; switch(ini.GetValue(tracing, "type").ToLower()) { case "graph": t = new Graph(this.sensors[ini.GetValue(tracing, "sensor").ToLower()],ini.GetSection(tracing)); break; + case "meter": t = new Meter(this.sensors[ini.GetValue(tracing, "sensor").ToLower()], ini.GetSection(tracing)); break; default: throw new ArgumentException("tracings.ini: " + tracing + " section has a missconfiguration in type"); } diff --git a/Mqtt-Dashboard/Tracings/ATracings.cs b/Mqtt-Dashboard/Tracings/ATracings.cs index 25db86c..cbd2283 100644 --- a/Mqtt-Dashboard/Tracings/ATracings.cs +++ b/Mqtt-Dashboard/Tracings/ATracings.cs @@ -6,7 +6,7 @@ using Dashboard.Sensor; namespace Dashboard.Tracings { public abstract class ATracings { protected ASensor sensor; - private Dictionary settings; + protected Dictionary settings; public ATracings(ASensor sensor, Dictionary settings) { this.sensor = sensor; diff --git a/Mqtt-Dashboard/Tracings/Graph.cs b/Mqtt-Dashboard/Tracings/Graph.cs index d899470..ad27182 100644 --- a/Mqtt-Dashboard/Tracings/Graph.cs +++ b/Mqtt-Dashboard/Tracings/Graph.cs @@ -3,18 +3,17 @@ using System.Collections.Generic; using System.Windows.Forms; using System.Windows.Forms.DataVisualization.Charting; using Dashboard.Sensor; +using System.Linq; namespace Dashboard.Tracings { class Graph : ATracings, IDisposable { private Series series; private Chart chart; + private Queue> hist = new Queue>(); + private Int32 Chart_Items; - public Graph(ASensor sensor, Dictionary settings) : base(sensor, settings) { } - - public void Dispose() { - this.chart.Series.Clear(); - this.series = null; - this.chart.Dispose(); + public Graph(ASensor sensor, Dictionary settings) : base(sensor, settings) { + this.Chart_Items = (settings.Keys.Contains("items")) ? Int32.Parse(settings["items"]) : 1000; } public override Panel GetPanel() { @@ -55,23 +54,34 @@ namespace Dashboard.Tracings { ((System.ComponentModel.ISupportInitialize)(this.chart)).EndInit(); panel.Size = new System.Drawing.Size(200, 100); - panel.ResumeLayout(false); return panel; } protected override void SensorUpdate(Object sender, EventArgs e) { + switch(this.sensor.Datatypes) { + case ASensor.Types.Bool: this.hist.Enqueue(new Tuple(this.sensor.Timestamp, (this.sensor.GetBool) ? 1 : 0)); break; + case ASensor.Types.Int: this.hist.Enqueue(new Tuple(this.sensor.Timestamp, this.sensor.GetInt)); break; + case ASensor.Types.Float: this.hist.Enqueue(new Tuple(this.sensor.Timestamp, this.sensor.GetFloat)); break; + } + if(this.hist.Count > this.Chart_Items) { + this.hist.Dequeue(); + } if (this.series != null) { this.chart.BeginInvoke((MethodInvoker)delegate { - if (this.sensor.Datatypes == ASensor.Types.Bool) { - this.series.Points.AddXY(this.sensor.Timestamp, (this.sensor.GetBool) ? 1 : 0); - } - if (this.sensor.Datatypes == ASensor.Types.Int) { - this.series.Points.AddXY(this.sensor.Timestamp, this.sensor.GetInt); - } - if (this.sensor.Datatypes == ASensor.Types.Float) { - this.series.Points.AddXY(this.sensor.Timestamp, this.sensor.GetFloat); - } + this.series.Points.Clear(); + try { + foreach(Tuple temp in this.hist) { + this.series.Points.AddXY(temp.Item1, temp.Item2); + } + } catch(Exception) { }; }); } } + public void Dispose() { + this.chart.Series.Clear(); + this.series = null; + this.hist.Clear(); + this.hist = null; + this.chart.Dispose(); + } } } diff --git a/Mqtt-Dashboard/Tracings/Meter.cs b/Mqtt-Dashboard/Tracings/Meter.cs new file mode 100644 index 0000000..363bfe3 --- /dev/null +++ b/Mqtt-Dashboard/Tracings/Meter.cs @@ -0,0 +1,61 @@ +using System.Collections.Generic; +using Dashboard.Sensor; +using System; +using System.Windows.Forms; +using System.Linq; +using System.Globalization; + +namespace Dashboard.Tracings { + class Meter : ATracings { + private Int32 DisplayItems; + private Queue DisplayAverage = new Queue(); + private String DisplayUnit; + private Label label1; + private Label label2; + + public Meter(ASensor sensor, Dictionary settings) : base(sensor, settings) { + this.DisplayUnit = (settings.Keys.Contains("unit")) ? settings["unit"] : ""; + this.DisplayItems = (settings.Keys.Contains("items")) ? Int32.Parse(settings["items"]) : 1000; + } + public override Panel GetPanel() { + Panel panel = new Panel(); + + label1 = new Label(); + label1.Font = new System.Drawing.Font("Lucida Sans Typewriter", 27.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + label1.Location = new System.Drawing.Point(0, 0); + label1.Size = new System.Drawing.Size(200, 43); + label1.Text = ""; + label1.TextAlign = System.Drawing.ContentAlignment.TopCenter; + + label2 = new Label(); + label2.AutoSize = true; + label2.Location = new System.Drawing.Point(0, 50); + label2.Size = new System.Drawing.Size(96, 13); + label2.Text = ""; + + panel.BorderStyle = BorderStyle.FixedSingle; + panel.Controls.Add(label2); + panel.Controls.Add(label1); + panel.Size = new System.Drawing.Size(200, 100); + return panel; + } + protected override void SensorUpdate(Object sender, EventArgs e) { + String value = ""; + switch(this.sensor.Datatypes) { + case ASensor.Types.Bool: this.DisplayAverage.Enqueue((this.sensor.GetBool) ? 1 : 0); value = this.sensor.GetBool.ToString(new CultureInfo("de-DE")); break; + case ASensor.Types.Int: this.DisplayAverage.Enqueue(this.sensor.GetInt); value = this.sensor.GetInt.ToString(); break; + case ASensor.Types.Float: this.DisplayAverage.Enqueue(this.sensor.GetFloat); value = this.sensor.GetFloat.ToString(new CultureInfo("de-DE")); break; + } + if(this.DisplayAverage.Count > this.DisplayItems) { + this.DisplayAverage.Dequeue(); + } + float average = this.DisplayAverage.Sum() / this.DisplayAverage.Count(); + this.label1.BeginInvoke((MethodInvoker)delegate { + label1.Text = value+" "+this.DisplayUnit; + }); + this.label2.BeginInvoke((MethodInvoker)delegate { + label2.Text = "Durchschnitt: " + Math.Round(average, 2) + " " + this.DisplayUnit; + }); + } + } +} \ No newline at end of file diff --git a/Mqtt-Dashboard/tracings.ini b/Mqtt-Dashboard/tracings.ini index 7db4ea1..602e3a0 100644 --- a/Mqtt-Dashboard/tracings.ini +++ b/Mqtt-Dashboard/tracings.ini @@ -12,4 +12,11 @@ [VentilatorGraph] sensor=Ventilator -type=Graph \ No newline at end of file +type=Graph +items=100 + +[VentilatorMeter] +sensor=Ventilator +type=Meter +unit=W +items=100 \ No newline at end of file