using System.Collections.Generic; using System.Threading.Tasks; using MediaBrowser.Controller.Library; using MediaBrowser.Controller.Net; using MediaBrowser.Controller.Session; using Microsoft.Extensions.Logging; namespace MediaBrowser.Api.Sessions { /// /// Class SessionInfoWebSocketListener /// public class SessionInfoWebSocketListener : BasePeriodicWebSocketListener, WebSocketListenerState> { /// /// Gets the name. /// /// The name. protected override string Name => "Sessions"; /// /// The _kernel /// private readonly ISessionManager _sessionManager; /// /// Initializes a new instance of the class. /// public SessionInfoWebSocketListener(ILogger logger, ISessionManager sessionManager) : base(logger) { _sessionManager = sessionManager; _sessionManager.SessionStarted += OnSessionManagerSessionStarted; _sessionManager.SessionEnded += OnSessionManagerSessionEnded; _sessionManager.PlaybackStart += OnSessionManagerPlaybackStart; _sessionManager.PlaybackStopped += OnSessionManagerPlaybackStopped; _sessionManager.PlaybackProgress += OnSessionManagerPlaybackProgress; _sessionManager.CapabilitiesChanged += OnSessionManagerCapabilitiesChanged; _sessionManager.SessionActivity += OnSessionManagerSessionActivity; } private void OnSessionManagerSessionActivity(object sender, SessionEventArgs e) { SendData(false); } private void OnSessionManagerCapabilitiesChanged(object sender, SessionEventArgs e) { SendData(true); } private void OnSessionManagerPlaybackProgress(object sender, PlaybackProgressEventArgs e) { SendData(!e.IsAutomated); } private void OnSessionManagerPlaybackStopped(object sender, PlaybackStopEventArgs e) { SendData(true); } private void OnSessionManagerPlaybackStart(object sender, PlaybackProgressEventArgs e) { SendData(true); } private void OnSessionManagerSessionEnded(object sender, SessionEventArgs e) { SendData(true); } private void OnSessionManagerSessionStarted(object sender, SessionEventArgs e) { SendData(true); } /// /// Gets the data to send. /// /// Task{SystemInfo}. protected override Task> GetDataToSend() { return Task.FromResult(_sessionManager.Sessions); } /// protected override void Dispose(bool dispose) { _sessionManager.SessionStarted -= OnSessionManagerSessionStarted; _sessionManager.SessionEnded -= OnSessionManagerSessionEnded; _sessionManager.PlaybackStart -= OnSessionManagerPlaybackStart; _sessionManager.PlaybackStopped -= OnSessionManagerPlaybackStopped; _sessionManager.PlaybackProgress -= OnSessionManagerPlaybackProgress; _sessionManager.CapabilitiesChanged -= OnSessionManagerCapabilitiesChanged; _sessionManager.SessionActivity -= OnSessionManagerSessionActivity; base.Dispose(dispose); } } }