Ejemplo n.º 1
0
        public static async Task Run([IoTHubTrigger("messages/events", Connection = "IOT_HUB_CONN")] EventData message, ILogger log)
        {
            log.LogInformation($"C# IoT Hub trigger function processed a message: {Encoding.UTF8.GetString(message.Body.Array)}");

            if (message.Properties.Keys.Contains("enterpriseid") == false)
            {
                return;
            }

            var connectionString   = Environment.GetEnvironmentVariable("EVENT_HUB_CONN");
            var eventHubNameString = Environment.GetEnvironmentVariable("EVENT_HUB_NAME");

            if (eventProducer == null)
            {
                eventProducer = new EventHubProducerClient(connectionString);
            }

            foreach (var prop in message.Properties)
            {
                log.LogInformation($"{prop.Key} - {prop.Value}");
            }

            var partitionKeyString = message.Properties["enterpriseid"] as string;

            var batchOptions = new CreateBatchOptions()
            {
                PartitionKey = partitionKeyString
            };

            var eventDataBatch = await eventProducer.CreateBatchAsync(batchOptions);

            var partitionKeyName = Environment.GetEnvironmentVariable("PARTITION_KEY_NAME");

            var ed = new AZEH.EventData(message.Body);

            ed.Properties.Add(partitionKeyName, partitionKeyString);

            foreach (var prop in message.Properties)
            {
                ed.Properties.Add(prop.Key, prop.Value);
            }

            eventDataBatch.TryAdd(ed);

            await eventProducer.SendAsync(eventDataBatch);
        }
Ejemplo n.º 2
0
        public async Task Start()
        {
            List <TenantModel> tenantList = await this.GetAllActiveTenantAsync();

            var tasks = tenantList.Select(async tenant =>
            {
                string tenantId = tenant.TenantId;

                try
                {
                    this.logger.LogInformation($"Started device twin migration of tenant: {tenantId} ");

                    List <Twin> deviceTwinList    = await this.GetDevices(tenantId);
                    var connectionString          = this.tenantConnectionHelper.GetEventHubConnectionString(Convert.ToString(tenantId));
                    EventHubHelper eventHubHelper = new EventHubHelper(connectionString);
                    var eventDatas = new ConcurrentBag <EventData>();
                    Parallel.ForEach(deviceTwinList, deviceTwin =>
                    {
                        JObject deviceTwinJson = new JObject();
                        deviceTwinJson.Add(DeviceId, deviceTwin.DeviceId);
                        deviceTwinJson.Add(TimeStamp, DateTime.UtcNow);
                        deviceTwinJson.Add(TimeReceived, DateTime.UnixEpoch);
                        deviceTwinJson.Add(EventOpType, "updateTwin");
                        deviceTwinJson.Add(IsDeleted, false);
                        deviceTwinJson.Add(Data, deviceTwin.ToJson());
                        deviceTwinJson.Add(DeviceCreatedDate, default(DateTime));

                        var byteMessage         = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(deviceTwinJson));
                        var eventDeviceTwinData = new Azure.Messaging.EventHubs.EventData(byteMessage);
                        eventDeviceTwinData.Properties.Add(DeviceId, deviceTwin.DeviceId);
                        eventDatas.Add(eventDeviceTwinData);
                    });

                    await eventHubHelper.SendMessageToEventHub($"{tenantId}-devicetwin", eventDatas.ToList());

                    this.logger.LogInformation($"Completed device twin migration of tenant: {tenantId} ");
                }
                catch (Exception ex)
                {
                    this.logger.LogError(ex, $"Failed device twin migration of tenant: {tenantId} ");
                }
            });

            await Task.WhenAll(tasks);
        }