Last active
May 27, 2018 03:22
-
-
Save avirup171/55259edb6743cb720b6c0c77b38fa1db to your computer and use it in GitHub Desktop.
This file contains 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
using System; | |
using System.Runtime.Serialization; | |
using System.ServiceModel.Description; | |
using MongoDB.Bson.IO; | |
using MongoDB.Bson; | |
using MongoDB; | |
using MongoDB.Driver; | |
using System.Security.Authentication; | |
using System.Text; | |
using Newtonsoft.Json; | |
using Newtonsoft.Json.Linq; | |
public static void Run(string myIoTHubMessage, TraceWriter log) | |
{ | |
log.Info($"C# IoT Hub trigger function processed a message: {myIoTHubMessage}"); | |
string deviceId="",data=""; | |
var raw_obj=JObject.Parse(myIoTHubMessage); | |
deviceId=(string)raw_obj["device_id"]; | |
data=(string)raw_obj["Data"]; | |
Cosmos cosmos= new Cosmos(deviceId,data); | |
cosmos.pushData(); | |
} | |
//CosmosDB class | |
public class Cosmos | |
{ | |
string deviceId="",data=""; | |
public BsonDocument document = new BsonDocument(); | |
public Cosmos(string deviceId, string data) | |
{ | |
this.deviceId=deviceId; | |
this.data=data; | |
} | |
public void pushData() | |
{ | |
MainAsync().Wait(); | |
} | |
public async Task MainAsync() | |
{ | |
string connectionString = | |
@"<CONNECTION STRING INSERT HERE- FOR DETAILS SEE QUICKSTART IN AZURE PORTAL UNDER .NET>"; | |
MongoClientSettings settings = MongoClientSettings.FromUrl(new MongoUrl(connectionString)); | |
settings.SslSettings = new SslSettings() { EnabledSslProtocols = SslProtocols.Tls12}; | |
var mongoClient = new MongoClient(settings); | |
IMongoDatabase db = mongoClient.GetDatabase("database_spektro"); | |
var icollection = db.GetCollection<BsonDocument>(deviceId); | |
document.Add("Data",data); | |
await icollection.InsertOneAsync(document); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment