[BF] Versuche Fehler zu finden, welcher verhindert das Anwendung geschlossen werden kann

This commit is contained in:
BlubbFish 2017-08-09 22:37:46 +00:00
parent 8e9e49e685
commit 241b0ca00a
6 changed files with 83 additions and 31 deletions

View File

@ -10,19 +10,25 @@ using System.Threading;
namespace Dashboard { namespace Dashboard {
public partial class Dashboard : Form { public partial class Dashboard : Form {
private Dictionary<String, ASensor> sensors = new Dictionary<string, ASensor>(); private Dictionary<String, ASensor> sensors = new Dictionary<String, ASensor>();
private Thread updateThread;
public Dashboard() { public Dashboard() {
InitializeComponent(); InitializeComponent();
Mqtt.Instance.Connect(); Mqtt.Instance.Connect();
this.generateSensors(); this.GenerateSensors();
this.generateForms(); this.GenerateForms();
this.updateThread = new Thread(this.SensorPolling);
this.updateThread.Start();
this.FormClosed += this.Dashboard_FormClosed;
Mqtt.Instance.MessageIncomming += this.Instance_MessageIncomming; 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"); InIReader ini = InIReader.GetInstance("sensor.ini");
List<String> sensorini = ini.GetSections(); List<String> sensorini = ini.GetSections();
foreach(String sensor in sensorini) { foreach(String sensor in sensorini) {
@ -35,7 +41,7 @@ namespace Dashboard {
this.sensors.Add(sensor.ToLower().Substring(1, sensor.Length - 2), s); this.sensors.Add(sensor.ToLower().Substring(1, sensor.Length - 2), s);
} }
} }
private void generateForms() { private void GenerateForms() {
InIReader ini = InIReader.GetInstance("tracings.ini"); InIReader ini = InIReader.GetInstance("tracings.ini");
List<String> tracingini = ini.GetSections(); List<String> tracingini = ini.GetSections();
foreach(String tracing in tracingini) { foreach(String tracing in tracingini) {
@ -51,11 +57,24 @@ namespace Dashboard {
private void SensorPolling() { private void SensorPolling() {
while(true) { while(true) {
Thread.Sleep(1000); Thread.Sleep(1000);
foreach(KeyValuePair<String, ASensor> sensor in sensors) { foreach(KeyValuePair<String, ASensor> sensor in this.sensors) {
sensor.Value.Poll(); 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) { 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()); System.Diagnostics.Debug.WriteLine("Received = " + Encoding.UTF8.GetString(e.Message) + " on topic " + e.Topic+" at "+DateTime.Now.ToUniversalTime());
} }

View File

@ -5,7 +5,7 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
namespace Dashboard.Sensor { namespace Dashboard.Sensor {
public abstract class ASensor { public abstract class ASensor : IDisposable {
private String topic; private String topic;
private Int32 pollcount; private Int32 pollcount;
private Dictionary<String, String> settings; private Dictionary<String, String> settings;
@ -38,12 +38,21 @@ namespace Dashboard.Sensor {
public Boolean GetBool { get; protected set; } public Boolean GetBool { get; protected set; }
public Types Datatypes { get; protected set; } public Types Datatypes { get; protected set; }
public DateTime Timestamp { get; protected set; } public DateTime Timestamp { get; protected set; }
public int Polling { get; private set; } public Int32 Polling { get; private set; }
public void Poll() { public void Poll() {
if(this.pollcount++ >= Polling) { if(this.pollcount++ >= this.Polling) {
this.pollcount = 1; this.pollcount = 1;
Mqtt.Instance.Send(this.topic + "/status",""); 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);
}
} }
} }

View File

@ -12,6 +12,7 @@ namespace Dashboard.Sensor {
this.GetInt = 0; this.GetInt = 0;
this.Datatypes = Types.Float; this.Datatypes = Types.Float;
} }
protected override void UpdateValue(MqttMsgPublishEventArgs e) { protected override void UpdateValue(MqttMsgPublishEventArgs e) {
this.GetFloat = Single.Parse(Encoding.UTF8.GetString(e.Message), new CultureInfo("en-US")); this.GetFloat = Single.Parse(Encoding.UTF8.GetString(e.Message), new CultureInfo("en-US"));
} }

View File

@ -5,11 +5,18 @@ using System.Windows.Forms.DataVisualization.Charting;
using Dashboard.Sensor; using Dashboard.Sensor;
namespace Dashboard.Tracings { namespace Dashboard.Tracings {
class Graph : ATracings { class Graph : ATracings, IDisposable {
private Series series; private Series series;
private Chart chart; private Chart chart;
public Graph(ASensor sensor, Dictionary<String, String> settings) : base(sensor, settings) { } public Graph(ASensor sensor, Dictionary<String, String> settings) : base(sensor, settings) { }
public void Dispose() {
this.chart.Series.Clear();
this.series = null;
this.chart.Dispose();
}
public override Panel GetPanel() { public override Panel GetPanel() {
Panel panel = new Panel(); Panel panel = new Panel();
@ -52,6 +59,7 @@ namespace Dashboard.Tracings {
return panel; return panel;
} }
protected override void SensorUpdate(Object sender, EventArgs e) { protected override void SensorUpdate(Object sender, EventArgs e) {
if (this.series != null) {
this.chart.BeginInvoke((MethodInvoker)delegate { this.chart.BeginInvoke((MethodInvoker)delegate {
if (this.sensor.Datatypes == ASensor.Types.Bool) { if (this.sensor.Datatypes == ASensor.Types.Bool) {
this.series.Points.AddXY(this.sensor.Timestamp, (this.sensor.GetBool) ? 1 : 0); this.series.Points.AddXY(this.sensor.Timestamp, (this.sensor.GetBool) ? 1 : 0);
@ -66,3 +74,4 @@ namespace Dashboard.Tracings {
} }
} }
} }
}

View File

@ -1,8 +1,14 @@
[Ventilator] [Thinkpad]
topic=zway/power/test/wVentilator topic=zway/wohnzimmer/thinkpad/power
type=Power type=Power
polling=10 polling=10
;zway/wohnzimmer/thinkpad/power ;[Tv]
;zway/wohnzimmer/tV/power ;topic=zway/wohnzimmer/tV/power
;zway/küche/kuehlschrank/power ;type=Power
;polling=10
;[Kühlschrank]
;topic=zway/küche/kuehlschrank/power
;type=Power
;polling=10

View File

@ -1,3 +1,11 @@
[VentilatorGraph] ;[ThinkpadGraph]
sensor=Ventilator ;sensor=Thinkpad
type=Graph ;type=Graph
;[TvGraph]
;sensor=Tv
;type=Graph
;[KühlschrankGraph]
;sensor=Kühlschrank
;type=Graph