TimerManager.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. using MediaBrowser.Common.Events;
  2. using MediaBrowser.Controller.LiveTv;
  3. using MediaBrowser.Model.Events;
  4. using MediaBrowser.Model.Logging;
  5. using MediaBrowser.Model.Serialization;
  6. using System;
  7. using System.Collections.Concurrent;
  8. using System.Globalization;
  9. using System.Linq;
  10. using System.Threading;
  11. using CommonIO;
  12. using MediaBrowser.Controller.Power;
  13. using MediaBrowser.Model.LiveTv;
  14. namespace MediaBrowser.Server.Implementations.LiveTv.EmbyTV
  15. {
  16. public class TimerManager : ItemDataProvider<TimerInfo>
  17. {
  18. private readonly ConcurrentDictionary<string, Timer> _timers = new ConcurrentDictionary<string, Timer>(StringComparer.OrdinalIgnoreCase);
  19. private readonly IPowerManagement _powerManagement;
  20. private readonly ILogger _logger;
  21. public event EventHandler<GenericEventArgs<TimerInfo>> TimerFired;
  22. public TimerManager(IFileSystem fileSystem, IJsonSerializer jsonSerializer, ILogger logger, string dataPath, IPowerManagement powerManagement, ILogger logger1)
  23. : base(fileSystem, jsonSerializer, logger, dataPath, (r1, r2) => string.Equals(r1.Id, r2.Id, StringComparison.OrdinalIgnoreCase))
  24. {
  25. _powerManagement = powerManagement;
  26. _logger = logger1;
  27. }
  28. public void RestartTimers()
  29. {
  30. StopTimers();
  31. foreach (var item in GetAll().ToList())
  32. {
  33. AddTimer(item);
  34. }
  35. }
  36. public void StopTimers()
  37. {
  38. foreach (var pair in _timers.ToList())
  39. {
  40. pair.Value.Dispose();
  41. }
  42. _timers.Clear();
  43. }
  44. public override void Delete(TimerInfo item)
  45. {
  46. base.Delete(item);
  47. StopTimer(item);
  48. }
  49. public override void Update(TimerInfo item)
  50. {
  51. base.Update(item);
  52. Timer timer;
  53. if (_timers.TryGetValue(item.Id, out timer))
  54. {
  55. var timespan = RecordingHelper.GetStartTime(item) - DateTime.UtcNow;
  56. timer.Change(timespan, TimeSpan.Zero);
  57. ScheduleWake(item);
  58. }
  59. else
  60. {
  61. AddTimer(item);
  62. }
  63. }
  64. public void AddOrUpdate(TimerInfo item, bool resetTimer)
  65. {
  66. if (resetTimer)
  67. {
  68. AddOrUpdate(item);
  69. return;
  70. }
  71. var list = GetAll().ToList();
  72. if (!list.Any(i => EqualityComparer(i, item)))
  73. {
  74. base.Add(item);
  75. }
  76. else
  77. {
  78. base.Update(item);
  79. }
  80. }
  81. public override void Add(TimerInfo item)
  82. {
  83. if (string.IsNullOrWhiteSpace(item.Id))
  84. {
  85. throw new ArgumentException("TimerInfo.Id cannot be null or empty.");
  86. }
  87. base.Add(item);
  88. AddTimer(item);
  89. ScheduleWake(item);
  90. }
  91. private void AddTimer(TimerInfo item)
  92. {
  93. if (item.Status == RecordingStatus.Completed)
  94. {
  95. return;
  96. }
  97. var startDate = RecordingHelper.GetStartTime(item);
  98. var now = DateTime.UtcNow;
  99. if (startDate < now)
  100. {
  101. EventHelper.FireEventIfNotNull(TimerFired, this, new GenericEventArgs<TimerInfo> { Argument = item }, Logger);
  102. return;
  103. }
  104. var timerLength = startDate - now;
  105. StartTimer(item, timerLength);
  106. }
  107. private void ScheduleWake(TimerInfo info)
  108. {
  109. var startDate = RecordingHelper.GetStartTime(info).AddMinutes(-5);
  110. try
  111. {
  112. _powerManagement.ScheduleWake(startDate);
  113. _logger.Info("Scheduled system wake timer at {0} (UTC)", startDate);
  114. }
  115. catch (NotImplementedException)
  116. {
  117. }
  118. catch (Exception ex)
  119. {
  120. _logger.ErrorException("Error scheduling wake timer", ex);
  121. }
  122. }
  123. public void StartTimer(TimerInfo item, TimeSpan dueTime)
  124. {
  125. StopTimer(item);
  126. var timer = new Timer(TimerCallback, item.Id, dueTime, TimeSpan.Zero);
  127. if (_timers.TryAdd(item.Id, timer))
  128. {
  129. _logger.Info("Creating recording timer for {0}, {1}. Timer will fire in {2} minutes", item.Id, item.Name, dueTime.TotalMinutes.ToString(CultureInfo.InvariantCulture));
  130. }
  131. else
  132. {
  133. timer.Dispose();
  134. _logger.Warn("Timer already exists for item {0}", item.Id);
  135. }
  136. }
  137. private void StopTimer(TimerInfo item)
  138. {
  139. Timer timer;
  140. if (_timers.TryRemove(item.Id, out timer))
  141. {
  142. timer.Dispose();
  143. }
  144. }
  145. private void TimerCallback(object state)
  146. {
  147. var timerId = (string)state;
  148. var timer = GetAll().FirstOrDefault(i => string.Equals(i.Id, timerId, StringComparison.OrdinalIgnoreCase));
  149. if (timer != null)
  150. {
  151. EventHelper.FireEventIfNotNull(TimerFired, this, new GenericEventArgs<TimerInfo> { Argument = timer }, Logger);
  152. }
  153. }
  154. }
  155. }