diff --git a/Mqtt-Dashboard/Dashboard.csproj b/Mqtt-Dashboard/Dashboard.csproj index f0d2bb4..94b8c73 100644 --- a/Mqtt-Dashboard/Dashboard.csproj +++ b/Mqtt-Dashboard/Dashboard.csproj @@ -64,6 +64,8 @@ + + Form1.cs diff --git a/Mqtt-Dashboard/Form1.Designer.cs b/Mqtt-Dashboard/Form1.Designer.cs index 787c2a1..489c590 100644 --- a/Mqtt-Dashboard/Form1.Designer.cs +++ b/Mqtt-Dashboard/Form1.Designer.cs @@ -29,7 +29,7 @@ // // statusStrip1 // - this.statusStrip1.Location = new System.Drawing.Point(0, 304); + this.statusStrip1.Location = new System.Drawing.Point(0, 370); this.statusStrip1.Name = "statusStrip1"; this.statusStrip1.Size = new System.Drawing.Size(843, 22); this.statusStrip1.TabIndex = 0; @@ -39,14 +39,14 @@ // this.flowLayoutPanel2.Location = new System.Drawing.Point(12, 12); this.flowLayoutPanel2.Name = "flowLayoutPanel2"; - this.flowLayoutPanel2.Size = new System.Drawing.Size(819, 289); + this.flowLayoutPanel2.Size = new System.Drawing.Size(819, 355); this.flowLayoutPanel2.TabIndex = 3; // // Dashboard // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(843, 326); + this.ClientSize = new System.Drawing.Size(843, 392); this.Controls.Add(this.flowLayoutPanel2); this.Controls.Add(this.statusStrip1); this.Name = "Dashboard"; diff --git a/Mqtt-Dashboard/Form1.cs b/Mqtt-Dashboard/Form1.cs index 9a46687..7df74d4 100644 --- a/Mqtt-Dashboard/Form1.cs +++ b/Mqtt-Dashboard/Form1.cs @@ -7,6 +7,7 @@ using Dashboard.Tracings; using BlubbFish.Utils; using System.Collections.Generic; using System.Threading; +using Dashboard.tracings; namespace Dashboard { public partial class Dashboard : Form { @@ -24,11 +25,17 @@ namespace Dashboard { this.updateThread.Start(); this.FormClosed += this.Dashboard_FormClosed; + this.SizeChanged += this.Dashboard_SizeChanged; Mqtt.Instance.MessageIncomming += this.Instance_MessageIncomming; } - private void GenerateSensors() { + private void Dashboard_SizeChanged(Object sender, EventArgs e) { + this.flowLayoutPanel2.Size = new System.Drawing.Size(this.Size.Width - 40, this.Size.Width - 76); + //this.Size.Height + } + + private void GenerateSensors() { InIReader ini = InIReader.GetInstance("sensor.ini"); List sensorini = ini.GetSections(); foreach(String sensor in sensorini) { @@ -47,10 +54,11 @@ namespace Dashboard { 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"); + 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; + case "powermeter": t = new PowerMeter(this.sensors[ini.GetValue(tracing, "sensor").ToLower()], ini.GetSection(tracing)); break; + case "switcher": t = new Switcher(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()); } diff --git a/Mqtt-Dashboard/Sensor/ASensor.cs b/Mqtt-Dashboard/Sensor/ASensor.cs index ca1a6cc..be70005 100644 --- a/Mqtt-Dashboard/Sensor/ASensor.cs +++ b/Mqtt-Dashboard/Sensor/ASensor.cs @@ -57,5 +57,9 @@ namespace Dashboard.Sensor { Dispose(true); GC.SuppressFinalize(this); } + + internal virtual void SetBool(Boolean v) { + Mqtt.Instance.Send(this.topic + "/set", v ? "on" : "off"); + } } } \ No newline at end of file diff --git a/Mqtt-Dashboard/Tracings/Meter.cs b/Mqtt-Dashboard/Tracings/Meter.cs index fbdf3ff..c7225c4 100644 --- a/Mqtt-Dashboard/Tracings/Meter.cs +++ b/Mqtt-Dashboard/Tracings/Meter.cs @@ -7,18 +7,22 @@ using System.Globalization; namespace Dashboard.Tracings { class Meter : ATracings { - private Int32 DisplayItems; - private Queue DisplayAverage = new Queue(); - private String DisplayUnit; - private Label label1; - private Label label2; + protected Int32 DisplayItems; + protected Queue DisplayAverage = new Queue(); + protected Label label1; + protected Label label2; public Meter(ASensor sensor, Dictionary settings) : base(sensor, settings) { - this.DisplayUnit = (settings.Keys.Contains("unit")) ? settings["unit"] : ""; this.DisplayItems = (settings.Keys.Contains("items")) ? Int32.Parse(settings["items"]) : 1000; + + this.label1 = new Label(); + this.label2 = new Label(); } public override Panel GetPanel() { - Panel panel = new Panel(); + Panel panel = new Panel() { + BorderStyle = BorderStyle.FixedSingle, + Size = new System.Drawing.Size(200, 100) + }; this.label1 = new Label() { Font = new System.Drawing.Font("Lucida Sans Typewriter", 27.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((Byte)(0))), @@ -27,6 +31,7 @@ namespace Dashboard.Tracings { Text = "", TextAlign = System.Drawing.ContentAlignment.TopCenter }; + panel.Controls.Add(this.label1); this.label2 = new Label() { AutoSize = true, @@ -34,11 +39,9 @@ namespace Dashboard.Tracings { Size = new System.Drawing.Size(96, 13), Text = "" }; - - panel.BorderStyle = BorderStyle.FixedSingle; panel.Controls.Add(this.label2); - panel.Controls.Add(this.label1); - panel.Size = new System.Drawing.Size(200, 100); + + return panel; } protected override void SensorUpdate(Object sender, EventArgs e) { @@ -53,10 +56,10 @@ namespace Dashboard.Tracings { } Single average = this.DisplayAverage.Sum() / this.DisplayAverage.Count(); this.label1.BeginInvoke((MethodInvoker)delegate { - this.label1.Text = value+" "+this.DisplayUnit; + this.label1.Text = value; }); this.label2.BeginInvoke((MethodInvoker)delegate { - this.label2.Text = "Durchschnitt: " + Math.Round(average, 2) + " " + this.DisplayUnit; + this.label2.Text = "Durchschnitt: " + Math.Round(average, 2); }); } } diff --git a/Mqtt-Dashboard/Tracings/PowerMeter.cs b/Mqtt-Dashboard/Tracings/PowerMeter.cs new file mode 100644 index 0000000..816d97b --- /dev/null +++ b/Mqtt-Dashboard/Tracings/PowerMeter.cs @@ -0,0 +1,44 @@ +using System; +using System.Collections.Generic; +using System.Globalization; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; +using Dashboard.Sensor; + +namespace Dashboard.Tracings { + class PowerMeter : Meter { + public PowerMeter(ASensor sensor, Dictionary settings) : base(sensor, settings) { + } + protected override void SensorUpdate(Object sender, EventArgs e) { + String value = ""; + switch (this.sensor.Datatypes) { + case ASensor.Types.Bool: + this.DisplayAverage.Enqueue((this.sensor.GetBool) ? 1 : 0); + value = this.sensor.GetBool.ToString(new CultureInfo("de-DE")); + break; + case ASensor.Types.Int: + this.DisplayAverage.Enqueue(this.sensor.GetInt); + value = this.sensor.GetInt.ToString(); + break; + case ASensor.Types.Float: + this.DisplayAverage.Enqueue(this.sensor.GetFloat); + value = this.sensor.GetFloat.ToString(new CultureInfo("de-DE")); + break; + } + if (this.DisplayAverage.Count > this.DisplayItems) { + this.DisplayAverage.Dequeue(); + } + Single average = this.DisplayAverage.Sum() / this.DisplayAverage.Count(); + try { + this.label1.BeginInvoke((MethodInvoker)delegate { + this.label1.Text = value + " W"; + }); + this.label2.BeginInvoke((MethodInvoker)delegate { + this.label2.Text = "Durchschnitt: " + Math.Round(average, 2) + " W"; + }); + } catch (Exception) { } + } + } +} diff --git a/Mqtt-Dashboard/Tracings/Switcher.cs b/Mqtt-Dashboard/Tracings/Switcher.cs new file mode 100644 index 0000000..0fe31f1 --- /dev/null +++ b/Mqtt-Dashboard/Tracings/Switcher.cs @@ -0,0 +1,81 @@ +using System; +using System.Collections.Generic; +using System.Globalization; +using System.Windows.Forms; +using Dashboard.Connector; +using Dashboard.Sensor; +using Dashboard.Tracings; + +namespace Dashboard.tracings { + class Switcher : ATracings { + private Label label; + private Button button1; + private Button button2; + + public Switcher(ASensor sensor, Dictionary settings) : base(sensor, settings) { + this.label = new Label(); + this.button1 = new Button(); + this.button2 = new Button(); + } + + public override Panel GetPanel() { + Panel panel = new Panel() { + BorderStyle = BorderStyle.FixedSingle, + Size = new System.Drawing.Size(200, 100) + }; + + this.label = new Label() { + AutoSize = true, + Location = new System.Drawing.Point(69, 16), + Size = new System.Drawing.Size(35, 13), + Text = "" + }; + panel.Controls.Add(this.label); + + this.button1 = new Button() { + BackColor = System.Drawing.Color.FromArgb(0, 192, 0), + Location = new System.Drawing.Point(94, 67), + Name = "on", + Size = new System.Drawing.Size(45, 23), + Text = "ON", + UseVisualStyleBackColor = false + }; + this.button1.Click += this.ButtonClicker; + panel.Controls.Add(this.button1); + + this.button2 = new Button() { + BackColor = System.Drawing.Color.Red, + Location = new System.Drawing.Point(145, 67), + Name = "off", + Size = new System.Drawing.Size(45, 23), + Text = "OFF", + UseVisualStyleBackColor = false + }; + this.button2.Click += this.ButtonClicker; + panel.Controls.Add(this.button2); + + return panel; + } + + private void ButtonClicker(Object sender, EventArgs e) { + Button b = (Button)sender; + if (b.Name == "on") { + this.sensor.SetBool(true); + } else if(b.Name == "off") { + this.sensor.SetBool(false); + } + } + + protected override void SensorUpdate(Object sender, EventArgs e) { + this.label.BeginInvoke((MethodInvoker)delegate { + this.label.Text = this.sensor.GetBool?"Eingeschaltet":"Ausgeschaltet"; + }); + this.button1.BeginInvoke((MethodInvoker)delegate { + this.button1.Enabled = !this.sensor.GetBool; + }); + this.button2.BeginInvoke((MethodInvoker)delegate { + this.button2.Enabled = this.sensor.GetBool; + }); + } + } +} \ No newline at end of file diff --git a/Mqtt-Dashboard/bin/Release/Dashboard.exe b/Mqtt-Dashboard/bin/Release/Dashboard.exe new file mode 100644 index 0000000..5a27e58 Binary files /dev/null and b/Mqtt-Dashboard/bin/Release/Dashboard.exe differ diff --git a/Mqtt-Dashboard/bin/Release/M2Mqtt.Net.dll b/Mqtt-Dashboard/bin/Release/M2Mqtt.Net.dll new file mode 100644 index 0000000..154580d Binary files /dev/null and b/Mqtt-Dashboard/bin/Release/M2Mqtt.Net.dll differ diff --git a/Mqtt-Dashboard/bin/Release/Utils.dll b/Mqtt-Dashboard/bin/Release/Utils.dll new file mode 100644 index 0000000..d2e7e9c Binary files /dev/null and b/Mqtt-Dashboard/bin/Release/Utils.dll differ diff --git a/Mqtt-Dashboard/bin/Release/sensor.ini b/Mqtt-Dashboard/bin/Release/sensor.ini new file mode 100644 index 0000000..ca51346 --- /dev/null +++ b/Mqtt-Dashboard/bin/Release/sensor.ini @@ -0,0 +1,24 @@ +[Thinkpad] +topic=zway/wohnzimmer/thinkpad/power +type=Power +polling=10 + +[Tv] +topic=zway/wohnzimmer/tV/power +type=Power +polling=10 + +[Kühlschrank] +topic=zway/küche/kuehlschrank/power +type=Power +polling=10 + +[TvSW] +topic=zway/wohnzimmer/tV/switch +type=switch +polling=10 + +;[Ventilator] +;topic=zway/power/test/wVentilator +;type=Power +;polling=10 \ No newline at end of file diff --git a/Mqtt-Dashboard/bin/Release/settings.ini b/Mqtt-Dashboard/bin/Release/settings.ini new file mode 100644 index 0000000..d392063 --- /dev/null +++ b/Mqtt-Dashboard/bin/Release/settings.ini @@ -0,0 +1,2 @@ +[general] +mqtt-server=localhost \ No newline at end of file diff --git a/Mqtt-Dashboard/bin/Release/tracings.ini b/Mqtt-Dashboard/bin/Release/tracings.ini new file mode 100644 index 0000000..1085580 --- /dev/null +++ b/Mqtt-Dashboard/bin/Release/tracings.ini @@ -0,0 +1,45 @@ +[ThinkpadMeter] +sensor=Thinkpad +type=PowerMeter +items=100 + +[TvMeter] +sensor=Tv +type=PowerMeter +items=100 + +[KühlschrankrMeter] +sensor=Kühlschrank +type=PowerMeter +items=100 + +[ThinkpadGraph] +sensor=Thinkpad +type=Graph +items=100 + +[TvGraph] +sensor=Tv +type=Graph +items=100 + +[KühlschrankGraph] +sensor=Kühlschrank +type=Graph +items=100 + +[TvSWMeter] +sensor=TvSW +type=Switcher +items=100 + +;[VentilatorGraph] +;sensor=Ventilator +;type=Graph +;items=100 + +;[VentilatorMeter] +;sensor=Ventilator +;type=Meter +;unit=W +;items=100 \ No newline at end of file diff --git a/Mqtt-Dashboard/sensor.ini b/Mqtt-Dashboard/sensor.ini index 23e9eb8..ca51346 100644 --- a/Mqtt-Dashboard/sensor.ini +++ b/Mqtt-Dashboard/sensor.ini @@ -1,19 +1,24 @@ -;[Thinkpad] -;topic=zway/wohnzimmer/thinkpad/power -;type=Power -;polling=10 - -;[Tv] -;topic=zway/wohnzimmer/tV/power -;type=Power -;polling=10 - -;[Kühlschrank] -;topic=zway/küche/kuehlschrank/power -;type=Power -;polling=10 - -[Ventilator] -topic=zway/power/test/wVentilator +[Thinkpad] +topic=zway/wohnzimmer/thinkpad/power type=Power -polling=10 \ No newline at end of file +polling=10 + +[Tv] +topic=zway/wohnzimmer/tV/power +type=Power +polling=10 + +[Kühlschrank] +topic=zway/küche/kuehlschrank/power +type=Power +polling=10 + +[TvSW] +topic=zway/wohnzimmer/tV/switch +type=switch +polling=10 + +;[Ventilator] +;topic=zway/power/test/wVentilator +;type=Power +;polling=10 \ No newline at end of file diff --git a/Mqtt-Dashboard/settings.ini b/Mqtt-Dashboard/settings.ini index 8fbdba0..d392063 100644 --- a/Mqtt-Dashboard/settings.ini +++ b/Mqtt-Dashboard/settings.ini @@ -1,2 +1,2 @@ [general] -mqtt-server=129.26.160.65 \ No newline at end of file +mqtt-server=localhost \ No newline at end of file diff --git a/Mqtt-Dashboard/tracings.ini b/Mqtt-Dashboard/tracings.ini index 602e3a0..1085580 100644 --- a/Mqtt-Dashboard/tracings.ini +++ b/Mqtt-Dashboard/tracings.ini @@ -1,22 +1,45 @@ -;[ThinkpadGraph] -;sensor=Thinkpad -;type=Graph +[ThinkpadMeter] +sensor=Thinkpad +type=PowerMeter +items=100 -;[TvGraph] -;sensor=Tv -;type=Graph +[TvMeter] +sensor=Tv +type=PowerMeter +items=100 -;[KühlschrankGraph] -;sensor=Kühlschrank -;type=Graph +[KühlschrankrMeter] +sensor=Kühlschrank +type=PowerMeter +items=100 -[VentilatorGraph] -sensor=Ventilator +[ThinkpadGraph] +sensor=Thinkpad type=Graph items=100 -[VentilatorMeter] -sensor=Ventilator -type=Meter -unit=W -items=100 \ No newline at end of file +[TvGraph] +sensor=Tv +type=Graph +items=100 + +[KühlschrankGraph] +sensor=Kühlschrank +type=Graph +items=100 + +[TvSWMeter] +sensor=TvSW +type=Switcher +items=100 + +;[VentilatorGraph] +;sensor=Ventilator +;type=Graph +;items=100 + +;[VentilatorMeter] +;sensor=Ventilator +;type=Meter +;unit=W +;items=100 \ No newline at end of file