Last active
August 23, 2018 23:36
-
-
Save ksasao/c475bc18b196ae9f33478df5c8e54605 to your computer and use it in GitHub Desktop.
Receives synapseWear data and writes to Azure Table Storage
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"bindings": [ | |
{ | |
"authLevel": "function", | |
"name": "req", | |
"type": "httpTrigger", | |
"direction": "in", | |
"methods": [ | |
"post" | |
] | |
}, | |
{ | |
"name": "$return", | |
"type": "http", | |
"direction": "out" | |
}, | |
{ | |
"type": "table", | |
"name": "outTable", | |
"tableName": "SynapseLog", | |
"connection": "AzureWebJobsDashboard", | |
"direction": "out" | |
} | |
], | |
"disabled": false | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#r "Newtonsoft.Json" | |
#r "Microsoft.WindowsAzure.Storage" | |
using System.Net; | |
using Microsoft.WindowsAzure.Storage.Table; | |
using System; | |
using System.Text; | |
using Newtonsoft.Json; | |
public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, ICollector<SynapseData> outTable, TraceWriter log) | |
{ | |
dynamic data = await req.Content.ReadAsStringAsync(); | |
if(data == null || !data.StartsWith("data")){ | |
log.Error($"Format error: {data}"); | |
return req.CreateResponse(HttpStatusCode.Forbidden, "Illegal format"); | |
} | |
var str = WebUtility.UrlDecode(data).Substring(5); | |
// log.Info(str); | |
var obj = JsonConvert.DeserializeObject<RootObject>(str); | |
log.Info($"Log started at {obj.data[0].date}"); | |
foreach(var c in obj.data){ | |
outTable.Add(new SynapseData() | |
{ | |
PartitionKey = "Functions", | |
RowKey = Guid.NewGuid().ToString(), | |
DeviceUuid = obj.deviceuuid, | |
Date = DateTimeOffset.Parse(c.date), | |
Temperature = c.temperature, | |
AirPressure = c.airpressure, | |
Voltage = c.voltage, | |
CO2 = c.CO2, | |
Illumination = c.illumination, | |
Humidity = c.humidity, | |
EnvSound = c.envsound | |
}); | |
} | |
return req.CreateResponse(HttpStatusCode.OK, $"{obj.data.Count} records are written to Azure Storage Table"); | |
} | |
// generated by json2csharp http://json2csharp.com/ | |
public class Datum | |
{ | |
public double voltage { get; set; } | |
public int CO2 { get; set; } | |
public double airpressure { get; set; } | |
public int illumination { get; set; } | |
public int humidity { get; set; } | |
public string date { get; set; } | |
public double temperature { get; set; } | |
public int envsound { get; set; } | |
public int dateunix { get; set; } | |
} | |
public class RootObject | |
{ | |
public string deviceuuid { get; set; } | |
public List<Datum> data { get; set; } | |
} | |
public class SynapseData : TableEntity | |
{ | |
public string DeviceUuid {get;set;} | |
public DateTimeOffset Date { get; set; } | |
public double Temperature { get; set; } | |
public double AirPressure { get; set; } | |
public double Voltage { get; set; } | |
public int CO2 { get; set; } | |
public int Illumination { get; set; } | |
public double EnvSound { get; set; } | |
public int Humidity { get; set; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment