support more kinds of remote control besides web socket

This commit is contained in:
Luke Pulverenti
2013-09-24 11:42:30 -04:00
parent 04319900f9
commit c61cc4a304
8 changed files with 163 additions and 45 deletions

View File

@@ -325,49 +325,11 @@ namespace MediaBrowser.Api
/// <param name="request">The request.</param>
public void Post(SendSystemCommand request)
{
var task = SendSystemCommand(request);
var task = _sessionManager.SendSystemCommand(request.Id, request.Command, CancellationToken.None);
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>