SessionInfoWebSocketListener.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. _sessionManager.SessionStarted -= OnSessionManagerSessionStarted;
  52. _sessionManager.SessionEnded -= OnSessionManagerSessionEnded;
  53. _sessionManager.PlaybackStart -= OnSessionManagerPlaybackStart;
  54. _sessionManager.PlaybackStopped -= OnSessionManagerPlaybackStopped;
  55. _sessionManager.PlaybackProgress -= OnSessionManagerPlaybackProgress;
  56. _sessionManager.CapabilitiesChanged -= OnSessionManagerCapabilitiesChanged;
  57. _sessionManager.SessionActivity -= OnSessionManagerSessionActivity;
  58. base.Dispose(dispose);
  59. }
  60. /// <summary>
  61. /// Starts sending messages over a session info web socket.
  62. /// </summary>
  63. /// <param name="message">The message.</param>
  64. protected override void Start(WebSocketMessageInfo message)
  65. {
  66. if (!message.Connection.AuthorizationInfo.User.HasPermission(PermissionKind.IsAdministrator))
  67. {
  68. throw new AuthenticationException("Only admin users can subscribe to session information.");
  69. }
  70. base.Start(message);
  71. }
  72. private async void OnSessionManagerSessionActivity(object? sender, SessionEventArgs e)
  73. {
  74. await SendData(false).ConfigureAwait(false);
  75. }
  76. private async void OnSessionManagerCapabilitiesChanged(object? sender, SessionEventArgs e)
  77. {
  78. await SendData(true).ConfigureAwait(false);
  79. }
  80. private async void OnSessionManagerPlaybackProgress(object? sender, PlaybackProgressEventArgs e)
  81. {
  82. await SendData(!e.IsAutomated).ConfigureAwait(false);
  83. }
  84. private async void OnSessionManagerPlaybackStopped(object? sender, PlaybackStopEventArgs e)
  85. {
  86. await SendData(true).ConfigureAwait(false);
  87. }
  88. private async void OnSessionManagerPlaybackStart(object? sender, PlaybackProgressEventArgs e)
  89. {
  90. await SendData(true).ConfigureAwait(false);
  91. }
  92. private async void OnSessionManagerSessionEnded(object? sender, SessionEventArgs e)
  93. {
  94. await SendData(true).ConfigureAwait(false);
  95. }
  96. private async void OnSessionManagerSessionStarted(object? sender, SessionEventArgs e)
  97. {
  98. await SendData(true).ConfigureAwait(false);
  99. }
  100. }