forked from cheahengsoon/iot-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
318 lines (262 loc) · 11.5 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
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
using System;
using System.Collections.Generic;
using System.Device.Location;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Azure.Devices.Client;
using Newtonsoft.Json;
using Azure.IoTHub.Examples.CSharp.Core;
namespace DeviceSimulator
{
public class TelemetryDataPoint
{
public string deviceId { get; set; }
public DateTime obsTime { get; set; }
public double windSpeed { get; set; }
public GeoCoordinate location { get; set; }
}
/// <summary>
/// Sample program that simulates an IoT device for use with Azure IoT Hub.
/// </summary>
public class Program
{
#region Data Generators
/// <summary>
/// Function to generate simple randomized device data as JSON string.
/// </summary>
private static readonly Func<string, string> GetFlatSampleDeviceData = deviceId =>
{
const double avgWindSpeed = 10; // m/s
var rand = new Random();
var currentWindSpeed = avgWindSpeed + rand.NextDouble()*4 - 2;
var telemetryDataPoint = new TelemetryDataPoint
{
deviceId = deviceId,
obsTime = DateTime.UtcNow,
windSpeed = currentWindSpeed
};
return JsonConvert.SerializeObject(telemetryDataPoint);
};
/// <summary>
/// Function to generate simple randomized device data in CSV format.
/// </summary>
private static readonly Func<string, string> GetCSVSampleDeviceData = deviceId =>
{
const string schema = "deviceId,obsTime,windSpeed";
const double avgWindSpeed = 10; // m/s
var rand = new Random();
var currentWindSpeed = avgWindSpeed + rand.NextDouble()*4 - 2;
var telemetryDataPoint = $"{deviceId},{DateTime.UtcNow},{currentWindSpeed}";
return telemetryDataPoint;
};
/// <summary>
/// Function to generate object graph of randomized device data .
/// </summary>
private static readonly Func<string, GeoCoordinate, string> GetJSONObjectGraphSampleDeviceData =
(deviceId, geoCoordinate) =>
{
const double avgWindSpeed = 10; // m/s
var rand = new Random();
var currentWindSpeed = avgWindSpeed + rand.NextDouble() * 4 - 2;
var telemetryDataPoint = new TelemetryDataPoint
{
deviceId = deviceId,
obsTime = DateTime.UtcNow,
windSpeed = currentWindSpeed,
location = geoCoordinate
};
return JsonConvert.SerializeObject(telemetryDataPoint);
};
private static readonly Func<int, double, double, float, GeoCoordinate> GetRandomGeoCoordinate =
(seed, latitude, longitude, radius) =>
{
// check out http://gis.stackexchange.com/a/68275 for where this calc originated.
var rand = new Random(seed);
var u = rand.NextDouble();
var v = rand.NextDouble();
var w = radius/111000*Math.Sqrt(u);
var t = 2*Math.PI*v;
var x = (w*Math.Cos(t))/Math.Cos(latitude);
var y = w*Math.Sin(t);
return new GeoCoordinate(y + latitude, x + longitude);
};
/// <summary>
/// Function to generate stream of randomized device data .
/// </summary>
private static IEnumerable<string> GetAggregateJSONDataStream(string deviceId)
{
const double avgWindSpeed = 10; // m/s
const int locationCount = 10;
var locations = new List<GeoCoordinate>();
for (var i = 0; i < locationCount; i++)
{
locations.Add(GetRandomGeoCoordinate(i, 47.640568390488625, -122.1293731033802, 5000));
}
while (true)
{
var telemetryData = locations.Select((loc, index) =>
{
var rand = new Random(DateTime.Now.Millisecond % (index+ 1));
var currentWindSpeed = avgWindSpeed + rand.NextDouble() * 4 - 2;
return JsonConvert.SerializeObject(new TelemetryDataPoint
{
deviceId = deviceId + index,
obsTime = DateTime.UtcNow,
windSpeed = currentWindSpeed,
location = loc
});
});
yield return string.Join("|", telemetryData);
}
}
#endregion Data Generators
/// <summary>
/// Sends the device to cloud messages (async).
/// </summary>
/// <param name="deviceClient">The device client.</param>
/// <param name="deviceId">The device identifier.</param>
/// <param name="dataGenerator">The data generator.</param>
/// <param name="ct">The cancellation token</param>
/// <returns></returns>
private static async Task SendDeviceToCloudMessagesAsync(
DeviceClient deviceClient,
string deviceId,
Func<string, string> dataGenerator,
CancellationToken ct)
{
while (true)
{
if (ct.IsCancellationRequested) break;
var messageString = dataGenerator(deviceId);
var message = new Message(Encoding.ASCII.GetBytes(messageString));
await deviceClient.SendEventAsync(message);
Console.WriteLine("{0} > Sending message: {1}", DateTime.Now, messageString);
await Task.Delay(1000, ct);
}
}
/// <summary>
/// Receives the cloud to device message (async).
/// </summary>
/// <param name="deviceClient">The device client.</param>
/// <param name="iotStorageContainerName">Name of the iot storage container.</param>
/// <param name="ct">The ct.</param>
/// <returns></returns>
private static async Task ReceiveC2DAsync(DeviceClient deviceClient, string iotStorageContainerName, CancellationToken ct)
{
Console.WriteLine("Receiving cloud to device messages from service");
while (true)
{
if (ct.IsCancellationRequested) break;
var receivedMessage = await deviceClient.ReceiveAsync();
if (receivedMessage == null) continue;
var messageText = Encoding.ASCII.GetString(receivedMessage.GetBytes());
switch (messageText.ToLowerInvariant())
{
case "upload bulk data":
const string bulkDataUploadFilePath = @"MOCK_DATA.csv";
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine("Uploading MOCK_DATA");
Console.ResetColor();
var sw = Stopwatch.StartNew();
using (var mockData = new FileStream(bulkDataUploadFilePath, FileMode.Open))
{
await deviceClient.UploadToBlobAsync(iotStorageContainerName, mockData);
}
sw.Stop();
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine("File uploaded in {0}ms", sw.ElapsedMilliseconds);
Console.ResetColor();
break;
default:
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("Received message: {0}", messageText);
Console.ResetColor();
break;
}
// this will remove the message from the queue.
await deviceClient.CompleteAsync(receivedMessage);
}
}
public static void Main(string[] args)
{
var config = @"../../../config/config.yaml".GetIoTConfiguration();
var testDevice = config.DeviceConfigs.First();
var azureConfig = config.AzureIoTHubConfig;
Console.WriteLine("Simulated device");
var deviceClient = DeviceClient.Create(
azureConfig.Hostname,
new DeviceAuthenticationWithRegistrySymmetricKey(testDevice.DeviceId, testDevice.Key));
var cts = new CancellationTokenSource();
// register cancellation request event.
Console.CancelKeyPress += (s, e) =>
{
e.Cancel = true;
cts.Cancel();
Console.WriteLine("Exiting ...");
};
/*
* Data Generator Functions
* Select a message type by uncommenting an assignment below
*/
// var dataGenerator = GetFlatSampleDeviceData;
// var dataGenerator = GetCSVSampleDeviceData;
// var dataGenerator = GetJSONObjectGraphSampleDeviceData;
/*
* kick off simple cloud data send.
*/
//var sendTask = SendDeviceToCloudMessagesAsync(deviceClient, testDevice.DeviceId, dataGenerator, cts.Token);
/*
* run batched & compressed data generator
*/
var sendTask = Task.Run(() =>
GetAggregateJSONDataStream(testDevice.DeviceId)
.Compress()
.SendDeviceToCloudMessageAsync(deviceClient), cts.Token);
/*
* kick off could data receive
*/
var receiveTask = ReceiveC2DAsync(deviceClient, azureConfig.IoTHubStorageContainer, cts.Token);
Task.WaitAny(sendTask, receiveTask);
Console.ReadLine();
}
}
public static class ExtensionMethods
{
public static IEnumerable<string> Compress(this IEnumerable<string> stream)
{
foreach (var message in stream)
{
// this string compression code is lifted & modified from
// http://madskristensen.net/post/Compress-and-decompress-strings-in-C
var buffer = Encoding.UTF8.GetBytes(message);
var ms = new MemoryStream();
using (var zip = new GZipStream(ms, CompressionMode.Compress, true))
{
zip.Write(buffer, 0, buffer.Length);
}
ms.Position = 0;
var compressed = new byte[ms.Length];
ms.Read(compressed, 0, compressed.Length);
var gzBuffer = new byte[compressed.Length + 4];
Buffer.BlockCopy(compressed, 0, gzBuffer, 4, compressed.Length);
Buffer.BlockCopy(BitConverter.GetBytes(buffer.Length), 0, gzBuffer, 0, 4);
yield return Convert.ToBase64String(gzBuffer);
}
}
public static async Task SendDeviceToCloudMessageAsync(this IEnumerable<string> stream, DeviceClient deviceClient)
{
foreach (var data in stream)
{
var message = new Message(Encoding.ASCII.GetBytes(data));
await deviceClient.SendEventAsync(message);
Console.WriteLine("{0} > Sending message: {1}", DateTime.Now, data);
await Task.Delay(5000);
}
}
}
}