forked from Azure/azure-iot-sdk-csharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainPage.xaml.cs
More file actions
172 lines (144 loc) · 6.07 KB
/
MainPage.xaml.cs
File metadata and controls
172 lines (144 loc) · 6.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
using System.Collections.Generic;
using System.Linq;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.ApplicationModel.Core;
using Windows.UI.Core;
using System;
using System.Diagnostics;
namespace Microsoft.Azure.Devices.Client.Samples
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{
IoTClient client;
struct ProtocolNameToIdLookupEntry
{
public string Name;
public TransportType Id;
}
List<ProtocolNameToIdLookupEntry> protocolLookup = new List<ProtocolNameToIdLookupEntry>()
{
new ProtocolNameToIdLookupEntry { Name = "HTTP", Id = TransportType.Http1 },
new ProtocolNameToIdLookupEntry { Name = "AMQP", Id = TransportType.Amqp },
new ProtocolNameToIdLookupEntry { Name = "MQTT", Id = TransportType.Mqtt },
new ProtocolNameToIdLookupEntry { Name = "MQTT-TCP", Id = TransportType.Mqtt_Tcp_Only },
new ProtocolNameToIdLookupEntry { Name = "MQTT-WebSockets", Id = TransportType.Mqtt_WebSocket_Only },
};
string GetProtocolNameFromId(TransportType protocolId)
{
return protocolLookup.Where(_ => _.Id == protocolId).First().Name;
}
TransportType GetProtocolIdFromName(string protocolName)
{
return protocolLookup.Where(_ => _.Name == protocolName).First().Id;
}
public MainPage()
{
this.InitializeComponent();
var defaultProtocol = TransportType.Mqtt;
this.client = new IoTClient(defaultProtocol, CallMeLogger, GetDeviceNameLogger, ErrorHandler);
protocolLookup.ForEach(_ =>
{
this.protocolComboBox.Items.Add(_.Name);
});
this.protocolComboBox.SelectedIndex = protocolLookup.IndexOf(new ProtocolNameToIdLookupEntry { Id = defaultProtocol, Name = GetProtocolNameFromId(defaultProtocol) });
#pragma warning disable 4014
client.Start();
#pragma warning restore 4014
}
private void sendButton_Click(object sender, RoutedEventArgs e)
{
string msg = this.messageText.Text;
this.client.SendEvent(msg);
}
private async void receiveButton_Click(object sender, RoutedEventArgs e)
{
string msg = await this.client.ReceiveCommand();
this.messageList.Items.Add(msg);
}
private async void fileUploadButton_Click(object sender, RoutedEventArgs e)
{
Windows.Storage.Pickers.FileOpenPicker picker = new Windows.Storage.Pickers.FileOpenPicker();
picker.ViewMode = Windows.Storage.Pickers.PickerViewMode.Thumbnail;
picker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.PicturesLibrary;
picker.FileTypeFilter.Add("*");
Windows.Storage.StorageFile file = await picker.PickSingleFileAsync();
if (file != null)
{
try
{
var watch = System.Diagnostics.Stopwatch.StartNew();
fileUploadTime.Items.Clear();
fileUploadTime.Items.Add("Uploading " + "'" + file.Name + "'");
await this.client.UploadFile(file);
fileUploadTime.Items.Clear();
watch.Stop();
fileUploadTime.Items.Add("'" + file.Name + "'" + " uploaded in: "+ watch.ElapsedMilliseconds + "ms");
}
catch (Exception ex)
{
fileUploadTime.Items.Clear();
fileUploadTime.Items.Add("Error occurred uploading the file. " + ex.Message);
Debug.WriteLine("{0}\n", ex.Message);
if (ex.InnerException != null)
{
Debug.WriteLine(ex.InnerException.Message + "\n");
}
}
}
}
private async void OnProtocolSelectionChanged(object sender, SelectionChangedEventArgs e)
{
TransportType protocol = GetProtocolIdFromName(this.protocolComboBox.SelectedValue.ToString());
if (protocol != client.Protocol)
{
if (this.client != null)
{
try
{
await this.client.CloseAsync();
}
catch (Exception ex)
{
var err = string.Format("Error {0} while closing the client", ex.Message);
Debug.WriteLine(err);
}
}
this.client = new IoTClient(protocol, CallMeLogger, GetDeviceNameLogger, ErrorHandler);
this.client.Start();
}
}
int callMeCounter = 0;
int getDeviceNameCounter = 0;
private void CallMeLogger(object element)
{
AddItemToListBox(methodCallList, string.Format("[{0}] {1}", callMeCounter++, element));
}
private void GetDeviceNameLogger(object element)
{
AddItemToListBox(getDeviceNameList, string.Format("[{0}] {1}", getDeviceNameCounter++, element));
}
private void ErrorHandler(object element)
{
AddItemToListBox(methodCallList, element.ToString());
AddItemToListBox(getDeviceNameList, element.ToString());
}
private void AddItemToListBox(ListBox list, string item)
{
CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
() =>
{
list.Items.Add(item);
var selectedIndex = list.Items.Count - 1;
if (selectedIndex < 0)
return;
list.SelectedIndex = selectedIndex;
list.UpdateLayout();
list.ScrollIntoView(list.SelectedItem);
});
}
}
}