public void Stop() { Log.Info("PS: Stopping PowerScheduler client plugin..."); try { // Stop and remove the StandbyWakeupThread if (_standbyWakeupThread != null) { _standbyWakeupThread.Abort(); _standbyWakeupThread.Join(100); _standbyWakeupThread = null; } // Stop and remove the RestoreMuteThread if (_restoreMuteThread != null) { _restoreMuteThread.Abort(); _restoreMuteThread.Join(100); _restoreMuteThread = null; } // Remove the EventWaitHandles if (_standbyWakeupTriggered != null) { _standbyWakeupTriggered.Close(); _standbyWakeupTriggered = null; } if (_standbyWakeupSuspend != null) { _standbyWakeupSuspend.Close(); _standbyWakeupSuspend = null; } if (_standbyWakeupResume != null) { _standbyWakeupResume.Close(); _standbyWakeupResume = null; } if (_standbyWakeupFinished != null) { _standbyWakeupFinished.Close(); _standbyWakeupFinished = null; } // Unregister PSClientPlugin as standby / wakeup handler (single-seat only) if (_singleSeat) UnregisterFromRemotePowerScheduler(); // Remove the standby/resume handlers if (_factory != null) { _factory.RemoveDefaultSet(); _factory = null; } Unregister(_wakeableStandbyHandler); Unregister(_wakeableWakeupHandler); Log.Debug("PS: Removed standby/wakeup handlers"); // Unregister as global service provider instance if (GlobalServiceProvider.Instance.IsRegistered<IPowerScheduler>()) { GlobalServiceProvider.Instance.Remove<IPowerScheduler>(); Log.Debug("PS: Unregistered IPowerScheduler service from GlobalServiceProvider"); } // Disable the wakeup timer if (_wakeupTimer != null) { _wakeupTimer.TimeToWakeup = DateTime.MaxValue; _wakeupTimer.Close(); _wakeupTimer = null; } // Unregister MediaPortal actions GUIWindowManager.OnNewAction -= new OnActionHandler(this.OnAction); SendPowerSchedulerEvent(PowerSchedulerEventType.Stopped); Log.Info("PS: PowerScheduler client plugin stopped"); } catch (Exception ex) { Log.Error("PS: Exception in Stop: {0}", ex); } }
public void Start() { Log.Info("PS: Starting PowerScheduler client plugin..."); try { // Register for MediaPortal actions to see if user is active GUIWindowManager.OnNewAction += new OnActionHandler(this.OnAction); // Create the timer that will wakeup the system after a specific amount of time after the // system has been put into standby _wakeupTimer = new WaitableTimer(); // Load settings LoadSettings(); // Unmute master volume if (_settings.GetSetting("UnmuteMasterVolume").Get<bool>()) { try { using (MasterVolume _masterVolume = new MasterVolume()) { if (_masterVolume.IsMuted) { Log.Debug("PS: Master volume is muted - unmute it"); _masterVolume.IsMuted = false; } } } catch (Exception ex) { Log.Error("PS: Error in handling master volume - {0}", ex.Message); } } // Register as global service provider instance if (!GlobalServiceProvider.Instance.IsRegistered<IPowerScheduler>()) { GlobalServiceProvider.Instance.Add<IPowerScheduler>(this); } Log.Debug("PS: Registered PowerScheduler as IPowerScheduler service to GlobalServiceProvider"); // Register standby/wakeup handlers _wakeableStandbyHandler = new WakeableStandbyHandler(); Register(_wakeableStandbyHandler); _wakeableWakeupHandler = new WakeableWakeupHandler(); Register(_wakeableWakeupHandler); if (!_singleSeat) { // Register special standby/wakeup handlers (remote client only) _factory = new PowerSchedulerFactory(); _factory.CreateDefaultSet(); } Log.Debug("PS: Registered standby/wakeup handlers to PowerScheduler client plugin"); // Register PSClientPlugin as standby / wakeup handler for PowerScheduler server (single-seat only) if (_singleSeat) RegisterToRemotePowerScheduler(); // Create the EventWaitHandles for the StandbyWakeupThread and GetCurrentState _standbyWakeupTriggered = new EventWaitHandle(false, EventResetMode.AutoReset, "MediaPortal.PowerScheduler.StandbyWakeupTriggered"); _standbyWakeupFinished = new AutoResetEvent(false); _standbyWakeupSuspend = new AutoResetEvent(false); // initially do not take suspend branch _standbyWakeupResume = new ManualResetEvent(true); // initially releases (no block) // Start the StandbyWakeupThread responsible for checking standby conditions and setting the wakeup timer _parentThread = Thread.CurrentThread; _standbyWakeupThread = new Thread(StandbyWakeupThread); _standbyWakeupThread.Name = "PS StandbyWakeup"; _standbyWakeupThread.Start(); Log.Debug("PS: StandbyWakeupThread started"); SendPowerSchedulerEvent(PowerSchedulerEventType.Started); Log.Info("PS: PowerScheduler client plugin started"); } catch (Exception ex) { Log.Error("PS: Exception in Start: {0}", ex); Stop(); } }