jellyfin/MediaBrowser.Api/Session/SessionInfoWebSocketListener.cs

117 lines
4.0 KiB
C#
Raw Normal View History

2015-02-04 20:16:23 +00:00
using MediaBrowser.Controller.Library;
2014-12-26 19:34:39 +00:00
using MediaBrowser.Controller.Net;
2013-05-31 03:38:46 +00:00
using MediaBrowser.Controller.Session;
using MediaBrowser.Model.Logging;
using MediaBrowser.Model.Session;
using System.Collections.Generic;
using System.Linq;
2017-11-23 15:46:16 +00:00
using System.Threading;
2016-11-01 03:07:45 +00:00
using MediaBrowser.Model.Threading;
2017-11-23 15:46:16 +00:00
using System.Threading.Tasks;
2013-05-31 03:38:46 +00:00
2014-08-10 22:13:17 +00:00
namespace MediaBrowser.Api.Session
2013-05-31 03:38:46 +00:00
{
/// <summary>
/// Class SessionInfoWebSocketListener
/// </summary>
2014-05-10 17:28:03 +00:00
class SessionInfoWebSocketListener : BasePeriodicWebSocketListener<IEnumerable<SessionInfoDto>, WebSocketListenerState>
2013-05-31 03:38:46 +00:00
{
/// <summary>
/// Gets the name.
/// </summary>
/// <value>The name.</value>
protected override string Name
{
get { return "Sessions"; }
}
/// <summary>
/// The _kernel
/// </summary>
private readonly ISessionManager _sessionManager;
/// <summary>
/// Initializes a new instance of the <see cref="SessionInfoWebSocketListener"/> class.
/// </summary>
2016-11-01 03:07:45 +00:00
public SessionInfoWebSocketListener(ILogger logger, ISessionManager sessionManager, ITimerFactory timerFactory)
: base(logger, timerFactory)
2013-05-31 03:38:46 +00:00
{
_sessionManager = sessionManager;
2014-05-14 00:46:45 +00:00
_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)
{
2017-04-25 18:23:38 +00:00
SendData(!e.IsAutomated);
2014-05-14 00:46:45 +00:00
}
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);
2013-05-31 03:38:46 +00:00
}
/// <summary>
/// Gets the data to send.
/// </summary>
/// <param name="state">The state.</param>
/// <returns>Task{SystemInfo}.</returns>
2017-11-23 15:46:16 +00:00
protected override Task<IEnumerable<SessionInfoDto>> GetDataToSend(WebSocketListenerState state, CancellationToken cancellationToken)
2013-05-31 03:38:46 +00:00
{
2014-04-06 17:53:23 +00:00
return Task.FromResult(_sessionManager.Sessions.Where(i => i.IsActive).Select(_sessionManager.GetSessionInfoDto));
2013-05-31 03:38:46 +00:00
}
2014-05-14 00:46:45 +00:00
protected override bool SendOnTimer
{
get
{
return false;
}
}
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);
}
2013-05-31 03:38:46 +00:00
}
}