SessionInfoWebSocketListener.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. private bool _disposed;
  18. /// <summary>
  19. /// Initializes a new instance of the <see cref="SessionInfoWebSocketListener"/> class.
  20. /// </summary>
  21. /// <param name="logger">Instance of the <see cref="ILogger{SessionInfoWebSocketListener}"/> interface.</param>
  22. /// <param name="sessionManager">Instance of the <see cref="ISessionManager"/> interface.</param>
  23. public SessionInfoWebSocketListener(ILogger<SessionInfoWebSocketListener> logger, ISessionManager sessionManager)
  24. : base(logger)
  25. {
  26. _sessionManager = sessionManager;
  27. _sessionManager.SessionStarted += OnSessionManagerSessionStarted;
  28. _sessionManager.SessionEnded += OnSessionManagerSessionEnded;
  29. _sessionManager.PlaybackStart += OnSessionManagerPlaybackStart;
  30. _sessionManager.PlaybackStopped += OnSessionManagerPlaybackStopped;
  31. _sessionManager.PlaybackProgress += OnSessionManagerPlaybackProgress;
  32. _sessionManager.CapabilitiesChanged += OnSessionManagerCapabilitiesChanged;
  33. _sessionManager.SessionActivity += OnSessionManagerSessionActivity;
  34. }
  35. /// <inheritdoc />
  36. protected override SessionMessageType Type => SessionMessageType.Sessions;
  37. /// <inheritdoc />
  38. protected override SessionMessageType StartType => SessionMessageType.SessionsStart;
  39. /// <inheritdoc />
  40. protected override SessionMessageType StopType => SessionMessageType.SessionsStop;
  41. /// <summary>
  42. /// Gets the data to send.
  43. /// </summary>
  44. /// <returns>Task{SystemInfo}.</returns>
  45. protected override Task<IEnumerable<SessionInfo>> GetDataToSend()
  46. {
  47. return Task.FromResult(_sessionManager.Sessions);
  48. }
  49. /// <inheritdoc />
  50. protected override async ValueTask DisposeAsyncCore()
  51. {
  52. if (!_disposed)
  53. {
  54. _sessionManager.SessionStarted -= OnSessionManagerSessionStarted;
  55. _sessionManager.SessionEnded -= OnSessionManagerSessionEnded;
  56. _sessionManager.PlaybackStart -= OnSessionManagerPlaybackStart;
  57. _sessionManager.PlaybackStopped -= OnSessionManagerPlaybackStopped;
  58. _sessionManager.PlaybackProgress -= OnSessionManagerPlaybackProgress;
  59. _sessionManager.CapabilitiesChanged -= OnSessionManagerCapabilitiesChanged;
  60. _sessionManager.SessionActivity -= OnSessionManagerSessionActivity;
  61. _disposed = true;
  62. }
  63. await base.DisposeAsyncCore().ConfigureAwait(false);
  64. }
  65. /// <summary>
  66. /// Starts sending messages over a session info web socket.
  67. /// </summary>
  68. /// <param name="message">The message.</param>
  69. protected override void Start(WebSocketMessageInfo message)
  70. {
  71. if (!message.Connection.AuthorizationInfo.User.HasPermission(PermissionKind.IsAdministrator))
  72. {
  73. throw new AuthenticationException("Only admin users can subscribe to session information.");
  74. }
  75. base.Start(message);
  76. }
  77. private void OnSessionManagerSessionActivity(object? sender, SessionEventArgs e)
  78. {
  79. SendData(false);
  80. }
  81. private void OnSessionManagerCapabilitiesChanged(object? sender, SessionEventArgs e)
  82. {
  83. SendData(true);
  84. }
  85. private void OnSessionManagerPlaybackProgress(object? sender, PlaybackProgressEventArgs e)
  86. {
  87. SendData(!e.IsAutomated);
  88. }
  89. private void OnSessionManagerPlaybackStopped(object? sender, PlaybackStopEventArgs e)
  90. {
  91. SendData(true);
  92. }
  93. private void OnSessionManagerPlaybackStart(object? sender, PlaybackProgressEventArgs e)
  94. {
  95. SendData(true);
  96. }
  97. private void OnSessionManagerSessionEnded(object? sender, SessionEventArgs e)
  98. {
  99. SendData(true);
  100. }
  101. private void OnSessionManagerSessionStarted(object? sender, SessionEventArgs e)
  102. {
  103. SendData(true);
  104. }
  105. }