SessionInfoWebSocketListener.cs 3.8 KB

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