RokuSessionController.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.Roku
  13. {
  14. public class RokuSessionController : 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. public RokuSessionController(IHttpClient httpClient, IJsonSerializer json, IServerApplicationHost appHost, SessionInfo session)
  21. {
  22. _httpClient = httpClient;
  23. _json = json;
  24. _appHost = appHost;
  25. Session = session;
  26. }
  27. public bool SupportsMediaRemoteControl
  28. {
  29. get { return false; }
  30. }
  31. public bool IsSessionActive
  32. {
  33. get
  34. {
  35. return (DateTime.UtcNow - Session.LastActivityDate).TotalMinutes <= 10;
  36. }
  37. }
  38. public Task SendMessageCommand(MessageCommand command, CancellationToken cancellationToken)
  39. {
  40. return SendCommand(new WebSocketMessage<MessageCommand>
  41. {
  42. MessageType = "MessageCommand",
  43. Data = command
  44. }, cancellationToken);
  45. }
  46. public Task SendPlayCommand(PlayRequest command, CancellationToken cancellationToken)
  47. {
  48. return SendCommand(new WebSocketMessage<PlayRequest>
  49. {
  50. MessageType = "Play",
  51. Data = command
  52. }, cancellationToken);
  53. }
  54. public Task SendBrowseCommand(BrowseRequest command, CancellationToken cancellationToken)
  55. {
  56. return SendCommand(new WebSocketMessage<BrowseRequest>
  57. {
  58. MessageType = "Browse",
  59. Data = command
  60. }, cancellationToken);
  61. }
  62. public Task SendPlaystateCommand(PlaystateRequest command, CancellationToken cancellationToken)
  63. {
  64. return SendCommand(new WebSocketMessage<PlaystateRequest>
  65. {
  66. MessageType = "Playstate",
  67. Data = command
  68. }, cancellationToken);
  69. }
  70. private readonly Task _cachedTask = Task.FromResult(true);
  71. public Task SendLibraryUpdateInfo(LibraryUpdateInfo info, CancellationToken cancellationToken)
  72. {
  73. // Roku probably won't care about this
  74. return _cachedTask;
  75. }
  76. public Task SendRestartRequiredNotification(CancellationToken cancellationToken)
  77. {
  78. return SendCommand(new WebSocketMessage<SystemInfo>
  79. {
  80. MessageType = "RestartRequired",
  81. Data = _appHost.GetSystemInfo()
  82. }, cancellationToken);
  83. }
  84. public Task SendUserDataChangeInfo(UserDataChangeInfo info, CancellationToken cancellationToken)
  85. {
  86. // Roku probably won't care about this
  87. return _cachedTask;
  88. }
  89. public Task SendServerShutdownNotification(CancellationToken cancellationToken)
  90. {
  91. return SendCommand(new WebSocketMessage<string>
  92. {
  93. MessageType = "ServerShuttingDown",
  94. Data = string.Empty
  95. }, cancellationToken);
  96. }
  97. public Task SendServerRestartNotification(CancellationToken cancellationToken)
  98. {
  99. return SendCommand(new WebSocketMessage<string>
  100. {
  101. MessageType = "ServerRestarting",
  102. Data = string.Empty
  103. }, cancellationToken);
  104. }
  105. private Task SendCommand(object obj, CancellationToken cancellationToken)
  106. {
  107. var json = _json.SerializeToString(obj);
  108. return _httpClient.Post(new HttpRequestOptions
  109. {
  110. Url = "http://" + Session.RemoteEndPoint + "/mb/remotecontrol",
  111. CancellationToken = cancellationToken,
  112. RequestContent = json,
  113. RequestContentType = "application/json"
  114. });
  115. }
  116. public Task SendGeneralCommand(GeneralCommand command, CancellationToken cancellationToken)
  117. {
  118. return SendCommand(new WebSocketMessage<GeneralCommand>
  119. {
  120. MessageType = "Command",
  121. Data = command
  122. }, cancellationToken);
  123. }
  124. }
  125. }