2
0

SessionsService.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 = "ItemIdentifier", Description = "The Id of the item, unless it is an Artist, Genre, Studio, or Person, in which case it should be the name.", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
  51. public string ItemIdentifier { get; set; }
  52. /// <summary>
  53. /// Gets or sets the context (Movies, Music, TvShows, etc)
  54. /// Applicable to genres, studios and persons only because the context of items and artists can be inferred.
  55. /// This is optional to supply and clients are free to ignore it.
  56. /// </summary>
  57. /// <value>The context.</value>
  58. [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")]
  59. public string Context { get; set; }
  60. }
  61. /// <summary>
  62. /// Class SessionsService
  63. /// </summary>
  64. public class SessionsService : BaseApiService
  65. {
  66. /// <summary>
  67. /// The _session manager
  68. /// </summary>
  69. private readonly ISessionManager _sessionManager;
  70. /// <summary>
  71. /// Initializes a new instance of the <see cref="SessionsService" /> class.
  72. /// </summary>
  73. /// <param name="sessionManager">The session manager.</param>
  74. public SessionsService(ISessionManager sessionManager)
  75. {
  76. _sessionManager = sessionManager;
  77. }
  78. /// <summary>
  79. /// Gets the specified request.
  80. /// </summary>
  81. /// <param name="request">The request.</param>
  82. /// <returns>System.Object.</returns>
  83. public object Get(GetSessions request)
  84. {
  85. var result = _sessionManager.Sessions.Where(i => i.IsActive);
  86. if (request.SupportsRemoteControl.HasValue)
  87. {
  88. result = result.Where(i => i.IsActive == request.SupportsRemoteControl.Value);
  89. }
  90. return ToOptimizedResult(result.Select(SessionInfoDtoBuilder.GetSessionInfoDto).ToList());
  91. }
  92. /// <summary>
  93. /// Posts the specified request.
  94. /// </summary>
  95. /// <param name="request">The request.</param>
  96. /// <exception cref="ResourceNotFoundException"></exception>
  97. public void Post(BrowseTo request)
  98. {
  99. var session = _sessionManager.Sessions.FirstOrDefault(i => i.Id == request.Id);
  100. if (session == null)
  101. {
  102. throw new ResourceNotFoundException(string.Format("Session {0} not found.", request.Id));
  103. }
  104. foreach (var socket in session.WebSockets)
  105. {
  106. socket.SendAsync(new WebSocketMessage<BrowseTo>
  107. {
  108. MessageType = "Browse",
  109. Data = request
  110. }, CancellationToken.None);
  111. }
  112. }
  113. }
  114. }