RokuSessionController.cs 4.8 KB

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