private void GenerateAction(EventHubElement eventHub)
        {
            // Event hub client
            var eventHubHelper = new EventHubHelper(eventHub.EventHubName, eventHub.ConnectionString);

            // Send order to event hub
            var order = GenerateOrder(eventHub.Region);

            Console.WriteLine($"Sending order message to {eventHub.Region}");
            eventHubHelper.SendMessageToEventHub(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(order))).GetAwaiter().GetResult();

            // Close connection
            eventHubHelper.CloseEventHub().GetAwaiter().GetResult();
        }
        private async Task AddDeviceGroupToADX(DeviceGroup deviceGroup, bool isDeleted = false)
        {
            bool isKustoEnabled = this.config.DeviceTelemetryService.Messages.TelemetryStorageType.Equals(Common.Services.Models.TelemetryStorageTypeConstants.Ade, StringComparison.OrdinalIgnoreCase);

            if (isKustoEnabled)
            {
                string tenantId         = this.httpContextAccessor.HttpContext.Request.GetTenant();
                var    connectionString = this.tenantConnectionHelper.GetEventHubConnectionString(tenantId);
                if (!string.IsNullOrWhiteSpace(connectionString))
                {
                    EventHubHelper eventHubHelper = new EventHubHelper(connectionString);
                    var            result         = this.GetDeviceGroupsForADX(deviceGroup, isDeleted);
                    await eventHubHelper.SendMessageToEventHub($"{tenantId}-devicegroup", new EventData[] { result });
                }
            }
        }
示例#3
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);
        }
示例#4
0
        private async Task ProcessTelemetryMessageAndWriteToEventHub(
            JObject telemetry,
            EventHubHelper eventHubHelper,
            ILogger log,
            object tenant,
            object deviceId,
            TelemetryTimestamp telemetryTimestamp)
        {
            try
            {
                if (telemetry == null || !telemetry.HasValues)
                {
                    throw new Exception("Telemetry message had no values to write to cosmos");
                }

                /*
                 * telemetry[DeviceTelemetryKeyConstants.DeviceId] = deviceId.ToString();
                 *
                 * telemetry.Add(DeviceTelemetryKeyConstants.DateTimeReceived, telemetryTimestamp.DateTime.ToString());
                 * telemetry.Add(DeviceTelemetryKeyConstants.TimeReceived, telemetryTimestamp.EpochTimestamp);
                 * telemetry.Add(DeviceTelemetryKeyConstants.Schema, DeviceTelemetryKeyConstants.MessageSchema);
                 */

                JObject telemetryData = new JObject();
                telemetryData.Add(DeviceTelemetryKeyConstants.Data, telemetry);
                telemetryData.Add(DeviceTelemetryKeyConstants.DeviceId, deviceId.ToString());
                telemetryData.Add(DeviceTelemetryKeyConstants.DateTimeReceived, telemetryTimestamp.DateTime);
                telemetryData.Add(DeviceTelemetryKeyConstants.TimeReceived, telemetryTimestamp.EpochTimestamp);
                telemetryData.Add(DeviceTelemetryKeyConstants.Schema, DeviceTelemetryKeyConstants.MessageSchema);

                // Save the telemetry message to EventHub for further processing
                var byteMessage = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(telemetryData));
                var eventData   = new Azure.Messaging.EventHubs.EventData(byteMessage);
                eventData.Properties.Add("deviceid", deviceId.ToString());

                await eventHubHelper.SendMessageToEventHub($"{tenant}-telemetry", new Azure.Messaging.EventHubs.EventData[] { eventData });
            }
            catch (Exception ex)
            {
                this.success = false;
                log.LogError($"Error occurrred : {ex.Message}");
            }
        }
示例#5
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 group migration of tenant: {tenantId} ");

                    IEnumerable <DeviceGroup> deviceGroupList = await this.GetDeviceGroups(tenantId);
                    var connectionString          = this.tenantConnectionHelper.GetEventHubConnectionString(Convert.ToString(tenantId));
                    EventHubHelper eventHubHelper = new EventHubHelper(connectionString);
                    List <EventData> events       = new List <EventData>();
                    foreach (var deviceGroup in deviceGroupList)
                    {
                        var result = this.GetDeviceGroupsForADX(deviceGroup);

                        if (result != null)
                        {
                            events.Add(result);
                        }
                    }

                    await eventHubHelper.SendMessageToEventHub($"{tenantId}-devicegroup", events);

                    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);
        }
