예제 #1
0
    public void Start(IController controller)
    {
      Log.Info("PS: Starting PowerScheduler server plugin...");

      try
      {
        // Save controller
        _controller = controller;

        // 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();

        // Register standby/wakeup handlers
        _powerControllerStandbyHandler = new PowerControllerStandbyHandler();
        Register(_powerControllerStandbyHandler);
        _powerControllerWakeupHandler = new PowerControllerWakeupHandler();
        Register(_powerControllerWakeupHandler);
        _remoteClientStandbyHandler = new RemoteClientStandbyHandler();
        Register(_remoteClientStandbyHandler);
        _factory = new PowerSchedulerFactory(controller);
        _factory.CreateDefaultSet();
        Log.Debug("PS: Registered standby/wakeup handlers to PowerScheduler server plugin");

        // Register power event handler to TVServer
        RegisterPowerEventHandler();

        // Register PowerScheduler as IPowerControl remoting service
        StartRemoting();

        // Create the EventWaitHandles for the StandbyWakeupThread and GetCurrentState
        _standbyWakeupTriggered = new EventWaitHandle(false, EventResetMode.AutoReset, "TvEngine.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 server plugin started");
      }
      catch (Exception ex)
      {
        Log.Error("PS: Exception in Start: {0}", ex);
        Log.Info("PS: Exception in Start: {0}", ex);
        Stop();
      }
    }
예제 #2
0
    public void Stop()
    {
      Log.Info("PS: Stopping PowerScheduler server plugin...");

      try
      {
        // Stop and remove the StandbyWakeupThread
        if (_standbyWakeupThread != null)
        {
          _standbyWakeupThread.Abort();
          _standbyWakeupThread.Join(100);
          _standbyWakeupThread = 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 power event handler
        UnRegisterPowerEventHandler();

        // Remove the standby/resume handlers
        if (_factory != null)
        {
          _factory.RemoveDefaultSet();
          _factory = null;
        }
        Unregister(_remoteClientStandbyHandler);
        Unregister(_powerControllerWakeupHandler);
        Unregister(_powerControllerStandbyHandler);
        Log.Debug("PS: Removed standby/wakeup handlers");

        // Disable the wakeup timer
        if (_wakeupTimer != null)
        {
          _wakeupTimer.TimeToWakeup = DateTime.MaxValue;
          _wakeupTimer.Close();
          _wakeupTimer = null;
        }

        SendPowerSchedulerEvent(PowerSchedulerEventType.Stopped);
        Log.Info("PS: PowerScheduler server plugin stopped");
      }
      catch (Exception ex)
      {
        Log.Error("PS: Exception in Stop: {0}", ex);
        Log.Info("PS: Exception in Stop: {0}", ex);
      }
    }
예제 #3
0
    public void Start(IController controller)
    {
      try
      {
        string threadname = Thread.CurrentThread.Name;
        if (string.IsNullOrEmpty(threadname))
          Thread.CurrentThread.Name = "Powerscheduler";
      }
      catch (Exception ex)
      {
        Log.Error("Powerscheduler: Error naming thread - {0}", ex.Message);
      }

      _controller = controller;

      Register(_clientStandbyHandler);
      Register(_clientWakeupHandler);
      RegisterPowerEventHandler();

      Log.Debug("PowerScheduler: Registered default set of standby/resume handlers to PowerScheduler");

      // Create the PowerManager that helps setting the correct thread executation state
      _powerManager = new PowerManager();

      // Create the timer that will wakeup the system after a specific amount of time after the
      // system has been put into standby
      if (_wakeupTimer == null)
      {
        _wakeupTimer = new WaitableTimer();
        _wakeupTimer.OnTimerExpired += new WaitableTimer.TimerExpiredHandler(OnWakeupTimerExpired);
        _wakeupTimer.OnTimerException += new WaitableTimer.TimerExceptionHandler(OnWakeupTimerException);
      }

      // Configure remoting if not already done
      StartRemoting();

      LoadSettings();

      // Create the default set of standby/resume handlers
      if (_factory == null)
        _factory = new PowerSchedulerFactory(controller);
      _factory.CreateDefaultSet();

      SendPowerSchedulerEvent(PowerSchedulerEventType.Started);

      Log.Debug("PowerScheduler: Starting poll thread");
      _stopThread = new ManualResetEvent(false);
      _pollThread = new Thread(new ThreadStart(PollThread));
      _pollThread.Name = "PowerScheduler poll thread";
      _pollThread.IsBackground = true;
      _pollThread.Priority = ThreadPriority.Normal;
      _pollThread.Start();

      Log.Info("Powerscheduler: started");
    }