using MediaBrowser.Model.Events;
using MediaBrowser.Model.Logging;
using MediaBrowser.Model.Tasks;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using MediaBrowser.Model.Threading;
namespace MediaBrowser.Api.ScheduledTasks
{
///
/// Class ScheduledTasksWebSocketListener
///
public class ScheduledTasksWebSocketListener : BasePeriodicWebSocketListener, WebSocketListenerState>
{
///
/// Gets or sets the task manager.
///
/// The task manager.
private ITaskManager TaskManager { get; set; }
///
/// Gets the name.
///
/// The name.
protected override string Name
{
get { return "ScheduledTasksInfo"; }
}
///
/// Initializes a new instance of the class.
///
public ScheduledTasksWebSocketListener(ILogger logger, ITaskManager taskManager, ITimerFactory timerFactory)
: base(logger, timerFactory)
{
TaskManager = taskManager;
TaskManager.TaskExecuting += TaskManager_TaskExecuting;
TaskManager.TaskCompleted += TaskManager_TaskCompleted;
}
void TaskManager_TaskCompleted(object sender, TaskCompletionEventArgs e)
{
SendData(true);
e.Task.TaskProgress -= Argument_TaskProgress;
}
void TaskManager_TaskExecuting(object sender, GenericEventArgs e)
{
SendData(true);
e.Argument.TaskProgress += Argument_TaskProgress;
}
void Argument_TaskProgress(object sender, GenericEventArgs e)
{
SendData(false);
}
///
/// Gets the data to send.
///
/// The state.
/// Task{IEnumerable{TaskInfo}}.
protected override Task> GetDataToSend(WebSocketListenerState state)
{
return Task.FromResult(TaskManager.ScheduledTasks
.OrderBy(i => i.Name)
.Select(ScheduledTaskHelpers.GetTaskInfo)
.Where(i => !i.IsHidden));
}
protected override bool SendOnTimer
{
get
{
return false;
}
}
protected override void Dispose(bool dispose)
{
TaskManager.TaskExecuting -= TaskManager_TaskExecuting;
TaskManager.TaskCompleted -= TaskManager_TaskCompleted;
base.Dispose(dispose);
}
}
}