From 13ab8987c7850dbd2ad2711f1fbc055951955bcd Mon Sep 17 00:00:00 2001
From: Roger Petri <roger@worfelden.de>
Date: Thu, 30 Jan 2020 15:47:05 +0100
Subject: [PATCH] MQTT connect eingefuegt

---
 Wetterstation/Form1.Designer.cs    | 30 +++++++------
 Wetterstation/Form1.cs             | 70 ++++++++++++++++++++++++++++++
 Wetterstation/Program.cs           |  3 ++
 Wetterstation/Wetterstation.csproj | 10 +++++
 Wetterstation/packages.config      |  6 +++
 5 files changed, 105 insertions(+), 14 deletions(-)
 create mode 100644 Wetterstation/packages.config

diff --git a/Wetterstation/Form1.Designer.cs b/Wetterstation/Form1.Designer.cs
index 49e1249..06e57ef 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 chartArea5 = new System.Windows.Forms.DataVisualization.Charting.ChartArea();
-            System.Windows.Forms.DataVisualization.Charting.Legend legend5 = new System.Windows.Forms.DataVisualization.Charting.Legend();
-            System.Windows.Forms.DataVisualization.Charting.Series series5 = new System.Windows.Forms.DataVisualization.Charting.Series();
-            System.Windows.Forms.DataVisualization.Charting.Title title5 = new System.Windows.Forms.DataVisualization.Charting.Title();
+            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();
             this.lbTextInnentemp = new System.Windows.Forms.Label();
             this.lbTextAussentemp = new System.Windows.Forms.Label();
             this.lbTextWindSpeed = new System.Windows.Forms.Label();
@@ -209,6 +209,7 @@ namespace Wetterstation
             this.lbLuftFeuchtInnen.Size = new System.Drawing.Size(133, 56);
             this.lbLuftFeuchtInnen.TabIndex = 17;
             this.lbLuftFeuchtInnen.Text = "36 %";
+            this.lbLuftFeuchtInnen.Click += new System.EventHandler(this.LbLuftFeuchtInnen_Click);
             // 
             // lbWindricht
             // 
@@ -437,6 +438,7 @@ namespace Wetterstation
             this.lbLuftfeuchtAussen.Size = new System.Drawing.Size(133, 56);
             this.lbLuftfeuchtAussen.TabIndex = 37;
             this.lbLuftfeuchtAussen.Text = "36 %";
+            this.lbLuftfeuchtAussen.Click += new System.EventHandler(this.LbLuftfeuchtAussen_Click);
             // 
             // lbTextLuftfeuchtAussen
             // 
@@ -477,22 +479,22 @@ namespace Wetterstation
             // chart1
             // 
             this.chart1.Anchor = System.Windows.Forms.AnchorStyles.None;
-            chartArea5.Name = "ChartArea1";
-            this.chart1.ChartAreas.Add(chartArea5);
-            legend5.Name = "Legend1";
-            this.chart1.Legends.Add(legend5);
+            chartArea2.Name = "ChartArea1";
+            this.chart1.ChartAreas.Add(chartArea2);
+            legend2.Name = "Legend1";
+            this.chart1.Legends.Add(legend2);
             this.chart1.Location = new System.Drawing.Point(28, 546);
             this.chart1.Name = "chart1";
             this.chart1.Palette = System.Windows.Forms.DataVisualization.Charting.ChartColorPalette.Excel;
-            series5.ChartArea = "ChartArea1";
-            series5.Legend = "Legend1";
-            series5.Name = "Series1";
-            this.chart1.Series.Add(series5);
+            series2.ChartArea = "ChartArea1";
+            series2.Legend = "Legend1";
+            series2.Name = "Series1";
+            this.chart1.Series.Add(series2);
             this.chart1.Size = new System.Drawing.Size(743, 107);
             this.chart1.TabIndex = 41;
             this.chart1.Text = "chTendenz";
-            title5.Name = "Title1";
-            this.chart1.Titles.Add(title5);
+            title2.Name = "Title1";
+            this.chart1.Titles.Add(title2);
             // 
             // lbHintergrund
             // 
