jellyfin/MediaBrowser.Server.Implementations/Session/HttpSessionController.cs

161 lines
4.9 KiB
C#
Raw Normal View History

2014-05-18 19:58:42 +00:00
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Net;
using MediaBrowser.Common.Net;
2013-12-24 00:00:27 +00:00
using MediaBrowser.Controller.Session;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Net;
using MediaBrowser.Model.Serialization;
using MediaBrowser.Model.Session;
using MediaBrowser.Model.System;
using System;
using System.Threading;
using System.Threading.Tasks;
2014-05-12 18:04:25 +00:00
namespace MediaBrowser.Server.Implementations.Session
2013-12-24 00:00:27 +00:00
{
2014-05-12 18:04:25 +00:00
public class HttpSessionController : ISessionController
2013-12-24 00:00:27 +00:00
{
private readonly IHttpClient _httpClient;
private readonly IJsonSerializer _json;
public SessionInfo Session { get; private set; }
2014-05-12 18:04:25 +00:00
private readonly string _postUrl;
2014-05-18 19:58:42 +00:00
public HttpSessionController(IHttpClient httpClient,
IJsonSerializer json,
SessionInfo session,
2014-05-12 18:04:25 +00:00
string postUrl)
2013-12-24 00:00:27 +00:00
{
_httpClient = httpClient;
_json = json;
Session = session;
2014-05-12 18:04:25 +00:00
_postUrl = postUrl;
2013-12-24 00:00:27 +00:00
}
public bool IsSessionActive
{
get
{
return (DateTime.UtcNow - Session.LastActivityDate).TotalMinutes <= 10;
}
}
2014-05-17 18:37:40 +00:00
public bool SupportsMediaControl
{
get { return true; }
}
2014-05-12 18:04:25 +00:00
private Task SendMessage(object obj, CancellationToken cancellationToken)
{
var json = _json.SerializeToString(obj);
return _httpClient.Post(new HttpRequestOptions
{
Url = _postUrl,
CancellationToken = cancellationToken,
RequestContent = json,
RequestContentType = "application/json"
});
}
2014-05-18 19:58:42 +00:00
private Task SendMessage(string name, CancellationToken cancellationToken)
{
return SendMessage(name, new NameValueCollection(), cancellationToken);
}
private Task SendMessage(string name, NameValueCollection args, CancellationToken cancellationToken)
{
return SendMessage(new WebSocketMessage<string>
{
MessageType = name,
Data = string.Empty
}, cancellationToken);
}
2014-04-06 17:53:23 +00:00
public Task SendSessionEndedNotification(SessionInfoDto sessionInfo, CancellationToken cancellationToken)
{
return Task.FromResult(true);
}
public Task SendPlaybackStartNotification(SessionInfoDto sessionInfo, CancellationToken cancellationToken)
{
return Task.FromResult(true);
}
public Task SendPlaybackStoppedNotification(SessionInfoDto sessionInfo, CancellationToken cancellationToken)
{
return Task.FromResult(true);
}
2013-12-24 00:00:27 +00:00
public Task SendPlayCommand(PlayRequest command, CancellationToken cancellationToken)
{
2014-05-18 19:58:42 +00:00
return Task.FromResult(true);
//return SendMessage(new WebSocketMessage<PlayRequest>
//{
// MessageType = "Play",
// Data = command
2013-12-24 00:00:27 +00:00
2014-05-18 19:58:42 +00:00
//}, cancellationToken);
2013-12-24 00:00:27 +00:00
}
public Task SendPlaystateCommand(PlaystateRequest command, CancellationToken cancellationToken)
{
2014-05-18 19:58:42 +00:00
var args = new Dictionary<string, string>();
if (command.Command == PlaystateCommand.Seek)
2013-12-24 00:00:27 +00:00
{
2014-05-18 19:58:42 +00:00
}
return SendMessage(command.Command.ToString(), cancellationToken);
2013-12-24 00:00:27 +00:00
}
public Task SendLibraryUpdateInfo(LibraryUpdateInfo info, CancellationToken cancellationToken)
{
2014-04-06 17:53:23 +00:00
return Task.FromResult(true);
2013-12-24 00:00:27 +00:00
}
2014-05-18 19:58:42 +00:00
public Task SendRestartRequiredNotification(SystemInfo info, CancellationToken cancellationToken)
2013-12-24 00:00:27 +00:00
{
2014-05-18 19:58:42 +00:00
return SendMessage("RestartRequired", cancellationToken);
2013-12-24 00:00:27 +00:00
}
public Task SendUserDataChangeInfo(UserDataChangeInfo info, CancellationToken cancellationToken)
{
2014-04-06 17:53:23 +00:00
return Task.FromResult(true);
2013-12-24 00:00:27 +00:00
}
public Task SendServerShutdownNotification(CancellationToken cancellationToken)
{
2014-05-18 19:58:42 +00:00
return SendMessage("ServerShuttingDown", cancellationToken);
2013-12-24 00:00:27 +00:00
}
public Task SendServerRestartNotification(CancellationToken cancellationToken)
{
2014-05-18 19:58:42 +00:00
return SendMessage("ServerRestarting", cancellationToken);
2013-12-24 00:00:27 +00:00
}
2014-03-31 21:04:22 +00:00
public Task SendGeneralCommand(GeneralCommand command, CancellationToken cancellationToken)
2014-03-28 19:58:18 +00:00
{
2014-05-12 18:04:25 +00:00
return SendMessage(new WebSocketMessage<GeneralCommand>
2014-03-28 19:58:18 +00:00
{
2014-05-12 18:04:25 +00:00
MessageType = "GeneralCommand",
2014-03-28 19:58:18 +00:00
Data = command
}, cancellationToken);
}
2014-05-18 19:58:42 +00:00
private string ToQueryString(Dictionary<string, string> nvc)
{
var array = (from item in nvc
select string.Format("{0}={1}", WebUtility.UrlEncode(item.Key), WebUtility.UrlEncode(item.Value)))
.ToArray();
return "?" + string.Join("&", array);
}
2013-12-24 00:00:27 +00:00
}
}