示例#6
0
        public async Task SaveDeviceTwinOperationAsync(string eventData, ILogger log, string tenant, string deviceId, string operationType, TelemetryTimestamp timeStamp, EventHubHelper eventHubHelper)
        {
            try
            {
                string  deviceTwin     = eventData;
                Twin    twin           = null;
                JObject deviceTwinJson = new JObject();
                deviceTwinJson.Add(DeviceTelemetryKeyConstants.DeviceId, deviceId.ToString());
                deviceTwinJson.Add(DeviceTelemetryKeyConstants.TimeStamp, timeStamp.DateTime);
                deviceTwinJson.Add(DeviceTelemetryKeyConstants.TimeReceived, timeStamp.EpochTimestamp);
                deviceTwinJson.Add(DeviceTelemetryKeyConstants.EventOpType, operationType);
                deviceTwinJson.Add(DeviceTelemetryKeyConstants.IsDeleted, false);

                if (operationType.ToString().Equals("createDeviceIdentity"))
                {
                    deviceTwinJson.Add(DeviceTelemetryKeyConstants.DeviceCreatedDate, timeStamp.DateTime);

                    try
                    {
                        twin = await TenantConnectionHelper.GetRegistry(Convert.ToString(tenant)).GetTwinAsync(deviceId.ToString());

                        deviceTwin = twin.ToJson();
                    }
                    catch (Exception e)
                    {
                        log.LogError($"Unable to fetch DeviceId: {deviceId} device twin from iothub of tenant: {tenant}.", e);
                    }
                }
                else
                {
                    JObject         previousTwin = null;
                    KustoOperations kustoClient  = await KustoOperations.GetClientAsync();

                    string kustoQuery     = $"DeviceTwin | where DeviceId == \"{deviceId}\" | summarize arg_max(TimeStamp, *) by DeviceId | where IsDeleted == false";
                    var    deviceTwinList = await kustoClient.QueryAsync <DeviceTwinModel>($"IoT-{tenant}", kustoQuery, null);

                    DeviceTwinModel preDeviceTwin = deviceTwinList.FirstOrDefault();

                    if (preDeviceTwin != null)
                    {
                        previousTwin = preDeviceTwin.Twin;
                        deviceTwinJson.Add(DeviceTelemetryKeyConstants.DeviceCreatedDate, preDeviceTwin.DeviceCreatedDate);
                    }
                    else
                    {
                        deviceTwinJson.Add(DeviceTelemetryKeyConstants.DeviceCreatedDate, default(DateTime)); // Set Device Created Date to Default if twin is not present in storage.

                        try
                        {
                            twin = await TenantConnectionHelper.GetRegistry(Convert.ToString(tenant)).GetTwinAsync(deviceId.ToString());

                            if (twin != null)
                            {
                                previousTwin = JObject.Parse(twin.ToJson());
                            }
                        }
                        catch (Exception e)
                        {
                            log.LogError($"Unable to fetch DeviceId: {deviceId} device twin from iothub of tenant: {tenant}.", e);
                        }
                    }

                    switch (operationType)
                    {
                    case "deviceConnected":
                        if (preDeviceTwin != null)
                        {
                            previousTwin["connectionState"]  = "Connected";
                            previousTwin["lastActivityTime"] = timeStamp.DateTime;
                        }

                        break;

                    case "deviceDisconnected":
                        if (preDeviceTwin != null)
                        {
                            previousTwin["connectionState"]  = "Disconnected";
                            previousTwin["lastActivityTime"] = timeStamp.DateTime;
                        }

                        break;

                    case "updateTwin":
                        if (preDeviceTwin != null)
                        {
                            JObject twinFragment = JObject.Parse(eventData);
                            previousTwin = previousTwin.UpdateJson(twinFragment);
                        }
                        else
                        {
                            previousTwin = JObject.Parse(eventData);
                        }

                        break;

                    case "deleteDeviceIdentity":
                        deviceTwinJson[DeviceTelemetryKeyConstants.IsDeleted] = true;
                        break;

                    default:
                        break;
                    }

                    deviceTwin = previousTwin?.ToString();
                }

                deviceTwinJson.Add(DeviceTelemetryKeyConstants.Data, deviceTwin);

                // Save the Device Twin Data to EventHub for further processing
                var byteMessage         = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(deviceTwinJson));
                var eventDeviceTwinData = new Azure.Messaging.EventHubs.EventData(byteMessage);
                eventDeviceTwinData.Properties.Add("deviceid", deviceId.ToString());

                await eventHubHelper.SendMessageToEventHub($"{tenant}-devicetwin", new Azure.Messaging.EventHubs.EventData[] { eventDeviceTwinData });
            }
            catch (Exception exception)
            {
                throw new ApplicationException($"Save Device Twin operation failed: {exception}, operation type: {operationType}, tenant: {tenant}, deviceId {deviceId}");
            }
        }