/// <summary>
        /// Create/register the default set of standby/wakeup handlers
        /// </summary>
        public void CreateDefaultSet()
        {
            IPowerScheduler powerScheduler = GlobalServiceProvider.Instance.Get <IPowerScheduler>();

            foreach (IStandbyHandler handler in _standbyHandlers)
            {
                powerScheduler.Register(handler);
            }
            foreach (IWakeupHandler handler in _wakeupHandlers)
            {
                powerScheduler.Register(handler);
            }
        }
        private void OnPowerSchedulerEvent(PowerSchedulerEventArgs args)
        {
            switch (args.EventType)
            {
            case PowerSchedulerEventType.Started:
            case PowerSchedulerEventType.Elapsed:

                IPowerScheduler ps = GlobalServiceProvider.Instance.Get <IPowerScheduler>();
                if (ps == null)
                {
                    return;
                }

                PowerSetting setting;
                bool         enabled;

                using (Settings reader = new MPSettings())
                {
                    EPGWakeupConfig config = new EPGWakeupConfig(reader.GetValueAsString("psclientplugin", "RebootConfig", String.Empty));

                    if (args.EventType == PowerSchedulerEventType.Started)
                    {
                        // Get time of last reboot
                        if (config.LastRun == DateTime.MinValue)
                        {
                            config.LastRun = DateTime.Now;
                        }
                        else
                        {
                            config.LastRun = DateTime.Now.AddMilliseconds(-Environment.TickCount);
                        }

                        // Save last reboot status
                        reader.SetValue("psclientplugin", "RebootConfig", config.SerializeAsString());
                        Log.Debug("RebootHandler: Set time of last reboot: {0}", config.LastRun);
                    }

                    // Check if system should wakeup for reboot
                    setting = ps.Settings.GetSetting("RebootWakeup");
                    enabled = reader.GetValueAsBool("psclientplugin", "RebootWakeup", false);
                    if (setting.Get <bool>() != enabled)
                    {
                        setting.Set <bool>(enabled);
                        if (enabled)
                        {
                            if (ps.IsRegistered(this as IWakeupHandler))
                            {
                                ps.Unregister(this as IWakeupHandler);
                            }
                            ps.Register(this as IWakeupHandler);
                        }
                        else
                        {
                            ps.Unregister(this as IWakeupHandler);
                        }
                        Log.Debug("RebootHandler: Wakeup system for reboot: {0}", enabled ? "enabled" : "disabled");
                    }

                    // Check if a reboot time is set
                    setting = ps.Settings.GetSetting("RebootConfig");
                    if (!config.Equals(setting.Get <EPGWakeupConfig>()))
                    {
                        setting.Set <EPGWakeupConfig>(config);
                        Log.Debug("RebootHandler: Reboot system at {0:00}:{1:00}", config.Hour, config.Minutes);
                        if (config.Days != null)
                        {
                            String days = "";
                            foreach (EPGGrabDays day in config.Days)
                            {
                                if (days == "")
                                {
                                    days = day.ToString();
                                }
                                else
                                {
                                    days = days + ", " + day.ToString();
                                }
                            }
                            Log.Debug("RebootHandler: Reboot system on: {0}", days);
                        }
                    }

                    if (args.EventType == PowerSchedulerEventType.Elapsed)
                    {
                        // Check if reboot is due
                        if (ShouldRunNow())
                        {
                            // See if system is idle
                            bool     unattended, disAllowShutdown;
                            String   disAllowShutdownHandler, nextWakeupHandler;
                            DateTime nextWakeupTime;

                            // Reboot only if all other handlers allow standby
                            ps.GetCurrentState(false, out unattended, out disAllowShutdown, out disAllowShutdownHandler,
                                               out nextWakeupTime, out nextWakeupHandler);
                            if (!disAllowShutdown)
                            {
                                // Kick off reboot thread
                                Log.Debug("RebootHandler: Reboot is due - reboot now");
                                Thread workerThread = new Thread(new ThreadStart(RebootThread));
                                workerThread.Name         = "RebootHandler";
                                workerThread.IsBackground = true;
                                workerThread.Priority     = ThreadPriority.Lowest;
                                workerThread.Start();
                            }
                            else
                            {
                                Log.Debug("RebootHandler: Reboot is due - reboot when standby is allowed");
                            }
                        }
                    }
                    break;
                }
            }
        }
