SessionInfoWebSocketListener.cs 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. using System.Collections.Generic;
  2. using System.Threading.Tasks;
  3. using MediaBrowser.Controller.Library;
  4. using MediaBrowser.Controller.Net;
  5. using MediaBrowser.Controller.Session;
  6. using Microsoft.Extensions.Logging;
  7. namespace Jellyfin.Api.WebSocketListeners
  8. {
  9. /// <summary>
  10. /// Class SessionInfoWebSocketListener.
  11. /// </summary>
  12. public class SessionInfoWebSocketListener : BasePeriodicWebSocketListener<IEnumerable<SessionInfo>, WebSocketListenerState>
  13. {
  14. private readonly ISessionManager _sessionManager;
  15. /// <summary>
  16. /// Initializes a new instance of the <see cref="SessionInfoWebSocketListener"/> class.
  17. /// </summary>
  18. /// <param name="logger">Instance of the <see cref="ILogger{SessionInfoWebSocketListener}"/> interface.</param>
  19. /// <param name="sessionManager">Instance of the <see cref="ISessionManager"/> interface.</param>
  20. public SessionInfoWebSocketListener(ILogger<SessionInfoWebSocketListener> logger, ISessionManager sessionManager)
  21. : base(logger)
  22. {
  23. _sessionManager = sessionManager;
  24. _sessionManager.SessionStarted += OnSessionManagerSessionStarted;
  25. _sessionManager.SessionEnded += OnSessionManagerSessionEnded;
  26. _sessionManager.PlaybackStart += OnSessionManagerPlaybackStart;
  27. _sessionManager.PlaybackStopped += OnSessionManagerPlaybackStopped;
  28. _sessionManager.PlaybackProgress += OnSessionManagerPlaybackProgress;
  29. _sessionManager.CapabilitiesChanged += OnSessionManagerCapabilitiesChanged;
  30. _sessionManager.SessionActivity += OnSessionManagerSessionActivity;
  31. }
  32. /// <inheritdoc />
  33. protected override string Name => "Sessions";
  34. /// <summary>
  35. /// Gets the data to send.
  36. /// </summary>
  37. /// <returns>Task{SystemInfo}.</returns>
  38. protected override Task<IEnumerable<SessionInfo>> GetDataToSend()
  39. {
  40. return Task.FromResult(_sessionManager.Sessions);
  41. }
  42. /// <inheritdoc />
  43. protected override void Dispose(bool dispose)
  44. {
  45. _sessionManager.SessionStarted -= OnSessionManagerSessionStarted;
  46. _sessionManager.SessionEnded -= OnSessionManagerSessionEnded;
  47. _sessionManager.PlaybackStart -= OnSessionManagerPlaybackStart;
  48. _sessionManager.PlaybackStopped -= OnSessionManagerPlaybackStopped;
  49. _sessionManager.PlaybackProgress -= OnSessionManagerPlaybackProgress;
  50. _sessionManager.CapabilitiesChanged -= OnSessionManagerCapabilitiesChanged;
  51. _sessionManager.SessionActivity -= OnSessionManagerSessionActivity;
  52. base.Dispose(dispose);
  53. }
  54. private async void OnSessionManagerSessionActivity(object sender, SessionEventArgs e)
  55. {
  56. await SendData(false).ConfigureAwait(false);
  57. }
  58. private async void OnSessionManagerCapabilitiesChanged(object sender, SessionEventArgs e)
  59. {
  60. await SendData(true).ConfigureAwait(false);
  61. }
  62. private async void OnSessionManagerPlaybackProgress(object sender, PlaybackProgressEventArgs e)
  63. {
  64. await SendData(!e.IsAutomated).ConfigureAwait(false);
  65. }
  66. private async void OnSessionManagerPlaybackStopped(object sender, PlaybackStopEventArgs e)
  67. {
  68. await SendData(true).ConfigureAwait(false);
  69. }
  70. private async void OnSessionManagerPlaybackStart(object sender, PlaybackProgressEventArgs e)
  71. {
  72. await SendData(true).ConfigureAwait(false);
  73. }
  74. private async void OnSessionManagerSessionEnded(object sender, SessionEventArgs e)
  75. {
  76. await SendData(true).ConfigureAwait(false);
  77. }
  78. private async void OnSessionManagerSessionStarted(object sender, SessionEventArgs e)
  79. {
  80. await SendData(true).ConfigureAwait(false);
  81. }
  82. }
  83. }