From b9463f44065eea07116e14822b37a524609d32b8 Mon Sep 17 00:00:00 2001
From: "de@itstall.de" <de@itstall.de>
Date: Mon, 3 Feb 2020 00:32:47 +0100
Subject: [PATCH] Update weather fields in main form

---
 Wetterstation/Form1.Designer.cs | 126 +++++++++++++-------------------
 Wetterstation/Form1.cs          |  59 ++++++++++++---
 2 files changed, 100 insertions(+), 85 deletions(-)

diff --git a/Wetterstation/Form1.Designer.cs b/Wetterstation/Form1.Designer.cs
index 4d991ad..7e6ab18 100644
--- a/Wetterstation/Form1.Designer.cs
+++ b/Wetterstation/Form1.Designer.cs
@@ -36,10 +36,10 @@ namespace Wetterstation
         /// </summary>
         private void InitializeComponent()
         {
-            System.Windows.Forms.DataVisualization.Charting.ChartArea chartArea2 = new System.Windows.Forms.DataVisualization.Charting.ChartArea();
-            System.Windows.Forms.DataVisualization.Charting.Legend legend2 = new System.Windows.Forms.DataVisualization.Charting.Legend();
-            System.Windows.Forms.DataVisualization.Charting.Series series2 = new System.Windows.Forms.DataVisualization.Charting.Series();
-            System.Windows.Forms.DataVisualization.Charting.Title title2 = new System.Windows.Forms.DataVisualization.Charting.Title();
+            System.Windows.Forms.DataVisualization.Charting.ChartArea chartArea1 = new System.Windows.Forms.DataVisualization.Charting.ChartArea();
+            System.Windows.Forms.DataVisualization.Charting.Legend legend1 = new System.Windows.Forms.DataVisualization.Charting.Legend();
+            System.Windows.Forms.DataVisualization.Charting.Series series1 = new System.Windows.Forms.DataVisualization.Charting.Series();
+            System.Windows.Forms.DataVisualization.Charting.Title title1 = new System.Windows.Forms.DataVisualization.Charting.Title();
             this.lbTextInnentemp = new System.Windows.Forms.Label();
             this.lbTextAussentemp = new System.Windows.Forms.Label();
             this.lbTextWindSpeed = new System.Windows.Forms.Label();
@@ -77,11 +77,9 @@ namespace Wetterstation
             this.label4 = new System.Windows.Forms.Label();
             this.label3 = new System.Windows.Forms.Label();
             this.label5 = new System.Windows.Forms.Label();
-            this.label6 = new System.Windows.Forms.Label();
-            this.label7 = new System.Windows.Forms.Label();
+            this.lbTempMax = new System.Windows.Forms.Label();
+            this.lbTempMin = new System.Windows.Forms.Label();
             this.label8 = new System.Windows.Forms.Label();
-            this.label9 = new System.Windows.Forms.Label();
-            this.label10 = new System.Windows.Forms.Label();
             ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
             ((System.ComponentModel.ISupportInitialize)(this.picTg2)).BeginInit();
             ((System.ComponentModel.ISupportInitialize)(this.picTg3)).BeginInit();
@@ -192,9 +190,9 @@ namespace Wetterstation
             this.lbAussenTemp.Location = new System.Drawing.Point(176, 186);
             this.lbAussenTemp.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
             this.lbAussenTemp.Name = "lbAussenTemp";
-            this.lbAussenTemp.Size = new System.Drawing.Size(306, 93);
+            this.lbAussenTemp.Size = new System.Drawing.Size(196, 93);
             this.lbAussenTemp.TabIndex = 15;
-            this.lbAussenTemp.Text = "25,7 C°";
+            this.lbAussenTemp.Text = "0 C°";
             // 
             // lbWindSpeed
             // 
@@ -202,12 +200,12 @@ namespace Wetterstation
             this.lbWindSpeed.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(90)))), ((int)(((byte)(10)))), ((int)(((byte)(80)))), ((int)(((byte)(147)))));
             this.lbWindSpeed.Font = new System.Drawing.Font("Arial", 18F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
             this.lbWindSpeed.ForeColor = System.Drawing.SystemColors.ButtonFace;
-            this.lbWindSpeed.Location = new System.Drawing.Point(813, 55);
+            this.lbWindSpeed.Location = new System.Drawing.Point(844, 55);
             this.lbWindSpeed.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
             this.lbWindSpeed.Name = "lbWindSpeed";
-            this.lbWindSpeed.Size = new System.Drawing.Size(142, 35);
+            this.lbWindSpeed.Size = new System.Drawing.Size(83, 35);
             this.lbWindSpeed.TabIndex = 16;
-            this.lbWindSpeed.Text = "12 m/sec";
+            this.lbWindSpeed.Text = "0 ms";
             // 
             // lbLuftFeuchtInnen
             // 
@@ -218,9 +216,9 @@ namespace Wetterstation
             this.lbLuftFeuchtInnen.Location = new System.Drawing.Point(547, 63);
             this.lbLuftFeuchtInnen.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
             this.lbLuftFeuchtInnen.Name = "lbLuftFeuchtInnen";
-            this.lbLuftFeuchtInnen.Size = new System.Drawing.Size(164, 70);
+            this.lbLuftFeuchtInnen.Size = new System.Drawing.Size(131, 70);
             this.lbLuftFeuchtInnen.TabIndex = 17;
-            this.lbLuftFeuchtInnen.Text = "36 %";
+            this.lbLuftFeuchtInnen.Text = "0 %";
             // 
             // lbWindricht
             // 
@@ -246,7 +244,7 @@ namespace Wetterstation
             this.lbUhr.Name = "lbUhr";
             this.lbUhr.Size = new System.Drawing.Size(137, 35);
             this.lbUhr.TabIndex = 19;
-            this.lbUhr.Text = "22:56:12";
+            this.lbUhr.Text = "00:00:00";
             // 
             // lbDatum
             // 
@@ -259,7 +257,7 @@ namespace Wetterstation
             this.lbDatum.Name = "lbDatum";
             this.lbDatum.Size = new System.Drawing.Size(151, 32);
             this.lbDatum.TabIndex = 20;
-            this.lbDatum.Text = "24.12.1872";
+            this.lbDatum.Text = "01.01.1970";
             // 
             // lbTextTendenz
             // 
@@ -397,9 +395,9 @@ namespace Wetterstation
             this.lbLuftfeuchtAussen.Location = new System.Drawing.Point(549, 196);
             this.lbLuftfeuchtAussen.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
             this.lbLuftfeuchtAussen.Name = "lbLuftfeuchtAussen";
-            this.lbLuftfeuchtAussen.Size = new System.Drawing.Size(164, 70);
+            this.lbLuftfeuchtAussen.Size = new System.Drawing.Size(131, 70);
             this.lbLuftfeuchtAussen.TabIndex = 37;
-            this.lbLuftfeuchtAussen.Text = "36 %";
+            this.lbLuftfeuchtAussen.Text = "0 %";
             // 
             // lbTextLuftfeuchtAussen
             // 
@@ -433,33 +431,33 @@ namespace Wetterstation
             this.lbLuftdruck.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(90)))), ((int)(((byte)(10)))), ((int)(((byte)(80)))), ((int)(((byte)(147)))));
             this.lbLuftdruck.Font = new System.Drawing.Font("Arial", 18F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
             this.lbLuftdruck.ForeColor = System.Drawing.SystemColors.ButtonFace;
-            this.lbLuftdruck.Location = new System.Drawing.Point(820, 132);
+            this.lbLuftdruck.Location = new System.Drawing.Point(832, 135);
             this.lbLuftdruck.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
             this.lbLuftdruck.Name = "lbLuftdruck";
-            this.lbLuftdruck.Size = new System.Drawing.Size(129, 35);
+            this.lbLuftdruck.Size = new System.Drawing.Size(95, 35);
             this.lbLuftdruck.TabIndex = 40;
-            this.lbLuftdruck.Text = "999 hPa";
+            this.lbLuftdruck.Text = "0 hPa";
             // 
             // chart1
             // 
             this.chart1.Anchor = System.Windows.Forms.AnchorStyles.None;
-            chartArea2.Name = "ChartArea1";
-            this.chart1.ChartAreas.Add(chartArea2);
-            legend2.Name = "Legend1";
-            this.chart1.Legends.Add(legend2);
+            chartArea1.Name = "ChartArea1";
+            this.chart1.ChartAreas.Add(chartArea1);
+            legend1.Name = "Legend1";
+            this.chart1.Legends.Add(legend1);
             this.chart1.Location = new System.Drawing.Point(37, 672);
             this.chart1.Margin = new System.Windows.Forms.Padding(4);
             this.chart1.Name = "chart1";
             this.chart1.Palette = System.Windows.Forms.DataVisualization.Charting.ChartColorPalette.Excel;
-            series2.ChartArea = "ChartArea1";
-            series2.Legend = "Legend1";
-            series2.Name = "Series1";
-            this.chart1.Series.Add(series2);
+            series1.ChartArea = "ChartArea1";
+            series1.Legend = "Legend1";
+            series1.Name = "Series1";
+            this.chart1.Series.Add(series1);
             this.chart1.Size = new System.Drawing.Size(991, 132);
             this.chart1.TabIndex = 41;
             this.chart1.Text = "chTendenz";
-            title2.Name = "Title1";
-            this.chart1.Titles.Add(title2);
+            title1.Name = "Title1";
+            this.chart1.Titles.Add(title1);
             // 
             // lbHintergrund
             // 
@@ -559,25 +557,25 @@ namespace Wetterstation
             this.label5.TabIndex = 51;
             this.label5.Text = "12,4 C°";
             // 
-            // label6
+            // lbTempMax
             // 
-            this.label6.AutoSize = true;
-            this.label6.Location = new System.Drawing.Point(281, 607);
-            this.label6.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
-            this.label6.Name = "label6";
-            this.label6.Size = new System.Drawing.Size(47, 17);
-            this.label6.TabIndex = 52;
-            this.label6.Text = "2,4 C°";
+            this.lbTempMax.AutoSize = true;
+            this.lbTempMax.Location = new System.Drawing.Point(281, 607);
+            this.lbTempMax.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
+            this.lbTempMax.Name = "lbTempMax";
+            this.lbTempMax.Size = new System.Drawing.Size(35, 17);
+            this.lbTempMax.TabIndex = 52;
+            this.lbTempMax.Text = "0 C°";
             // 
-            // label7
+            // lbTempMin
             // 
-            this.label7.AutoSize = true;
-            this.label7.Location = new System.Drawing.Point(281, 634);
-            this.label7.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
-            this.label7.Name = "label7";
-            this.label7.Size = new System.Drawing.Size(47, 17);
-            this.label7.TabIndex = 54;
-            this.label7.Text = "2,4 C°";
+            this.lbTempMin.AutoSize = true;
+            this.lbTempMin.Location = new System.Drawing.Point(281, 634);
+            this.lbTempMin.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
+            this.lbTempMin.Name = "lbTempMin";
+            this.lbTempMin.Size = new System.Drawing.Size(35, 17);
+            this.lbTempMin.TabIndex = 54;
+            this.lbTempMin.Text = "0 C°";
             // 
             // label8
             // 
@@ -589,26 +587,6 @@ namespace Wetterstation
             this.label8.TabIndex = 53;
             this.label8.Text = "12,4 C°";
             // 
-            // label9
-            // 
-            this.label9.AutoSize = true;
-            this.label9.Location = new System.Drawing.Point(324, 277);
-            this.label9.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
-            this.label9.Name = "label9";
-            this.label9.Size = new System.Drawing.Size(47, 17);
-            this.label9.TabIndex = 56;
-            this.label9.Text = "2,4 C°";
-            // 
-            // label10
-            // 
-            this.label10.AutoSize = true;
-            this.label10.Location = new System.Drawing.Point(204, 277);
-            this.label10.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
-            this.label10.Name = "label10";
-            this.label10.Size = new System.Drawing.Size(55, 17);
-            this.label10.TabIndex = 55;
-            this.label10.Text = "12,4 C°";
-            // 
             // Hintergrund
             // 
             this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
@@ -616,11 +594,9 @@ namespace Wetterstation
             this.BackgroundImage = global::Wetterstation.Properties.Resources.Bild_Dennis_Formatiert;
             this.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
             this.ClientSize = new System.Drawing.Size(1344, 897);
-            this.Controls.Add(this.label9);
-            this.Controls.Add(this.label10);
-            this.Controls.Add(this.label7);
+            this.Controls.Add(this.lbTempMin);
             this.Controls.Add(this.label8);
-            this.Controls.Add(this.label6);
+            this.Controls.Add(this.lbTempMax);
             this.Controls.Add(this.label5);
             this.Controls.Add(this.label3);
             this.Controls.Add(this.label4);
@@ -714,11 +690,9 @@ namespace Wetterstation
         private Label label4;
         private Label label3;
         private Label label5;
-        private Label label6;
-        private Label label7;
+        private Label lbTempMin;
         private Label label8;
-        private Label label9;
-        private Label label10;
+        private Label lbTempMax;
     }
 }
 
