From 51e7cd725e63ef514ce53c0c0b66a5739b8adff2 Mon Sep 17 00:00:00 2001 From: Christian Kratky Date: Tue, 19 Sep 2017 23:03:30 +0200 Subject: [PATCH] Extend UWP test app --- .../MQTTnet.TestApp.UniversalWindows/App.xaml | 2 - .../MQTTnet.TestApp.UniversalWindows.csproj | 5 +- .../MainPage.xaml | 98 +++++++++++---- .../MainPage.xaml.cs | 113 +++++++++++++++++- .../Package.appxmanifest | 2 +- 5 files changed, 193 insertions(+), 27 deletions(-) diff --git a/Tests/MQTTnet.TestApp.UniversalWindows/App.xaml b/Tests/MQTTnet.TestApp.UniversalWindows/App.xaml index 5f70fb2..bdd5cc0 100644 --- a/Tests/MQTTnet.TestApp.UniversalWindows/App.xaml +++ b/Tests/MQTTnet.TestApp.UniversalWindows/App.xaml @@ -2,7 +2,5 @@ x:Class="MQTTnet.TestApp.UniversalWindows.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" - xmlns:local="using:MQTTnet.TestApp.UniversalWindows" RequestedTheme="Light"> - diff --git a/Tests/MQTTnet.TestApp.UniversalWindows/MQTTnet.TestApp.UniversalWindows.csproj b/Tests/MQTTnet.TestApp.UniversalWindows/MQTTnet.TestApp.UniversalWindows.csproj index d1dac53..e7c65f2 100644 --- a/Tests/MQTTnet.TestApp.UniversalWindows/MQTTnet.TestApp.UniversalWindows.csproj +++ b/Tests/MQTTnet.TestApp.UniversalWindows/MQTTnet.TestApp.UniversalWindows.csproj @@ -137,7 +137,10 @@ - 5.3.3 + 5.4.0 + + + 2.0.0 diff --git a/Tests/MQTTnet.TestApp.UniversalWindows/MainPage.xaml b/Tests/MQTTnet.TestApp.UniversalWindows/MainPage.xaml index 643460f..b097b50 100644 --- a/Tests/MQTTnet.TestApp.UniversalWindows/MainPage.xaml +++ b/Tests/MQTTnet.TestApp.UniversalWindows/MainPage.xaml @@ -2,9 +2,10 @@ x:Class="MQTTnet.TestApp.UniversalWindows.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" - xmlns:local="using:MQTTnet.TestApp.UniversalWindows" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" + d:DesignHeight="800" + d:DesignWidth="800" mc:Ignorable="d"> @@ -12,33 +13,86 @@ + - - Server: - - User: - - Password: - - ClientId: - - - - TCP - WS - - - Use TLS - - Trace: - + + + + Server: + + User: + + Password: + + ClientId: + + + + TCP + WS + Use TLS + + + + + + + + + + + + Topic: + + + Payload: + + + Text + Base64 + + Retain: + - - + QoS: + + 0 (At most once) + 1 (At least once) + 2 (Exactly once) + + + + + + + + Topic: + + + QoS: + + 0 (At most once) + 1 (At least once) + 2 (Exactly once) + + + + + + + + + + + + + + + diff --git a/Tests/MQTTnet.TestApp.UniversalWindows/MainPage.xaml.cs b/Tests/MQTTnet.TestApp.UniversalWindows/MainPage.xaml.cs index 88cce71..e05e847 100644 --- a/Tests/MQTTnet.TestApp.UniversalWindows/MainPage.xaml.cs +++ b/Tests/MQTTnet.TestApp.UniversalWindows/MainPage.xaml.cs @@ -1,9 +1,13 @@ using System; +using System.Text; using System.Threading.Tasks; using Windows.UI.Core; using Windows.UI.Xaml; +using MQTTnet.Core; using MQTTnet.Core.Client; using MQTTnet.Core.Diagnostics; +using MQTTnet.Core.Packets; +using MQTTnet.Core.Protocol; namespace MQTTnet.TestApp.UniversalWindows { @@ -22,7 +26,7 @@ namespace MQTTnet.TestApp.UniversalWindows { await Trace.Dispatcher.RunAsync(CoreDispatcherPriority.High, () => { - var text = $"[{DateTime.Now:O}] [{e.Level}] [{e.Source}] [{e.ThreadId}] [{e.Message}]{Environment.NewLine}"; + var text = $"[{DateTime.Now:yyyy-MM-dd HH:mm:ss.fff}] [{e.Level}] [{e.Source}] [{e.ThreadId}] [{e.Message}]{Environment.NewLine}"; if (e.Exception != null) { text += $"{e.Exception}{Environment.NewLine}"; @@ -60,5 +64,112 @@ namespace MQTTnet.TestApp.UniversalWindows Trace.Text += exception + Environment.NewLine; } } + + private async void Publish(object sender, RoutedEventArgs e) + { + if (_mqttClient == null) + { + return; + } + + try + { + var qos = MqttQualityOfServiceLevel.AtMostOnce; + if (QoS1.IsChecked == true) + { + qos = MqttQualityOfServiceLevel.AtLeastOnce; + } + + if (QoS2.IsChecked == true) + { + qos = MqttQualityOfServiceLevel.ExactlyOnce; + } + + var payload = new byte[0]; + if (Text.IsChecked == true) + { + payload = Encoding.UTF8.GetBytes(Payload.Text); + } + + if (Base64.IsChecked == true) + { + payload = Convert.FromBase64String(Payload.Text); + } + + var message = new MqttApplicationMessage( + Topic.Text, + payload, + qos, + Retain.IsChecked == true); + + await _mqttClient.PublishAsync(message); + } + catch (Exception exception) + { + Trace.Text += exception + Environment.NewLine; + } + } + + private async void Disconnect(object sender, RoutedEventArgs e) + { + try + { + await _mqttClient.DisconnectAsync(); + } + catch (Exception exception) + { + Trace.Text += exception + Environment.NewLine; + } + } + + private void Clear(object sender, RoutedEventArgs e) + { + Trace.Text = string.Empty; + } + + private async void Subscribe(object sender, RoutedEventArgs e) + { + if (_mqttClient == null) + { + return; + } + + try + { + var qos = MqttQualityOfServiceLevel.AtMostOnce; + if (SubscribeQoS1.IsChecked == true) + { + qos = MqttQualityOfServiceLevel.AtLeastOnce; + } + + if (SubscribeQoS2.IsChecked == true) + { + qos = MqttQualityOfServiceLevel.ExactlyOnce; + } + + await _mqttClient.SubscribeAsync(new TopicFilter(SubscribeTopic.Text, qos)); + } + catch (Exception exception) + { + Trace.Text += exception + Environment.NewLine; + } + } + + private async void Unsubscribe(object sender, RoutedEventArgs e) + { + if (_mqttClient == null) + { + return; + } + + try + { + await _mqttClient.UnsubscribeAsync(SubscribeTopic.Text); + } + catch (Exception exception) + { + Trace.Text += exception + Environment.NewLine; + } + } } } diff --git a/Tests/MQTTnet.TestApp.UniversalWindows/Package.appxmanifest b/Tests/MQTTnet.TestApp.UniversalWindows/Package.appxmanifest index 0eb9c88..d6a7c4c 100644 --- a/Tests/MQTTnet.TestApp.UniversalWindows/Package.appxmanifest +++ b/Tests/MQTTnet.TestApp.UniversalWindows/Package.appxmanifest @@ -15,7 +15,7 @@ - +