2019-01-13 13:01:16 -07:00
|
|
|
using System.Collections.Generic;
|
2019-01-13 12:24:58 -07:00
|
|
|
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;
|
2020-09-25 00:25:59 -07:00
|
|
|
using MediaBrowser.Model.Session;
|
2018-12-13 06:18:25 -07:00
|
|
|
using Microsoft.Extensions.Logging;
|
2013-05-30 20:38:46 -07:00
|
|
|
|
2020-08-03 12:09:32 -07:00
|
|
|
namespace Jellyfin.Api.WebSocketListeners
|
2013-05-30 20:38:46 -07:00
|
|
|
{
|
|
|
|
/// <summary>
|
2020-06-15 15:37:52 -07:00
|
|
|
/// Class SessionInfoWebSocketListener.
|
2013-05-30 20:38:46 -07:00
|
|
|
/// </summary>
|
2019-02-15 15:05:14 -07:00
|
|
|
public class SessionInfoWebSocketListener : BasePeriodicWebSocketListener<IEnumerable<SessionInfo>, WebSocketListenerState>
|
2013-05-30 20:38:46 -07:00
|
|
|
{
|
|
|
|
private readonly ISessionManager _sessionManager;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Initializes a new instance of the <see cref="SessionInfoWebSocketListener"/> class.
|
|
|
|
/// </summary>
|
2020-08-03 12:33:43 -07:00
|
|
|
/// <param name="logger">Instance of the <see cref="ILogger{SessionInfoWebSocketListener}"/> interface.</param>
|
|
|
|
/// <param name="sessionManager">Instance of the <see cref="ISessionManager"/> interface.</param>
|
2020-03-03 15:07:10 -07:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-08-03 12:33:43 -07:00
|
|
|
/// <inheritdoc />
|
2020-09-25 00:25:59 -07:00
|
|
|
protected override SessionMessageType Type => SessionMessageType.Sessions;
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
protected override SessionMessageType StartType => SessionMessageType.SessionsStart;
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
protected override SessionMessageType StopType => SessionMessageType.SessionsStop;
|
2020-08-03 12:33:43 -07:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets the data to send.
|
|
|
|
/// </summary>
|
|
|
|
/// <returns>Task{SystemInfo}.</returns>
|
|
|
|
protected override Task<IEnumerable<SessionInfo>> GetDataToSend()
|
|
|
|
{
|
|
|
|
return Task.FromResult(_sessionManager.Sessions);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
protected override void Dispose(bool dispose)
|
|
|
|
{
|
|
|
|
_sessionManager.SessionStarted -= OnSessionManagerSessionStarted;
|
|
|
|
_sessionManager.SessionEnded -= OnSessionManagerSessionEnded;
|
|
|
|
_sessionManager.PlaybackStart -= OnSessionManagerPlaybackStart;
|
|
|
|
_sessionManager.PlaybackStopped -= OnSessionManagerPlaybackStopped;
|
|
|
|
_sessionManager.PlaybackProgress -= OnSessionManagerPlaybackProgress;
|
|
|
|
_sessionManager.CapabilitiesChanged -= OnSessionManagerCapabilitiesChanged;
|
|
|
|
_sessionManager.SessionActivity -= OnSessionManagerSessionActivity;
|
|
|
|
|
|
|
|
base.Dispose(dispose);
|
|
|
|
}
|
|
|
|
|
2020-05-25 14:52:51 -07:00
|
|
|
private async void OnSessionManagerSessionActivity(object sender, SessionEventArgs e)
|
2014-05-13 17:46:45 -07:00
|
|
|
{
|
2020-05-25 14:52:51 -07:00
|
|
|
await SendData(false).ConfigureAwait(false);
|
2014-05-13 17:46:45 -07:00
|
|
|
}
|
|
|
|
|
2020-05-25 14:52:51 -07:00
|
|
|
private async void OnSessionManagerCapabilitiesChanged(object sender, SessionEventArgs e)
|
2014-05-13 17:46:45 -07:00
|
|
|
{
|
2020-05-25 14:52:51 -07:00
|
|
|
await SendData(true).ConfigureAwait(false);
|
2014-05-13 17:46:45 -07:00
|
|
|
}
|
|
|
|
|
2020-05-25 14:52:51 -07:00
|
|
|
private async void OnSessionManagerPlaybackProgress(object sender, PlaybackProgressEventArgs e)
|
2014-05-13 17:46:45 -07:00
|
|
|
{
|
2020-05-25 14:52:51 -07:00
|
|
|
await SendData(!e.IsAutomated).ConfigureAwait(false);
|
2014-05-13 17:46:45 -07:00
|
|
|
}
|
|
|
|
|
2020-05-25 14:52:51 -07:00
|
|
|
private async void OnSessionManagerPlaybackStopped(object sender, PlaybackStopEventArgs e)
|
2014-05-13 17:46:45 -07:00
|
|
|
{
|
2020-05-25 14:52:51 -07:00
|
|
|
await SendData(true).ConfigureAwait(false);
|
2014-05-13 17:46:45 -07:00
|
|
|
}
|
|
|
|
|
2020-05-25 14:52:51 -07:00
|
|
|
private async void OnSessionManagerPlaybackStart(object sender, PlaybackProgressEventArgs e)
|
2014-05-13 17:46:45 -07:00
|
|
|
{
|
2020-05-25 14:52:51 -07:00
|
|
|
await SendData(true).ConfigureAwait(false);
|
2014-05-13 17:46:45 -07:00
|
|
|
}
|
|
|
|
|
2020-05-25 14:52:51 -07:00
|
|
|
private async void OnSessionManagerSessionEnded(object sender, SessionEventArgs e)
|
2014-05-13 17:46:45 -07:00
|
|
|
{
|
2020-05-25 14:52:51 -07:00
|
|
|
await SendData(true).ConfigureAwait(false);
|
2014-05-13 17:46:45 -07:00
|
|
|
}
|
|
|
|
|
2020-05-25 14:52:51 -07:00
|
|
|
private async void OnSessionManagerSessionStarted(object sender, SessionEventArgs e)
|
2014-05-13 17:46:45 -07:00
|
|
|
{
|
2020-05-25 14:52:51 -07:00
|
|
|
await SendData(true).ConfigureAwait(false);
|
2013-05-30 20:38:46 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|