diff --git a/Wetterstation/Form1.cs b/Wetterstation/Form1.cs
index c116fe1..d2a8b33 100644
--- a/Wetterstation/Form1.cs
+++ b/Wetterstation/Form1.cs
@@ -109,21 +109,42 @@ namespace Wetterstation
             // Handle message received
             var message = System.Text.Encoding.Default.GetString(e.Message);
             System.Console.WriteLine("Message received: " + message);
-
-            this.updateFormWeather(message);
+            Console.WriteLine("With Topic: " + e.Topic);
+            
+            if (e.Topic.Equals(WeatherstationSettings.mqtt_topic_frontend + WeatherstationSettings.frontendId + "/weather"))
+            {
+                this.updateFormWeather(message);
+            }
+            else if (e.Topic.Equals(WeatherstationSettings.mqtt_topic_frontend + WeatherstationSettings.frontendId + "/forecast"))
+            {
+                this.updateFormForecast(message);
+            }
+            else if (e.Topic.Equals(WeatherstationSettings.mqtt_topic_frontend + WeatherstationSettings.frontendId + "/sensors"))
+            {
+                this.updateFormSensors(message);
+            }
         }
 
-        private void BtSystemKnopf_Click(object sender, EventArgs e)
+        public void updateFormWeather(String message)
         {
-            Form settings = new Settings();
-            // abhängig von Form1
-            settings.ShowDialog();
-            updateFormComplete();
-            this.updateSettings2Mqtt();
+            Console.WriteLine("Entered updateFormWeather");
+            try
+            {
+                dynamic weatherData = JsonConvert.DeserializeObject(message.ToString());
+                lbAussenTemp.Invoke(new Action(() => { lbAussenTemp.Text = weatherData.sensors[1].temperature + " °C"; }));
+                lbTempMin.Invoke(new Action(() => { lbTempMin.Text = weatherData.tempMin + " °C"; }));
+                lbTempMax.Invoke(new Action(() => { lbTempMax.Text = weatherData.tempMax + " °C"; }));
+                lbLuftfeuchtAussen.Invoke(new Action(() => { lbLuftfeuchtAussen.Text = weatherData.humidity + " %"; }));
+                lbWindSpeed.Invoke(new Action(() => { lbWindSpeed.Text = weatherData.windSpeed + " ms"; }));
+                lbWindricht.Invoke(new Action(() => { lbWindricht.Text = weatherData.windDeg + " °"; }));
+                lbLuftdruck.Invoke(new Action(() => { lbLuftdruck.Text = weatherData.pressure + " mbar"; }));
+            }
+            catch (Exception ignored) { }
         }
 
