HttpSessionController.cs 4.6 KB

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