SessionsService.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. using MediaBrowser.Common.Extensions;
  2. using MediaBrowser.Controller.Dto;
  3. using MediaBrowser.Controller.Session;
  4. using MediaBrowser.Model.Net;
  5. using MediaBrowser.Model.Session;
  6. using ServiceStack.ServiceHost;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Threading;
  11. namespace MediaBrowser.Api
  12. {
  13. /// <summary>
  14. /// Class GetSessions
  15. /// </summary>
  16. [Route("/Sessions", "GET")]
  17. [Api(("Gets a list of sessions"))]
  18. public class GetSessions : IReturn<List<SessionInfoDto>>
  19. {
  20. /// <summary>
  21. /// Gets or sets a value indicating whether [supports remote control].
  22. /// </summary>
  23. /// <value><c>null</c> if [supports remote control] contains no value, <c>true</c> if [supports remote control]; otherwise, <c>false</c>.</value>
  24. [ApiMember(Name = "SupportsRemoteControl", Description = "Optional. Filter by sessions that can be remote controlled.", IsRequired = false, DataType = "boolean", ParameterType = "query", Verb = "GET")]
  25. public bool? SupportsRemoteControl { get; set; }
  26. }
  27. /// <summary>
  28. /// Class BrowseTo
  29. /// </summary>
  30. [Route("/Sessions/{Id}/Viewing", "POST")]
  31. [Api(("Instructs a session to browse to an item or view"))]
  32. public class BrowseTo : IReturnVoid
  33. {
  34. /// <summary>
  35. /// Gets or sets the id.
  36. /// </summary>
  37. /// <value>The id.</value>
  38. [ApiMember(Name = "Id", Description = "Session Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
  39. public Guid Id { get; set; }
  40. /// <summary>
  41. /// Artist, Genre, Studio, Person, or any kind of BaseItem
  42. /// </summary>
  43. /// <value>The type of the item.</value>
  44. [ApiMember(Name = "ItemType", Description = "Only required if the item is an Artist, Genre, Studio, or Person.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "POST")]
  45. public string ItemType { get; set; }
  46. /// <summary>
  47. /// Artist name, genre name, item Id, etc
  48. /// </summary>
  49. /// <value>The item identifier.</value>
  50. [ApiMember(Name = "ItemId", Description = "The Id of the item.", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
  51. public string ItemId { get; set; }
  52. /// <summary>
  53. /// Gets or sets the name of the item.
  54. /// </summary>
  55. /// <value>The name of the item.</value>
  56. [ApiMember(Name = "ItemName", Description = "The name of the item.", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
  57. public string ItemName { get; set; }
  58. /// <summary>
  59. /// Gets or sets the context (Movies, Music, TvShows, etc)
  60. /// Applicable to genres, studios and persons only because the context of items and artists can be inferred.
  61. /// This is optional to supply and clients are free to ignore it.
  62. /// </summary>
  63. /// <value>The context.</value>
  64. [ApiMember(Name = "Context", Description = "The navigation context for the client (movies, music, tvshows, games etc). This is optional to supply and clients are free to ignore it.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "POST")]
  65. public string Context { get; set; }
  66. }
  67. /// <summary>
  68. /// Class SessionsService
  69. /// </summary>
  70. public class SessionsService : BaseApiService
  71. {
  72. /// <summary>
  73. /// The _session manager
  74. /// </summary>
  75. private readonly ISessionManager _sessionManager;
  76. /// <summary>
  77. /// Initializes a new instance of the <see cref="SessionsService" /> class.
  78. /// </summary>
  79. /// <param name="sessionManager">The session manager.</param>
  80. public SessionsService(ISessionManager sessionManager)
  81. {
  82. _sessionManager = sessionManager;
  83. }
  84. /// <summary>
  85. /// Gets the specified request.
  86. /// </summary>
  87. /// <param name="request">The request.</param>
  88. /// <returns>System.Object.</returns>
  89. public object Get(GetSessions request)
  90. {
  91. var result = _sessionManager.Sessions.Where(i => i.IsActive);
  92. if (request.SupportsRemoteControl.HasValue)
  93. {
  94. result = result.Where(i => i.IsActive == request.SupportsRemoteControl.Value);
  95. }
  96. return ToOptimizedResult(result.Select(SessionInfoDtoBuilder.GetSessionInfoDto).ToList());
  97. }
  98. /// <summary>
  99. /// Posts the specified request.
  100. /// </summary>
  101. /// <param name="request">The request.</param>
  102. /// <exception cref="ResourceNotFoundException"></exception>
  103. public async void Post(BrowseTo request)
  104. {
  105. var session = _sessionManager.Sessions.FirstOrDefault(i => i.Id == request.Id);
  106. if (session == null)
  107. {
  108. throw new ResourceNotFoundException(string.Format("Session {0} not found.", request.Id));
  109. }
  110. var socket = session.WebSockets.OrderByDescending(i => i.LastActivityDate).FirstOrDefault(i => i.State == WebSocketState.Open);
  111. if (socket != null)
  112. {
  113. try
  114. {
  115. await socket.SendAsync(new WebSocketMessage<BrowseTo>
  116. {
  117. MessageType = "Browse",
  118. Data = request
  119. }, CancellationToken.None).ConfigureAwait(false);
  120. }
  121. catch (Exception ex)
  122. {
  123. Logger.ErrorException("Error sending web socket message", ex);
  124. }
  125. }
  126. else
  127. {
  128. throw new InvalidOperationException("The requested session does not have an open web socket.");
  129. }
  130. }
  131. }
  132. }