using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Net;
using MediaBrowser.Controller.Session;
using Microsoft.Extensions.Logging;
namespace MediaBrowser.Api.Session
{
///
/// Class SessionInfoWebSocketListener
///
class SessionInfoWebSocketListener : BasePeriodicWebSocketListener, WebSocketListenerState>
{
///
/// Gets the name.
///
/// The name.
protected override string Name
{
get { return "Sessions"; }
}
///
/// The _kernel
///
private readonly ISessionManager _sessionManager;
///
/// Initializes a new instance of the class.
///
public SessionInfoWebSocketListener(ILogger logger, ISessionManager sessionManager)
: base(logger)
{
_sessionManager = sessionManager;
_sessionManager.SessionStarted += _sessionManager_SessionStarted;
_sessionManager.SessionEnded += _sessionManager_SessionEnded;
_sessionManager.PlaybackStart += _sessionManager_PlaybackStart;
_sessionManager.PlaybackStopped += _sessionManager_PlaybackStopped;
_sessionManager.PlaybackProgress += _sessionManager_PlaybackProgress;
_sessionManager.CapabilitiesChanged += _sessionManager_CapabilitiesChanged;
_sessionManager.SessionActivity += _sessionManager_SessionActivity;
}
void _sessionManager_SessionActivity(object sender, SessionEventArgs e)
{
SendData(false);
}
void _sessionManager_CapabilitiesChanged(object sender, SessionEventArgs e)
{
SendData(true);
}
void _sessionManager_PlaybackProgress(object sender, PlaybackProgressEventArgs e)
{
SendData(!e.IsAutomated);
}
void _sessionManager_PlaybackStopped(object sender, PlaybackStopEventArgs e)
{
SendData(true);
}
void _sessionManager_PlaybackStart(object sender, PlaybackProgressEventArgs e)
{
SendData(true);
}
void _sessionManager_SessionEnded(object sender, SessionEventArgs e)
{
SendData(true);
}
void _sessionManager_SessionStarted(object sender, SessionEventArgs e)
{
SendData(true);
}
///
/// Gets the data to send.
///
/// The state.
/// Task{SystemInfo}.
protected override Task> GetDataToSend(WebSocketListenerState state, CancellationToken cancellationToken)
{
return Task.FromResult(_sessionManager.Sessions);
}
protected override void Dispose(bool dispose)
{
_sessionManager.SessionStarted -= _sessionManager_SessionStarted;
_sessionManager.SessionEnded -= _sessionManager_SessionEnded;
_sessionManager.PlaybackStart -= _sessionManager_PlaybackStart;
_sessionManager.PlaybackStopped -= _sessionManager_PlaybackStopped;
_sessionManager.PlaybackProgress -= _sessionManager_PlaybackProgress;
_sessionManager.CapabilitiesChanged -= _sessionManager_CapabilitiesChanged;
_sessionManager.SessionActivity -= _sessionManager_SessionActivity;
base.Dispose(dispose);
}
}
}