From 8e9e49e685d9f44cf2c4d32d5364a4c67b72f176 Mon Sep 17 00:00:00 2001 From: BlubbFish Date: Wed, 9 Aug 2017 15:44:48 +0000 Subject: [PATCH] =?UTF-8?q?[NF]=20Funktioniert=20nun=20=C3=BCber=20Konfigf?= =?UTF-8?q?iles?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Mqtt-Dashboard/Connector/Mqtt.cs | 4 ++ Mqtt-Dashboard/Dashboard.csproj | 6 +++ Mqtt-Dashboard/Form1.Designer.cs | 30 ------------- Mqtt-Dashboard/Form1.cs | 63 ++++++++++++++++++---------- Mqtt-Dashboard/Sensor/ASensor.cs | 28 ++++++++++--- Mqtt-Dashboard/Sensor/Power.cs | 4 +- Mqtt-Dashboard/Tracings/ATracings.cs | 8 +++- Mqtt-Dashboard/Tracings/Graph.cs | 28 +++++++++---- Mqtt-Dashboard/sensor.ini | 8 ++++ Mqtt-Dashboard/tracings.ini | 3 ++ 10 files changed, 115 insertions(+), 67 deletions(-) create mode 100644 Mqtt-Dashboard/sensor.ini create mode 100644 Mqtt-Dashboard/tracings.ini diff --git a/Mqtt-Dashboard/Connector/Mqtt.cs b/Mqtt-Dashboard/Connector/Mqtt.cs index f291bef..fa22e99 100644 --- a/Mqtt-Dashboard/Connector/Mqtt.cs +++ b/Mqtt-Dashboard/Connector/Mqtt.cs @@ -33,5 +33,9 @@ namespace Dashboard.Connector { private void Client_MqttMsgPublishReceived(Object sender, MqttMsgPublishEventArgs e) { this.MessageIncomming?.Invoke(this, e); } + + public void Send(String topic, String data) { + this.client.Publish(topic, Encoding.UTF8.GetBytes(data)); + } } } diff --git a/Mqtt-Dashboard/Dashboard.csproj b/Mqtt-Dashboard/Dashboard.csproj index 608abd2..0a91e0c 100644 --- a/Mqtt-Dashboard/Dashboard.csproj +++ b/Mqtt-Dashboard/Dashboard.csproj @@ -85,9 +85,15 @@ Settings.settings True + + PreserveNewest + PreserveNewest + + PreserveNewest + diff --git a/Mqtt-Dashboard/Form1.Designer.cs b/Mqtt-Dashboard/Form1.Designer.cs index 8f454fa..787c2a1 100644 --- a/Mqtt-Dashboard/Form1.Designer.cs +++ b/Mqtt-Dashboard/Form1.Designer.cs @@ -23,15 +23,8 @@ /// Der Inhalt der Methode darf nicht mit dem Code-Editor geändert werden. /// private void InitializeComponent() { - 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.flowLayoutPanel2 = new System.Windows.Forms.FlowLayoutPanel(); - this.chart1 = new System.Windows.Forms.DataVisualization.Charting.Chart(); - this.flowLayoutPanel2.SuspendLayout(); - ((System.ComponentModel.ISupportInitialize)(this.chart1)).BeginInit(); this.SuspendLayout(); // // statusStrip1 @@ -44,31 +37,11 @@ // // flowLayoutPanel2 // - 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); @@ -78,8 +51,6 @@ this.Controls.Add(this.statusStrip1); this.Name = "Dashboard"; this.Text = "Dashboard"; - this.flowLayoutPanel2.ResumeLayout(false); - ((System.ComponentModel.ISupportInitialize)(this.chart1)).EndInit(); this.ResumeLayout(false); this.PerformLayout(); @@ -89,7 +60,6 @@ private System.Windows.Forms.StatusStrip statusStrip1; 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 d4d636b..bb14a1a 100644 --- a/Mqtt-Dashboard/Form1.cs +++ b/Mqtt-Dashboard/Form1.cs @@ -4,37 +4,58 @@ using System.Windows.Forms; using Dashboard.Connector; using Dashboard.Sensor; using Dashboard.Tracings; +using BlubbFish.Utils; +using System.Collections.Generic; +using System.Threading; namespace Dashboard { public partial class Dashboard : Form { + private Dictionary sensors = new Dictionary(); public Dashboard() { InitializeComponent(); Mqtt.Instance.Connect(); - 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()); + this.generateSensors(); + this.generateForms(); Mqtt.Instance.MessageIncomming += this.Instance_MessageIncomming; + Thread a = new Thread(this.SensorPolling); + a.Start(); + } + private void generateSensors() { + InIReader ini = InIReader.GetInstance("sensor.ini"); + List sensorini = ini.GetSections(); + foreach(String sensor in sensorini) { + ASensor s; + switch(ini.GetValue(sensor, "type").ToLower()) { + case "power": s = new Power(ini.GetSection(sensor)); break; + default: + throw new ArgumentException("sensor.ini: " + sensor + " section has a missconfiguration in type"); + } + this.sensors.Add(sensor.ToLower().Substring(1, sensor.Length - 2), s); + } + } + private void generateForms() { + InIReader ini = InIReader.GetInstance("tracings.ini"); + List tracingini = ini.GetSections(); + foreach(String tracing in tracingini) { + ATracings t; + switch(ini.GetValue(tracing, "type").ToLower()) { + case "graph": t = new Graph(this.sensors[ini.GetValue(tracing, "sensor").ToLower()],ini.GetSection(tracing)); break; + default: + throw new ArgumentException("tracings.ini: " + tracing + " section has a missconfiguration in type"); + } + this.flowLayoutPanel2.Controls.Add(t.GetPanel()); + } + } + private void SensorPolling() { + while(true) { + Thread.Sleep(1000); + foreach(KeyValuePair sensor in sensors) { + sensor.Value.Poll(); + } + } } - private void Instance_MessageIncomming(Object sender, uPLibrary.Networking.M2Mqtt.Messages.MqttMsgPublishEventArgs e) { 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 9e69231..a635a1b 100644 --- a/Mqtt-Dashboard/Sensor/ASensor.cs +++ b/Mqtt-Dashboard/Sensor/ASensor.cs @@ -1,20 +1,31 @@ -using System; +using Dashboard.Connector; +using System; using uPLibrary.Networking.M2Mqtt.Messages; +using System.Collections.Generic; +using System.Linq; namespace Dashboard.Sensor { public abstract class ASensor { private String topic; + private Int32 pollcount; + private Dictionary settings; 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 void IncommingMqttMessage(Object sender, MqttMsgPublishEventArgs e) { + + public ASensor(Dictionary settings) { + this.topic = (settings.Keys.Contains("topic")) ? settings["topic"] : ""; + this.Polling = (settings.Keys.Contains("polling")) ? Int32.Parse(settings["polling"]) : 60; + this.pollcount = this.Polling; + this.settings = settings; + Mqtt.Instance.MessageIncomming += this.IncommingMqttMessage; + } + private void IncommingMqttMessage(Object sender, MqttMsgPublishEventArgs e) { if(e.Topic == this.topic) { this.UpdateValue(e); this.Timestamp = DateTime.Now; @@ -27,5 +38,12 @@ namespace Dashboard.Sensor { public Boolean GetBool { get; protected set; } public Types Datatypes { get; protected set; } public DateTime Timestamp { get; protected set; } + public int Polling { get; private set; } + public void Poll() { + if(this.pollcount++ >= Polling) { + this.pollcount = 1; + Mqtt.Instance.Send(this.topic + "/status",""); + } + } } } \ No newline at end of file diff --git a/Mqtt-Dashboard/Sensor/Power.cs b/Mqtt-Dashboard/Sensor/Power.cs index 81cecd9..2e00623 100644 --- a/Mqtt-Dashboard/Sensor/Power.cs +++ b/Mqtt-Dashboard/Sensor/Power.cs @@ -1,17 +1,17 @@ using System; +using System.Collections.Generic; using System.Globalization; using System.Text; using uPLibrary.Networking.M2Mqtt.Messages; namespace Dashboard.Sensor { class Power : ASensor { - public Power() { + public Power(Dictionary settings) : base(settings) { 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 ae2d7d8..25db86c 100644 --- a/Mqtt-Dashboard/Tracings/ATracings.cs +++ b/Mqtt-Dashboard/Tracings/ATracings.cs @@ -1,15 +1,19 @@ using System; +using System.Collections.Generic; using System.Windows.Forms; using Dashboard.Sensor; namespace Dashboard.Tracings { public abstract class ATracings { protected ASensor sensor; - public void SetSensor(ASensor sensor) { + private Dictionary settings; + + public ATracings(ASensor sensor, Dictionary settings) { this.sensor = sensor; this.sensor.Update += this.SensorUpdate; + this.settings = settings; } protected abstract void SensorUpdate(Object sender, EventArgs e); - public abstract Panel GetTracing(); + public abstract Panel GetPanel(); } } \ No newline at end of file diff --git a/Mqtt-Dashboard/Tracings/Graph.cs b/Mqtt-Dashboard/Tracings/Graph.cs index 0d727a5..37077ca 100644 --- a/Mqtt-Dashboard/Tracings/Graph.cs +++ b/Mqtt-Dashboard/Tracings/Graph.cs @@ -1,24 +1,39 @@ using System; +using System.Collections.Generic; using System.Windows.Forms; using System.Windows.Forms.DataVisualization.Charting; +using Dashboard.Sensor; namespace Dashboard.Tracings { class Graph : ATracings { private Series series; private Chart chart; - public override Panel GetTracing() { + public Graph(ASensor sensor, Dictionary settings) : base(sensor, settings) { } + public override Panel GetPanel() { Panel panel = new Panel(); this.chart = new Chart(); ((System.ComponentModel.ISupportInitialize)(this.chart)).BeginInit(); ChartArea chartArea = new ChartArea(); chartArea.AxisX.LabelStyle.Enabled = false; + chartArea.AxisX.LineColor = System.Drawing.Color.Gainsboro; + chartArea.AxisX.MajorGrid.LineColor = System.Drawing.Color.Gainsboro; + chartArea.AxisX.MinorGrid.Enabled = true; + chartArea.AxisX.MinorGrid.LineColor = System.Drawing.Color.Gainsboro; + chartArea.AxisX.MinorGrid.LineDashStyle = ChartDashStyle.Dot; + + chartArea.AxisY.LineColor = System.Drawing.Color.Gainsboro; + chartArea.AxisY.MajorGrid.LineColor = System.Drawing.Color.Gainsboro; + chartArea.AxisY.MinorGrid.Enabled = true; + chartArea.AxisY.MinorGrid.LineColor = System.Drawing.Color.Gainsboro; + chartArea.AxisY.MinorGrid.LineDashStyle = ChartDashStyle.Dot; this.chart.ChartAreas.Add(chartArea); this.series = new Series() { ChartType = SeriesChartType.Line, - XValueType = ChartValueType.DateTime - }; + XValueType = ChartValueType.DateTime, + Color = System.Drawing.Color.Red + }; if (this.sensor.Datatypes == Sensor.ASensor.Types.Bool) { this.series.YValueType = ChartValueType.Int32; } else if (this.sensor.Datatypes == Sensor.ASensor.Types.Int) { @@ -36,16 +51,15 @@ namespace Dashboard.Tracings { 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) { + if (this.sensor.Datatypes == ASensor.Types.Bool) { this.series.Points.AddXY(this.sensor.Timestamp, (this.sensor.GetBool) ? 1 : 0); } - if (this.sensor.Datatypes == Sensor.ASensor.Types.Int) { + if (this.sensor.Datatypes == ASensor.Types.Int) { this.series.Points.AddXY(this.sensor.Timestamp, this.sensor.GetInt); } - if (this.sensor.Datatypes == Sensor.ASensor.Types.Float) { + if (this.sensor.Datatypes == ASensor.Types.Float) { this.series.Points.AddXY(this.sensor.Timestamp, this.sensor.GetFloat); } }); diff --git a/Mqtt-Dashboard/sensor.ini b/Mqtt-Dashboard/sensor.ini new file mode 100644 index 0000000..bee284c --- /dev/null +++ b/Mqtt-Dashboard/sensor.ini @@ -0,0 +1,8 @@ +[Ventilator] +topic=zway/power/test/wVentilator +type=Power +polling=10 + +;zway/wohnzimmer/thinkpad/power +;zway/wohnzimmer/tV/power +;zway/küche/kuehlschrank/power \ No newline at end of file diff --git a/Mqtt-Dashboard/tracings.ini b/Mqtt-Dashboard/tracings.ini new file mode 100644 index 0000000..c2d0c1d --- /dev/null +++ b/Mqtt-Dashboard/tracings.ini @@ -0,0 +1,3 @@ +[VentilatorGraph] +sensor=Ventilator +type=Graph \ No newline at end of file