Beispiel #3
0
        private void EpgGrabbingHandler_OnPowerSchedulerEvent(PowerSchedulerEventArgs args)
        {
            switch (args.EventType)
            {
            case PowerSchedulerEventType.Started:
            case PowerSchedulerEventType.Elapsed:

                IPowerScheduler ps    = GlobalServiceProvider.Instance.Get <IPowerScheduler>();
                TvBusinessLayer layer = new TvBusinessLayer();
                PowerSetting    setting;
                bool            enabled;

                // Check if standby should be prevented when grabbing EPG
                setting = ps.Settings.GetSetting("EPGPreventStandby");
                enabled = Convert.ToBoolean(layer.GetSetting("PowerSchedulerEPGPreventStandby", "false").Value);
                if (setting.Get <bool>() != enabled)
                {
                    setting.Set <bool>(enabled);
                    if (enabled)
                    {
                        if (ps.IsRegistered(this as IStandbyHandler))
                        {
                            ps.Unregister(this as IStandbyHandler);
                        }
                        ps.Register(this as IStandbyHandler);
                    }
                    else
                    {
                        ps.Unregister(this as IStandbyHandler);
                    }
                    Log.Debug("EpgGrabbingHandler: Preventing standby when grabbing EPG: {0}", enabled);
                }

                // Check if away mode should be used
                setting      = ps.Settings.GetSetting("EPGAwayMode");
                _useAwayMode = Convert.ToBoolean(layer.GetSetting("PowerSchedulerEPGAwayMode", "false").Value);
                if (setting.Get <bool>() != _useAwayMode)
                {
                    setting.Set <bool>(_useAwayMode);
                    Log.Debug("EpgGrabbingHandler: Use away mode: {0}", _useAwayMode);
                }

                // Check if system should wakeup for EPG grabs
                setting = ps.Settings.GetSetting("EPGWakeup");
                enabled = Convert.ToBoolean(layer.GetSetting("PowerSchedulerEPGWakeup", "false").Value);
                if (setting.Get <bool>() != enabled)
                {
                    setting.Set <bool>(enabled);
                    if (enabled)
                    {
                        if (ps.IsRegistered(this as IWakeupHandler))
                        {
                            ps.Unregister(this as IWakeupHandler);
                        }
                        ps.Register(this as IWakeupHandler);
                    }
                    else
                    {
                        ps.Unregister(this as IWakeupHandler);
                    }
                    Log.Debug("EpgGrabbingHandler: Wakeup system for EPG grabbing: {0}", enabled ? "enabled" : "disabled");
                }

                // Check if a wakeup time is set
                setting = ps.Settings.GetSetting("EPGWakeupConfig");
                EPGWakeupConfig config = new EPGWakeupConfig((layer.GetSetting("PowerSchedulerEPGWakeupConfig", String.Empty).Value));
                if (!config.Equals(setting.Get <EPGWakeupConfig>()))
                {
                    setting.Set <EPGWakeupConfig>(config);
                    Log.Debug("EpgGrabbingHandler: EPG grabbing at {0:00}:{1:00}", config.Hour, config.Minutes);
                    if (config.Days != null)
                    {
                        String days = "";
                        foreach (EPGGrabDays day in config.Days)
                        {
                            if (days == "")
                            {
                                days = day.ToString();
                            }
                            else
                            {
                                days = days + ", " + day.ToString();
                            }
                        }
                        Log.Debug("EpgGrabbingHandler: EPG grabbing on: {0}", days);
                    }
                    Log.Debug("EpgGrabbingHandler: EPG last run was at {0}", config.LastRun);
                }

                // check if schedule is due
                // check if we've already run today
                if (ShouldRunNow() && !_epgThreadRunning)
                {
                    // kick off EPG thread
                    _epgThreadRunning = true;
                    Thread workerThread = new Thread(new ThreadStart(EPGThreadFunction));
                    workerThread.Name         = "EPG Grabbing Handler";
                    workerThread.IsBackground = true;
                    workerThread.Priority     = ThreadPriority.Lowest;
                    workerThread.Start();
                }

                // Cleanup of expired grabber sources
                // A grabber is said to be expired, when its timeout has passed and there is no valid wakeup time
                // However, when the timeout has passed, the alow-standby flag is set true
                List <object> expired = new List <object>();
                foreach (object o in _extGrabbers.Keys)
                {
                    GrabberSource s = _extGrabbers[o];
                    if (s.Timeout < DateTime.Now)
                    {
                        Log.Debug("EpgGrabbingHandler: EPG source '{0}' timed out, setting allow-standby = true for this source.",
                                  s.Name);
                        // timeout passed, standby is allowed
                        s.SetStandbyAllowed(true, 0);

                        // no valid wakeup-time -> expired
                        if (s.NextWakeupTime == DateTime.MaxValue)
                        {
                            expired.Add(o);
                        }
                    }
                }
                foreach (object o in expired)
                {
                    _extGrabbers.Remove(o);
                }
                expired = null;

                break;
            }
        }