using MediaBrowser.Common.Events;
using MediaBrowser.Common.Net;
using MediaBrowser.Common.Plugins;
using MediaBrowser.Common.ScheduledTasks;
using MediaBrowser.Controller;
using MediaBrowser.Controller.Dto;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Plugins;
using MediaBrowser.Controller.Updates;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Logging;
using MediaBrowser.Model.Tasks;
using MediaBrowser.Model.Updates;
using System;
using System.Linq;
using System.Threading;
namespace MediaBrowser.ServerApplication.EntryPoints
{
///
/// Class WebSocketEvents
///
public class WebSocketEvents : IServerEntryPoint
{
///
/// The _server manager
///
private readonly IServerManager _serverManager;
///
/// The _logger
///
private readonly ILogger _logger;
///
/// The _user manager
///
private readonly IUserManager _userManager;
///
/// The _library manager
///
private readonly ILibraryManager _libraryManager;
///
/// The _installation manager
///
private readonly IInstallationManager _installationManager;
///
/// The _kernel
///
private readonly IServerApplicationHost _appHost;
///
/// The _task manager
///
private readonly ITaskManager _taskManager;
///
/// The _library changed sync lock
///
private readonly object _libraryChangedSyncLock = new object();
///
/// Gets or sets the library update info.
///
/// The library update info.
private LibraryUpdateInfo LibraryUpdateInfo { get; set; }
///
/// Gets or sets the library update timer.
///
/// The library update timer.
private Timer LibraryUpdateTimer { get; set; }
///
/// The library update duration
///
private const int LibraryUpdateDuration = 60000;
///
/// Initializes a new instance of the class.
///
/// The server manager.
/// The logger.
/// The user manager.
public WebSocketEvents(IServerManager serverManager, IServerApplicationHost appHost, ILogger logger, IUserManager userManager, ILibraryManager libraryManager, IInstallationManager installationManager, ITaskManager taskManager)
{
_serverManager = serverManager;
_logger = logger;
_userManager = userManager;
_libraryManager = libraryManager;
_installationManager = installationManager;
_appHost = appHost;
_taskManager = taskManager;
}
public void Run()
{
_userManager.UserDeleted += userManager_UserDeleted;
_userManager.UserUpdated += userManager_UserUpdated;
_appHost.HasPendingRestartChanged += kernel_HasPendingRestartChanged;
_libraryManager.ItemAdded += libraryManager_ItemAdded;
_libraryManager.ItemUpdated += libraryManager_ItemUpdated;
_libraryManager.ItemRemoved += libraryManager_ItemRemoved;
_installationManager.PluginUninstalled += InstallationManager_PluginUninstalled;
_installationManager.PackageInstalling += installationManager_PackageInstalling;
_installationManager.PackageInstallationCancelled += installationManager_PackageInstallationCancelled;
_installationManager.PackageInstallationCompleted += installationManager_PackageInstallationCompleted;
_installationManager.PackageInstallationFailed += installationManager_PackageInstallationFailed;
_taskManager.TaskExecuting += _taskManager_TaskExecuting;
_taskManager.TaskCompleted += _taskManager_TaskCompleted;
}
void _taskManager_TaskCompleted(object sender, GenericEventArgs e)
{
_serverManager.SendWebSocketMessage("ScheduledTaskEnded", e.Argument);
}
void _taskManager_TaskExecuting(object sender, EventArgs e)
{
var task = (IScheduledTask)sender;
_serverManager.SendWebSocketMessage("ScheduledTaskStarted", task.Name);
}
///
/// Installations the manager_ package installation failed.
///
/// The sender.
/// The e.
void installationManager_PackageInstallationFailed(object sender, GenericEventArgs e)
{
_serverManager.SendWebSocketMessage("PackageInstallationFailed", e.Argument);
}
///
/// Installations the manager_ package installation completed.
///
/// The sender.
/// The e.
void installationManager_PackageInstallationCompleted(object sender, GenericEventArgs e)
{
_serverManager.SendWebSocketMessage("PackageInstallationCompleted", e.Argument);
}
///
/// Installations the manager_ package installation cancelled.
///
/// The sender.
/// The e.
void installationManager_PackageInstallationCancelled(object sender, GenericEventArgs e)
{
_serverManager.SendWebSocketMessage("PackageInstallationCancelled", e.Argument);
}
///
/// Installations the manager_ package installing.
///
/// The sender.
/// The e.
void installationManager_PackageInstalling(object sender, GenericEventArgs e)
{
_serverManager.SendWebSocketMessage("PackageInstalling", e.Argument);
}
///
/// Handles the ItemAdded event of the libraryManager control.
///
/// The source of the event.
/// The instance containing the event data.
void libraryManager_ItemAdded(object sender, ItemChangeEventArgs e)
{
lock (_libraryChangedSyncLock)
{
if (LibraryUpdateInfo == null)
{
LibraryUpdateInfo = new LibraryUpdateInfo();
}
if (LibraryUpdateTimer == null)
{
LibraryUpdateTimer = new Timer(LibraryUpdateTimerCallback, null, LibraryUpdateDuration,
Timeout.Infinite);
}
else
{
LibraryUpdateTimer.Change(LibraryUpdateDuration, Timeout.Infinite);
}
if (e.Item.Parent != null)
{
LibraryUpdateInfo.FoldersAddedTo.Add(e.Item.Parent.Id);
}
LibraryUpdateInfo.ItemsAdded.Add(e.Item.Id);
}
}
///
/// Handles the ItemUpdated event of the libraryManager control.
///
/// The source of the event.
/// The instance containing the event data.
void libraryManager_ItemUpdated(object sender, ItemChangeEventArgs e)
{
lock (_libraryChangedSyncLock)
{
if (LibraryUpdateInfo == null)
{
LibraryUpdateInfo = new LibraryUpdateInfo();
}
if (LibraryUpdateTimer == null)
{
LibraryUpdateTimer = new Timer(LibraryUpdateTimerCallback, null, LibraryUpdateDuration,
Timeout.Infinite);
}
else
{
LibraryUpdateTimer.Change(LibraryUpdateDuration, Timeout.Infinite);
}
LibraryUpdateInfo.ItemsUpdated.Add(e.Item.Id);
}
}
///
/// Handles the ItemRemoved event of the libraryManager control.
///
/// The source of the event.
/// The instance containing the event data.
void libraryManager_ItemRemoved(object sender, ItemChangeEventArgs e)
{
lock (_libraryChangedSyncLock)
{
if (LibraryUpdateInfo == null)
{
LibraryUpdateInfo = new LibraryUpdateInfo();
}
if (LibraryUpdateTimer == null)
{
LibraryUpdateTimer = new Timer(LibraryUpdateTimerCallback, null, LibraryUpdateDuration,
Timeout.Infinite);
}
else
{
LibraryUpdateTimer.Change(LibraryUpdateDuration, Timeout.Infinite);
}
if (e.Item.Parent != null)
{
LibraryUpdateInfo.FoldersRemovedFrom.Add(e.Item.Parent.Id);
}
LibraryUpdateInfo.ItemsRemoved.Add(e.Item.Id);
}
}
///
/// Libraries the update timer callback.
///
/// The state.
private void LibraryUpdateTimerCallback(object state)
{
lock (_libraryChangedSyncLock)
{
// Remove dupes in case some were saved multiple times
LibraryUpdateInfo.FoldersAddedTo = LibraryUpdateInfo.FoldersAddedTo.Distinct().ToList();
LibraryUpdateInfo.FoldersRemovedFrom = LibraryUpdateInfo.FoldersRemovedFrom.Distinct().ToList();
LibraryUpdateInfo.ItemsUpdated = LibraryUpdateInfo.ItemsUpdated.Distinct().ToList();
_serverManager.SendWebSocketMessage("LibraryChanged", LibraryUpdateInfo);
if (LibraryUpdateTimer != null)
{
LibraryUpdateTimer.Dispose();
LibraryUpdateTimer = null;
}
LibraryUpdateInfo = null;
}
}
///
/// Installations the manager_ plugin uninstalled.
///
/// The sender.
/// The e.
void InstallationManager_PluginUninstalled(object sender, GenericEventArgs e)
{
_serverManager.SendWebSocketMessage("PluginUninstalled", e.Argument.GetPluginInfo());
}
///
/// Handles the HasPendingRestartChanged event of the kernel control.
///
/// The source of the event.
/// The instance containing the event data.
void kernel_HasPendingRestartChanged(object sender, EventArgs e)
{
_serverManager.SendWebSocketMessage("RestartRequired", _appHost.GetSystemInfo());
}
///
/// Users the manager_ user updated.
///
/// The sender.
/// The e.
async void userManager_UserUpdated(object sender, GenericEventArgs e)
{
var dto = await new UserDtoBuilder(_logger).GetUserDto(e.Argument).ConfigureAwait(false);
_serverManager.SendWebSocketMessage("UserUpdated", dto);
}
///
/// Users the manager_ user deleted.
///
/// The sender.
/// The e.
void userManager_UserDeleted(object sender, GenericEventArgs e)
{
_serverManager.SendWebSocketMessage("UserDeleted", e.Argument.Id.ToString());
}
///
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
///
public void Dispose()
{
Dispose(true);
}
///
/// Releases unmanaged and - optionally - managed resources.
///
/// true to release both managed and unmanaged resources; false to release only unmanaged resources.
protected virtual void Dispose(bool dispose)
{
if (dispose)
{
if (LibraryUpdateTimer != null)
{
LibraryUpdateTimer.Dispose();
LibraryUpdateTimer = null;
}
_libraryManager.ItemAdded -= libraryManager_ItemAdded;
_libraryManager.ItemUpdated -= libraryManager_ItemUpdated;
_libraryManager.ItemRemoved -= libraryManager_ItemRemoved;
_userManager.UserDeleted -= userManager_UserDeleted;
_userManager.UserUpdated -= userManager_UserUpdated;
_installationManager.PluginUninstalled -= InstallationManager_PluginUninstalled;
_installationManager.PackageInstalling -= installationManager_PackageInstalling;
_installationManager.PackageInstallationCancelled -= installationManager_PackageInstallationCancelled;
_installationManager.PackageInstallationCompleted -= installationManager_PackageInstallationCompleted;
_installationManager.PackageInstallationFailed -= installationManager_PackageInstallationFailed;
_appHost.HasPendingRestartChanged -= kernel_HasPendingRestartChanged;
}
}
}
}