SessionsService.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. [ApiMember(Name = "SupportsRemoteControl", Description = "Optional. Filter by sessions that can be remote controlled.", IsRequired = false, DataType = "boolean", ParameterType = "query", Verb = "GET")]
  21. public bool? SupportsRemoteControl { get; set; }
  22. }
  23. [Route("/Sessions/{Id}/Viewing", "POST")]
  24. [Api(("Instructs a session to browse to an item or view"))]
  25. public class BrowseTo : IReturnVoid
  26. {
  27. [ApiMember(Name = "Id", Description = "Session Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
  28. public Guid Id { get; set; }
  29. /// <summary>
  30. /// Artist, Genre, Studio, Person, or any kind of BaseItem
  31. /// </summary>
  32. /// <value>The type of the item.</value>
  33. [ApiMember(Name = "ItemType", Description = "Only required if the item is an Artist, Genre, Studio, or Person.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "POST")]
  34. public string ItemType { get; set; }
  35. /// <summary>
  36. /// Artist name, genre name, item Id, etc
  37. /// </summary>
  38. /// <value>The item identifier.</value>
  39. [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")]
  40. public string ItemIdentifier { get; set; }
  41. /// <summary>
  42. /// Gets or sets the context (Movies, Music, TvShows, etc)
  43. /// Applicable to genres, studios and persons only because the context of items and artists can be inferred.
  44. /// This is optional to supply and clients are free to ignore it.
  45. /// </summary>
  46. /// <value>The context.</value>
  47. [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")]
  48. public string Context { get; set; }
  49. }
  50. /// <summary>
  51. /// Class SessionsService
  52. /// </summary>
  53. public class SessionsService : BaseApiService
  54. {
  55. /// <summary>
  56. /// The _session manager
  57. /// </summary>
  58. private readonly ISessionManager _sessionManager;
  59. /// <summary>
  60. /// Initializes a new instance of the <see cref="SessionsService"/> class.
  61. /// </summary>
  62. /// <param name="sessionManager">The session manager.</param>
  63. public SessionsService(ISessionManager sessionManager)
  64. {
  65. _sessionManager = sessionManager;
  66. }
  67. /// <summary>
  68. /// Gets the specified request.
  69. /// </summary>
  70. /// <param name="request">The request.</param>
  71. /// <returns>System.Object.</returns>
  72. public object Get(GetSessions request)
  73. {
  74. var result = _sessionManager.Sessions.Where(i => i.IsActive);
  75. if (request.SupportsRemoteControl.HasValue)
  76. {
  77. result = result.Where(i => i.IsActive == request.SupportsRemoteControl.Value);
  78. }
  79. return ToOptimizedResult(result.Select(SessionInfoDtoBuilder.GetSessionInfoDto).ToList());
  80. }
  81. public void Post(BrowseTo request)
  82. {
  83. var session = _sessionManager.Sessions.FirstOrDefault(i => i.Id == request.Id);
  84. if (session == null)
  85. {
  86. throw new ResourceNotFoundException(string.Format("Session {0} not found.", request.Id));
  87. }
  88. session.WebSocket.SendAsync(new WebSocketMessage<BrowseTo>
  89. {
  90. MessageType = "Browse",
  91. Data = request
  92. }, CancellationToken.None);
  93. }
  94. }
  95. }