KeepServerAwake.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. using MediaBrowser.Controller;
  2. using MediaBrowser.Controller.Plugins;
  3. using MediaBrowser.Controller.Session;
  4. using MediaBrowser.Model.Logging;
  5. using System;
  6. using System.Linq;
  7. using MediaBrowser.Model.System;
  8. using MediaBrowser.Model.Threading;
  9. namespace Emby.Server.Implementations.EntryPoints
  10. {
  11. public class KeepServerAwake : IServerEntryPoint
  12. {
  13. private readonly ISessionManager _sessionManager;
  14. private readonly ILogger _logger;
  15. private ITimer _timer;
  16. private readonly IServerApplicationHost _appHost;
  17. private readonly ITimerFactory _timerFactory;
  18. private readonly IPowerManagement _powerManagement;
  19. public KeepServerAwake(ISessionManager sessionManager, ILogger logger, IServerApplicationHost appHost, ITimerFactory timerFactory, IPowerManagement powerManagement)
  20. {
  21. _sessionManager = sessionManager;
  22. _logger = logger;
  23. _appHost = appHost;
  24. _timerFactory = timerFactory;
  25. _powerManagement = powerManagement;
  26. }
  27. public void Run()
  28. {
  29. _timer = _timerFactory.Create(OnTimerCallback, null, TimeSpan.FromMinutes(5), TimeSpan.FromMinutes(5));
  30. }
  31. private void OnTimerCallback(object state)
  32. {
  33. var now = DateTime.UtcNow;
  34. try
  35. {
  36. if (_sessionManager.Sessions.Any(i => (now - i.LastActivityDate).TotalMinutes < 15))
  37. {
  38. _powerManagement.PreventSystemStandby();
  39. }
  40. else
  41. {
  42. _powerManagement.AllowSystemStandby();
  43. }
  44. }
  45. catch (Exception ex)
  46. {
  47. _logger.ErrorException("Error resetting system standby timer", ex);
  48. }
  49. }
  50. public void Dispose()
  51. {
  52. if (_timer != null)
  53. {
  54. _timer.Dispose();
  55. _timer = null;
  56. }
  57. }
  58. }
  59. }