2013-05-10 05:18:07 -07:00
using MediaBrowser.Common.Extensions ;
using MediaBrowser.Controller.Dto ;
2013-05-09 15:43:11 -07:00
using MediaBrowser.Controller.Session ;
2013-05-10 05:18:07 -07:00
using MediaBrowser.Model.Net ;
2013-05-09 10:38:02 -07:00
using MediaBrowser.Model.Session ;
using ServiceStack.ServiceHost ;
2013-05-10 05:18:07 -07:00
using System ;
2013-05-09 10:38:02 -07:00
using System.Collections.Generic ;
using System.Linq ;
2013-05-10 05:18:07 -07:00
using System.Threading ;
2013-05-28 10:25:10 -07:00
using System.Threading.Tasks ;
2013-05-09 10:38:02 -07:00
namespace MediaBrowser.Api
{
/// <summary>
/// Class GetSessions
/// </summary>
[Route("/Sessions", "GET")]
[Api(("Gets a list of sessions"))]
2013-05-09 15:43:11 -07:00
public class GetSessions : IReturn < List < SessionInfoDto > >
2013-05-09 10:38:02 -07:00
{
2013-05-11 09:45:20 -07: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 05:18:07 -07: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 09:45:20 -07:00
/// <summary>
/// Class BrowseTo
/// </summary>
2013-05-10 05:18:07 -07:00
[Route("/Sessions/{Id}/Viewing", "POST")]
[Api(("Instructs a session to browse to an item or view"))]
public class BrowseTo : IReturnVoid
{
2013-05-11 09:45:20 -07:00
/// <summary>
/// Gets or sets the id.
/// </summary>
/// <value>The id.</value>
2013-05-10 05:18:07 -07:00
[ApiMember(Name = "Id", Description = "Session Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
public Guid Id { get ; set ; }
2013-05-09 10:38:02 -07:00
/// <summary>
2013-05-10 05:18:07 -07:00
/// Artist, Genre, Studio, Person, or any kind of BaseItem
2013-05-09 10:38:02 -07:00
/// </summary>
2013-05-10 05:18:07 -07:00
/// <value>The type of the item.</value>
2013-05-28 10:25:10 -07:00
[ApiMember(Name = "ItemType", Description = "The type of item to browse to.", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
2013-05-10 05:18:07 -07:00
public string ItemType { get ; set ; }
/// <summary>
/// Artist name, genre name, item Id, etc
/// </summary>
/// <value>The item identifier.</value>
2013-05-25 17:53:51 -07: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 05:18:07 -07:00
2013-05-25 17:53:51 -07: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-28 10:25:10 -07:00
2013-05-10 05:18:07 -07: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-25 18:53:34 -07: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 05:18:07 -07:00
public string Context { get ; set ; }
2013-05-09 10:38:02 -07:00
}
2013-05-28 10:25:10 -07:00
[Route("/Sessions/{Id}/Playing", "POST")]
[Api(("Instructs a session to play an item"))]
public class Play : IReturnVoid
{
/// <summary>
/// Gets or sets the id.
/// </summary>
/// <value>The id.</value>
[ApiMember(Name = "Id", Description = "Session Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
public Guid Id { get ; set ; }
/// <summary>
/// Artist, Genre, Studio, Person, or any kind of BaseItem
/// </summary>
/// <value>The type of the item.</value>
[ApiMember(Name = "ItemIds", Description = "The ids of the items to play, comma delimited", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST", AllowMultiple = true)]
public string ItemIds { get ; set ; }
/// <summary>
/// Gets or sets the start position ticks that the first item should be played at
/// </summary>
/// <value>The start position ticks.</value>
[ApiMember(Name = "StartPositionTicks", Description = "The starting position of the first item.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "POST")]
public long? StartPositionTicks { get ; set ; }
/// <summary>
/// Gets or sets the play command.
/// </summary>
/// <value>The play command.</value>
[ApiMember(Name = "PlayCommand", Description = "The type of play command to issue (PlayNow, PlayNext, PlayLast). Clients who have not yet implemented play next and play last may play now.", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
public PlayCommand PlayCommand { get ; set ; }
}
2013-07-09 09:11:16 -07:00
[Route("/Sessions/{Id}/Playing/{Command}", "POST")]
[Api(("Issues a playstate command to a client"))]
public class SendPlaystateCommand : IReturnVoid
{
/// <summary>
/// Gets or sets the id.
/// </summary>
/// <value>The id.</value>
[ApiMember(Name = "Id", Description = "Session Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
public Guid Id { get ; set ; }
/// <summary>
/// Gets or sets the position to seek to
/// </summary>
2013-07-19 11:36:44 -07:00
[ApiMember(Name = "SeekPositionTicks", Description = "The position to seek to.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "POST")]
public long? SeekPositionTicks { get ; set ; }
2013-07-09 09:11:16 -07:00
/// <summary>
/// Gets or sets the play command.
/// </summary>
/// <value>The play command.</value>
[ApiMember(Name = "Command", Description = "The command to send - stop, pause, unpause, nexttrack, previoustrack, seek.", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
public PlaystateCommand Command { get ; set ; }
}
2013-08-27 21:16:21 -07:00
[Route("/Sessions/{Id}/System/{Command}", "POST")]
[Api(("Issues a system command to a client"))]
public class SendSystemCommand : IReturnVoid
{
/// <summary>
/// Gets or sets the id.
/// </summary>
/// <value>The id.</value>
[ApiMember(Name = "Id", Description = "Session Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
public Guid Id { get ; set ; }
/// <summary>
/// Gets or sets the command.
/// </summary>
/// <value>The play command.</value>
[ApiMember(Name = "Command", Description = "The command to send.", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
public SystemCommand Command { get ; set ; }
}
[Route("/Sessions/{Id}/Message", "POST")]
[Api(("Issues a command to a client to display a message to the user"))]
public class SendMessageCommand : IReturnVoid
{
/// <summary>
/// Gets or sets the id.
/// </summary>
/// <value>The id.</value>
[ApiMember(Name = "Id", Description = "Session Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
public Guid Id { get ; set ; }
[ApiMember(Name = "Text", Description = "The message text.", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
public string Text { get ; set ; }
[ApiMember(Name = "Header", Description = "The message header.", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
public string Header { get ; set ; }
2013-08-27 21:31:34 -07:00
[ApiMember(Name = "TimeoutMs", Description = "The message timeout. If omitted the user will have to confirm viewing the message.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "POST")]
2013-08-27 21:16:21 -07:00
public long? TimeoutMs { get ; set ; }
}
2013-05-09 10:38:02 -07:00
/// <summary>
/// Class SessionsService
/// </summary>
public class SessionsService : BaseApiService
{
/// <summary>
/// The _session manager
/// </summary>
private readonly ISessionManager _sessionManager ;
2013-09-04 10:02:19 -07:00
private readonly IDtoService _dtoService ;
2013-05-09 10:38:02 -07:00
/// <summary>
2013-05-11 09:45:20 -07:00
/// Initializes a new instance of the <see cref="SessionsService" /> class.
2013-05-09 10:38:02 -07:00
/// </summary>
/// <param name="sessionManager">The session manager.</param>
2013-09-04 10:02:19 -07:00
public SessionsService ( ISessionManager sessionManager , IDtoService dtoService )
2013-05-09 10:38:02 -07:00
{
_sessionManager = sessionManager ;
2013-09-04 10:02:19 -07:00
_dtoService = dtoService ;
2013-05-09 10:38:02 -07:00
}
/// <summary>
/// Gets the specified request.
/// </summary>
/// <param name="request">The request.</param>
/// <returns>System.Object.</returns>
public object Get ( GetSessions request )
{
2013-05-10 05:18:07 -07:00
var result = _sessionManager . Sessions . Where ( i = > i . IsActive ) ;
if ( request . SupportsRemoteControl . HasValue )
{
2013-06-12 08:53:29 -07:00
result = result . Where ( i = > i . SupportsRemoteControl = = request . SupportsRemoteControl . Value ) ;
2013-05-10 05:18:07 -07:00
}
2013-05-09 10:38:02 -07:00
2013-09-04 10:02:19 -07:00
return ToOptimizedResult ( result . Select ( _dtoService . GetSessionInfoDto ) . ToList ( ) ) ;
2013-05-09 10:38:02 -07:00
}
2013-05-10 05:18:07 -07:00
2013-07-09 09:11:16 -07:00
public void Post ( SendPlaystateCommand request )
{
var task = SendPlaystateCommand ( request ) ;
Task . WaitAll ( task ) ;
}
private async Task SendPlaystateCommand ( SendPlaystateCommand request )
{
var session = _sessionManager . Sessions . FirstOrDefault ( i = > i . Id = = request . Id ) ;
if ( session = = null )
{
throw new ResourceNotFoundException ( string . Format ( "Session {0} not found." , request . Id ) ) ;
}
if ( ! session . SupportsRemoteControl )
{
throw new ArgumentException ( string . Format ( "Session {0} does not support remote control." , session . Id ) ) ;
}
var socket = session . WebSockets . OrderByDescending ( i = > i . LastActivityDate ) . FirstOrDefault ( i = > i . State = = WebSocketState . Open ) ;
if ( socket ! = null )
{
try
{
2013-07-09 09:16:52 -07:00
await socket . SendAsync ( new WebSocketMessage < PlaystateRequest >
2013-07-09 09:11:16 -07:00
{
MessageType = "Playstate" ,
2013-07-09 09:16:52 -07:00
Data = new PlaystateRequest
2013-07-09 09:11:16 -07:00
{
Command = request . Command ,
2013-07-19 11:36:44 -07:00
SeekPositionTicks = request . SeekPositionTicks
2013-07-09 09:11:16 -07: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-08-27 21:31:34 -07:00
2013-05-11 09:45:20 -07:00
/// <summary>
/// Posts the specified request.
/// </summary>
/// <param name="request">The request.</param>
2013-05-28 10:25:10 -07:00
public void Post ( BrowseTo request )
{
var task = BrowseTo ( request ) ;
Task . WaitAll ( task ) ;
}
/// <summary>
/// Browses to.
/// </summary>
/// <param name="request">The request.</param>
/// <returns>Task.</returns>
2013-05-11 09:45:20 -07:00
/// <exception cref="ResourceNotFoundException"></exception>
2013-05-28 10:25:10 -07:00
/// <exception cref="System.ArgumentException"></exception>
/// <exception cref="System.InvalidOperationException">The requested session does not have an open web socket.</exception>
private async Task BrowseTo ( BrowseTo request )
2013-05-10 05:18:07 -07: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 18:23:46 -07:00
if ( ! session . SupportsRemoteControl )
{
throw new ArgumentException ( string . Format ( "Session {0} does not support remote control." , session . Id ) ) ;
}
2013-05-25 17:53:51 -07:00
var socket = session . WebSockets . OrderByDescending ( i = > i . LastActivityDate ) . FirstOrDefault ( i = > i . State = = WebSocketState . Open ) ;
if ( socket ! = null )
2013-05-10 05:18:07 -07:00
{
2013-05-25 17:53:51 -07:00
try
2013-05-11 09:41:50 -07:00
{
2013-05-25 17:53:51 -07:00
await socket . SendAsync ( new WebSocketMessage < BrowseTo >
{
MessageType = "Browse" ,
Data = request
2013-05-10 05:18:07 -07:00
2013-05-25 17:53:51 -07: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 09:41:50 -07:00
}
2013-05-10 05:18:07 -07:00
}
2013-05-28 10:25:10 -07:00
2013-08-27 21:16:21 -07:00
/// <summary>
/// Posts the specified request.
/// </summary>
/// <param name="request">The request.</param>
public void Post ( SendSystemCommand request )
{
var task = SendSystemCommand ( request ) ;
Task . WaitAll ( task ) ;
}
private async Task SendSystemCommand ( SendSystemCommand request )
{
var session = _sessionManager . Sessions . FirstOrDefault ( i = > i . Id = = request . Id ) ;
if ( session = = null )
{
throw new ResourceNotFoundException ( string . Format ( "Session {0} not found." , request . Id ) ) ;
}
if ( ! session . SupportsRemoteControl )
{
throw new ArgumentException ( string . Format ( "Session {0} does not support remote control." , session . Id ) ) ;
}
var socket = session . WebSockets . OrderByDescending ( i = > i . LastActivityDate ) . FirstOrDefault ( i = > i . State = = WebSocketState . Open ) ;
if ( socket ! = null )
{
try
{
await socket . SendAsync ( new WebSocketMessage < string >
{
MessageType = "SystemCommand" ,
Data = request . Command . ToString ( )
} , 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." ) ;
}
}
/// <summary>
/// Posts the specified request.
/// </summary>
/// <param name="request">The request.</param>
public void Post ( SendMessageCommand request )
{
var task = SendMessageCommand ( request ) ;
Task . WaitAll ( task ) ;
}
private async Task SendMessageCommand ( SendMessageCommand request )
{
var session = _sessionManager . Sessions . FirstOrDefault ( i = > i . Id = = request . Id ) ;
if ( session = = null )
{
throw new ResourceNotFoundException ( string . Format ( "Session {0} not found." , request . Id ) ) ;
}
if ( ! session . SupportsRemoteControl )
{
throw new ArgumentException ( string . Format ( "Session {0} does not support remote control." , session . Id ) ) ;
}
var socket = session . WebSockets . OrderByDescending ( i = > i . LastActivityDate ) . FirstOrDefault ( i = > i . State = = WebSocketState . Open ) ;
if ( socket ! = null )
{
try
{
await socket . SendAsync ( new WebSocketMessage < MessageCommand >
{
MessageType = "MessageCommand" ,
Data = new MessageCommand
{
2013-08-27 21:31:34 -07:00
Header = string . IsNullOrEmpty ( request . Header ) ? "Message from Server" : request . Header ,
2013-08-27 21:16:21 -07:00
TimeoutMs = request . TimeoutMs ,
Text = request . Text
}
} , 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-28 10:25:10 -07:00
/// <summary>
/// Posts the specified request.
/// </summary>
/// <param name="request">The request.</param>
public void Post ( Play request )
{
var task = Play ( request ) ;
Task . WaitAll ( task ) ;
}
/// <summary>
/// Plays the specified request.
/// </summary>
/// <param name="request">The request.</param>
/// <returns>Task.</returns>
/// <exception cref="ResourceNotFoundException"></exception>
/// <exception cref="System.ArgumentException"></exception>
/// <exception cref="System.InvalidOperationException">The requested session does not have an open web socket.</exception>
private async Task Play ( Play request )
{
var session = _sessionManager . Sessions . FirstOrDefault ( i = > i . Id = = request . Id ) ;
if ( session = = null )
{
throw new ResourceNotFoundException ( string . Format ( "Session {0} not found." , request . Id ) ) ;
}
if ( ! session . SupportsRemoteControl )
{
throw new ArgumentException ( string . Format ( "Session {0} does not support remote control." , session . Id ) ) ;
}
var socket = session . WebSockets . OrderByDescending ( i = > i . LastActivityDate ) . FirstOrDefault ( i = > i . State = = WebSocketState . Open ) ;
if ( socket ! = null )
{
try
{
await socket . SendAsync ( new WebSocketMessage < PlayRequest >
{
MessageType = "Play" ,
Data = new PlayRequest
{
ItemIds = request . ItemIds . Split ( ',' ) . ToArray ( ) ,
PlayCommand = request . PlayCommand ,
StartPositionTicks = request . StartPositionTicks
}
} , 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-09 10:38:02 -07:00
}
}