2019-01-13 20:01:16 +00:00
|
|
|
using System.Collections.Generic;
|
2019-01-13 19:24:58 +00:00
|
|
|
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;
|
2018-12-13 13:18:25 +00:00
|
|
|
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>
|
2020-06-15 22:37:52 +00:00
|
|
|
/// Class SessionInfoWebSocketListener.
|
2013-05-31 03:38:46 +00:00
|
|
|
/// </summary>
|
2019-02-15 22:05:14 +00:00
|
|
|
public class SessionInfoWebSocketListener : BasePeriodicWebSocketListener<IEnumerable<SessionInfo>, WebSocketListenerState>
|
2013-05-31 03:38:46 +00:00
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// Gets the name.
|
|
|
|
/// </summary>
|
|
|
|
/// <value>The name.</value>
|
2019-01-13 20:31:14 +00:00
|
|
|
protected override string Name => "Sessions";
|
2013-05-31 03:38:46 +00:00
|
|
|
|
|
|
|
/// <summary>
|
2020-06-15 22:37:52 +00:00
|
|
|
/// The _kernel.
|
2013-05-31 03:38:46 +00:00
|
|
|
/// </summary>
|
|
|
|
private readonly ISessionManager _sessionManager;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Initializes a new instance of the <see cref="SessionInfoWebSocketListener"/> class.
|
|
|
|
/// </summary>
|
2020-03-03 22:07:10 +00:00
|
|
|
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
|
|
|
|
2019-12-17 22:15:02 +00:00
|
|
|
_sessionManager.SessionStarted += OnSessionManagerSessionStarted;
|
|
|
|
_sessionManager.SessionEnded += OnSessionManagerSessionEnded;
|
|
|
|
_sessionManager.PlaybackStart += OnSessionManagerPlaybackStart;
|
|
|
|
_sessionManager.PlaybackStopped += OnSessionManagerPlaybackStopped;
|
|
|
|
_sessionManager.PlaybackProgress += OnSessionManagerPlaybackProgress;
|
|
|
|
_sessionManager.CapabilitiesChanged += OnSessionManagerCapabilitiesChanged;
|
|
|
|
_sessionManager.SessionActivity += OnSessionManagerSessionActivity;
|
2014-05-14 00:46:45 +00:00
|
|
|
}
|
|
|
|
|
2020-05-25 21:52:51 +00:00
|
|
|
private async void OnSessionManagerSessionActivity(object sender, SessionEventArgs e)
|
2014-05-14 00:46:45 +00:00
|
|
|
{
|
2020-05-25 21:52:51 +00:00
|
|
|
await SendData(false).ConfigureAwait(false);
|
2014-05-14 00:46:45 +00:00
|
|
|
}
|
|
|
|
|
2020-05-25 21:52:51 +00:00
|
|
|
private async void OnSessionManagerCapabilitiesChanged(object sender, SessionEventArgs e)
|
2014-05-14 00:46:45 +00:00
|
|
|
{
|
2020-05-25 21:52:51 +00:00
|
|
|
await SendData(true).ConfigureAwait(false);
|
2014-05-14 00:46:45 +00:00
|
|
|
}
|
|
|
|
|
2020-05-25 21:52:51 +00:00
|
|
|
private async void OnSessionManagerPlaybackProgress(object sender, PlaybackProgressEventArgs e)
|
2014-05-14 00:46:45 +00:00
|
|
|
{
|
2020-05-25 21:52:51 +00:00
|
|
|
await SendData(!e.IsAutomated).ConfigureAwait(false);
|
2014-05-14 00:46:45 +00:00
|
|
|
}
|
|
|
|
|
2020-05-25 21:52:51 +00:00
|
|
|
private async void OnSessionManagerPlaybackStopped(object sender, PlaybackStopEventArgs e)
|
2014-05-14 00:46:45 +00:00
|
|
|
{
|
2020-05-25 21:52:51 +00:00
|
|
|
await SendData(true).ConfigureAwait(false);
|
2014-05-14 00:46:45 +00:00
|
|
|
}
|
|
|
|
|
2020-05-25 21:52:51 +00:00
|
|
|
private async void OnSessionManagerPlaybackStart(object sender, PlaybackProgressEventArgs e)
|
2014-05-14 00:46:45 +00:00
|
|
|
{
|
2020-05-25 21:52:51 +00:00
|
|
|
await SendData(true).ConfigureAwait(false);
|
2014-05-14 00:46:45 +00:00
|
|
|
}
|
|
|
|
|
2020-05-25 21:52:51 +00:00
|
|
|
private async void OnSessionManagerSessionEnded(object sender, SessionEventArgs e)
|
2014-05-14 00:46:45 +00:00
|
|
|
{
|
2020-05-25 21:52:51 +00:00
|
|
|
await SendData(true).ConfigureAwait(false);
|
2014-05-14 00:46:45 +00:00
|
|
|
}
|
|
|
|
|
2020-05-25 21:52:51 +00:00
|
|
|
private async void OnSessionManagerSessionStarted(object sender, SessionEventArgs e)
|
2014-05-14 00:46:45 +00:00
|
|
|
{
|
2020-05-25 21:52:51 +00:00
|
|
|
await SendData(true).ConfigureAwait(false);
|
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
|
|
|
}
|
|
|
|
|
2019-12-17 22:15:02 +00:00
|
|
|
/// <inheritdoc />
|
2014-05-14 00:46:45 +00:00
|
|
|
protected override void Dispose(bool dispose)
|
|
|
|
{
|
2019-12-17 22:15:02 +00:00
|
|
|
_sessionManager.SessionStarted -= OnSessionManagerSessionStarted;
|
|
|
|
_sessionManager.SessionEnded -= OnSessionManagerSessionEnded;
|
|
|
|
_sessionManager.PlaybackStart -= OnSessionManagerPlaybackStart;
|
|
|
|
_sessionManager.PlaybackStopped -= OnSessionManagerPlaybackStopped;
|
|
|
|
_sessionManager.PlaybackProgress -= OnSessionManagerPlaybackProgress;
|
|
|
|
_sessionManager.CapabilitiesChanged -= OnSessionManagerCapabilitiesChanged;
|
|
|
|
_sessionManager.SessionActivity -= OnSessionManagerSessionActivity;
|
2014-05-14 00:46:45 +00:00
|
|
|
|
|
|
|
base.Dispose(dispose);
|
|
|
|
}
|
2013-05-31 03:38:46 +00:00
|
|
|
}
|
|
|
|
}
|