예제 #1
0
        private static async Task SimulateAlarms()
        {
            _client.DefaultRequestHeaders.Accept.Clear();
            _client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            _client.DefaultRequestHeaders.Add("aeg-sas-key", _eventAegSasKey);

            while (true)
            {
                try
                {
                    // For each device, send a new alarm, keeping the location static
                    for (int i = 0; i < _devices.Length; i++)
                    {
                        _devices[i].Status = GetAlarmStatus();
                        _devices[i].Image  = GetAlarmImage();
                        _devices[i].Text   = _devices[i].Status + " alert image: " + _devices[i].Image;

                        // Create a new event
                        AlarmEvent alarmEvent = new AlarmEvent {
                            topic     = _eventTopicResource,
                            subject   = "Alarm",
                            id        = Guid.NewGuid().ToString(),
                            eventType = "recordInserted",
                            eventTime = DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss.FFFFFFK"),
                            data      = _devices[i]
                        };

                        // Event Grid data is an array with one element
                        AlarmEvent[] alarmEvents = { alarmEvent };

                        // Post the data
                        HttpResponseMessage response = await _client.PostAsync(_eventTopicEndpoint, new JsonContent(alarmEvents));

                        if (response.IsSuccessStatusCode)
                        {
                            Console.WriteLine("\n Device " + _devices[i].DeviceId
                                              + ". Status: " + _devices[i].Status + ". Longitude: "
                                              + _devices[i].Longitude + ". Latitude: " + _devices[i].Latitude
                                              + ". Image: " + _devices[i].Image);
                        }
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine("\nError sending alarm:" + e.Message);
                }

                // Exit if max time reached
                if (IsMaxTime())
                {
                    Console.WriteLine("Maximum time reached (" + _maxRunTime + " mins), simulator stopping.");
                    Environment.Exit(1);
                }
                // Pause specified interval before the next batch of alarms
                Thread.Sleep(_eventInterval);
            }
        }
예제 #2
0
        private static async Task SimulateAlarms()
        {
            _client.DefaultRequestHeaders.Accept.Clear();
            _client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            _client.DefaultRequestHeaders.Add("aeg-sas-key", _eventAegSasKey);

            Random randomTimer = new Random(Guid.NewGuid().GetHashCode());

            while (true)
            {
                try
                {
                    // Choose a random device to send an event from
                    Random randomDevice = new Random(Guid.NewGuid().GetHashCode());
                    int    deviceId     = randomDevice.Next(0, _devices.Length);

                    _devices[deviceId].Image = GetAlarmImage();
                    _devices[deviceId].Text  = "Alarm event raised";

                    // Create a new event
                    AlarmEvent alarmEvent = new AlarmEvent {
                        subject   = "Alarm",
                        id        = Guid.NewGuid().ToString(),
                        eventType = "recordInserted",
                        eventTime = DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss.FFFFFFK"),
                        data      = _devices[deviceId]
                    };

                    // Event Grid data is an array with one element
                    AlarmEvent[] alarmEvents = { alarmEvent };

                    // Post the data
                    HttpResponseMessage response = await _client.PostAsync(_eventTopicEndpoint, new JsonContent(alarmEvents));

                    if (response.IsSuccessStatusCode)
                    {
                        Console.WriteLine("\n Device " + _devices[deviceId].DeviceId
                                          + ". Longitude: " + _devices[deviceId].Longitude
                                          + ". Latitude: " + _devices[deviceId].Latitude
                                          + ". Image: " + _devices[deviceId].Image);
                    }
                    else
                    {
                        Console.WriteLine("Post unsuccessful: " + response.StatusCode + " " + response.ReasonPhrase);
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine("\nError sending alarm:" + e.Message + e.ToString());
                }

                // Exit if max time reached
                if (IsMaxTime())
                {
                    Console.WriteLine("Maximum time reached (" + _maxRunTime + " mins), simulator stopping.");
                    Environment.Exit(1);
                }
                // Pause specified interval before the next batch of alarms
                Thread.Sleep(randomTimer.Next(0, _eventInterval));
            }
        }