SystemService.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. using MediaBrowser.Common.Extensions;
  2. using MediaBrowser.Controller;
  3. using MediaBrowser.Controller.Configuration;
  4. using MediaBrowser.Model.Configuration;
  5. using MediaBrowser.Model.Serialization;
  6. using MediaBrowser.Model.System;
  7. using ServiceStack.ServiceHost;
  8. using System;
  9. using System.IO;
  10. using System.Threading.Tasks;
  11. namespace MediaBrowser.Api
  12. {
  13. /// <summary>
  14. /// Class GetSystemInfo
  15. /// </summary>
  16. [Route("/System/Info", "GET")]
  17. [ServiceStack.ServiceHost.Api(Description = "Gets information about the server")]
  18. public class GetSystemInfo : IReturn<SystemInfo>
  19. {
  20. }
  21. /// <summary>
  22. /// Class RestartApplication
  23. /// </summary>
  24. [Route("/System/Restart", "POST")]
  25. [ServiceStack.ServiceHost.Api(("Restarts the application, if needed"))]
  26. public class RestartApplication
  27. {
  28. }
  29. [Route("/System/Shutdown", "POST")]
  30. [ServiceStack.ServiceHost.Api(("Shuts down the application"))]
  31. public class ShutdownApplication
  32. {
  33. }
  34. /// <summary>
  35. /// Class GetConfiguration
  36. /// </summary>
  37. [Route("/System/Configuration", "GET")]
  38. [ServiceStack.ServiceHost.Api(("Gets application configuration"))]
  39. public class GetConfiguration : IReturn<ServerConfiguration>
  40. {
  41. }
  42. /// <summary>
  43. /// Class UpdateConfiguration
  44. /// </summary>
  45. [Route("/System/Configuration", "POST")]
  46. [ServiceStack.ServiceHost.Api(("Updates application configuration"))]
  47. public class UpdateConfiguration : ServerConfiguration, IReturnVoid
  48. {
  49. }
  50. /// <summary>
  51. /// Class SystemInfoService
  52. /// </summary>
  53. public class SystemService : BaseApiService
  54. {
  55. /// <summary>
  56. /// The _json serializer
  57. /// </summary>
  58. private readonly IJsonSerializer _jsonSerializer;
  59. /// <summary>
  60. /// The _app host
  61. /// </summary>
  62. private readonly IServerApplicationHost _appHost;
  63. /// <summary>
  64. /// The _configuration manager
  65. /// </summary>
  66. private readonly IServerConfigurationManager _configurationManager;
  67. /// <summary>
  68. /// Initializes a new instance of the <see cref="SystemService" /> class.
  69. /// </summary>
  70. /// <param name="jsonSerializer">The json serializer.</param>
  71. /// <param name="appHost">The app host.</param>
  72. /// <param name="configurationManager">The configuration manager.</param>
  73. /// <exception cref="System.ArgumentNullException">jsonSerializer</exception>
  74. public SystemService(IJsonSerializer jsonSerializer, IServerApplicationHost appHost, IServerConfigurationManager configurationManager)
  75. : base()
  76. {
  77. if (jsonSerializer == null)
  78. {
  79. throw new ArgumentNullException("jsonSerializer");
  80. }
  81. if (appHost == null)
  82. {
  83. throw new ArgumentNullException("appHost");
  84. }
  85. _appHost = appHost;
  86. _configurationManager = configurationManager;
  87. _jsonSerializer = jsonSerializer;
  88. }
  89. /// <summary>
  90. /// Gets the specified request.
  91. /// </summary>
  92. /// <param name="request">The request.</param>
  93. /// <returns>System.Object.</returns>
  94. public object Get(GetSystemInfo request)
  95. {
  96. var result = _appHost.GetSystemInfo();
  97. return ToOptimizedResult(result);
  98. }
  99. /// <summary>
  100. /// Gets the specified request.
  101. /// </summary>
  102. /// <param name="request">The request.</param>
  103. /// <returns>System.Object.</returns>
  104. public object Get(GetConfiguration request)
  105. {
  106. var dateModified = File.GetLastWriteTimeUtc(_configurationManager.ApplicationPaths.SystemConfigurationFilePath);
  107. var cacheKey = (_configurationManager.ApplicationPaths.SystemConfigurationFilePath + dateModified.Ticks).GetMD5();
  108. return ToOptimizedResultUsingCache(cacheKey, dateModified, null, () => _configurationManager.Configuration);
  109. }
  110. /// <summary>
  111. /// Posts the specified request.
  112. /// </summary>
  113. /// <param name="request">The request.</param>
  114. public void Post(RestartApplication request)
  115. {
  116. Task.Run(async () =>
  117. {
  118. await Task.Delay(100);
  119. _appHost.PerformPendingRestart();
  120. });
  121. }
  122. /// <summary>
  123. /// Posts the specified request.
  124. /// </summary>
  125. /// <param name="request">The request.</param>
  126. public void Post(ShutdownApplication request)
  127. {
  128. Task.Run(async () =>
  129. {
  130. await Task.Delay(100);
  131. _appHost.Shutdown();
  132. });
  133. }
  134. /// <summary>
  135. /// Posts the specified configuraiton.
  136. /// </summary>
  137. /// <param name="request">The request.</param>
  138. public void Post(UpdateConfiguration request)
  139. {
  140. // Silly, but we need to serialize and deserialize or the XmlSerializer will write the xml with an element name of UpdateConfiguration
  141. var json = _jsonSerializer.SerializeToString(request);
  142. var config = _jsonSerializer.DeserializeFromString<ServerConfiguration>(json);
  143. _configurationManager.ReplaceConfiguration(config);
  144. }
  145. }
  146. }