-
Notifications
You must be signed in to change notification settings - Fork 0
/
SaveMessage.cs
81 lines (71 loc) · 3.42 KB
/
SaveMessage.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
using IoTHubTrigger = Microsoft.Azure.WebJobs.EventHubTriggerAttribute;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Azure.EventHubs;
using System.Text;
using System.Net.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Microsoft.WindowsAzure.Storage.Table;
using System;
using AF_1cosmosdb.models;
namespace AF_1cosmosdb
{
public static class SaveMessageCosmosDB
{
private static HttpClient client = new HttpClient();
[FunctionName("SaveMessage")]
[return: Table("Messages")]
public static DynamicTableEntity Run(
[IoTHubTrigger("messages/events", Connection = "IotHubConnection", ConsumerGroup = "$Default")] EventData message,
[CosmosDB(
databaseName: "con-cosmos",
collectionName: "Messages",
ConnectionStringSetting = "CosmosConnection",
CreateIfNotExists = true
)]out dynamic cosmos,
ILogger log
)
{
// table storage returntype
var ent = new DynamicTableEntity();
// format message into table storage format depending on sensor type
switch (message.Properties["type"])
{
case "dht":
DhtMessage dht = JsonConvert.DeserializeObject<DhtMessage>(Encoding.UTF8.GetString(message.Body.Array));
ent.Properties["ID"] = new EntityProperty(dht.ID);
ent.Properties["temperature"] = new EntityProperty(dht.temperature);
ent.Properties["humidity"] = new EntityProperty(dht.humidity);
ent.Properties["messageCreated"] = new EntityProperty(dht.messageCreated);
ent.PartitionKey = message.Properties["type"].ToString();
ent.RowKey = Guid.NewGuid().ToString();
break;
case "volt":
VoltMessage volt = JsonConvert.DeserializeObject<VoltMessage>(Encoding.UTF8.GetString(message.Body.Array));
ent.Properties["ID"] = new EntityProperty(volt.ID);
ent.Properties["volt"] = new EntityProperty(volt.volt);
ent.Properties["messageCreated"] = new EntityProperty(volt.messageCreated);
ent.PartitionKey = message.Properties["type"].ToString();
ent.RowKey = Guid.NewGuid().ToString();
break;
case "luminosity":
LuminosityMessage lum = JsonConvert.DeserializeObject<LuminosityMessage>(Encoding.UTF8.GetString(message.Body.Array));
ent.Properties["ID"] = new EntityProperty(lum.ID);
ent.Properties["luminosity"] = new EntityProperty(lum.luminosity);
ent.Properties["messageCreated"] = new EntityProperty(lum.messageCreated);
ent.PartitionKey = message.Properties["type"].ToString();
ent.RowKey = Guid.NewGuid().ToString();
break;
default:
log.LogInformation("unknown type");
break;
}
// save message to CosmosDB
cosmos = Encoding.UTF8.GetString(message.Body.Array);
log.LogInformation($"message processed: {Encoding.UTF8.GetString(message.Body.Array)}");
// save message to table storage
return ent;
}
}
}