private static async void SendDeviceToCloudMessagesAsync()

in Hands-on lab/simulated-device/SimulatedDevice.cs [26:84]


        private static async void SendDeviceToCloudMessagesAsync()
        {
            Random rand = new Random();
            string category = "Security";
            int eventId = 1000;

            while (true)
            {
                string appName = "App" + rand.Next(3).ToString().PadLeft(2,'0');
                int level = rand.Next(3);

                switch (rand.Next(3))
                {
                    case 0:
                        category = "Security";
                        eventId = rand.Next(1000, 1100);
                        break;
                    case 1:
                        category = "Application";
                        eventId = rand.Next(2000, 2100);
                        break;
                    case 2:
                        category = "System";
                        eventId = rand.Next(100, 200);
                        break;
                    case 3:
                        category = "Unknown";
                        eventId = rand.Next(10000, 11000);
                        break;
                }
                
                DateTime timestamp = DateTime.Now;
                string messageId = Guid.NewGuid().ToString();

                // Create JSON message
                var telemetryDataPoint = new
                {
                    AppName = appName,
                    TimeStamp = timestamp,
                    MessageId = messageId,
                    Category = category,
                    Level = level,
                    EventId = eventId
                };

                var messageString = JsonConvert.SerializeObject(telemetryDataPoint);
                var message = new Message(Encoding.ASCII.GetBytes(messageString));

                // Add a custom application property to the message.
                // An IoT Hub can filter on these properties without access to the message body.
                message.Properties.Add("SecurityAlert", (category == "Security") ? "true" : "false");

                // Send the telemetry message
                await s_deviceClient.SendEventAsync(message);
                Console.WriteLine("{0} > Sending message: {1}", DateTime.Now, messageString);

                await Task.Delay(1000);
            }
        }