RokuSessionController.cs 4.5 KB

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