SystemService.cs 5.1 KB

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