RokuSessionController.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 true; }
  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. public Task SendLibraryUpdateInfo(LibraryUpdateInfo info, CancellationToken cancellationToken)
  79. {
  80. // Roku probably won't care about this
  81. return Task.FromResult(true);
  82. }
  83. public Task SendRestartRequiredNotification(CancellationToken cancellationToken)
  84. {
  85. return SendCommand(new WebSocketMessage<SystemInfo>
  86. {
  87. MessageType = "RestartRequired",
  88. Data = _appHost.GetSystemInfo()
  89. }, cancellationToken);
  90. }
  91. public Task SendUserDataChangeInfo(UserDataChangeInfo info, CancellationToken cancellationToken)
  92. {
  93. // Roku probably won't care about this
  94. return Task.FromResult(true);
  95. }
  96. public Task SendServerShutdownNotification(CancellationToken cancellationToken)
  97. {
  98. return SendCommand(new WebSocketMessage<string>
  99. {
  100. MessageType = "ServerShuttingDown",
  101. Data = string.Empty
  102. }, cancellationToken);
  103. }
  104. public Task SendServerRestartNotification(CancellationToken cancellationToken)
  105. {
  106. return SendCommand(new WebSocketMessage<string>
  107. {
  108. MessageType = "ServerRestarting",
  109. Data = string.Empty
  110. }, cancellationToken);
  111. }
  112. private Task SendCommand(object obj, CancellationToken cancellationToken)
  113. {
  114. var json = _json.SerializeToString(obj);
  115. return _httpClient.Post(new HttpRequestOptions
  116. {
  117. Url = "mb/remotecontrol",
  118. CancellationToken = cancellationToken,
  119. RequestContent = json,
  120. RequestContentType = "application/json"
  121. });
  122. }
  123. }
  124. }