From 39c0ab4e3cbe923e25f93608a2db8260dc1bfad2 Mon Sep 17 00:00:00 2001 From: BlubbFish Date: Thu, 10 Aug 2017 22:15:52 +0000 Subject: [PATCH] [NF] Schalter auslesen [BF] C# Guidlines --- Mqtt-Dashboard/Dashboard.csproj | 1 + Mqtt-Dashboard/Form1.cs | 4 ++-- Mqtt-Dashboard/Sensor/ASensor.cs | 3 +++ Mqtt-Dashboard/Sensor/Switch.cs | 15 +++++++++++++ Mqtt-Dashboard/Tracings/Graph.cs | 10 ++++----- Mqtt-Dashboard/Tracings/Meter.cs | 36 +++++++++++++++++--------------- 6 files changed, 45 insertions(+), 24 deletions(-) create mode 100644 Mqtt-Dashboard/Sensor/Switch.cs diff --git a/Mqtt-Dashboard/Dashboard.csproj b/Mqtt-Dashboard/Dashboard.csproj index b0018c4..f0d2bb4 100644 --- a/Mqtt-Dashboard/Dashboard.csproj +++ b/Mqtt-Dashboard/Dashboard.csproj @@ -60,6 +60,7 @@ + diff --git a/Mqtt-Dashboard/Form1.cs b/Mqtt-Dashboard/Form1.cs index 4edc86b..9a46687 100644 --- a/Mqtt-Dashboard/Form1.cs +++ b/Mqtt-Dashboard/Form1.cs @@ -35,8 +35,8 @@ namespace Dashboard { 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"); + case "switch": s = new Switch(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); } diff --git a/Mqtt-Dashboard/Sensor/ASensor.cs b/Mqtt-Dashboard/Sensor/ASensor.cs index 2d219ef..ca1a6cc 100644 --- a/Mqtt-Dashboard/Sensor/ASensor.cs +++ b/Mqtt-Dashboard/Sensor/ASensor.cs @@ -19,6 +19,9 @@ namespace Dashboard.Sensor { public event UpdatedValue Update; public ASensor(Dictionary settings) { + this.GetBool = true; + this.GetFloat = 0.0f; + this.GetInt = 0; this.topic = (settings.Keys.Contains("topic")) ? settings["topic"] : ""; this.Polling = (settings.Keys.Contains("polling")) ? Int32.Parse(settings["polling"]) : 60; this.pollcount = this.Polling; diff --git a/Mqtt-Dashboard/Sensor/Switch.cs b/Mqtt-Dashboard/Sensor/Switch.cs new file mode 100644 index 0000000..3452d5d --- /dev/null +++ b/Mqtt-Dashboard/Sensor/Switch.cs @@ -0,0 +1,15 @@ +using System.Collections.Generic; +using System.Text; +using uPLibrary.Networking.M2Mqtt.Messages; + +namespace Dashboard.Sensor { + class Switch : ASensor { + public Switch(Dictionary settings) : base(settings) { + this.Datatypes = Types.Bool; + } + + protected override void UpdateValue(MqttMsgPublishEventArgs e) { + this.GetBool = (Encoding.UTF8.GetString(e.Message).ToLower() == "on") ? true : false; + } + } +} \ No newline at end of file diff --git a/Mqtt-Dashboard/Tracings/Graph.cs b/Mqtt-Dashboard/Tracings/Graph.cs index ad27182..e5c0945 100644 --- a/Mqtt-Dashboard/Tracings/Graph.cs +++ b/Mqtt-Dashboard/Tracings/Graph.cs @@ -9,7 +9,7 @@ namespace Dashboard.Tracings { class Graph : ATracings, IDisposable { private Series series; private Chart chart; - private Queue> hist = new Queue>(); + private Queue> hist = new Queue>(); private Int32 Chart_Items; public Graph(ASensor sensor, Dictionary settings) : base(sensor, settings) { @@ -58,9 +58,9 @@ namespace Dashboard.Tracings { } 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; + 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(); @@ -69,7 +69,7 @@ namespace Dashboard.Tracings { this.chart.BeginInvoke((MethodInvoker)delegate { this.series.Points.Clear(); try { - foreach(Tuple temp in this.hist) { + foreach(Tuple temp in this.hist) { this.series.Points.AddXY(temp.Item1, temp.Item2); } } catch(Exception) { }; diff --git a/Mqtt-Dashboard/Tracings/Meter.cs b/Mqtt-Dashboard/Tracings/Meter.cs index 363bfe3..fbdf3ff 100644 --- a/Mqtt-Dashboard/Tracings/Meter.cs +++ b/Mqtt-Dashboard/Tracings/Meter.cs @@ -8,7 +8,7 @@ using System.Globalization; namespace Dashboard.Tracings { class Meter : ATracings { private Int32 DisplayItems; - private Queue DisplayAverage = new Queue(); + private Queue DisplayAverage = new Queue(); private String DisplayUnit; private Label label1; private Label label2; @@ -20,22 +20,24 @@ namespace Dashboard.Tracings { 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; + this.label1 = new Label() { + Font = new System.Drawing.Font("Lucida Sans Typewriter", 27.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((Byte)(0))), + Location = new System.Drawing.Point(0, 0), + Size = new System.Drawing.Size(200, 43), + Text = "", + 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 = ""; + this.label2 = new Label() { + AutoSize = true, + Location = new System.Drawing.Point(0, 50), + Size = new System.Drawing.Size(96, 13), + Text = "" + }; panel.BorderStyle = BorderStyle.FixedSingle; - panel.Controls.Add(label2); - panel.Controls.Add(label1); + panel.Controls.Add(this.label2); + panel.Controls.Add(this.label1); panel.Size = new System.Drawing.Size(200, 100); return panel; } @@ -49,12 +51,12 @@ namespace Dashboard.Tracings { if(this.DisplayAverage.Count > this.DisplayItems) { this.DisplayAverage.Dequeue(); } - float average = this.DisplayAverage.Sum() / this.DisplayAverage.Count(); + Single average = this.DisplayAverage.Sum() / this.DisplayAverage.Count(); this.label1.BeginInvoke((MethodInvoker)delegate { - label1.Text = value+" "+this.DisplayUnit; + this.label1.Text = value+" "+this.DisplayUnit; }); this.label2.BeginInvoke((MethodInvoker)delegate { - label2.Text = "Durchschnitt: " + Math.Round(average, 2) + " " + this.DisplayUnit; + this.label2.Text = "Durchschnitt: " + Math.Round(average, 2) + " " + this.DisplayUnit; }); } }