-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.csx
49 lines (44 loc) · 1.58 KB
/
run.csx
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
#r "Microsoft.Azure.Devices.Client"
#r "Newtonsoft.Json"
using System.IO;
using Microsoft.Azure.Devices.Client;
using Newtonsoft.Json;
public static async Task Run(Message messageReceived, IAsyncCollector<Message> output, TraceWriter log)
{
byte[] messageBytes = messageReceived.GetBytes();
var messageString = System.Text.Encoding.UTF8.GetString(messageBytes);
if (!string.IsNullOrEmpty(messageString))
{
log.Info(string.Format("Info: Received message: {0}", messageString));
var messageBody = JsonConvert.DeserializeObject<MessageBody>(messageString);
// Convert from var to atm
messageBody.machine.pressure = messageBody.machine.pressure * 0.98692326671601;
var outString = JsonConvert.SerializeObject(messageBody);
byte[] outBytes = System.Text.Encoding.UTF8.GetBytes(outString);
var outMessage = new Message(outBytes);
foreach (KeyValuePair<string, string> prop in messageReceived.Properties)
{
log.Info(prop.Key);
outMessage.Properties.Add(prop.Key, prop.Value);
}
await output.AddAsync(outMessage);
log.Info(string.Format("Info: Sending message {0}", outString));
}
}
// Message schema
class MessageBody
{
public Machine machine {get; set;}
public Ambient ambient {get; set;}
public string timeCreated {get; set;}
}
class Machine
{
public double temperature {get; set;}
public double pressure {get; set;}
}
class Ambient
{
public double temperature {get; set;}
public int humidity {get; set;}
}