HttpSessionController.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. using System.Collections.Generic;
  2. using System.Collections.Specialized;
  3. using System.Linq;
  4. using System.Net;
  5. using MediaBrowser.Common.Net;
  6. using MediaBrowser.Controller.Session;
  7. using MediaBrowser.Model.Entities;
  8. using MediaBrowser.Model.Net;
  9. using MediaBrowser.Model.Serialization;
  10. using MediaBrowser.Model.Session;
  11. using MediaBrowser.Model.System;
  12. using System;
  13. using System.Threading;
  14. using System.Threading.Tasks;
  15. namespace MediaBrowser.Server.Implementations.Session
  16. {
  17. public class HttpSessionController : ISessionController
  18. {
  19. private readonly IHttpClient _httpClient;
  20. private readonly IJsonSerializer _json;
  21. public SessionInfo Session { get; private set; }
  22. private readonly string _postUrl;
  23. public HttpSessionController(IHttpClient httpClient,
  24. IJsonSerializer json,
  25. SessionInfo session,
  26. string postUrl)
  27. {
  28. _httpClient = httpClient;
  29. _json = json;
  30. Session = session;
  31. _postUrl = postUrl;
  32. }
  33. public bool IsSessionActive
  34. {
  35. get
  36. {
  37. return (DateTime.UtcNow - Session.LastActivityDate).TotalMinutes <= 10;
  38. }
  39. }
  40. public bool SupportsMediaControl
  41. {
  42. get { return true; }
  43. }
  44. private Task SendMessage(object obj, CancellationToken cancellationToken)
  45. {
  46. var json = _json.SerializeToString(obj);
  47. return _httpClient.Post(new HttpRequestOptions
  48. {
  49. Url = _postUrl,
  50. CancellationToken = cancellationToken,
  51. RequestContent = json,
  52. RequestContentType = "application/json"
  53. });
  54. }
  55. private Task SendMessage(string name, CancellationToken cancellationToken)
  56. {
  57. return SendMessage(name, new Dictionary<string, string>(), cancellationToken);
  58. }
  59. private Task SendMessage(string name, Dictionary<string, string> args, CancellationToken cancellationToken)
  60. {
  61. var url = _postUrl + "/" + name + ToQueryString(args);
  62. return _httpClient.Post(new HttpRequestOptions
  63. {
  64. Url = url,
  65. CancellationToken = cancellationToken
  66. });
  67. }
  68. public Task SendSessionEndedNotification(SessionInfoDto sessionInfo, CancellationToken cancellationToken)
  69. {
  70. return Task.FromResult(true);
  71. }
  72. public Task SendPlaybackStartNotification(SessionInfoDto sessionInfo, CancellationToken cancellationToken)
  73. {
  74. return Task.FromResult(true);
  75. }
  76. public Task SendPlaybackStoppedNotification(SessionInfoDto sessionInfo, CancellationToken cancellationToken)
  77. {
  78. return Task.FromResult(true);
  79. }
  80. public Task SendPlayCommand(PlayRequest command, CancellationToken cancellationToken)
  81. {
  82. return Task.FromResult(true);
  83. //return SendMessage(new WebSocketMessage<PlayRequest>
  84. //{
  85. // MessageType = "Play",
  86. // Data = command
  87. //}, cancellationToken);
  88. }
  89. public Task SendPlaystateCommand(PlaystateRequest command, CancellationToken cancellationToken)
  90. {
  91. var args = new Dictionary<string, string>();
  92. if (command.Command == PlaystateCommand.Seek)
  93. {
  94. }
  95. return SendMessage(command.Command.ToString(), cancellationToken);
  96. }
  97. public Task SendLibraryUpdateInfo(LibraryUpdateInfo info, CancellationToken cancellationToken)
  98. {
  99. return Task.FromResult(true);
  100. }
  101. public Task SendRestartRequiredNotification(SystemInfo info, CancellationToken cancellationToken)
  102. {
  103. return SendMessage("RestartRequired", cancellationToken);
  104. }
  105. public Task SendUserDataChangeInfo(UserDataChangeInfo info, CancellationToken cancellationToken)
  106. {
  107. return Task.FromResult(true);
  108. }
  109. public Task SendServerShutdownNotification(CancellationToken cancellationToken)
  110. {
  111. return SendMessage("ServerShuttingDown", cancellationToken);
  112. }
  113. public Task SendServerRestartNotification(CancellationToken cancellationToken)
  114. {
  115. return SendMessage("ServerRestarting", cancellationToken);
  116. }
  117. public Task SendGeneralCommand(GeneralCommand command, CancellationToken cancellationToken)
  118. {
  119. return SendMessage(command.Name, command.Arguments, cancellationToken);
  120. }
  121. private string ToQueryString(Dictionary<string, string> nvc)
  122. {
  123. var array = (from item in nvc
  124. select string.Format("{0}={1}", WebUtility.UrlEncode(item.Key), WebUtility.UrlEncode(item.Value)))
  125. .ToArray();
  126. var args = string.Join("&", array);
  127. if (string.IsNullOrEmpty(args))
  128. {
  129. return args;
  130. }
  131. return "?" + args;
  132. }
  133. }
  134. }