using MediaBrowser.Common.Extensions;
using MediaBrowser.Controller.Dto;
using MediaBrowser.Controller.Session;
using MediaBrowser.Model.Net;
using MediaBrowser.Model.Session;
using ServiceStack.ServiceHost;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
namespace MediaBrowser.Api
{
///
/// Class GetSessions
///
[Route("/Sessions", "GET")]
[Api(("Gets a list of sessions"))]
public class GetSessions : IReturn>
{
///
/// Gets or sets a value indicating whether [supports remote control].
///
/// null if [supports remote control] contains no value, true if [supports remote control]; otherwise, false.
[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; }
}
///
/// Class BrowseTo
///
[Route("/Sessions/{Id}/Viewing", "POST")]
[Api(("Instructs a session to browse to an item or view"))]
public class BrowseTo : IReturnVoid
{
///
/// Gets or sets the id.
///
/// The id.
[ApiMember(Name = "Id", Description = "Session Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
public Guid Id { get; set; }
///
/// Artist, Genre, Studio, Person, or any kind of BaseItem
///
/// The type of the item.
[ApiMember(Name = "ItemType", Description = "Only required if the item is an Artist, Genre, Studio, or Person.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "POST")]
public string ItemType { get; set; }
///
/// Artist name, genre name, item Id, etc
///
/// The item identifier.
[ApiMember(Name = "ItemId", Description = "The Id of the item.", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
public string ItemId { get; set; }
///
/// Gets or sets the name of the item.
///
/// The name of the item.
[ApiMember(Name = "ItemName", Description = "The name of the item.", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
public string ItemName { get; set; }
///
/// 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.
///
/// The context.
[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")]
public string Context { get; set; }
}
///
/// Class SessionsService
///
public class SessionsService : BaseApiService
{
///
/// The _session manager
///
private readonly ISessionManager _sessionManager;
///
/// Initializes a new instance of the class.
///
/// The session manager.
public SessionsService(ISessionManager sessionManager)
{
_sessionManager = sessionManager;
}
///
/// Gets the specified request.
///
/// The request.
/// System.Object.
public object Get(GetSessions request)
{
var result = _sessionManager.Sessions.Where(i => i.IsActive);
if (request.SupportsRemoteControl.HasValue)
{
result = result.Where(i => i.IsActive == request.SupportsRemoteControl.Value);
}
return ToOptimizedResult(result.Select(SessionInfoDtoBuilder.GetSessionInfoDto).ToList());
}
///
/// Posts the specified request.
///
/// The request.
///
public async void Post(BrowseTo 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));
}
var socket = session.WebSockets.OrderByDescending(i => i.LastActivityDate).FirstOrDefault(i => i.State == WebSocketState.Open);
if (socket != null)
{
try
{
await socket.SendAsync(new WebSocketMessage
{
MessageType = "Browse",
Data = request
}, 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.");
}
}
}
}