private static async Task SimulateData(CancellationToken ct)
        {
            var serializer = new DataContractJsonSerializer(typeof(TelemetryMessage));

            var sensor = DeviceInfo.Sensors.FirstOrDefault(x => (x.DataType == Configuration[SensorDataTypeSetting]));

            if (sensor == null)
            {
                throw new Exception($"No preconfigured Sensor for DataType '{Configuration[SensorDataTypeSetting]}' found.");
            }

            while (true)
            {
                if (ct.IsCancellationRequested)
                {
                    break;
                }

                if (Math.Abs(_lastAmbientLightSent - _ambientLight) > double.Epsilon)
                {
                    _lastAmbientLightSent = _ambientLight;
                    var telemetryMessage = new TelemetryMessage()
                    {
                        SensorId       = sensor.Id,
                        SensorReading  = _ambientLight.ToString(CultureInfo.InvariantCulture),
                        EventTimestamp = DateTime.UtcNow.ToString("o"),
                        SensorType     = sensor.Type,
                        SensorDataType = sensor.DataType,
                        SpaceId        = sensor.SpaceId
                    };

                    try
                    {
                        List <Task> tasks = new List <Task>();

                        using (var stream = new MemoryStream())
                        {
                            serializer.WriteObject(stream, telemetryMessage);
                            var     binaryMessage = stream.ToArray();
                            Message eventMessage  = new Message(binaryMessage);
                            eventMessage.Properties.Add("Sensor", "");
                            eventMessage.Properties.Add("MessageVersion", "1.0");
                            eventMessage.Properties.Add("x-ms-flighting-udf-execution-manually-enabled", "true");
                            Console.WriteLine(
                                $"\t{DateTime.UtcNow.ToLocalTime()}> Sending message: {Encoding.ASCII.GetString( binaryMessage )}");

                            tasks.Add(TopologyDeviceClient.SendEventAsync(eventMessage));
                        }

                        await Task.WhenAll(tasks);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine($"Error occurred in {nameof( SimulateData )}: {ex}");
                    }
                }

                await Task.Delay(int.Parse(Configuration[MessageIntervalInMilliSecondsSetting]), ct);
            }
        }
        private static async Task SimulateData(CancellationToken ct)
        {
            bool atLeastOneSensorMatchFound = false;
            var  serializer = new DataContractJsonSerializer(typeof(TelemetryMessage));

            foreach (Sensor sensor in DeviceInfo.Sensors)
            {
                if (SensorInfosByDataType.ContainsKey(sensor.DataType))
                {
                    atLeastOneSensorMatchFound = true;
                    break;
                }
            }

            if (!atLeastOneSensorMatchFound)
            {
                throw new Exception(
                          $"No preconfigured Sensor found for any of the following datatypes: {string.Join( ", ", SensorInfosByDataType.Keys )}");
            }

            Console.WriteLine();
            Console.WriteLine("Beginning to simulate data...");
            Console.WriteLine();

            while (true)
            {
                if (ct.IsCancellationRequested)
                {
                    break;
                }

                foreach (Sensor sensor in DeviceInfo.Sensors)
                {
                    if (!SensorInfosByDataType.TryGetValue(sensor.DataType, out SensorInfo sensorInfo))
                    {
                        continue;
                    }

                    if (sensorInfo.IsCurrentValueDifferent())
                    {
                        sensorInfo.UpdateLastValueSentWithCurrentValue();
                        var currentValue     = sensorInfo.GetCurrentValue();
                        var telemetryMessage = new TelemetryMessage()
                        {
                            SensorId       = sensor.Id,
                            SensorReading  = currentValue.ToString(),
                            EventTimestamp = DateTime.UtcNow.ToString("o"),
                            SensorType     = sensor.Type,
                            SensorDataType = sensor.DataType,
                            SpaceId        = sensor.SpaceId,
                            IoTHubDeviceId = IoTHubDeviceId
                        };

                        try
                        {
                            List <Task> tasks = new List <Task>();

                            using (var stream = new MemoryStream())
                            {
                                serializer.WriteObject(stream, telemetryMessage);
                                var     binaryMessage = stream.ToArray();
                                Message eventMessage  = new Message(binaryMessage);
                                eventMessage.Properties.Add("Sensor", "");
                                eventMessage.Properties.Add("MessageVersion", "1.0");
                                eventMessage.Properties.Add("x-ms-flighting-udf-execution-manually-enabled", "true");
                                Console.WriteLine(
                                    $"\t{DateTime.UtcNow.ToLocalTime()}> Sending message: {Encoding.ASCII.GetString( binaryMessage )}");

                                tasks.Add(TopologyDeviceClient.SendEventAsync(eventMessage));
                            }

                            await Task.WhenAll(tasks);
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine($"Error occurred in {nameof( SimulateData )}: {ex}");
                        }
                    }
                }

                await Task.Delay(int.Parse(Configuration[MessageIntervalInMilliSecondsSetting]), ct);
            }
        }