From 241b0ca00aff491afd82a00371af990c9b98ca01 Mon Sep 17 00:00:00 2001 From: BlubbFish Date: Wed, 9 Aug 2017 22:37:46 +0000 Subject: [PATCH] [BF] Versuche Fehler zu finden, welcher verhindert das Anwendung geschlossen werden kann --- Mqtt-Dashboard/Form1.cs | 35 ++++++++++++++++++++++++-------- Mqtt-Dashboard/Sensor/ASensor.cs | 15 +++++++++++--- Mqtt-Dashboard/Sensor/Power.cs | 1 + Mqtt-Dashboard/Tracings/Graph.cs | 33 +++++++++++++++++++----------- Mqtt-Dashboard/sensor.ini | 16 ++++++++++----- Mqtt-Dashboard/tracings.ini | 14 ++++++++++--- 6 files changed, 83 insertions(+), 31 deletions(-) diff --git a/Mqtt-Dashboard/Form1.cs b/Mqtt-Dashboard/Form1.cs index bb14a1a..4eefbea 100644 --- a/Mqtt-Dashboard/Form1.cs +++ b/Mqtt-Dashboard/Form1.cs @@ -10,19 +10,25 @@ using System.Threading; namespace Dashboard { public partial class Dashboard : Form { - private Dictionary sensors = new Dictionary(); + private Dictionary sensors = new Dictionary(); + private Thread updateThread; + public Dashboard() { InitializeComponent(); Mqtt.Instance.Connect(); - this.generateSensors(); - this.generateForms(); + this.GenerateSensors(); + this.GenerateForms(); + + this.updateThread = new Thread(this.SensorPolling); + this.updateThread.Start(); + + this.FormClosed += this.Dashboard_FormClosed; Mqtt.Instance.MessageIncomming += this.Instance_MessageIncomming; - Thread a = new Thread(this.SensorPolling); - a.Start(); } - private void generateSensors() { + + private void GenerateSensors() { InIReader ini = InIReader.GetInstance("sensor.ini"); List sensorini = ini.GetSections(); foreach(String sensor in sensorini) { @@ -35,7 +41,7 @@ namespace Dashboard { this.sensors.Add(sensor.ToLower().Substring(1, sensor.Length - 2), s); } } - private void generateForms() { + private void GenerateForms() { InIReader ini = InIReader.GetInstance("tracings.ini"); List tracingini = ini.GetSections(); foreach(String tracing in tracingini) { @@ -51,11 +57,24 @@ namespace Dashboard { private void SensorPolling() { while(true) { Thread.Sleep(1000); - foreach(KeyValuePair sensor in sensors) { + foreach(KeyValuePair sensor in this.sensors) { sensor.Value.Poll(); } } } + private void Dashboard_FormClosed(Object sender, FormClosedEventArgs e) { + this.Dispose(); + } + private new void Dispose() { + this.updateThread.Abort(); + while (this.updateThread.ThreadState != ThreadState.Aborted) { } + foreach (String item in this.sensors.Keys) { + this.sensors[item] = null; + } + this.sensors.Clear(); + Mqtt.Instance.MessageIncomming -= this.Instance_MessageIncomming; + this.Dispose(true); + } 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 a635a1b..2d219ef 100644 --- a/Mqtt-Dashboard/Sensor/ASensor.cs +++ b/Mqtt-Dashboard/Sensor/ASensor.cs @@ -5,7 +5,7 @@ using System.Collections.Generic; using System.Linq; namespace Dashboard.Sensor { - public abstract class ASensor { + public abstract class ASensor : IDisposable { private String topic; private Int32 pollcount; private Dictionary settings; @@ -38,12 +38,21 @@ 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 Int32 Polling { get; private set; } public void Poll() { - if(this.pollcount++ >= Polling) { + if(this.pollcount++ >= this.Polling) { this.pollcount = 1; Mqtt.Instance.Send(this.topic + "/status",""); } } + protected virtual void Dispose(Boolean disposing) { + if (disposing) { + Mqtt.Instance.MessageIncomming -= this.IncommingMqttMessage; + } + } + public void Dispose() { + Dispose(true); + GC.SuppressFinalize(this); + } } } \ No newline at end of file diff --git a/Mqtt-Dashboard/Sensor/Power.cs b/Mqtt-Dashboard/Sensor/Power.cs index 2e00623..a07d8c8 100644 --- a/Mqtt-Dashboard/Sensor/Power.cs +++ b/Mqtt-Dashboard/Sensor/Power.cs @@ -12,6 +12,7 @@ namespace Dashboard.Sensor { 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/Graph.cs b/Mqtt-Dashboard/Tracings/Graph.cs index 37077ca..d899470 100644 --- a/Mqtt-Dashboard/Tracings/Graph.cs +++ b/Mqtt-Dashboard/Tracings/Graph.cs @@ -5,11 +5,18 @@ using System.Windows.Forms.DataVisualization.Charting; using Dashboard.Sensor; namespace Dashboard.Tracings { - class Graph : ATracings { + class Graph : ATracings, IDisposable { private Series series; private Chart chart; public Graph(ASensor sensor, Dictionary settings) : base(sensor, settings) { } + + public void Dispose() { + this.chart.Series.Clear(); + this.series = null; + this.chart.Dispose(); + } + public override Panel GetPanel() { Panel panel = new Panel(); @@ -52,17 +59,19 @@ namespace Dashboard.Tracings { return panel; } protected override void SensorUpdate(Object sender, EventArgs e) { - 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); - } - }); + 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); + } + }); + } } } } diff --git a/Mqtt-Dashboard/sensor.ini b/Mqtt-Dashboard/sensor.ini index bee284c..6916ea3 100644 --- a/Mqtt-Dashboard/sensor.ini +++ b/Mqtt-Dashboard/sensor.ini @@ -1,8 +1,14 @@ -[Ventilator] -topic=zway/power/test/wVentilator +[Thinkpad] +topic=zway/wohnzimmer/thinkpad/power type=Power polling=10 -;zway/wohnzimmer/thinkpad/power -;zway/wohnzimmer/tV/power -;zway/küche/kuehlschrank/power \ No newline at end of file +;[Tv] +;topic=zway/wohnzimmer/tV/power +;type=Power +;polling=10 + +;[Kühlschrank] +;topic=zway/küche/kuehlschrank/power +;type=Power +;polling=10 \ No newline at end of file diff --git a/Mqtt-Dashboard/tracings.ini b/Mqtt-Dashboard/tracings.ini index c2d0c1d..fe2414d 100644 --- a/Mqtt-Dashboard/tracings.ini +++ b/Mqtt-Dashboard/tracings.ini @@ -1,3 +1,11 @@ -[VentilatorGraph] -sensor=Ventilator -type=Graph \ No newline at end of file +;[ThinkpadGraph] +;sensor=Thinkpad +;type=Graph + +;[TvGraph] +;sensor=Tv +;type=Graph + +;[KühlschrankGraph] +;sensor=Kühlschrank +;type=Graph \ No newline at end of file