SessionInfoWebSocketListener.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. using System.Collections.Generic;
  2. using System.Threading.Tasks;
  3. using Jellyfin.Data.Enums;
  4. using MediaBrowser.Controller.Authentication;
  5. using MediaBrowser.Controller.Library;
  6. using MediaBrowser.Controller.Net;
  7. using MediaBrowser.Controller.Session;
  8. using MediaBrowser.Model.Session;
  9. using Microsoft.Extensions.Logging;
  10. namespace Jellyfin.Api.WebSocketListeners;
  11. /// <summary>
  12. /// Class SessionInfoWebSocketListener.
  13. /// </summary>
  14. public class SessionInfoWebSocketListener : BasePeriodicWebSocketListener<IEnumerable<SessionInfo>, WebSocketListenerState>
  15. {
  16. private readonly ISessionManager _sessionManager;
  17. /// <summary>
  18. /// Initializes a new instance of the <see cref="SessionInfoWebSocketListener"/> class.
  19. /// </summary>
  20. /// <param name="logger">Instance of the <see cref="ILogger{SessionInfoWebSocketListener}"/> interface.</param>
  21. /// <param name="sessionManager">Instance of the <see cref="ISessionManager"/> interface.</param>
  22. public SessionInfoWebSocketListener(ILogger<SessionInfoWebSocketListener> logger, ISessionManager sessionManager)
  23. : base(logger)
  24. {
  25. _sessionManager = sessionManager;
  26. _sessionManager.SessionStarted += OnSessionManagerSessionStarted;
  27. _sessionManager.SessionEnded += OnSessionManagerSessionEnded;
  28. _sessionManager.PlaybackStart += OnSessionManagerPlaybackStart;
  29. _sessionManager.PlaybackStopped += OnSessionManagerPlaybackStopped;
  30. _sessionManager.PlaybackProgress += OnSessionManagerPlaybackProgress;
  31. _sessionManager.CapabilitiesChanged += OnSessionManagerCapabilitiesChanged;
  32. _sessionManager.SessionActivity += OnSessionManagerSessionActivity;
  33. }
  34. /// <inheritdoc />
  35. protected override SessionMessageType Type => SessionMessageType.Sessions;
  36. /// <inheritdoc />
  37. protected override SessionMessageType StartType => SessionMessageType.SessionsStart;
  38. /// <inheritdoc />
  39. protected override SessionMessageType StopType => SessionMessageType.SessionsStop;
  40. /// <summary>
  41. /// Gets the data to send.
  42. /// </summary>
  43. /// <returns>Task{SystemInfo}.</returns>
  44. protected override Task<IEnumerable<SessionInfo>> GetDataToSend()
  45. {
  46. return Task.FromResult(_sessionManager.Sessions);
  47. }
  48. /// <inheritdoc />
  49. protected override void Dispose(bool dispose)
  50. {
  51. if (dispose)
  52. {
  53. _sessionManager.SessionStarted -= OnSessionManagerSessionStarted;
  54. _sessionManager.SessionEnded -= OnSessionManagerSessionEnded;
  55. _sessionManager.PlaybackStart -= OnSessionManagerPlaybackStart;
  56. _sessionManager.PlaybackStopped -= OnSessionManagerPlaybackStopped;
  57. _sessionManager.PlaybackProgress -= OnSessionManagerPlaybackProgress;
  58. _sessionManager.CapabilitiesChanged -= OnSessionManagerCapabilitiesChanged;
  59. _sessionManager.SessionActivity -= OnSessionManagerSessionActivity;
  60. }
  61. base.Dispose(dispose);
  62. }
  63. /// <summary>
  64. /// Starts sending messages over a session info web socket.
  65. /// </summary>
  66. /// <param name="message">The message.</param>
  67. protected override void Start(WebSocketMessageInfo message)
  68. {
  69. if (!message.Connection.AuthorizationInfo.User.HasPermission(PermissionKind.IsAdministrator))
  70. {
  71. throw new AuthenticationException("Only admin users can subscribe to session information.");
  72. }
  73. base.Start(message);
  74. }
  75. private async void OnSessionManagerSessionActivity(object? sender, SessionEventArgs e)
  76. {
  77. await SendData(false).ConfigureAwait(false);
  78. }
  79. private async void OnSessionManagerCapabilitiesChanged(object? sender, SessionEventArgs e)
  80. {
  81. await SendData(true).ConfigureAwait(false);
  82. }
  83. private async void OnSessionManagerPlaybackProgress(object? sender, PlaybackProgressEventArgs e)
  84. {
  85. await SendData(!e.IsAutomated).ConfigureAwait(false);
  86. }
  87. private async void OnSessionManagerPlaybackStopped(object? sender, PlaybackStopEventArgs e)
  88. {
  89. await SendData(true).ConfigureAwait(false);
  90. }
  91. private async void OnSessionManagerPlaybackStart(object? sender, PlaybackProgressEventArgs e)
  92. {
  93. await SendData(true).ConfigureAwait(false);
  94. }
  95. private async void OnSessionManagerSessionEnded(object? sender, SessionEventArgs e)
  96. {
  97. await SendData(true).ConfigureAwait(false);
  98. }
  99. private async void OnSessionManagerSessionStarted(object? sender, SessionEventArgs e)
  100. {
  101. await SendData(true).ConfigureAwait(false);
  102. }
  103. }