Exemple #1
0
        public static async Task <int> ManualDeviceImportOrchestration_Runner([OrchestrationTrigger] DurableOrchestrationContext context)
        {
            var       blobContainerName        = context.GetInput <string>();
            var       start                    = 0;
            const int DEVICE_UPDATE_BATCH_SIZE = 100;
            var       changedDeviceCount       = 0;

            while (true)
            {
                var request = new UpdateDevicesActivityRequest
                {
                    DeviceCount       = DEVICE_UPDATE_BATCH_SIZE,
                    Start             = start,
                    BlobContainerName = blobContainerName
                };

                var result = await context.CallActivityAsync <UpdateDevicesActivityResult>(nameof(UpdateDevicesActivity), request);

                changedDeviceCount += result.ChangedDeviceCount;

                if (result.ProcessedDeviceCount < request.DeviceCount)
                {
                    break;
                }

                start = result.Start + result.ProcessedDeviceCount;
            }

            return(changedDeviceCount);
        }
Exemple #2
0
        public static async Task <UpdateDevicesActivityResult> UpdateDevicesActivity([ActivityTrigger] UpdateDevicesActivityRequest request, TraceWriter log)
        {
            var result = new UpdateDevicesActivityResult
            {
                ProcessedDeviceCount = 0,
                ChangedDeviceCount   = 0,
                Start = request.Start
            };

            CloudStorageAccount storage = CloudStorageAccount.Parse(Settings.Instance.StorageAccountConnectionString);
            CloudBlobClient     client  = storage.CreateCloudBlobClient();


            CloudBlobContainer container = client.GetContainerReference(request.BlobContainerName);
            var blob = container.GetBlobReference(Utils.DeviceToImportBlobName);

            using (var streamReader = new StreamReader(await blob.OpenReadAsync()))
            {
                // skip "start" lines
                for (var line = 0; line < request.Start; ++line)
                {
                    await streamReader.ReadLineAsync();
                }

                // process items
                var registryManager = RegistryManager.CreateFromConnectionString(Settings.Instance.IoTHubConnectionString);
                for (var count = 0; count < request.DeviceCount; ++count)
                {
                    try
                    {
                        var line = await streamReader.ReadLineAsync();

                        if (!string.IsNullOrEmpty(line))
                        {
                            var device = JsonConvert.DeserializeObject <ExportImportDevice>(line);
                            if (await HandleDevice(device, registryManager, log))
                            {
                                result.ChangedDeviceCount++;
                            }

                            result.ProcessedDeviceCount++;
                        }
                    }
                    catch (Exception ex)
                    {
                        log.Error("error", ex);
                        throw;
                    }
                }
            }

            return(result);
        }