aboutsummaryrefslogtreecommitdiff
path: root/Jellyfin.Api/WebSocketListeners/SessionInfoWebSocketListener.cs
blob: d996ac69f99a09f544dc295b76432eca53ff37c5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
using System.Collections.Generic;
using System.Threading.Tasks;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Net;
using MediaBrowser.Controller.Session;
using MediaBrowser.Model.Session;
using Microsoft.Extensions.Logging;

namespace Jellyfin.Api.WebSocketListeners
{
    /// <summary>
    /// Class SessionInfoWebSocketListener.
    /// </summary>
    public class SessionInfoWebSocketListener : BasePeriodicWebSocketListener<IEnumerable<SessionInfo>, WebSocketListenerState>
    {
        private readonly ISessionManager _sessionManager;

        /// <summary>
        /// Initializes a new instance of the <see cref="SessionInfoWebSocketListener"/> class.
        /// </summary>
        /// <param name="logger">Instance of the <see cref="ILogger{SessionInfoWebSocketListener}"/> interface.</param>
        /// <param name="sessionManager">Instance of the <see cref="ISessionManager"/> interface.</param>
        public SessionInfoWebSocketListener(ILogger<SessionInfoWebSocketListener> 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;
        }

        /// <inheritdoc />
        protected override SessionMessageType Type => SessionMessageType.Sessions;

        /// <inheritdoc />
        protected override SessionMessageType StartType => SessionMessageType.SessionsStart;

        /// <inheritdoc />
        protected override SessionMessageType StopType => SessionMessageType.SessionsStop;

        /// <summary>
        /// Gets the data to send.
        /// </summary>
        /// <returns>Task{SystemInfo}.</returns>
        protected override Task<IEnumerable<SessionInfo>> GetDataToSend()
        {
            return Task.FromResult(_sessionManager.Sessions);
        }

        /// <inheritdoc />
        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);
        }

        private async void OnSessionManagerSessionActivity(object? sender, SessionEventArgs e)
        {
            await SendData(false).ConfigureAwait(false);
        }

        private async void OnSessionManagerCapabilitiesChanged(object? sender, SessionEventArgs e)
        {
            await SendData(true).ConfigureAwait(false);
        }

        private async void OnSessionManagerPlaybackProgress(object? sender, PlaybackProgressEventArgs e)
        {
            await SendData(!e.IsAutomated).ConfigureAwait(false);
        }

        private async void OnSessionManagerPlaybackStopped(object? sender, PlaybackStopEventArgs e)
        {
            await SendData(true).ConfigureAwait(false);
        }

        private async void OnSessionManagerPlaybackStart(object? sender, PlaybackProgressEventArgs e)
        {
            await SendData(true).ConfigureAwait(false);
        }

        private async void OnSessionManagerSessionEnded(object? sender, SessionEventArgs e)
        {
            await SendData(true).ConfigureAwait(false);
        }

        private async void OnSessionManagerSessionStarted(object? sender, SessionEventArgs e)
        {
            await SendData(true).ConfigureAwait(false);
        }
    }
}