RokuSessionController.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 SendSystemCommand(SystemCommand command, CancellationToken cancellationToken)
  39. {
  40. return SendCommand(new WebSocketMessage<string>
  41. {
  42. MessageType = "SystemCommand",
  43. Data = command.ToString()
  44. }, cancellationToken);
  45. }
  46. public Task SendMessageCommand(MessageCommand command, CancellationToken cancellationToken)
  47. {
  48. return SendCommand(new WebSocketMessage<MessageCommand>
  49. {
  50. MessageType = "MessageCommand",
  51. Data = command
  52. }, cancellationToken);
  53. }
  54. public Task SendPlayCommand(PlayRequest command, CancellationToken cancellationToken)
  55. {
  56. return SendCommand(new WebSocketMessage<PlayRequest>
  57. {
  58. MessageType = "Play",
  59. Data = command
  60. }, cancellationToken);
  61. }
  62. public Task SendBrowseCommand(BrowseRequest command, CancellationToken cancellationToken)
  63. {
  64. return SendCommand(new WebSocketMessage<BrowseRequest>
  65. {
  66. MessageType = "Browse",
  67. Data = command
  68. }, cancellationToken);
  69. }
  70. public Task SendPlaystateCommand(PlaystateRequest command, CancellationToken cancellationToken)
  71. {
  72. return SendCommand(new WebSocketMessage<PlaystateRequest>
  73. {
  74. MessageType = "Playstate",
  75. Data = command
  76. }, cancellationToken);
  77. }
  78. private readonly Task _cachedTask = Task.FromResult(true);
  79. public Task SendLibraryUpdateInfo(LibraryUpdateInfo info, CancellationToken cancellationToken)
  80. {
  81. // Roku probably won't care about this
  82. return _cachedTask;
  83. }
  84. public Task SendRestartRequiredNotification(CancellationToken cancellationToken)
  85. {
  86. return SendCommand(new WebSocketMessage<SystemInfo>
  87. {
  88. MessageType = "RestartRequired",
  89. Data = _appHost.GetSystemInfo()
  90. }, cancellationToken);
  91. }
  92. public Task SendUserDataChangeInfo(UserDataChangeInfo info, CancellationToken cancellationToken)
  93. {
  94. // Roku probably won't care about this
  95. return _cachedTask;
  96. }
  97. public Task SendServerShutdownNotification(CancellationToken cancellationToken)
  98. {
  99. return SendCommand(new WebSocketMessage<string>
  100. {
  101. MessageType = "ServerShuttingDown",
  102. Data = string.Empty
  103. }, cancellationToken);
  104. }
  105. public Task SendServerRestartNotification(CancellationToken cancellationToken)
  106. {
  107. return SendCommand(new WebSocketMessage<string>
  108. {
  109. MessageType = "ServerRestarting",
  110. Data = string.Empty
  111. }, cancellationToken);
  112. }
  113. private Task SendCommand(object obj, CancellationToken cancellationToken)
  114. {
  115. var json = _json.SerializeToString(obj);
  116. return _httpClient.Post(new HttpRequestOptions
  117. {
  118. Url = "http://" + Session.RemoteEndPoint + "/mb/remotecontrol",
  119. CancellationToken = cancellationToken,
  120. RequestContent = json,
  121. RequestContentType = "application/json"
  122. });
  123. }
  124. public Task SendGenericCommand(GenericCommand command, CancellationToken cancellationToken)
  125. {
  126. return SendCommand(new WebSocketMessage<GenericCommand>
  127. {
  128. MessageType = "Command",
  129. Data = command
  130. }, cancellationToken);
  131. }
  132. }
  133. }