82 lines
3.1 KiB
C#
82 lines
3.1 KiB
C#
using System;
|
|
using System.Text;
|
|
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<String, ASensor> sensors = new Dictionary<String, ASensor>();
|
|
private Thread updateThread;
|
|
|
|
public Dashboard() {
|
|
InitializeComponent();
|
|
Mqtt.Instance.Connect();
|
|
|
|
this.GenerateSensors();
|
|
this.GenerateForms();
|
|
|
|
this.updateThread = new Thread(this.SensorPolling);
|
|
this.updateThread.Start();
|
|
|
|
this.FormClosed += this.Dashboard_FormClosed;
|
|
|
|
Mqtt.Instance.MessageIncomming += this.Instance_MessageIncomming;
|
|
}
|
|
|
|
private void GenerateSensors() {
|
|
InIReader ini = InIReader.GetInstance("sensor.ini");
|
|
List<String> 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;
|
|
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);
|
|
}
|
|
}
|
|
private void GenerateForms() {
|
|
InIReader ini = InIReader.GetInstance("tracings.ini");
|
|
List<String> 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;
|
|
case "meter": t = new Meter(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<String, ASensor> 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) { }
|
|
this.sensors.Clear();
|
|
Mqtt.Instance.MessageIncomming -= this.Instance_MessageIncomming;
|
|
Mqtt.Instance.Dispose();
|
|
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());
|
|
}
|
|
}
|
|
}
|