2013-05-10 14:18:07 +02:00
using MediaBrowser.Common.Extensions ;
using MediaBrowser.Controller.Dto ;
2013-05-10 00:43:11 +02:00
using MediaBrowser.Controller.Session ;
2013-05-10 14:18:07 +02:00
using MediaBrowser.Model.Net ;
2013-05-09 19:38:02 +02:00
using MediaBrowser.Model.Session ;
using ServiceStack.ServiceHost ;
2013-05-10 14:18:07 +02:00
using System ;
2013-05-09 19:38:02 +02:00
using System.Collections.Generic ;
using System.Linq ;
2013-05-10 14:18:07 +02:00
using System.Threading ;
2013-05-09 19:38:02 +02:00
namespace MediaBrowser.Api
{
/// <summary>
/// Class GetSessions
/// </summary>
[Route("/Sessions", "GET")]
[Api(("Gets a list of sessions"))]
2013-05-10 00:43:11 +02:00
public class GetSessions : IReturn < List < SessionInfoDto > >
2013-05-09 19:38:02 +02:00
{
2013-05-11 18:45:20 +02:00
/// <summary>
/// Gets or sets a value indicating whether [supports remote control].
/// </summary>
/// <value><c>null</c> if [supports remote control] contains no value, <c>true</c> if [supports remote control]; otherwise, <c>false</c>.</value>
2013-05-10 14:18:07 +02:00
[ApiMember(Name = "SupportsRemoteControl", Description = "Optional. Filter by sessions that can be remote controlled.", IsRequired = false, DataType = "boolean", ParameterType = "query", Verb = "GET")]
public bool? SupportsRemoteControl { get ; set ; }
}
2013-05-11 18:45:20 +02:00
/// <summary>
/// Class BrowseTo
/// </summary>
2013-05-10 14:18:07 +02:00
[Route("/Sessions/{Id}/Viewing", "POST")]
[Api(("Instructs a session to browse to an item or view"))]
public class BrowseTo : IReturnVoid
{
2013-05-11 18:45:20 +02:00
/// <summary>
/// Gets or sets the id.
/// </summary>
/// <value>The id.</value>
2013-05-10 14:18:07 +02:00
[ApiMember(Name = "Id", Description = "Session Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
public Guid Id { get ; set ; }
2013-05-09 19:38:02 +02:00
/// <summary>
2013-05-10 14:18:07 +02:00
/// Artist, Genre, Studio, Person, or any kind of BaseItem
2013-05-09 19:38:02 +02:00
/// </summary>
2013-05-10 14:18:07 +02:00
/// <value>The type of the item.</value>
2013-05-26 03:53:34 +02:00
[ApiMember(Name = "ItemType", Description = "Only required if the item is an Artist, Genre, Studio, or Person.", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
2013-05-10 14:18:07 +02:00
public string ItemType { get ; set ; }
/// <summary>
/// Artist name, genre name, item Id, etc
/// </summary>
/// <value>The item identifier.</value>
2013-05-26 02:53:51 +02:00
[ApiMember(Name = "ItemId", Description = "The Id of the item.", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
public string ItemId { get ; set ; }
2013-05-10 14:18:07 +02:00
2013-05-26 02:53:51 +02:00
/// <summary>
/// Gets or sets the name of the item.
/// </summary>
/// <value>The name of the item.</value>
[ApiMember(Name = "ItemName", Description = "The name of the item.", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
public string ItemName { get ; set ; }
2013-05-10 14:18:07 +02:00
/// <summary>
/// Gets or sets the context (Movies, Music, TvShows, etc)
/// Applicable to genres, studios and persons only because the context of items and artists can be inferred.
/// This is optional to supply and clients are free to ignore it.
/// </summary>
/// <value>The context.</value>
2013-05-26 03:53:34 +02:00
[ApiMember(Name = "Context", Description = "The ui context for the client (movies, music, tv, games etc). This is optional to supply and clients are free to ignore it.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "POST")]
2013-05-10 14:18:07 +02:00
public string Context { get ; set ; }
2013-05-09 19:38:02 +02:00
}
/// <summary>
/// Class SessionsService
/// </summary>
public class SessionsService : BaseApiService
{
/// <summary>
/// The _session manager
/// </summary>
private readonly ISessionManager _sessionManager ;
/// <summary>
2013-05-11 18:45:20 +02:00
/// Initializes a new instance of the <see cref="SessionsService" /> class.
2013-05-09 19:38:02 +02:00
/// </summary>
/// <param name="sessionManager">The session manager.</param>
public SessionsService ( ISessionManager sessionManager )
{
_sessionManager = sessionManager ;
}
/// <summary>
/// Gets the specified request.
/// </summary>
/// <param name="request">The request.</param>
/// <returns>System.Object.</returns>
public object Get ( GetSessions request )
{
2013-05-10 14:18:07 +02:00
var result = _sessionManager . Sessions . Where ( i = > i . IsActive ) ;
if ( request . SupportsRemoteControl . HasValue )
{
result = result . Where ( i = > i . IsActive = = request . SupportsRemoteControl . Value ) ;
}
2013-05-09 19:38:02 +02:00
2013-05-10 00:43:11 +02:00
return ToOptimizedResult ( result . Select ( SessionInfoDtoBuilder . GetSessionInfoDto ) . ToList ( ) ) ;
2013-05-09 19:38:02 +02:00
}
2013-05-10 14:18:07 +02:00
2013-05-11 18:45:20 +02:00
/// <summary>
/// Posts the specified request.
/// </summary>
/// <param name="request">The request.</param>
/// <exception cref="ResourceNotFoundException"></exception>
2013-05-26 02:53:51 +02:00
public async void Post ( BrowseTo request )
2013-05-10 14:18:07 +02:00
{
var session = _sessionManager . Sessions . FirstOrDefault ( i = > i . Id = = request . Id ) ;
if ( session = = null )
{
throw new ResourceNotFoundException ( string . Format ( "Session {0} not found." , request . Id ) ) ;
}
2013-05-26 02:53:51 +02:00
var socket = session . WebSockets . OrderByDescending ( i = > i . LastActivityDate ) . FirstOrDefault ( i = > i . State = = WebSocketState . Open ) ;
if ( socket ! = null )
2013-05-10 14:18:07 +02:00
{
2013-05-26 02:53:51 +02:00
try
2013-05-11 18:41:50 +02:00
{
2013-05-26 02:53:51 +02:00
await socket . SendAsync ( new WebSocketMessage < BrowseTo >
{
MessageType = "Browse" ,
Data = request
2013-05-10 14:18:07 +02:00
2013-05-26 02:53:51 +02:00
} , CancellationToken . None ) . ConfigureAwait ( false ) ;
}
catch ( Exception ex )
{
Logger . ErrorException ( "Error sending web socket message" , ex ) ;
}
}
else
{
throw new InvalidOperationException ( "The requested session does not have an open web socket." ) ;
2013-05-11 18:41:50 +02:00
}
2013-05-10 14:18:07 +02:00
}
2013-05-09 19:38:02 +02:00
}
}