-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIoTHubPrimitivesRunner.cs
269 lines (241 loc) · 12.6 KB
/
IoTHubPrimitivesRunner.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
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
using System;
using System.Collections.Generic;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Azure.Devices;
using Microsoft.Azure.Devices.Client;
using Microsoft.Azure.Devices.Client.Transport.Mqtt;
using Microsoft.Azure.Devices.Shared;
using C2DMessage = Microsoft.Azure.Devices.Message;
using D2CMessage = Microsoft.Azure.Devices.Client.Message;
using TransportType = Microsoft.Azure.Devices.Client.TransportType;
namespace Microsoft.Azure.Edge.Test
{
internal class IoTHubPrimitivesRunner
{
private const string MethodName = "test-method";
private const int ChildCount = 5;
private const string PropertyName = "Name";
private const string PropertyValue = "Value";
private static readonly Dictionary<string, int> D2COperationCounts = new Dictionary<string, int>();
private static readonly Dictionary<string, int> C2DOperationCounts = new Dictionary<string, int>();
private static readonly TimeSpan OperationTimeOut = TimeSpan.FromMinutes(2);
private static readonly TimeSpan TestPeriod = TimeSpan.FromHours(72);
private static readonly TimeSpan TestFrequency = TimeSpan.FromSeconds(30);
private static readonly TimeSpan TokenTTL = TimeSpan.FromMinutes(30);
public async Task RunAsync()
{
ConsoleLogger.LogInfo($"IotHubHost={EnvironmentVariables.IotHubHost}, GatewayHost={EnvironmentVariables.EdgeHubHost}, ParentEdgeDeviceId={EnvironmentVariables.EdgeDeviceId}, LeafDevicePrefix={EnvironmentVariables.LeafDeviceIdPrefix}.");
var cancellationTokenSource = new CancellationTokenSource(TestPeriod);
var devices = await DeviceManager.RetrieveDevicesAsync(ChildCount);
var tasks = new List<Task>();
var serviceClient = ServiceClient.CreateFromConnectionString(EnvironmentVariables.IoTHubOwnerConnectionString);
await serviceClient.OpenAsync();
var deviceClients = new List<DeviceClient>();
foreach (var device in devices)
{
var deviceClient = await CreateDeviceClientAsync(device);
deviceClients.Add(deviceClient);
tasks.Add(D2CLoop(deviceClient, device.Id, cancellationTokenSource.Token));
tasks.Add(C2DLoop(serviceClient, deviceClient, device.Id, cancellationTokenSource.Token));
}
await Task.WhenAll(tasks);
await serviceClient.CloseAsync();
foreach (var deviceClient in deviceClients)
{
deviceClient.Dispose();
}
}
private static async Task<DeviceClient> CreateDeviceClientAsync(Device device)
{
var deviceId = device.Id;
D2COperationCounts[deviceId] = 0;
C2DOperationCounts[deviceId] = 0;
ConsoleLogger.LogInfo($"Device {device.Id} started");
var tokenRefresher = new DeviceAuthenticationWithSharedAccessKey(EnvironmentVariables.IotHubHost, device.Id, device.Authentication.SymmetricKey.PrimaryKey, TokenTTL);
ITransportSettings transportSetting;
if (deviceId.GetHashCode() % 2 == 0)
{
transportSetting = new MqttTransportSettings(TransportType.Mqtt_Tcp_Only)
{
RemoteCertificateValidationCallback = (object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) => true
};
}
else
{
transportSetting = new AmqpTransportSettings(TransportType.Amqp_Tcp_Only)
{
RemoteCertificateValidationCallback = (object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) => true
};
}
ConsoleLogger.LogInfo($"Device {device.Id} transportType: {transportSetting.GetTransportType()}.");
var deviceClient = DeviceClient.Create(EnvironmentVariables.IotHubHost, EnvironmentVariables.EdgeHubHost, tokenRefresher, new ITransportSettings[] { transportSetting });
deviceClient.SetConnectionStatusChangesHandler((state, reason) =>
{ ConsoleLogger.LogInfo($"Device {device.Id} Connection state change: state={state}, reason={reason}"); });
await deviceClient.SetDesiredPropertyUpdateCallbackAsync((desiredProperties, context) =>
{
ConsoleLogger.LogInfo($"Device {device.Id} Desired properties change: desiredProperties={desiredProperties.ToJson()}");
return Task.CompletedTask;
},
deviceClient);
await deviceClient.SetMethodHandlerAsync(
MethodName,
(request, _) =>
{
var echoResponse = new MethodResponse(request.Data, 200);
return Task.FromResult(echoResponse);
},
deviceClient);
return deviceClient;
}
private static async Task D2CLoop(DeviceClient deviceClient, string deviceId, CancellationToken token)
{
while (!token.IsCancellationRequested)
{
var index = D2COperationCounts[deviceId];
var identity = $"[Device={deviceId}, Index={index}, Direction=D2C]";
ConsoleLogger.LogDebug($"{identity}: Enter D2C loop...");
var operationName = "UpdatReportedProperties";
try
{
var name = $"Name: {identity}";
var value = $"Value: {identity}";
var reportedProperties = new TwinCollection();
reportedProperties[PropertyName] = name;
reportedProperties[PropertyValue] = value;
await deviceClient.UpdateReportedPropertiesAsync(reportedProperties);
ConsoleLogger.LogDebug($"{identity}: Updated reported properties successfully.");
operationName = "GetTwin";
var twin = await deviceClient.GetTwinAsync();
var retrievedName = twin.Properties.Reported[PropertyName];
var retrievedValue = twin.Properties.Reported[PropertyValue];
if (retrievedName == name && retrievedValue == value)
{
ConsoleLogger.LogDebug($"{identity}: Get twin successfully.");
}
else
{
ConsoleLogger.LogInfo($"{identity}: Get twin failed, expected ({PropertyName}, {PropertyValue})=({name}, {value}) but was ({retrievedName}, {retrievedValue}).");
}
operationName = "SendTelemetryMessage";
var messagePayload = $"Telemetry: {identity}";
var message = new D2CMessage(Encoding.UTF8.GetBytes(messagePayload))
{
ContentEncoding = "utf-8",
ContentType = "application/json"
};
message.Properties.Add("MachineName", deviceId);
await deviceClient.SendEventAsync(message);
ConsoleLogger.LogDebug($"{identity}: Sent message successfully.");
}
catch (Exception ex)
{
ConsoleLogger.LogInfo($"{identity}: Operation {operationName} failed: {ex}");
}
finally
{
if ((index + 1) % 100 == 0)
{
ConsoleLogger.LogInfo($"{identity}: finished {index + 1} D2C loop.");
}
D2COperationCounts[deviceId] = index + 1;
ConsoleLogger.LogDebug($"{identity}: Exit D2C loop.");
await Task.Delay(TestFrequency);
}
}
}
private static async Task C2DLoop(ServiceClient serviceClient, DeviceClient deviceClient, string deviceId, CancellationToken token)
{
while (!token.IsCancellationRequested)
{
var index = C2DOperationCounts[deviceId];
var identity = $"[Device={deviceId}, Index={index}, Direction=C2D]";
ConsoleLogger.LogDebug($"{identity}: Enter C2D loop...");
var operationName = "SendC2DMessage";
var messagePayload = $"C2D message: {identity}";
var message = new C2DMessage(Encoding.UTF8.GetBytes(messagePayload));
try
{
await serviceClient.SendAsync(deviceId, message);
ConsoleLogger.LogDebug($"{identity}: Sent C2D message successfully.");
operationName = "ReceiveC2DMessage";
var received = await deviceClient.ReceiveAsync(OperationTimeOut);
while (received != null)
{
var content = Encoding.UTF8.GetString(received.GetBytes());
ConsoleLogger.LogDebug($"{identity}: Received C2D message successfully.");
try
{
await deviceClient.CompleteAsync(received);
}
catch (Exception e)
{
// swallow CompleteAsync failure
ConsoleLogger.LogInfo($"{identity}: Complete C2D message failed: {e}.");
}
if (content == messagePayload)
{
// quit loop when message is received
break;
}
}
if (message == null)
{
ConsoleLogger.LogInfo($"{identity}: Receive C2D message failed: not received.");
}
else
{
ConsoleLogger.LogDebug($"{identity}: Receive C2D message successfully.");
}
operationName = "InvokeDeviceMethod";
var methodPayload = new TwinCollection();
methodPayload["Operation"] = $"Operation: {identity}";
methodPayload["Args"] = $"Args: {identity}";
var methodRequest = new CloudToDeviceMethod(MethodName);
methodRequest.SetPayloadJson(methodPayload.ToJson());
var methodResponse = await serviceClient.InvokeDeviceMethodAsync(deviceId, methodRequest);
var status = methodResponse.Status;
ConsoleLogger.LogDebug($"{identity}: Invoke method response: status={status}, payload={methodResponse.GetPayloadAsJson()}.");
if (status != 200)
{
ConsoleLogger.LogInfo($"{identity}: Invoke method failed: status={status}, payload={methodResponse.GetPayloadAsJson()}.");
}
}
catch (Exception ex)
{
ConsoleLogger.LogInfo($"{identity}: Operation {operationName} failed: {ex}");
}
finally
{
if ((index + 1) % 100 == 0)
{
ConsoleLogger.LogInfo($"{identity}: finished {index + 1} C2D loop.");
}
C2DOperationCounts[deviceId] = index + 1;
ConsoleLogger.LogDebug($"{identity}: Exit C2D loop.");
await Task.Delay(TestFrequency);
}
}
}
private class DeviceAuthenticationWithSharedAccessKey : DeviceAuthenticationWithTokenRefresh
{
private string host;
private string deviceId;
private string sharedAccessKey;
internal DeviceAuthenticationWithSharedAccessKey(string host, string deviceId, string sharedAccessKey, TimeSpan timeToLive, int timeBufferPercentage = 20) : base(deviceId, Convert.ToInt32(timeToLive.TotalSeconds), timeBufferPercentage)
{
this.host = host;
this.deviceId = deviceId;
this.sharedAccessKey = sharedAccessKey;
}
protected override Task<string> SafeCreateNewToken(string _, int suggestedTimeToLive)
{
var token = DeviceManager.CreateDeviceSasToken(host, deviceId, sharedAccessKey, suggestedTimeToLive);
return Task.FromResult(token);
}
}
}
}