public static async Task Run([EventHubTrigger(eventHubName: "twin-change", Connection = "TwinChangeEventHubConnectionString", ConsumerGroup = "%DeviceDeploymentHistoryConsumerGroup%")] EventData[] events, ILogger log) { bool exceptionOccurred = false; List <Task> list = new List <Task>(); foreach (EventData message in events) { try { message.Properties.TryGetValue("tenant", out object tenant); if (tenant != null) { string eventData = Encoding.UTF8.GetString(message.Body.Array); Twin twin = JsonConvert.DeserializeObject <Twin>(eventData); TwinServiceModel twinServiceModel = new TwinServiceModel(twin); Dictionary <string, JToken> reportedProperties = twinServiceModel.ReportedProperties; var json = JToken.Parse(JsonConvert.SerializeObject(reportedProperties)); var fieldsCollector = new JsonFieldsCollector(json); var fields = fieldsCollector.GetAllFields(); bool isFirmWareUpdate = false; foreach (var field in fields) { isFirmWareUpdate = field.Key.Contains(FIRMWARE, StringComparison.OrdinalIgnoreCase); if (isFirmWareUpdate) { break; } } if (isFirmWareUpdate) { message.SystemProperties.TryGetValue("iothub-connection-device-id", out object deviceId); var newTwin = await TenantConnectionHelper.GetRegistry(Convert.ToString(tenant)).GetTwinAsync(deviceId.ToString()); var appliedConfigurations = newTwin.Configurations.Where(c => c.Value.Status.Equals(ConfigurationStatus.Applied)); if (appliedConfigurations != null && appliedConfigurations.Count() > 0) { DeploymentSyncService service = new DeploymentSyncService(); var appliedDeploymentFromStorage = service.GetDeploymentsByIdFromStorage(Convert.ToString(tenant), appliedConfigurations.Select(ac => ac.Key).ToArray()).Result.FirstOrDefault(); await service.SaveDeploymentHistory(Convert.ToString(tenant), appliedDeploymentFromStorage, newTwin); } } } } 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([QueueTrigger("tenantstosync", Connection = "AzureStorageConnectionString")] string myQueueItem, ILogger log) { log.LogInformation($"C# Queue trigger function processed: {myQueueItem}"); if (!string.IsNullOrEmpty(myQueueItem)) { TenantQueueItem tenant = JsonConvert.DeserializeObject <TenantQueueItem>(myQueueItem); if (tenant != null && !string.IsNullOrWhiteSpace(tenant.TenantId)) { List <DeploymentServiceModel> deploymentsToSync = new List <DeploymentServiceModel>(); IEnumerable <Configuration> deploymentsFromHub = null; try { deploymentsFromHub = await TenantConnectionHelper.GetRegistry(tenant.TenantId).GetConfigurationsAsync(100); DeploymentSyncService service = new DeploymentSyncService(); deploymentsToSync.AddRange(await service.GetDeploymentsToSync(tenant.TenantId, deploymentsFromHub)); } catch (Exception) { log.LogError($"Error occurrred while fetching deployments"); throw; } if (deploymentsToSync != null && deploymentsToSync.Count > 0) { // Get the connection string from app settings string connectionString = Environment.GetEnvironmentVariable("AzureStorageConnectionString", EnvironmentVariableTarget.Process); // Instantiate a QueueClient which will be used to create and manipulate the queue QueueClient queueClient = new QueueClient(connectionString, "deploymentstosync"); await queueClient.CreateIfNotExistsAsync(); if (queueClient.Exists()) { foreach (var deploymentToSync in deploymentsToSync) { DeploymentModel deployment = new DeploymentModel(); deployment.TenantId = tenant.TenantId; deployment.Deployment = deploymentToSync; deployment.Configuration = deploymentsFromHub.FirstOrDefault(d => d.Id == deploymentToSync.Id); var deploymentToSyncString = JsonConvert.SerializeObject(deployment); queueClient.SendMessage(Base64Encode(deploymentToSyncString)); } } } } } }
public static async Task Run([QueueTrigger("deploymentstosync", Connection = "AzureStorageConnectionString")] string myQueueItem, ILogger log) { log.LogInformation($"C# Queue trigger function processed: {myQueueItem}"); if (!string.IsNullOrEmpty(myQueueItem)) { DeploymentModel deployment = JsonConvert.DeserializeObject <DeploymentModel>(myQueueItem); if (deployment != null) { DeploymentSyncService service = new DeploymentSyncService(); try { await service.UpdateAndSaveDeployment(deployment.TenantId, deployment.Deployment, deployment.Configuration); } catch (Exception) { log.LogError($"Error occurrred while saving deployment"); throw; } } } }