-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProcessDeviceEvent.cs
45 lines (41 loc) · 1.57 KB
/
ProcessDeviceEvent.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
using IoTHubTrigger = Microsoft.Azure.WebJobs.EventHubTriggerAttribute;
using Newtonsoft.Json;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.EventHubs;
using System.Text;
using Microsoft.Extensions.Logging;
using System;
using System.Threading.Tasks;
using Siliconvalve.Demo.Model;
namespace Siliconvalve.Demo
{
public class ProcessDeviceEvent
{
[FunctionName("ProcessDeviceEvent")]
public static async Task Run(
[IoTHubTrigger("messages/events", Connection = "IOT_HUB_CONNECTION")]EventData[] eventHubMessages,
[CosmosDB(
databaseName: "%AIRDATA_COSMOS_DB%",
collectionName: "%AIRDATA_COSMOS_COLLECTION%",
ConnectionStringSetting = "COSMOS_DB_CONNECTION")]
IAsyncCollector<SensorData> sensortDataOut,
ILogger log)
{
log.LogInformation($"Processing {eventHubMessages.Length} messages.");
try
{
foreach (var message in eventHubMessages)
{
log.LogInformation($"Message enqueue time: {message.SystemProperties.EnqueuedTimeUtc}");
var sensorReading = JsonConvert.DeserializeObject<SensorData>(Encoding.UTF8.GetString(message.Body));
await sensortDataOut.AddAsync(sensorReading);
}
}
catch (Exception e)
{
log.LogError(e,$"Message processing error. {e.Message}");
throw;
}
}
}
}