Example #1
0
        //Store the document in a blob
        public async Task StoreDocument(EventData eventData)
        {
            try
            {
                if (container == null)
                {
                    await CreateClient();
                }

                var payload = Encoding.UTF8.GetString(eventData.Body.Array, eventData.Body.Offset, eventData.Body.Count);

                //parse the data and insert depending on message schema type / if it's not a truck we discard the message
                TruckDeviceEvent parsed = JsonConvert.DeserializeObject <TruckDeviceEvent>(payload);

                if (eventData.Properties.ContainsKey("$$MessageSchema") && eventData.Properties["$$MessageSchema"].ToString().ToLower() == "truck-sensors;v1")
                {
                    Console.WriteLine("Parsing Truck Document...");

                    var newDocument = new
                    {
                        id = eventData.SystemProperties["iothub-connection-device-id"].ToString(),
                        processedUtcTime = DateTime.UtcNow,
                        enqueuedUtcTime  = eventData.SystemProperties.EnqueuedTimeUtc,
                        deviceid         = eventData.SystemProperties["iothub-connection-device-id"].ToString(),
                        devicetype       = "truck",
                        location         = new Point(parsed.longitude, parsed.latitude),
                        speed            = parsed.speed,
                        speed_unit       = parsed.speed_unit,
                        temperature      = parsed.temperature,
                        temperature_unit = parsed.temperature_unit
                    };

                    Console.WriteLine("Truck document parsed: {0}", JsonConvert.SerializeObject(newDocument));

                    //Create the blob name and path for the new blob
                    DateTime timeNow  = DateTime.Now;
                    string   blobName = newDocument.deviceid + $"/" + timeNow.Year + $"/" + timeNow.Month + $"/" + timeNow.Day + $"/" + timeNow.Hour + $"/" + timeNow.ToLongTimeString() + ".json";

                    CloudBlockBlob blockBlob = container.GetBlockBlobReference(blobName);

                    // Set the blob's content type so that the browser knows to treat it as an image.
                    blockBlob.Properties.ContentType = "json";

                    var blobContent = JsonConvert.SerializeObject(newDocument);
                    await blockBlob.UploadTextAsync(blobContent);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
Example #2
0
        //Store the document in a Cosmos DB collection
        public async Task StoreDocument(EventData eventData)
        {
            var payload = Encoding.UTF8.GetString(eventData.Body.Array, eventData.Body.Offset, eventData.Body.Count);

            //parse the data and insert depending on message schema type / if it's not a truck we discard the message
            TruckDeviceEvent parsed = JsonConvert.DeserializeObject <TruckDeviceEvent>(payload);

            Console.WriteLine("EventData contains key ..." + eventData.Properties.ContainsKey("$$MessageSchema").ToString());

            if (eventData.Properties.ContainsKey("$$MessageSchema") && eventData.Properties["$$MessageSchema"].ToString().ToLower() == "truck-sensors;v1")
            {
                Console.WriteLine("Parsing Truck Document...");

                var newDocument = new
                {
                    id = eventData.SystemProperties["iothub-connection-device-id"].ToString(),
                    processedUtcTime = DateTime.UtcNow,
                    enqueuedUtcTime  = eventData.SystemProperties.EnqueuedTimeUtc,
                    partition        = eventData.SystemProperties.PartitionKey,
                    messageOffset    = eventData.Body.Offset,
                    deviceid         = eventData.SystemProperties["iothub-connection-device-id"].ToString(),
                    devicetype       = "truck",
                    location         = new Point(parsed.longitude, parsed.latitude),
                    speed            = parsed.speed,
                    speed_unit       = parsed.speed_unit,
                    temperature      = parsed.temperature,
                    temperature_unit = parsed.temperature_unit
                };

                try
                {
                    Console.WriteLine("Inserting new doc...");
                    // Uri documentCollection = UriFactory.CreateDocumentCollectionUri(dbName, collectionName);
                    // await _documentClient.CreateDocumentAsync(UriFactory.CreateDocumentCollectionUri(dbName, collectionName), newDocument);
                    await _documentClient.UpsertDocumentAsync(UriFactory.CreateDocumentCollectionUri(dbName, collectionName), newDocument);
                }
                catch (Exception e)
                {
                    if (e is DocumentClientException docEx)
                    {
                        Console.WriteLine($"ERROR with document creation - Statuc code {docEx.StatusCode} - {docEx.Message}");
                    }
                    else
                    {
                        Console.WriteLine($"ERROR CosmosDB - {e.Message}");
                    }
                }
            }
        }