forked from cheahengsoon/iot-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
135 lines (108 loc) · 4.89 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
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
using System;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Azure.Devices;
using Azure.IoTHub.Examples.CSharp.Core;
using Microsoft.Azure.Devices.Common;
namespace SendCloudToDeviceMessages
{
/// <summary>
///
/// </summary>
public class Program
{
/// <summary>
/// Sends a cloud to device message (async).
/// </summary>
/// <param name="serviceClient">The service client.</param>
/// <param name="deviceId">The device identifier.</param>
/// <param name="message">The message.</param>
/// <returns></returns>
private static async Task SendCloudToDeviceMessageAsync(ServiceClient serviceClient, string deviceId, string message)
{
var commandMessage = new Message(Encoding.ASCII.GetBytes(message)) {Ack = DeliveryAcknowledgement.Full};
await serviceClient.SendAsync(deviceId, commandMessage);
}
/// <summary>
/// Receives the message ACK from the device (async).
/// </summary>
/// <param name="serviceClient">The service client.</param>
/// <param name="token">The token.</param>
/// <returns></returns>
private static async Task ReceiveFeedbackAsync(ServiceClient serviceClient, CancellationToken token)
{
var feedbackReceiver = serviceClient.GetFeedbackReceiver();
Console.WriteLine("Waiting for c2d ACK from service");
while (true)
{
if (token.IsCancellationRequested)
{
Console.WriteLine("Listenter Cancelled, Exiting ...");
break;
}
var result = await feedbackReceiver.ReceiveAsync(TimeSpan.FromSeconds(1));
if (result == null) continue;
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("Received ACK: {0}", string.Join(", ", result.Records.Select(f => f.StatusCode)));
Console.ResetColor();
await feedbackReceiver.CompleteAsync(result);
}
}
/// <summary>
/// Receives the file upload notification (async).
/// </summary>
/// <param name="serviceClient">The service client.</param>
/// <returns></returns>
private static async Task ReceiveFileUploadNotificationAsync(ServiceClient serviceClient)
{
var notificationReceiver = serviceClient.GetFileNotificationReceiver();
Console.WriteLine("Receiving file upload notification from service");
while (true)
{
var fileUploadNotification = await notificationReceiver.ReceiveAsync(TimeSpan.FromSeconds(1));
if (fileUploadNotification == null) continue;
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("Received file upload noticiation: {0}", string.Join(", ", fileUploadNotification.BlobName));
Console.ResetColor();
await notificationReceiver.CompleteAsync(fileUploadNotification);
}
}
private static async Task MessageSendTaskAsync(ServiceClient serviceClient, string deviceId, CancellationToken token)
{
while (true)
{
if (token.IsCancellationRequested)
{
Console.WriteLine("Listenter Cancelled, Exiting ...");
break;
}
Console.WriteLine("Type a message and press <ENTER> to send a C2D message.");
// niave impl - press ctrl-c twice or close window to exit
var message = Console.ReadLine();
if (!message.IsNullOrWhiteSpace())
await SendCloudToDeviceMessageAsync(serviceClient, deviceId, message);
}
}
static void Main(string[] args)
{
var config = @"../../../config/config.yaml".GetIoTConfiguration();
var cts = new CancellationTokenSource();
Console.CancelKeyPress += (s, e) =>
{
e.Cancel = true;
cts.Cancel();
Console.WriteLine("Exiting...");
};
Console.WriteLine("Send Cloud-to-Device message");
var serviceClient = ServiceClient.CreateFromConnectionString(config.AzureIoTHubConfig.ConnectionString);
// monitor for ACKs from cloud to device mesages
var ackTask = ReceiveFeedbackAsync(serviceClient, cts.Token);
// monitor for File Upload Notifications
var fileNotificationTask = ReceiveFileUploadNotificationAsync(serviceClient);
var sendTask = MessageSendTaskAsync(serviceClient, config.DeviceConfigs.First().DeviceId, cts.Token);
Task.WaitAll(ackTask, fileNotificationTask, sendTask);
}
}
}