示例#1
0
        private static async Task SendMessage(DeviceClient device, MeasurementMessage measurement)
        {
            var json    = JsonConvert.SerializeObject(measurement);
            var message = new Message(Encoding.UTF8.GetBytes(json));

            await device.SendEventAsync(message);
        }
示例#2
0
        private static async Task RunSimulatedDataGeneratorAsync(DeviceClient device, CancellationToken ct)
        {
            var rand = new Random();

            while (!ct.IsCancellationRequested)
            {
                var measurement = new MeasurementMessage
                {
                    TemperatureUnit = "Temperature",
                    Temperature     = (rand.Next(200, 259) / 10f),
                    HumidityUnit    = "%",
                    Humidity        = rand.Next(400, 600) / 10f,
                    PressureUnit    = "hPa",
                    Pressure        = rand.Next(9400, 10500) / 10f
                };

                Console.WriteLine("Sending measurement...");

                await SendMessage(device, measurement);

                await Task.Delay(1000, ct);
            }
        }
        private static async Task RunSimulatedGeneratorDataAsync(DeviceClient device, CancellationToken ct)
        {
            var rand = new Random();

            const int anomalyTimeinterval = 20;
            var       nextAnomalousEvent  = DateTime.Now.AddSeconds(anomalyTimeinterval);

            while (!ct.IsCancellationRequested)
            {
                var measurement = new MeasurementMessage
                {
                    TemperatureUnit = "Temperature",
                    Temperature     = (rand.Next(200, 209) / 10f),
                    HumidityUnit    = "%",
                    Humidity        = rand.Next(480, 520) / 10f,
                    PressureUnit    = "hPa",
                    Pressure        = rand.Next(9400, 9500) / 10f
                };

                if (nextAnomalousEvent < DateTime.Now)
                {
                    Console.WriteLine("**GENERATING ANOMALY**");
                    measurement.Temperature = rand.Next(300, 350) / 10f;
                    measurement.Humidity    = rand.Next(800, 1000) / 10f;
                    nextAnomalousEvent      = DateTime.Now.AddSeconds(anomalyTimeinterval);
                }
                else
                {
                    Console.WriteLine("Sending measurement...");
                }

                await SendMessage(device, measurement);

                await Task.Delay(1000, ct);
            }
        }