From 0fe7c5067f8b17ad22071c2a61a899fcc358a123 Mon Sep 17 00:00:00 2001 From: BlubbFish Date: Tue, 8 Aug 2017 21:23:49 +0000 Subject: [PATCH] [NF] PowerSensor und GraphTracing funktioniert nun --- Mqtt-Dashboard/Dashboard.csproj | 3 ++ Mqtt-Dashboard/Form1.Designer.cs | 65 ++++++++++++---------------- Mqtt-Dashboard/Form1.cs | 41 ++++++++++-------- Mqtt-Dashboard/Sensor/ASensor.cs | 23 +++++++--- Mqtt-Dashboard/Sensor/Power.cs | 15 +++++-- Mqtt-Dashboard/Tracings/ATracings.cs | 4 +- Mqtt-Dashboard/Tracings/Graph.cs | 52 ++++++++++++++++++++-- 7 files changed, 134 insertions(+), 69 deletions(-) diff --git a/Mqtt-Dashboard/Dashboard.csproj b/Mqtt-Dashboard/Dashboard.csproj index 886b263..608abd2 100644 --- a/Mqtt-Dashboard/Dashboard.csproj +++ b/Mqtt-Dashboard/Dashboard.csproj @@ -58,6 +58,8 @@ + + @@ -96,5 +98,6 @@ Utils + \ No newline at end of file diff --git a/Mqtt-Dashboard/Form1.Designer.cs b/Mqtt-Dashboard/Form1.Designer.cs index 2aa85a4..8f454fa 100644 --- a/Mqtt-Dashboard/Form1.Designer.cs +++ b/Mqtt-Dashboard/Form1.Designer.cs @@ -23,16 +23,15 @@ /// Der Inhalt der Methode darf nicht mit dem Code-Editor geändert werden. /// private void InitializeComponent() { - System.Windows.Forms.DataVisualization.Charting.ChartArea chartArea2 = new System.Windows.Forms.DataVisualization.Charting.ChartArea(); - System.Windows.Forms.DataVisualization.Charting.Legend legend2 = new System.Windows.Forms.DataVisualization.Charting.Legend(); - System.Windows.Forms.DataVisualization.Charting.Series series2 = new System.Windows.Forms.DataVisualization.Charting.Series(); + System.Windows.Forms.DataVisualization.Charting.ChartArea chartArea1 = new System.Windows.Forms.DataVisualization.Charting.ChartArea(); + System.Windows.Forms.DataVisualization.Charting.Series series1 = new System.Windows.Forms.DataVisualization.Charting.Series(); + System.Windows.Forms.DataVisualization.Charting.DataPoint dataPoint1 = new System.Windows.Forms.DataVisualization.Charting.DataPoint(42955.833344907405D, 10D); + System.Windows.Forms.DataVisualization.Charting.DataPoint dataPoint2 = new System.Windows.Forms.DataVisualization.Charting.DataPoint(42955D, 2D); this.statusStrip1 = new System.Windows.Forms.StatusStrip(); - this.chart1 = new System.Windows.Forms.DataVisualization.Charting.Chart(); - this.panel1 = new System.Windows.Forms.Panel(); this.flowLayoutPanel2 = new System.Windows.Forms.FlowLayoutPanel(); - ((System.ComponentModel.ISupportInitialize)(this.chart1)).BeginInit(); - this.panel1.SuspendLayout(); + this.chart1 = new System.Windows.Forms.DataVisualization.Charting.Chart(); this.flowLayoutPanel2.SuspendLayout(); + ((System.ComponentModel.ISupportInitialize)(this.chart1)).BeginInit(); this.SuspendLayout(); // // statusStrip1 @@ -43,39 +42,33 @@ this.statusStrip1.TabIndex = 0; this.statusStrip1.Text = "statusStrip1"; // - // chart1 - // - chartArea2.Name = "ChartArea1"; - this.chart1.ChartAreas.Add(chartArea2); - legend2.Name = "Legend1"; - this.chart1.Legends.Add(legend2); - this.chart1.Location = new System.Drawing.Point(3, 3); - this.chart1.Name = "chart1"; - series2.ChartArea = "ChartArea1"; - series2.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line; - series2.Legend = "Legend1"; - series2.Name = "Series1"; - this.chart1.Series.Add(series2); - this.chart1.Size = new System.Drawing.Size(183, 136); - this.chart1.TabIndex = 1; - this.chart1.Text = "chart1"; - // - // panel1 - // - this.panel1.Controls.Add(this.chart1); - this.panel1.Location = new System.Drawing.Point(3, 3); - this.panel1.Name = "panel1"; - this.panel1.Size = new System.Drawing.Size(201, 155); - this.panel1.TabIndex = 2; - // // flowLayoutPanel2 // - this.flowLayoutPanel2.Controls.Add(this.panel1); + this.flowLayoutPanel2.Controls.Add(this.chart1); this.flowLayoutPanel2.Location = new System.Drawing.Point(12, 12); this.flowLayoutPanel2.Name = "flowLayoutPanel2"; this.flowLayoutPanel2.Size = new System.Drawing.Size(819, 289); this.flowLayoutPanel2.TabIndex = 3; // + // chart1 + // + chartArea1.AxisX.LabelStyle.Enabled = false; + chartArea1.Name = "ChartArea1"; + this.chart1.ChartAreas.Add(chartArea1); + this.chart1.Location = new System.Drawing.Point(3, 3); + this.chart1.Name = "chart1"; + series1.ChartArea = "ChartArea1"; + series1.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line; + series1.Name = "Series1"; + series1.Points.Add(dataPoint1); + series1.Points.Add(dataPoint2); + series1.XValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.DateTime; + series1.YValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Single; + this.chart1.Series.Add(series1); + this.chart1.Size = new System.Drawing.Size(101, 66); + this.chart1.TabIndex = 0; + this.chart1.Text = "chart1"; + // // Dashboard // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); @@ -85,9 +78,8 @@ this.Controls.Add(this.statusStrip1); this.Name = "Dashboard"; this.Text = "Dashboard"; - ((System.ComponentModel.ISupportInitialize)(this.chart1)).EndInit(); - this.panel1.ResumeLayout(false); this.flowLayoutPanel2.ResumeLayout(false); + ((System.ComponentModel.ISupportInitialize)(this.chart1)).EndInit(); this.ResumeLayout(false); this.PerformLayout(); @@ -96,9 +88,8 @@ #endregion private System.Windows.Forms.StatusStrip statusStrip1; - private System.Windows.Forms.DataVisualization.Charting.Chart chart1; - private System.Windows.Forms.Panel panel1; private System.Windows.Forms.FlowLayoutPanel flowLayoutPanel2; + private System.Windows.Forms.DataVisualization.Charting.Chart chart1; } } diff --git a/Mqtt-Dashboard/Form1.cs b/Mqtt-Dashboard/Form1.cs index 34e9031..d4d636b 100644 --- a/Mqtt-Dashboard/Form1.cs +++ b/Mqtt-Dashboard/Form1.cs @@ -1,11 +1,5 @@ using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Data; -using System.Drawing; -using System.Linq; using System.Text; -using System.Threading.Tasks; using System.Windows.Forms; using Dashboard.Connector; using Dashboard.Sensor; @@ -16,20 +10,33 @@ namespace Dashboard { public Dashboard() { InitializeComponent(); Mqtt.Instance.Connect(); - ATracings g = new Graph(); - ASensor s = new Power(); - s.SetMqttTopic("zway/wohnzimmer/thinkpad/power"); - Mqtt.Instance.MessageIncomming += s.IncommingMqttMessage; - g.SetSensor(s); - this.flowLayoutPanel2.Controls.Add(g.GetTracing()); - this.chart1.ChartAreas[0].AxisX.Maximum = 100; + + ATracings g1 = new Graph(); + ASensor s1 = new Power(); + s1.SetMqttTopic("zway/wohnzimmer/thinkpad/power"); + Mqtt.Instance.MessageIncomming += s1.IncommingMqttMessage; + g1.SetSensor(s1); + this.flowLayoutPanel2.Controls.Add(g1.GetTracing()); + + ATracings g2 = new Graph(); + ASensor s2 = new Power(); + s2.SetMqttTopic("zway/wohnzimmer/tV/power"); + Mqtt.Instance.MessageIncomming += s2.IncommingMqttMessage; + g2.SetSensor(s2); + this.flowLayoutPanel2.Controls.Add(g2.GetTracing()); + + ATracings g3 = new Graph(); + ASensor s3 = new Power(); + s3.SetMqttTopic("zway/küche/kuehlschrank/power"); + Mqtt.Instance.MessageIncomming += s3.IncommingMqttMessage; + g3.SetSensor(s3); + this.flowLayoutPanel2.Controls.Add(g3.GetTracing()); + + Mqtt.Instance.MessageIncomming += this.Instance_MessageIncomming; } private void Instance_MessageIncomming(Object sender, uPLibrary.Networking.M2Mqtt.Messages.MqttMsgPublishEventArgs e) { - if(e.Topic == "zway/wohnzimmer/thinkpad/power") { - - } - System.Diagnostics.Debug.WriteLine("Received = " + Encoding.UTF8.GetString(e.Message) + " on topic " + e.Topic); + System.Diagnostics.Debug.WriteLine("Received = " + Encoding.UTF8.GetString(e.Message) + " on topic " + e.Topic+" at "+DateTime.Now.ToUniversalTime()); } } } diff --git a/Mqtt-Dashboard/Sensor/ASensor.cs b/Mqtt-Dashboard/Sensor/ASensor.cs index 36e5543..9e69231 100644 --- a/Mqtt-Dashboard/Sensor/ASensor.cs +++ b/Mqtt-Dashboard/Sensor/ASensor.cs @@ -4,15 +4,28 @@ using uPLibrary.Networking.M2Mqtt.Messages; namespace Dashboard.Sensor { public abstract class ASensor { private String topic; - + public enum Types { + Bool, + Int, + Float + } public void SetMqttTopic(String topic) { this.topic = topic; } public delegate void UpdatedValue(Object sender, EventArgs e); public event UpdatedValue Update; - internal abstract void IncommingMqttMessage(Object sender, MqttMsgPublishEventArgs e); - public abstract Int32 GetInt(); - public abstract Single GetFloat(); - public abstract Boolean GetBool(); + internal void IncommingMqttMessage(Object sender, MqttMsgPublishEventArgs e) { + if(e.Topic == this.topic) { + this.UpdateValue(e); + this.Timestamp = DateTime.Now; + this.Update?.Invoke(this, e); + } + } + protected abstract void UpdateValue(MqttMsgPublishEventArgs e); + public Int32 GetInt { get; protected set; } + public Single GetFloat { get; protected set; } + public Boolean GetBool { get; protected set; } + public Types Datatypes { get; protected set; } + public DateTime Timestamp { get; protected set; } } } \ No newline at end of file diff --git a/Mqtt-Dashboard/Sensor/Power.cs b/Mqtt-Dashboard/Sensor/Power.cs index 6c53e81..81cecd9 100644 --- a/Mqtt-Dashboard/Sensor/Power.cs +++ b/Mqtt-Dashboard/Sensor/Power.cs @@ -1,10 +1,19 @@ using System; -using System.Collections.Generic; -using System.Linq; +using System.Globalization; using System.Text; -using System.Threading.Tasks; +using uPLibrary.Networking.M2Mqtt.Messages; namespace Dashboard.Sensor { class Power : ASensor { + public Power() { + this.GetBool = true; + this.GetFloat = 0.0f; + this.GetInt = 0; + this.Datatypes = Types.Float; + } + + protected override void UpdateValue(MqttMsgPublishEventArgs e) { + this.GetFloat = Single.Parse(Encoding.UTF8.GetString(e.Message), new CultureInfo("en-US")); + } } } diff --git a/Mqtt-Dashboard/Tracings/ATracings.cs b/Mqtt-Dashboard/Tracings/ATracings.cs index e427f3c..ae2d7d8 100644 --- a/Mqtt-Dashboard/Tracings/ATracings.cs +++ b/Mqtt-Dashboard/Tracings/ATracings.cs @@ -4,9 +4,7 @@ using Dashboard.Sensor; namespace Dashboard.Tracings { public abstract class ATracings { - private ASensor sensor; - public abstract Int32 ElementWidth { get; } - public abstract Int32 ElementHeight { get; } + protected ASensor sensor; public void SetSensor(ASensor sensor) { this.sensor = sensor; this.sensor.Update += this.SensorUpdate; diff --git a/Mqtt-Dashboard/Tracings/Graph.cs b/Mqtt-Dashboard/Tracings/Graph.cs index 4d5eff2..0d727a5 100644 --- a/Mqtt-Dashboard/Tracings/Graph.cs +++ b/Mqtt-Dashboard/Tracings/Graph.cs @@ -1,10 +1,54 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using System.Windows.Forms; +using System.Windows.Forms.DataVisualization.Charting; namespace Dashboard.Tracings { class Graph : ATracings { + private Series series; + private Chart chart; + + public override Panel GetTracing() { + Panel panel = new Panel(); + + this.chart = new Chart(); + ((System.ComponentModel.ISupportInitialize)(this.chart)).BeginInit(); + ChartArea chartArea = new ChartArea(); + chartArea.AxisX.LabelStyle.Enabled = false; + this.chart.ChartAreas.Add(chartArea); + this.series = new Series() { + ChartType = SeriesChartType.Line, + XValueType = ChartValueType.DateTime + }; + if (this.sensor.Datatypes == Sensor.ASensor.Types.Bool) { + this.series.YValueType = ChartValueType.Int32; + } else if (this.sensor.Datatypes == Sensor.ASensor.Types.Int) { + this.series.YValueType = ChartValueType.Int32; + } else if (this.sensor.Datatypes == Sensor.ASensor.Types.Float) { + this.series.YValueType = ChartValueType.Single; + } + this.chart.Series.Add(this.series); + this.chart.Location = new System.Drawing.Point(0, 0); + this.chart.Size = new System.Drawing.Size(200, 100); + panel.Controls.Add(this.chart); + ((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) { + this.chart.BeginInvoke((MethodInvoker)delegate { + if (this.sensor.Datatypes == Sensor.ASensor.Types.Bool) { + this.series.Points.AddXY(this.sensor.Timestamp, (this.sensor.GetBool) ? 1 : 0); + } + if (this.sensor.Datatypes == Sensor.ASensor.Types.Int) { + this.series.Points.AddXY(this.sensor.Timestamp, this.sensor.GetInt); + } + if (this.sensor.Datatypes == Sensor.ASensor.Types.Float) { + this.series.Points.AddXY(this.sensor.Timestamp, this.sensor.GetFloat); + } + }); + } } }