public static async Task Run([EventHubTrigger(eventHubName: "telemetry", Connection = "TelemetryEventHubConnectionString", ConsumerGroup = "%DeviceStreamConsumerGroup%")] EventData[] events, ILogger log) { try { var cosmosDbRus = Convert.ToInt32(Environment.GetEnvironmentVariable("CosmosDBRus", EnvironmentVariableTarget.Process)); var cosmosDb = Environment.GetEnvironmentVariable("DeviceStreamDatabaseId", EnvironmentVariableTarget.Process); var batchThreshold = Convert.ToInt32(Environment.GetEnvironmentVariable("BatchThreshold", EnvironmentVariableTarget.Process)); var batchWriteDelay = Convert.ToInt32(Environment.GetEnvironmentVariable("BatchWriteDelay", EnvironmentVariableTarget.Process)); CosmosOperations docClient = await CosmosOperations.GetClientAsync(); await docClient.CreateDatabaseIfNotExistsAsync(cosmosDb, cosmosDbRus); DeviceService deviceService = new DeviceService(); await deviceService.ProcessTelemetryAsync( events, log, batchThreshold : batchThreshold == 0? 12 : batchThreshold, // default to 12 if the value is 0. 0 causes an exception batchWriteDelay : batchWriteDelay); } catch (Exception ex) { log.LogInformation($"Unable to process telemetry message. Error Message: {ex.Message}"); throw ex; } }
public async Task <IEnumerable <DeviceGroup> > GetDeviceGroups(string tenantId) { var sqlQuery = CosmosOperations.GetDocumentsByCollectionId("CollectionId", "devicegroups"); string cosmosConnectionString = this.appConfig.Global.CosmosDb.DocumentDbConnectionString; CosmosOperations storageClient = await CosmosOperations.GetClientAsync(cosmosConnectionString); var docs = await storageClient.QueryDocumentsAsync( "pcs-storage", $"pcs-{tenantId}", this.DefaultQueryOptions, sqlQuery, 0, 1000); var result = docs.Select(doc => this.CreateDeviceGroupModel(doc)); return(result); }
public static async Task Run([EventHubTrigger(eventHubName: "twin-change", Connection = "TwinChangeEventHubConnectionString", ConsumerGroup = "%DeviceStreamConsumerGroup%")] EventData[] events, ILogger log) { bool exceptionOccurred = false; List <Task> list = new List <Task>(); try { var cosmosDbRus = Convert.ToInt32(Environment.GetEnvironmentVariable("CosmosDBRus", EnvironmentVariableTarget.Process)); var cosmosDb = Environment.GetEnvironmentVariable("DeviceStreamDatabaseId", EnvironmentVariableTarget.Process); CosmosOperations docClient = await CosmosOperations.GetClientAsync(); await docClient.CreateDatabaseIfNotExistsAsync(cosmosDb, cosmosDbRus); } catch (Exception) { log.LogError($"Error occurrred while creating Cosmos DB"); throw; } foreach (EventData message in events) { try { message.Properties.TryGetValue("tenant", out object tenant); message.SystemProperties.TryGetValue(DeviceTelemetryKeyConstants.IotHubEnqueuedTime, out object dateTimeReceived); var telemetryTimestamp = new TelemetryTimestamp(Convert.ToDateTime(dateTimeReceived)); if (tenant != null) { string eventData = Encoding.UTF8.GetString(message.Body.Array); message.SystemProperties.TryGetValue("iothub-connection-device-id", out object deviceId); DeviceService deviceService = new DeviceService(); list.Add(Task.Run(async() => await deviceService.SaveDeviceTwinAsync(eventData, Convert.ToString(tenant), deviceId.ToString(), telemetryTimestamp.EpochTimestamp))); } else { log.LogError($"Error occurrred Tenant was not set"); exceptionOccurred = true; } } catch (Exception ex) { log.LogError($"Error occurrred : {ex.Message} StackTrace: {ex.StackTrace} Inner Exception: {(string.IsNullOrEmpty(ex.StackTrace) ? string.Empty : ex.StackTrace)}"); exceptionOccurred = true; } } try { await Task.WhenAll(list.ToArray()); } catch (Exception ex) { log.LogError($"Error occurrred : {ex.Message} StackTrace: {ex.StackTrace} Inner Exception: {(string.IsNullOrEmpty(ex.StackTrace) ? string.Empty : ex.StackTrace)}"); exceptionOccurred = true; } if (exceptionOccurred) { throw new Exception("Function Failed with exception"); } }
public static async Task Run([EventHubTrigger(eventHubName: "lifecycle", Connection = "LifecycleEventHubConnectionString", ConsumerGroup = "%DeviceStreamConsumerGroup%")] EventData[] events, ILogger log) { bool exceptionOccurred = false; List <Task> list = new List <Task>(); try { var cosmosDbRus = Convert.ToInt32(Environment.GetEnvironmentVariable("CosmosDBRus", EnvironmentVariableTarget.Process)); var cosmosDb = Environment.GetEnvironmentVariable("DeviceStreamDatabaseId", EnvironmentVariableTarget.Process); CosmosOperations docClient = await CosmosOperations.GetClientAsync(); await docClient.CreateDatabaseIfNotExistsAsync(cosmosDb, cosmosDbRus); } catch (Exception) { log.LogError($"Error occurrred while creating Cosmos DB"); throw; } foreach (EventData message in events) { try { message.Properties.TryGetValue("tenant", out object tenant); if (tenant != null) { string eventData = Encoding.UTF8.GetString(message.Body.Array); message.SystemProperties.TryGetValue("iothub-connection-device-id", out object deviceId); message.Properties.TryGetValue("opType", out object operationType); if (operationType.ToString().Equals("createDeviceIdentity") || operationType.ToString().Equals("deleteDeviceIdentity")) { DeviceService deviceService = new DeviceService(); list.Add(Task.Run(async() => await deviceService.SaveDeviceLifeCycleOperationAsync(eventData, Convert.ToString(tenant), deviceId.ToString(), operationType.ToString()))); } } else { // This operation is added to take care of the migration of device twins from the ported IoT hubs var document = JObject.Parse(Encoding.UTF8.GetString(message.Body.Array)); // The migration tool adds the tenant information while forming the request, we will pull that out of the body and use it for processing the request if (document != null && document["tenant"] != null && !string.IsNullOrEmpty(Convert.ToString(document["tenant"]))) { string tenentInfo = document["tenant"].ToString(); string device = document["deviceId"].ToString(); log.LogInformation($"{device}"); // once the tenant is pulled out remove it from the request body so that it is not save in the twin information document.Property("tenant").Remove(); if (!string.IsNullOrEmpty(tenentInfo) && !string.IsNullOrEmpty(device)) { DeviceService deviceService = new DeviceService(); list.Add(Task.Run(async() => await deviceService.SaveDeviceLifeCycleOperationAsync(document.ToString(), Convert.ToString(tenentInfo), device, "createDeviceIdentity"))); } } else { // If the tenant information is still not there in the body we log an exception log.LogError($"Error occurrred Tenant was not set"); exceptionOccurred = true; } } } catch (Exception ex) { log.LogError($"Error occurrred in for loop: {ex.Message} StackTrace: {ex.StackTrace} Inner Exception: {(string.IsNullOrEmpty(ex.StackTrace) ? string.Empty : ex.StackTrace)}"); exceptionOccurred = true; } } try { await Task.WhenAll(list.ToArray()); } catch (Exception ex) { log.LogError($"Error occurrred : {ex.Message} StackTrace: {ex.StackTrace} Inner Exception: {(string.IsNullOrEmpty(ex.StackTrace) ? string.Empty : ex.StackTrace)}"); exceptionOccurred = true; } if (exceptionOccurred) { throw new Exception("Function Failed with exception"); } }