forked from Azure/azure-iot-sdk-csharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
100 lines (84 loc) · 3.64 KB
/
Program.cs
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
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Text;
using System.Threading.Tasks;
namespace Microsoft.Azure.Devices.Client.Samples
{
class Program
{
// String containing Hostname, Device Id & Device Key in one of the following formats:
// "HostName=<iothub_host_name>;DeviceId=<device_id>;SharedAccessKey=<device_key>"
// "HostName=<iothub_host_name>;CredentialType=SharedAccessSignature;DeviceId=<device_id>;SharedAccessSignature=SharedAccessSignature sr=<iot_host>/devices/<device_id>&sig=<token>&se=<expiry_time>";
private const string DeviceConnectionString = "<replace>";
private static int MESSAGE_COUNT = 5;
static void Main(string[] args)
{
try
{
DeviceClient deviceClient = DeviceClient.CreateFromConnectionString(DeviceConnectionString, TransportType.Mqtt);
deviceClient.OpenAsync().Wait();
SendEvent(deviceClient).Wait();
ReceiveCommands(deviceClient).Wait();
Console.WriteLine("Exited!");
}
catch (AggregateException ex)
{
foreach (Exception exception in ex.InnerExceptions)
{
Console.WriteLine();
Console.WriteLine("Error in sample: {0}", exception);
}
}
catch (Exception ex)
{
Console.WriteLine();
Console.WriteLine("Error in sample: {0}", ex.Message);
}
Console.WriteLine("Press enter to exit...");
Console.ReadLine();
}
static async Task SendEvent(DeviceClient deviceClient)
{
string dataBuffer;
Console.WriteLine("Device sending {0} messages to IoTHub...\n", MESSAGE_COUNT);
for (int count = 0; count < MESSAGE_COUNT; count++)
{
dataBuffer = Guid.NewGuid().ToString();
var eventMessage = new Message(Encoding.UTF8.GetBytes(dataBuffer));
Console.WriteLine("\t{0}> Sending message: {1}, Data: [{2}]", DateTime.Now.ToLocalTime(), count, dataBuffer);
await deviceClient.SendEventAsync(eventMessage);
}
}
static async Task ReceiveCommands(DeviceClient deviceClient)
{
Console.WriteLine("\nDevice waiting for commands from IoTHub...\n");
Message receivedMessage = null;
while (true)
{
try
{
receivedMessage = await deviceClient.ReceiveAsync(TimeSpan.FromSeconds(1));
if (receivedMessage != null)
{
string messageData = Encoding.ASCII.GetString(receivedMessage.GetBytes());
Console.WriteLine("\t{0}> Received message: {1}", DateTime.Now.ToLocalTime(), messageData);
int propCount = 0;
foreach (var prop in receivedMessage.Properties)
{
Console.WriteLine("\t\tProperty[{0}> Key={1} : Value={2}", propCount++, prop.Key, prop.Value);
}
await deviceClient.CompleteAsync(receivedMessage);
}
}
finally
{
if (receivedMessage != null)
{
receivedMessage.Dispose();
}
}
}
}
}
}