mirror of
https://github.com/jellyfin/jellyfin.git
synced 2024-09-06 11:34:57 +02:00
Add Argument*Exceptions now use proper nameof operators. Added exception messages to quite a few Argument*Exceptions. Fixed rethorwing to be proper syntax. Added a ton of null checkes. (This is only a start, there are about 500 places that need proper null handling) Added some TODOs to log certain exceptions. Fix sln again. Fixed all AssemblyInfo's and added proper copyright (where I could find them) We live in *current year*. Fixed the use of braces. Fixed a ton of properties, and made a fair amount of functions static that should be and can be static. Made more Methods that should be static static. You can now use static to find bad functions! Removed unused variable. And added one more proper XML comment.
171 lines
5 KiB
C#
171 lines
5 KiB
C#
using System;
|
|
using System.Collections.Concurrent;
|
|
using System.Globalization;
|
|
using System.Linq;
|
|
using MediaBrowser.Controller.LiveTv;
|
|
using MediaBrowser.Model.Events;
|
|
using Microsoft.Extensions.Logging;
|
|
using MediaBrowser.Model.Serialization;
|
|
using MediaBrowser.Model.IO;
|
|
using MediaBrowser.Model.LiveTv;
|
|
using MediaBrowser.Model.Threading;
|
|
|
|
namespace Emby.Server.Implementations.LiveTv.EmbyTV
|
|
{
|
|
public class TimerManager : ItemDataProvider<TimerInfo>
|
|
{
|
|
private readonly ConcurrentDictionary<string, ITimer> _timers = new ConcurrentDictionary<string, ITimer>(StringComparer.OrdinalIgnoreCase);
|
|
private readonly ILogger _logger;
|
|
|
|
public event EventHandler<GenericEventArgs<TimerInfo>> TimerFired;
|
|
private readonly ITimerFactory _timerFactory;
|
|
|
|
public TimerManager(IFileSystem fileSystem, IJsonSerializer jsonSerializer, ILogger logger, string dataPath, ILogger logger1, ITimerFactory timerFactory)
|
|
: base(fileSystem, jsonSerializer, logger, dataPath, (r1, r2) => string.Equals(r1.Id, r2.Id, StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
_logger = logger1;
|
|
_timerFactory = timerFactory;
|
|
}
|
|
|
|
public void RestartTimers()
|
|
{
|
|
StopTimers();
|
|
|
|
foreach (var item in GetAll().ToList())
|
|
{
|
|
AddOrUpdateSystemTimer(item);
|
|
}
|
|
}
|
|
|
|
public void StopTimers()
|
|
{
|
|
foreach (var pair in _timers.ToList())
|
|
{
|
|
pair.Value.Dispose();
|
|
}
|
|
|
|
_timers.Clear();
|
|
}
|
|
|
|
public override void Delete(TimerInfo item)
|
|
{
|
|
base.Delete(item);
|
|
StopTimer(item);
|
|
}
|
|
|
|
public override void Update(TimerInfo item)
|
|
{
|
|
base.Update(item);
|
|
AddOrUpdateSystemTimer(item);
|
|
}
|
|
|
|
public void AddOrUpdate(TimerInfo item, bool resetTimer)
|
|
{
|
|
if (resetTimer)
|
|
{
|
|
AddOrUpdate(item);
|
|
return;
|
|
}
|
|
|
|
var list = GetAll().ToList();
|
|
|
|
if (!list.Any(i => EqualityComparer(i, item)))
|
|
{
|
|
base.Add(item);
|
|
}
|
|
else
|
|
{
|
|
base.Update(item);
|
|
}
|
|
}
|
|
|
|
public override void Add(TimerInfo item)
|
|
{
|
|
if (string.IsNullOrEmpty(item.Id))
|
|
{
|
|
throw new ArgumentException("TimerInfo.Id cannot be null or empty.");
|
|
}
|
|
|
|
base.Add(item);
|
|
AddOrUpdateSystemTimer(item);
|
|
}
|
|
|
|
private static bool ShouldStartTimer(TimerInfo item)
|
|
{
|
|
if (item.Status == RecordingStatus.Completed ||
|
|
item.Status == RecordingStatus.Cancelled)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
private void AddOrUpdateSystemTimer(TimerInfo item)
|
|
{
|
|
StopTimer(item);
|
|
|
|
if (!ShouldStartTimer(item))
|
|
{
|
|
return;
|
|
}
|
|
|
|
var startDate = RecordingHelper.GetStartTime(item);
|
|
var now = DateTime.UtcNow;
|
|
|
|
if (startDate < now)
|
|
{
|
|
TimerFired?.Invoke(this, new GenericEventArgs<TimerInfo> { Argument = item });
|
|
return;
|
|
}
|
|
|
|
var dueTime = startDate - now;
|
|
StartTimer(item, dueTime);
|
|
}
|
|
|
|
private void StartTimer(TimerInfo item, TimeSpan dueTime)
|
|
{
|
|
var timer = _timerFactory.Create(TimerCallback, item.Id, dueTime, TimeSpan.Zero);
|
|
|
|
if (_timers.TryAdd(item.Id, timer))
|
|
{
|
|
_logger.LogInformation("Creating recording timer for {id}, {name}. Timer will fire in {minutes} minutes", item.Id, item.Name, dueTime.TotalMinutes.ToString(CultureInfo.InvariantCulture));
|
|
}
|
|
else
|
|
{
|
|
timer.Dispose();
|
|
_logger.LogWarning("Timer already exists for item {id}", item.Id);
|
|
}
|
|
}
|
|
|
|
private void StopTimer(TimerInfo item)
|
|
{
|
|
ITimer timer;
|
|
if (_timers.TryRemove(item.Id, out timer))
|
|
{
|
|
timer.Dispose();
|
|
}
|
|
}
|
|
|
|
private void TimerCallback(object state)
|
|
{
|
|
var timerId = (string)state;
|
|
|
|
var timer = GetAll().FirstOrDefault(i => string.Equals(i.Id, timerId, StringComparison.OrdinalIgnoreCase));
|
|
if (timer != null)
|
|
{
|
|
TimerFired?.Invoke(this, new GenericEventArgs<TimerInfo> { Argument = timer });
|
|
}
|
|
}
|
|
|
|
public TimerInfo GetTimer(string id)
|
|
{
|
|
return GetAll().FirstOrDefault(r => string.Equals(r.Id, id, StringComparison.OrdinalIgnoreCase));
|
|
}
|
|
|
|
public TimerInfo GetTimerByProgramId(string programId)
|
|
{
|
|
return GetAll().FirstOrDefault(r => string.Equals(r.ProgramId, programId, StringComparison.OrdinalIgnoreCase));
|
|
}
|
|
}
|
|
}
|