2015-07-20 11:32:55 -07:00
using MediaBrowser.Common.Events ;
using MediaBrowser.Controller.LiveTv ;
using MediaBrowser.Model.Events ;
using MediaBrowser.Model.Logging ;
using MediaBrowser.Model.Serialization ;
using System ;
using System.Collections.Concurrent ;
2016-01-28 12:10:56 -07:00
using System.Globalization ;
2015-07-20 11:32:55 -07:00
using System.Linq ;
using System.Threading ;
2016-10-25 12:02:04 -07:00
using MediaBrowser.Common.IO ;
using MediaBrowser.Controller.IO ;
using MediaBrowser.Model.IO ;
2016-06-20 15:07:18 -07:00
using MediaBrowser.Model.LiveTv ;
2015-07-20 11:32:55 -07:00
namespace MediaBrowser.Server.Implementations.LiveTv.EmbyTV
{
public class TimerManager : ItemDataProvider < TimerInfo >
{
private readonly ConcurrentDictionary < string , Timer > _timers = new ConcurrentDictionary < string , Timer > ( StringComparer . OrdinalIgnoreCase ) ;
2016-01-21 10:29:14 -07:00
private readonly ILogger _logger ;
2015-07-20 11:32:55 -07:00
public event EventHandler < GenericEventArgs < TimerInfo > > TimerFired ;
2016-09-12 14:24:05 -07:00
public TimerManager ( IFileSystem fileSystem , IJsonSerializer jsonSerializer , ILogger logger , string dataPath , ILogger logger1 )
2015-09-13 16:07:54 -07:00
: base ( fileSystem , jsonSerializer , logger , dataPath , ( r1 , r2 ) = > string . Equals ( r1 . Id , r2 . Id , StringComparison . OrdinalIgnoreCase ) )
2015-07-20 11:32:55 -07:00
{
2016-01-21 10:29:14 -07:00
_logger = logger1 ;
2015-07-20 11:32:55 -07:00
}
public void RestartTimers ( )
{
StopTimers ( ) ;
2015-07-28 20:42:03 -07:00
foreach ( var item in GetAll ( ) . ToList ( ) )
{
2016-09-26 11:59:18 -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 ) ;
2016-09-26 11:59:18 -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 ;
}
var list = GetAll ( ) . ToList ( ) ;
if ( ! list . Any ( i = > EqualityComparer ( i , item ) ) )
{
base . Add ( item ) ;
}
else
{
base . Update ( item ) ;
}
}
2015-07-20 11:32:55 -07:00
public override void Add ( TimerInfo item )
{
if ( string . IsNullOrWhiteSpace ( item . Id ) )
{
throw new ArgumentException ( "TimerInfo.Id cannot be null or empty." ) ;
}
base . Add ( item ) ;
2016-09-26 11:59:18 -07:00
AddOrUpdateSystemTimer ( item ) ;
}
private bool ShouldStartTimer ( TimerInfo item )
{
if ( item . Status = = RecordingStatus . Completed | |
item . Status = = RecordingStatus . Cancelled )
{
return false ;
}
return true ;
2015-07-20 11:32:55 -07:00
}
2016-09-26 11:59:18 -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 )
{
EventHelper . FireEventIfNotNull ( TimerFired , this , new GenericEventArgs < TimerInfo > { Argument = item } , Logger ) ;
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
{
2016-06-20 15:07:18 -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 ) )
{
2016-06-20 15:07:18 -07:00
_logger . Info ( "Creating recording timer for {0}, {1}. Timer will fire in {2} 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 ( ) ;
2016-01-28 12:10:56 -07:00
_logger . Warn ( "Timer already exists for item {0}" , item . Id ) ;
2015-07-20 11:32:55 -07:00
}
}
2015-08-22 11:29:12 -07:00
private void StopTimer ( TimerInfo item )
{
Timer timer ;
if ( _timers . TryRemove ( item . Id , out timer ) )
{
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 )
{
EventHelper . FireEventIfNotNull ( TimerFired , this , new GenericEventArgs < TimerInfo > { Argument = timer } , Logger ) ;
}
}
2016-09-26 22:13:56 -07:00
public TimerInfo GetTimer ( string id )
{
return GetAll ( ) . FirstOrDefault ( r = > string . Equals ( r . Id , id , StringComparison . OrdinalIgnoreCase ) ) ;
}
2015-07-20 11:32:55 -07:00
}
}