forked from Azure/azure-iot-sdks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
80 lines (66 loc) · 3.17 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
// 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;
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.Http1);
SendEvent(deviceClient).Wait();
ReceiveCommands(deviceClient).Wait();
Console.WriteLine("Exited!\n");
}
catch (Exception ex)
{
Console.WriteLine("Error in sample: {0}", ex.Message);
}
}
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();
Message 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;
string messageData;
while (true)
{
receivedMessage = await deviceClient.ReceiveAsync();
if (receivedMessage != null)
{
messageData = Encoding.ASCII.GetString(receivedMessage.GetBytes());
Console.WriteLine("\t{0}> Received message: {1}", DateTime.Now.ToLocalTime(), messageData);
await deviceClient.CompleteAsync(receivedMessage);
}
// Note: In this sample, the polling interval is set to
// 10 seconds to enable you to see messages as they are sent.
// To enable an IoT solution to scale, you should extend this // interval. For example, to scale to 1 million devices, set
// the polling interval to 25 minutes.
// For further information, see
// https://azure.microsoft.com/documentation/articles/iot-hub-devguide/#messaging
Thread.Sleep(10000);
}
}
}
}