jellyfin/MediaBrowser.Api/Sessions/SessionInfoWebSocketListener.cs

102 lines
3.6 KiB
C#
Raw Normal View History

using System.Collections.Generic;
using System.Threading.Tasks;
using MediaBrowser.Controller.Library;
2014-12-26 12:34:39 -07:00
using MediaBrowser.Controller.Net;
2013-05-30 20:38:46 -07:00
using MediaBrowser.Controller.Session;
using Microsoft.Extensions.Logging;
2013-05-30 20:38:46 -07:00
2020-02-05 08:47:50 -07:00
namespace MediaBrowser.Api.Sessions
2013-05-30 20:38:46 -07:00
{
/// <summary>
/// Class SessionInfoWebSocketListener
/// </summary>
public class SessionInfoWebSocketListener : BasePeriodicWebSocketListener<IEnumerable<SessionInfo>, WebSocketListenerState>
2013-05-30 20:38:46 -07:00
{
/// <summary>
/// Gets the name.
/// </summary>
/// <value>The name.</value>
protected override string Name => "Sessions";
2013-05-30 20:38:46 -07: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 10:26:21 -07:00
: base(logger)
2013-05-30 20:38:46 -07:00
{
_sessionManager = sessionManager;
2014-05-13 17:46:45 -07:00
2019-12-17 15:15:02 -07:00
_sessionManager.SessionStarted += OnSessionManagerSessionStarted;
_sessionManager.SessionEnded += OnSessionManagerSessionEnded;
_sessionManager.PlaybackStart += OnSessionManagerPlaybackStart;
_sessionManager.PlaybackStopped += OnSessionManagerPlaybackStopped;
_sessionManager.PlaybackProgress += OnSessionManagerPlaybackProgress;
_sessionManager.CapabilitiesChanged += OnSessionManagerCapabilitiesChanged;
_sessionManager.SessionActivity += OnSessionManagerSessionActivity;
2014-05-13 17:46:45 -07:00
}
2019-12-17 15:15:02 -07:00
private void OnSessionManagerSessionActivity(object sender, SessionEventArgs e)
2014-05-13 17:46:45 -07:00
{
SendData(false);
}
2019-12-17 15:15:02 -07:00
private void OnSessionManagerCapabilitiesChanged(object sender, SessionEventArgs e)
2014-05-13 17:46:45 -07:00
{
SendData(true);
}
2019-12-17 15:15:02 -07:00
private void OnSessionManagerPlaybackProgress(object sender, PlaybackProgressEventArgs e)
2014-05-13 17:46:45 -07:00
{
2017-04-25 11:23:38 -07:00
SendData(!e.IsAutomated);
2014-05-13 17:46:45 -07:00
}
2019-12-17 15:15:02 -07:00
private void OnSessionManagerPlaybackStopped(object sender, PlaybackStopEventArgs e)
2014-05-13 17:46:45 -07:00
{
SendData(true);
}
2019-12-17 15:15:02 -07:00
private void OnSessionManagerPlaybackStart(object sender, PlaybackProgressEventArgs e)
2014-05-13 17:46:45 -07:00
{
SendData(true);
}
2019-12-17 15:15:02 -07:00
private void OnSessionManagerSessionEnded(object sender, SessionEventArgs e)
2014-05-13 17:46:45 -07:00
{
SendData(true);
}
2019-12-17 15:15:02 -07:00
private void OnSessionManagerSessionStarted(object sender, SessionEventArgs e)
2014-05-13 17:46:45 -07:00
{
SendData(true);
2013-05-30 20:38:46 -07:00
}
/// <summary>
/// Gets the data to send.
/// </summary>
/// <returns>Task{SystemInfo}.</returns>
2019-02-27 14:09:22 -07:00
protected override Task<IEnumerable<SessionInfo>> GetDataToSend()
2013-05-30 20:38:46 -07:00
{
2018-09-12 10:26:21 -07:00
return Task.FromResult(_sessionManager.Sessions);
2014-05-13 17:46:45 -07:00
}
2019-12-17 15:15:02 -07:00
/// <inheritdoc />
2014-05-13 17:46:45 -07:00
protected override void Dispose(bool dispose)
{
2019-12-17 15:15:02 -07:00
_sessionManager.SessionStarted -= OnSessionManagerSessionStarted;
_sessionManager.SessionEnded -= OnSessionManagerSessionEnded;
_sessionManager.PlaybackStart -= OnSessionManagerPlaybackStart;
_sessionManager.PlaybackStopped -= OnSessionManagerPlaybackStopped;
_sessionManager.PlaybackProgress -= OnSessionManagerPlaybackProgress;
_sessionManager.CapabilitiesChanged -= OnSessionManagerCapabilitiesChanged;
_sessionManager.SessionActivity -= OnSessionManagerSessionActivity;
2014-05-13 17:46:45 -07:00
base.Dispose(dispose);
}
2013-05-30 20:38:46 -07:00
}
}