public void ShowAlertWindow(bool playAlarm = true) { if (_defaultDeviceChanged) { return; } _isOverlayAlertPlaying = true; if (!playAlarm) { _overlayWorking = true; return; } System.Diagnostics.Process[] p = System.Diagnostics.Process.GetProcesses(); var processSharp = new ProcessSharp((int)GetForegroundProcessId(), MemoryType.Remote); if (_alertOverlay != null) { // _overlayWorking = false; if (!_alertOverlay.IsEnabled) { _overlayWorking = false; } return; } _alertOverlay = new AlertOverlay(); _alertOverlay.Initialize(processSharp.WindowFactory.MainWindow); _alertOverlay.Enable(); _overlayWorking = true; // Do work _timerOverlayUpdate.Start(); }
public AlarmSystem(bool isController = false, int micCaptureBoost = 100, float delayBeforeAlarm = 0, float delayBeforeOverlay = 0, float alarmVolume = 0, float systemVolume = 0, float threshold = 100, bool isSoundAlertEnabled = false, bool isOverlayAlertEnabled = false) { //_loggingEnabled = Trace.Listeners.Count > 1; try { Trace.TraceInformation("Alarm system init"); Trace.Indent(); state = States.Running; this._isControllerMode = isController; MMNotificationClient deviceNotification = new MMNotificationClient(); deviceNotification.DefaultDeviceChanged += DeviceNotification_DefaultDeviceChanged; deviceNotification.DeviceAdded += DeviceNotification_DeviceAdded; deviceNotification.DeviceRemoved += DeviceNotification_DeviceRemoved; GetCapture(isController); Trace.TraceInformation("Sound Capture OK"); _soundSource = GetSoundSource(); _soundSource = _soundSource.Loop(); GetOutputSound(); Trace.TraceInformation("Sound Out OK"); this._isSoundAlertEnabled = isSoundAlertEnabled; this._isOverlayAlertEnabled = isOverlayAlertEnabled; this._micCaptureBoost = micCaptureBoost; this._delayBeforeAlarm = delayBeforeAlarm; this._delayBeforeOverlay = delayBeforeOverlay; this.AlarmVolume = alarmVolume; this._alarmThreshold = threshold; _systemSimpleAudioVolume = GetSimpleAudioVolume(); this.SystemVolume = systemVolume; if (!isController) { _bgInputListener.WorkerSupportsCancellation = true; _bgInputListener.DoWork += bgInputListener_DoWork; _bgInputListener.RunWorkerCompleted += bgInputListener_RunWorkerCompleted; _bgInputListener.RunWorkerAsync(); } Trace.TraceInformation("Background worker running"); #region Timers _timerAlarmDelay = new System.Timers.Timer(); _timerAlarmDelay.Elapsed += (s, args) => { if (_timerAlarmDelayArgs.ElapsedTime.Seconds >= _delayBeforeAlarm) { _timerAlarmDelayArgs.alarmActive = true; _timerAlarmDelay.Stop(); PlayAlarm(!_isControllerMode); } OnUpdateTimerAlarmDelay(this, _timerAlarmDelayArgs); }; _timerOverlayShow = new System.Timers.Timer(); _timerOverlayShow.Elapsed += (s, args) => { if (_timerOverlayDelayArgs.ElapsedTime.Seconds >= _delayBeforeOverlay) { _timerOverlayDelayArgs.alarmActive = true; _timerOverlayShow.Stop(); Application.Current.Dispatcher.Invoke((Action) delegate { ShowAlertWindow(!_isControllerMode); }); } OnUpdateTimerOverlayDelay(this, _timerOverlayDelayArgs); //if (_timerOverlayShow.Dispatcher.HasShutdownStarted) // _timerOverlayDelayArgs = null; }; _timerOverlayUpdate = new System.Timers.Timer(); _timerOverlayUpdate.Interval = 10; _timerOverlayUpdate.Elapsed += (s, args) => { _alertOverlay.Update(); if (!_overlayWorking) { _timerOverlayUpdate.Stop(); if (_alertOverlay != null) { _alertOverlay.Dispose(); _alertOverlay = null; } } }; _timerZeroVolumeDelay = new System.Timers.Timer(10000); _timerZeroVolumeDelay.AutoReset = false; _timerZeroVolumeDelay.Elapsed += (s, args) => { _currentCaptureRole -= 1; if (_currentCaptureRole < 0) { _currentCaptureRole = Role.Communications; } GetCapture(isController); }; _timerZeroVolumeDelay.Start(); #endregion Trace.TraceInformation("Timers initialized"); Trace.TraceInformation("Alarm System up and running!"); Trace.Unindent(); } catch (Exception e) { Trace.TraceError(e.Message); Trace.TraceError(e.StackTrace); Application.Current.Shutdown(); } }