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

152 lines
4.6 KiB
C#
Raw Normal View History

2013-12-24 00:00:27 +00:00
using MediaBrowser.Common.Net;
using MediaBrowser.Controller;
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;
private readonly IServerApplicationHost _appHost;
public SessionInfo Session { get; private set; }
2014-05-12 18:04:25 +00:00
//var postUrl = string.Format("http://{0}/mediabrowser/message", session.RemoteEndPoint);
private readonly string _postUrl;
public HttpSessionController(IHttpClient httpClient,
IJsonSerializer json,
IServerApplicationHost appHost,
SessionInfo session,
string postUrl)
2013-12-24 00:00:27 +00:00
{
_httpClient = httpClient;
_json = json;
_appHost = appHost;
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-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-12 18:04:25 +00:00
return SendMessage(new WebSocketMessage<PlayRequest>
2013-12-24 00:00:27 +00:00
{
MessageType = "Play",
Data = command
}, cancellationToken);
}
public Task SendPlaystateCommand(PlaystateRequest command, CancellationToken cancellationToken)
{
2014-05-12 18:04:25 +00:00
return SendMessage(new WebSocketMessage<PlaystateRequest>
2013-12-24 00:00:27 +00:00
{
MessageType = "Playstate",
Data = command
}, cancellationToken);
}
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
}
public Task SendRestartRequiredNotification(CancellationToken cancellationToken)
{
2014-05-12 18:04:25 +00:00
return SendMessage(new WebSocketMessage<SystemInfo>
2013-12-24 00:00:27 +00:00
{
MessageType = "RestartRequired",
Data = _appHost.GetSystemInfo()
}, cancellationToken);
}
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-12 18:04:25 +00:00
return SendMessage(new WebSocketMessage<string>
2013-12-24 00:00:27 +00:00
{
MessageType = "ServerShuttingDown",
Data = string.Empty
}, cancellationToken);
}
public Task SendServerRestartNotification(CancellationToken cancellationToken)
{
2014-05-12 18:04:25 +00:00
return SendMessage(new WebSocketMessage<string>
2013-12-24 00:00:27 +00:00
{
MessageType = "ServerRestarting",
Data = string.Empty
}, cancellationToken);
}
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);
}
2013-12-24 00:00:27 +00:00
}
}