2020-02-06 07:20:23 -07:00
|
|
|
#pragma warning disable CS1591
|
|
|
|
|
2019-01-06 13:50:43 -07:00
|
|
|
using System;
|
2018-12-28 07:30:53 -07:00
|
|
|
using System.Collections.Concurrent;
|
|
|
|
using System.Globalization;
|
|
|
|
using System.Linq;
|
2019-02-05 01:49:46 -07:00
|
|
|
using System.Threading;
|
2015-07-20 11:32:55 -07:00
|
|
|
using MediaBrowser.Controller.LiveTv;
|
|
|
|
using MediaBrowser.Model.Events;
|
2016-06-20 15:07:18 -07:00
|
|
|
using MediaBrowser.Model.LiveTv;
|
2019-01-13 12:22:00 -07:00
|
|
|
using MediaBrowser.Model.Serialization;
|
|
|
|
using Microsoft.Extensions.Logging;
|
2015-07-20 11:32:55 -07:00
|
|
|
|
2016-11-03 16:35:19 -07:00
|
|
|
namespace Emby.Server.Implementations.LiveTv.EmbyTV
|
2015-07-20 11:32:55 -07:00
|
|
|
{
|
|
|
|
public class TimerManager : ItemDataProvider<TimerInfo>
|
|
|
|
{
|
2019-02-05 01:49:46 -07:00
|
|
|
private readonly ConcurrentDictionary<string, Timer> _timers = new ConcurrentDictionary<string, Timer>(StringComparer.OrdinalIgnoreCase);
|
2015-07-20 11:32:55 -07:00
|
|
|
|
2019-09-12 12:30:57 -07:00
|
|
|
public TimerManager(IJsonSerializer jsonSerializer, ILogger logger, string dataPath)
|
2019-02-06 12:38:42 -07:00
|
|
|
: base(jsonSerializer, logger, dataPath, (r1, r2) => string.Equals(r1.Id, r2.Id, StringComparison.OrdinalIgnoreCase))
|
2015-07-20 11:32:55 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-09-12 12:30:57 -07:00
|
|
|
public event EventHandler<GenericEventArgs<TimerInfo>> TimerFired;
|
|
|
|
|
2015-07-20 11:32:55 -07:00
|
|
|
public void RestartTimers()
|
|
|
|
{
|
|
|
|
StopTimers();
|
2015-07-28 20:42:03 -07:00
|
|
|
|
2019-09-12 12:30:57 -07:00
|
|
|
foreach (var item in GetAll())
|
2015-07-28 20:42:03 -07:00
|
|
|
{
|
2019-01-02 07:57:48 -07:00
|
|
|
AddOrUpdateSystemTimer(item);
|
2015-07-28 20:42:03 -07:00
|
|
|
}
|
2015-07-20 11:32:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public void StopTimers()
|
|
|
|
{
|
|
|
|
foreach (var pair in _timers.ToList())
|
|
|
|
{
|
|
|
|
pair.Value.Dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
_timers.Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
public override void Delete(TimerInfo item)
|
|
|
|
{
|
|
|
|
base.Delete(item);
|
2015-08-22 11:29:12 -07:00
|
|
|
StopTimer(item);
|
2015-07-20 11:32:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public override void Update(TimerInfo item)
|
|
|
|
{
|
|
|
|
base.Update(item);
|
2019-01-02 07:57:48 -07:00
|
|
|
AddOrUpdateSystemTimer(item);
|
2015-07-20 11:32:55 -07:00
|
|
|
}
|
|
|
|
|
2016-06-22 22:26:34 -07:00
|
|
|
public void AddOrUpdate(TimerInfo item, bool resetTimer)
|
|
|
|
{
|
|
|
|
if (resetTimer)
|
|
|
|
{
|
|
|
|
AddOrUpdate(item);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-09-12 12:30:57 -07:00
|
|
|
base.AddOrUpdate(item);
|
|
|
|
}
|
2016-06-22 22:26:34 -07:00
|
|
|
|
2019-09-12 12:30:57 -07:00
|
|
|
public override void AddOrUpdate(TimerInfo item)
|
|
|
|
{
|
|
|
|
base.AddOrUpdate(item);
|
|
|
|
AddOrUpdateSystemTimer(item);
|
2016-06-22 22:26:34 -07:00
|
|
|
}
|
|
|
|
|
2015-07-20 11:32:55 -07:00
|
|
|
public override void Add(TimerInfo item)
|
|
|
|
{
|
2018-09-12 10:26:21 -07:00
|
|
|
if (string.IsNullOrEmpty(item.Id))
|
2015-07-20 11:32:55 -07:00
|
|
|
{
|
|
|
|
throw new ArgumentException("TimerInfo.Id cannot be null or empty.");
|
|
|
|
}
|
|
|
|
|
|
|
|
base.Add(item);
|
2019-01-02 07:57:48 -07:00
|
|
|
AddOrUpdateSystemTimer(item);
|
2016-09-26 11:59:18 -07:00
|
|
|
}
|
|
|
|
|
2019-01-06 13:50:43 -07:00
|
|
|
private static bool ShouldStartTimer(TimerInfo item)
|
2016-09-26 11:59:18 -07:00
|
|
|
{
|
2019-09-12 12:30:57 -07:00
|
|
|
if (item.Status == RecordingStatus.Completed
|
|
|
|
|| item.Status == RecordingStatus.Cancelled)
|
2016-09-26 11:59:18 -07:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2015-07-20 11:32:55 -07:00
|
|
|
}
|
|
|
|
|
2019-01-02 07:57:48 -07:00
|
|
|
private void AddOrUpdateSystemTimer(TimerInfo item)
|
2015-07-20 11:32:55 -07:00
|
|
|
{
|
2016-09-26 11:59:18 -07:00
|
|
|
StopTimer(item);
|
|
|
|
|
|
|
|
if (!ShouldStartTimer(item))
|
2016-06-20 15:07:18 -07:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-07-28 20:42:03 -07:00
|
|
|
var startDate = RecordingHelper.GetStartTime(item);
|
|
|
|
var now = DateTime.UtcNow;
|
|
|
|
|
|
|
|
if (startDate < now)
|
|
|
|
{
|
2020-04-05 09:10:56 -07:00
|
|
|
TimerFired?.Invoke(this, new GenericEventArgs<TimerInfo>(item));
|
2015-07-28 20:42:03 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-09-26 11:59:18 -07:00
|
|
|
var dueTime = startDate - now;
|
|
|
|
StartTimer(item, dueTime);
|
2015-08-22 11:29:12 -07:00
|
|
|
}
|
2015-07-20 11:32:55 -07:00
|
|
|
|
2016-09-26 11:59:18 -07:00
|
|
|
private void StartTimer(TimerInfo item, TimeSpan dueTime)
|
2015-08-22 11:29:12 -07:00
|
|
|
{
|
2019-02-05 01:49:46 -07:00
|
|
|
var timer = new Timer(TimerCallback, item.Id, dueTime, TimeSpan.Zero);
|
2015-07-20 11:32:55 -07:00
|
|
|
|
2016-01-28 12:10:56 -07:00
|
|
|
if (_timers.TryAdd(item.Id, timer))
|
|
|
|
{
|
2019-09-12 12:30:57 -07:00
|
|
|
Logger.LogInformation(
|
|
|
|
"Creating recording timer for {Id}, {Name}. Timer will fire in {Minutes} minutes",
|
|
|
|
item.Id,
|
|
|
|
item.Name,
|
|
|
|
dueTime.TotalMinutes.ToString(CultureInfo.InvariantCulture));
|
2016-01-28 12:10:56 -07:00
|
|
|
}
|
|
|
|
else
|
2015-07-20 11:32:55 -07:00
|
|
|
{
|
|
|
|
timer.Dispose();
|
2019-09-12 12:30:57 -07:00
|
|
|
Logger.LogWarning("Timer already exists for item {Id}", item.Id);
|
2015-07-20 11:32:55 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-22 11:29:12 -07:00
|
|
|
private void StopTimer(TimerInfo item)
|
|
|
|
{
|
2019-01-13 13:46:33 -07:00
|
|
|
if (_timers.TryRemove(item.Id, out var timer))
|
2015-08-22 11:29:12 -07:00
|
|
|
{
|
|
|
|
timer.Dispose();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-20 11:32:55 -07:00
|
|
|
private void TimerCallback(object state)
|
|
|
|
{
|
|
|
|
var timerId = (string)state;
|
|
|
|
|
|
|
|
var timer = GetAll().FirstOrDefault(i => string.Equals(i.Id, timerId, StringComparison.OrdinalIgnoreCase));
|
|
|
|
if (timer != null)
|
|
|
|
{
|
2020-04-05 09:10:56 -07:00
|
|
|
TimerFired?.Invoke(this, new GenericEventArgs<TimerInfo>(timer));
|
2015-07-20 11:32:55 -07:00
|
|
|
}
|
|
|
|
}
|
2016-09-26 22:13:56 -07:00
|
|
|
|
|
|
|
public TimerInfo GetTimer(string id)
|
|
|
|
{
|
|
|
|
return GetAll().FirstOrDefault(r => string.Equals(r.Id, id, StringComparison.OrdinalIgnoreCase));
|
|
|
|
}
|
2017-03-25 21:21:32 -07:00
|
|
|
|
|
|
|
public TimerInfo GetTimerByProgramId(string programId)
|
|
|
|
{
|
|
|
|
return GetAll().FirstOrDefault(r => string.Equals(r.ProgramId, programId, StringComparison.OrdinalIgnoreCase));
|
|
|
|
}
|
2015-07-20 11:32:55 -07:00
|
|
|
}
|
|
|
|
}
|