diff --git a/Wetterstation/Form1.cs b/Wetterstation/Form1.cs
index f9241ac..0aae021 100644
--- a/Wetterstation/Form1.cs
+++ b/Wetterstation/Form1.cs
@@ -7,14 +7,21 @@ using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 using System.Windows.Forms;
+using uPLibrary.Networking.M2Mqtt;
+using uPLibrary.Networking.M2Mqtt.Messages;
+using Newtonsoft.Json;
 
 namespace Wetterstation
 {
     public partial class Hintergrund : Form
     {
+        private MqttClient myClient;
+
+
         public Hintergrund()
         {
             InitializeComponent();
+            mqttStart();
         }
 
         private void InnenTemp_TextChanged(object sender, EventArgs e)
@@ -137,5 +144,68 @@ namespace Wetterstation
         {
 
         }
+
+        private void LbLuftfeuchtAussen_Click(object sender, EventArgs e)
+        {
+
+        }
+
+        //MQTT connect
+        public void mqttStart()
+        {
+            String topic_publish = "/wetterstation/backend/";
+            String topic_subscribe = "/wetterstation/frontend/#";
+
+            System.Console.WriteLine("Hello, World!");
+            // Create Client instance
+            this.myClient = new MqttClient("mqtt.itstall.de");
+
+            // Register to message received
+            myClient.MqttMsgPublishReceived += client_recievedMessage;
+
+            string clientId = Guid.NewGuid().ToString();
+            this.myClient.Connect("", "wetterstation", "wetterstation");
+
+            // Subscribe to topic
+            this.myClient.Subscribe(new String[] { topic_subscribe }, new byte[] { MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE });
+
+            this.myClient.Publish(topic_publish, Encoding.UTF8.GetBytes("Request_frontend"));
+        }
+
+        public void updateForm(String message)
+        {
+            try
+            {
+                dynamic weatherData = JsonConvert.DeserializeObject(message.ToString());
+                //hierAussen.Invoke(new Action(() => { hierAussen.Text = weatherData.current.temp + " °C"; }));
+                lbAussenTemp.Invoke(new Action(() => { lbAussenTemp.Text = weatherData.sensors[1].temperature + " °C"; }));
+
+                //lbLuftFeuchtInnen.Invoke(new Action(() => { lbLuftFeuchtInnen.Text = weatherData.sensors[0].humidity + " %"; }));
+                //lblpressure.Invoke(new Action(() => { lblpressure.Text = weatherData.result.current.pressure; }));
+                //lblrain3h.Invoke(new Action(() => { lblrain3h.Text = weatherData.result.current.rain3h; }));
+                //lblsnow3h.Invoke(new Action(() => { lblsnow3h.Text = weatherData.result.current.snow3h; }));
+                //lbltemp.Invoke(new Action(() => { lbltemp.Text = weatherData.result.current.temp; }));
+                }
+            catch (Exception ignored) { }
+        }
+
+        public void msgPublish(String topic, String message)
+        {
+            this.myClient.Publish(topic, Encoding.UTF8.GetBytes(message));
+        }
+
+        private void client_recievedMessage(object sender, MqttMsgPublishEventArgs e)
+        {
+            // Handle message received
+            var message = System.Text.Encoding.Default.GetString(e.Message);
+            System.Console.WriteLine("Message received: " + message);
+
+            this.updateForm(message);
+        }
+
+        private void LbLuftFeuchtInnen_Click(object sender, EventArgs e)
+        {
+
+        }
     }
 }
diff --git a/Wetterstation/Program.cs b/Wetterstation/Program.cs
index 704b24d..2d932e2 100644
--- a/Wetterstation/Program.cs
+++ b/Wetterstation/Program.cs
@@ -3,6 +3,9 @@ using System.Collections.Generic;
 using System.Linq;
 using System.Threading.Tasks;
 using System.Windows.Forms;
+using System.Text;
+using uPLibrary.Networking.M2Mqtt;
+using uPLibrary.Networking.M2Mqtt.Messages;
 
 namespace Wetterstation
 {
diff --git a/Wetterstation/Wetterstation.csproj b/Wetterstation/Wetterstation.csproj
index 0958e3f..0983d70 100644
--- a/Wetterstation/Wetterstation.csproj
+++ b/Wetterstation/Wetterstation.csproj
@@ -33,6 +33,15 @@
     <WarningLevel>4</WarningLevel>
   </PropertyGroup>
   <ItemGroup>
+    <Reference Include="LiveCharts, Version=0.9.7.0, Culture=neutral, PublicKeyToken=0bc1f845d1ebb8df, processorArchitecture=MSIL">
+      <HintPath>..\packages\LiveCharts.0.9.7\lib\net45\LiveCharts.dll</HintPath>
+    </Reference>
+    <Reference Include="M2Mqtt.Net, Version=4.3.0.0, Culture=neutral, processorArchitecture=MSIL">
+      <HintPath>..\packages\M2Mqtt.4.3.0.0\lib\net45\M2Mqtt.Net.dll</HintPath>
+    </Reference>
+    <Reference Include="Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
+      <HintPath>..\packages\Newtonsoft.Json.12.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
+    </Reference>
     <Reference Include="System" />
     <Reference Include="System.Core" />
     <Reference Include="System.Windows.Forms.DataVisualization" />
@@ -77,6 +86,7 @@
     <EmbeddedResource Include="Settings.resx">
       <DependentUpon>Settings.cs</DependentUpon>
     </EmbeddedResource>
+    <None Include="packages.config" />
     <None Include="Properties\Settings.settings">
       <Generator>SettingsSingleFileGenerator</Generator>
       <LastGenOutput>Settings.Designer.cs</LastGenOutput>
diff --git a/Wetterstation/packages.config b/Wetterstation/packages.config
new file mode 100644
index 0000000..224026e
--- /dev/null
+++ b/Wetterstation/packages.config
@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="utf-8"?>
+<packages>
+  <package id="LiveCharts" version="0.9.7" targetFramework="net472" />
+  <package id="M2Mqtt" version="4.3.0.0" targetFramework="net472" />
+  <package id="Newtonsoft.Json" version="12.0.3" targetFramework="net472" />
+</packages>
\ No newline at end of file
-- 
GitLab