-        public void updateFormWeather(String message)
+        public void updateFormForecast(String message)
         {
+            Console.WriteLine("Entered updateFormForecast");
             try
             {
                 dynamic weatherData = JsonConvert.DeserializeObject(message.ToString());
@@ -132,6 +153,17 @@ namespace Wetterstation
             catch (Exception ignored) { }
         }
 
+        public void updateFormSensors(String message)
+        {
+            Console.WriteLine("Entered updateFormSensors");
+            try
+            {
+                dynamic weatherData = JsonConvert.DeserializeObject(message.ToString());
+                lbInnenTemp.Invoke(new Action(() => { lbInnenTemp.Text = weatherData.sensors[0].temperature + " °C"; }));
+            }
+            catch (Exception ignored) { }
+        }
+
         public void updateFormComplete()
         {
             WeatherstationSettings = SqliteDataAccess.LoadWeatherstationSettings();
@@ -144,6 +176,15 @@ namespace Wetterstation
             this.msgPublish(WeatherstationSettings.mqtt_topic_backend + WeatherstationSettings.frontendId + "/settings", JsonConvert.SerializeObject(WeatherstationSettings).ToString());
         }
 
+        private void BtSystemKnopf_Click(object sender, EventArgs e)
+        {
+            Form settings = new Settings();
+            // abhängig von Form1
+            settings.ShowDialog();
+            updateFormComplete();
+            this.updateSettings2Mqtt();
+        }
+
         private void Hintergrund_Load(object sender, EventArgs e)
         {
 
-- 
GitLab