// Removes, updates, and adds alarms
        private void RefreshAlarms(ICollection <Alarm> alarms)
        {
            // Show that several concurrent alarms exist
            AlarmGrid.Visibility      = Visibility.Visible;
            NoAlarmPnl.Visibility     = Visibility.Hidden;
            InfoCenterCtrl.Visibility = Visibility.Hidden;
            InfoCenterCtrl.Stop();
            MultiAlarmGrid.Visibility = alarms.Count > 1 ? Visibility.Visible : Visibility.Collapsed;

            // Remove finished alarms, refresh existing alarms, show new alarms
            var removeIds             = mAlarmId2Ctrl.Keys.Except(alarms.Select(a => a.Id)).ToList();
            int numberOfRemovedAlarms = RemoveAlarms(removeIds);

            UpdateAlarms(alarms);
            var newAlarms = AddAlarms(alarms);

            // Ensure that an alarm is shown
            if (newAlarms.Count == 0 && numberOfRemovedAlarms > 0)
            {
                ShowNextAlarm();
            }

            #region Turn screen on, bring window to front, play sound

            if (newAlarms.Count > 0)
            {
                // Turn on screen
                try
                {
                    if (mPublicDisplayControl != null)
                    {
                        mPublicDisplayControl.TurnOn();
                    }
                    else
                    {
                        // Stop turning off screen
                        if (mTurnScreenOffTimer != null)
                        {
                            mTurnScreenOffTimer.Stop();
                        }
                        ScreenPower.TurnOn(this);
                    }
                }
                catch (Exception ex)
                {
                    Log.GetInstance().LogError("Error in turning on screen.", ex);
                }

                // Bring the AIC window to front
                ShowAndActivate();

                // Play a sound
                if (!Starting)
                {
                    try
                    {
                        mSoundHelper.Alarm = newAlarms.Last();

                        if (AicSettings.Global.PlayAlarmSound && !AicSettings.Global.PlayAnnouncement)
                        {
                            mSoundHelper.PlayAlarmSound();
                        }
                        else if (AicSettings.Global.PlayAnnouncement)
                        {
                            mSoundHelper.PlaySequence(AicSettings.Global.PlayAlarmSound, AicSettings.Global.AnnouncementIntervals);
                        }
                    }
                    catch (Exception ex)
                    {
                        Log.GetInstance().LogError("Could not play sound.", ex);
                    }
                }
            }

            #endregion
        }
 // Occurs whenever a key has been pressed
 private void Window_KeyDown(object sender, KeyEventArgs e)
 {
     if (e.Key == Key.F1)
     {
         TaskBarNotifier.ShowInfo(this);
     }
     else if (e.Key == Key.F5)
     {
         bool connectionOk = mAicMessageListener.CheckConnection();
         if (connectionOk)
         {
             mAicMessageListener.SendRequestAsync();
         }
         else
         {
             mAicMessageListener.Disconnect();
         }
         if (!mExistentAlarm && AicSettings.Global.InfoCenterEnabled)
         {
             InfoCenterCtrl.LoadData();
         }
     }
     else if (e.Key == Key.F7)
     {
         if (CurrentAlarmCtrl != null)
         {
             mSoundHelper.Alarm = CurrentAlarmCtrl.Alarm.BaseAlarm;
             mSoundHelper.PlayAnnouncement();
         }
     }
     else if (e.Key == Key.F8)
     {
         mSoundHelper.Stop();
     }
     else if (e.Key == Key.F12)
     {
         ShowSettings();
     }
     else if (Keyboard.Modifiers == ModifierKeys.Control && e.Key == Key.P)
     {
         if (CurrentAlarmCtrl != null)
         {
             new PrintWindow(mAlarmPrinter.ServerName, mAlarmPrinter.PrinterName).ShowAndClose();
             mAlarmPrinter.Print(CurrentAlarmCtrl.Alarm.BaseAlarm);
         }
     }
     else if (e.Key == Key.Left)
     {
         mChangeTabTimer.Stop();                                 // Restart the timer
         mChangeTabTimer.Start();
         ShowPreviousAlarm();
         if (InfoCenterCtrl.IsActive)
         {
             InfoCenterCtrl.ShowPreviousPage();
             InfoCenterCtrl.ResetTimer();
         }
     }
     else if (e.Key == Key.Right)
     {
         mChangeTabTimer.Stop();                                 // Restart the timer
         mChangeTabTimer.Start();
         ShowNextAlarm();
         if (InfoCenterCtrl.IsActive)
         {
             InfoCenterCtrl.ShowNextPage();
             InfoCenterCtrl.ResetTimer();
         }
     }
 }
        // Refreshes the main window
        private void RefreshMainWindow(ICollection <Alarm> alarms = null)
        {
            bool removeAlarms = false;

            if (!ConnectionAicToServerOk || !mConnectionWasToServerOk)
            {
                #region No connection client to server or no connection WAS to server

                if (!ConnectionAicToServerOk)
                {
                    NoAlarmLbl.Content = "Keine Verbindung zum Server vorhanden.";
                }
                else
                {
                    NoAlarmLbl.Content = AicSettings.Global.StandAloneMode ? "Keine Verbindung zum WAS vorhanden." : "Keine Verbindung zwischen WAS und Server vorhanden.";
                }

                NoAlarmPnl.Visibility     = Visibility.Visible;
                InfoCenterCtrl.Visibility = Visibility.Hidden;
                InfoCenterCtrl.Stop();
                removeAlarms = true;

                #endregion
            }
            else if (alarms != null)
            {
                #region Handle alarms
                if (alarms.Count == 0)                                  // Remove all alarms and show InfoCenter
                {
                    #region No alarm. Show InfoCenter

                    // Stop sound
                    mSoundHelper.Alarm = null;

                    // Turn off the public screen if there was an alarm during the night hours
                    if (mPublicDisplayControl != null && mAlarmId2Ctrl.Count > 0 && IsTurnScreenOffTime())
                    {
                        mPublicDisplayControl.TurnOff();
                    }

                    // Start the timer for turning off the screen
                    if (mTurnScreenOffTimer != null && !mTurnScreenOffTimer.IsEnabled && mTurnScreenOffTimer.Interval.Ticks > 0)
                    {
                        mTurnScreenOffTimer.Start();                                    // Start() resets and starts the timer
                    }

                    removeAlarms       = true;
                    NoAlarmLbl.Content = "Zur Zeit liegt kein Alarm vor.";

                    // Load the InfoCenter data and show it
                    if (!AicSettings.Global.InfoCenterEnabled)
                    {
                        NoAlarmPnl.Visibility = Visibility.Visible;
                    }
                    else if (InfoCenterCtrl.Status == Status.Stopped)
                    {
                        NoAlarmPnl.Visibility = Visibility.Visible;
                        InfoCenterCtrl.LoadData();
                    }

                    #endregion
                }
                else
                {
                    RefreshAlarms(alarms);
                }
                #endregion
            }

            if (removeAlarms)
            {
                #region Remove all alarms

                AlarmGrid.Visibility = Visibility.Hidden;
                foreach (TabItem tab in AlarmTabCtrl.Items)
                {
                    DisposeAlarm(tab);
                }
                AlarmTabCtrl.Items.Clear();
                mAlarmId2Ctrl.Clear();

                #endregion
            }
        }