jellyfin-server/MediaBrowser.Api/Sessions/SessionInfoWebSocketListener.cs

101 lines
3.5 KiB
C#
Raw Normal View History

using System.Collections.Generic;
using System.Threading.Tasks;
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 Microsoft.Extensions.Logging;
2013-05-31 03:38:46 +00:00
2020-02-05 15:47:50 +00:00
namespace MediaBrowser.Api.Sessions
2013-05-31 03:38:46 +00:00
{
/// <summary>
/// Class SessionInfoWebSocketListener
/// </summary>
public class SessionInfoWebSocketListener : BasePeriodicWebSocketListener<IEnumerable<SessionInfo>, WebSocketListenerState>
2013-05-31 03:38:46 +00:00
{
/// <summary>
/// Gets the name.
/// </summary>
/// <value>The name.</value>
protected override string Name => "Sessions";
2013-05-31 03:38:46 +00:00
/// <summary>
/// The _kernel
/// </summary>
private readonly ISessionManager _sessionManager;
/// <summary>
/// Initializes a new instance of the <see cref="SessionInfoWebSocketListener"/> class.
/// </summary>
public SessionInfoWebSocketListener(ILogger<SessionInfoWebSocketListener> logger, ISessionManager sessionManager)
2018-09-12 17:26:21 +00:00
: base(logger)
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>
/// <returns>Task{SystemInfo}.</returns>
2019-02-27 21:09:22 +00:00
protected override Task<IEnumerable<SessionInfo>> GetDataToSend()
2013-05-31 03:38:46 +00:00
{
2018-09-12 17:26:21 +00:00
return Task.FromResult(_sessionManager.Sessions);
2014-05-14 00:46:45 +00:00
}
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
}
}