SystemSettingsViewModel.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. using LocalhostMES.Api.Hosting;
  2. using LocalhostMES.Api.Services;
  3. using LocalhostMES.Core;
  4. using LocalhostMES.ViewModels.Services;
  5. using Prism.Commands;
  6. using Prism.Ioc;
  7. using Prism.Mvvm;
  8. using System;
  9. using System.Windows.Media;
  10. namespace LocalhostMES.ViewModels.Tabs
  11. {
  12. public class SystemSettingsViewModel : BindableBase
  13. {
  14. private readonly IMesWorkspace _workspace;
  15. private readonly IContainerProvider _container;
  16. public SystemSettingsViewModel(IMesWorkspace workspace, IContainerProvider container)
  17. {
  18. _workspace = workspace;
  19. _container = container;
  20. ViewLoadedCommand = new DelegateCommand(UpdateServiceStatus);
  21. _isDarkTheme = ThemeManager.CurrentTheme == ThemeMode.Dark;
  22. }
  23. public DelegateCommand ViewLoadedCommand { get; }
  24. private bool _isDarkTheme;
  25. public bool IsDarkTheme
  26. {
  27. get => _isDarkTheme;
  28. set
  29. {
  30. if (SetProperty(ref _isDarkTheme, value))
  31. {
  32. ThemeManager.ApplyTheme(value ? ThemeMode.Dark : ThemeMode.Light);
  33. _workspace.ShowStatus(value ? "已切换到深色主题" : "已切换到浅色主题", false);
  34. }
  35. }
  36. }
  37. public Management Management => _workspace.MesManagement;
  38. private int _serverPort = 8081;
  39. public int ServerPort
  40. {
  41. get => _serverPort;
  42. set => SetProperty(ref _serverPort, value);
  43. }
  44. private string _apiUrl = "http://localhost:5000";
  45. public string ApiUrl
  46. {
  47. get => _apiUrl;
  48. set => SetProperty(ref _apiUrl, value);
  49. }
  50. private string _appKey = "API_XXM";
  51. public string AppKey
  52. {
  53. get => _appKey;
  54. set => SetProperty(ref _appKey, value);
  55. }
  56. private string _token = "7c5766afa3ca82f8a21af120f9b1cca6d";
  57. public string Token
  58. {
  59. get => _token;
  60. set => SetProperty(ref _token, value);
  61. }
  62. private string _serviceStatus = "服务状态: 已停止";
  63. public string ServiceStatus
  64. {
  65. get => _serviceStatus;
  66. set => SetProperty(ref _serviceStatus, value);
  67. }
  68. private Brush _serviceBrushes = Brushes.Green;
  69. public Brush Servicebrushes
  70. {
  71. get => _serviceBrushes;
  72. set => SetProperty(ref _serviceBrushes, value);
  73. }
  74. private bool _startServiceEnable = true;
  75. public bool StartServiceEnable
  76. {
  77. get => _startServiceEnable;
  78. set => SetProperty(ref _startServiceEnable, value);
  79. }
  80. private bool _stopServiceEnable = true;
  81. public bool StopServiceEnable
  82. {
  83. get => _stopServiceEnable;
  84. set => SetProperty(ref _stopServiceEnable, value);
  85. }
  86. private DelegateCommand _startServiceCommand;
  87. public DelegateCommand StartServiceCommand =>
  88. _startServiceCommand ?? (_startServiceCommand = new DelegateCommand(StartService));
  89. private DelegateCommand _stopServiceCommand;
  90. public DelegateCommand StopServiceCommand =>
  91. _stopServiceCommand ?? (_stopServiceCommand = new DelegateCommand(StopService));
  92. private DelegateCommand _saveSettingsCommand;
  93. public DelegateCommand SaveSettingsCommand =>
  94. _saveSettingsCommand ?? (_saveSettingsCommand = new DelegateCommand(SaveSettings));
  95. public async void StartService()
  96. {
  97. try
  98. {
  99. _workspace.Initialize();
  100. if (ServerPort < 1 || ServerPort > 65535)
  101. {
  102. _workspace.ShowStatus("请输入有效的端口号(1-65535)", true);
  103. return;
  104. }
  105. if (Management.WebApiService == null)
  106. {
  107. Management.WebApiService = _container.Resolve<WebApiService>();
  108. }
  109. if (Management.WebApiService.IsRunning)
  110. {
  111. await Management.WebApiService.StopAsync();
  112. }
  113. Management.WebApiService.Init(ServerPort);
  114. await Management.WebApiService.StartAsync();
  115. UpdateServiceStatus();
  116. _workspace.ShowStatus($"Web API服务已启动,端口: {ServerPort}", false);
  117. }
  118. catch (Exception ex)
  119. {
  120. _workspace.ShowStatus($"服务启动失败: {ex.Message}", true);
  121. }
  122. }
  123. private async void StopService()
  124. {
  125. try
  126. {
  127. if (Management.WebApiService != null)
  128. {
  129. await Management.WebApiService.StopAsync();
  130. }
  131. UpdateServiceStatus();
  132. _workspace.ShowStatus("Web API服务已停止", false);
  133. }
  134. catch (Exception ex)
  135. {
  136. _workspace.ShowStatus($"服务停止失败: {ex.Message}", true);
  137. }
  138. }
  139. public void SaveSettings()
  140. {
  141. try
  142. {
  143. var apiUrl = ApiUrl.Trim();
  144. var appKey = AppKey.Trim();
  145. var token = Token.Trim();
  146. if (string.IsNullOrEmpty(apiUrl))
  147. {
  148. _workspace.ShowStatus("请输入API地址", true);
  149. return;
  150. }
  151. Management.ApiClient = new MesApiClient(apiUrl, appKey, token);
  152. _workspace.ShowStatus("API客户端配置已保存", false);
  153. }
  154. catch (Exception ex)
  155. {
  156. _workspace.ShowStatus($"保存失败: {ex.Message}", true);
  157. }
  158. }
  159. private void UpdateServiceStatus()
  160. {
  161. var running = Management.WebApiService?.IsRunning == true;
  162. if (running)
  163. {
  164. ServiceStatus = "服务状态: 运行中";
  165. Servicebrushes = Brushes.Green;
  166. StartServiceEnable = false;
  167. StopServiceEnable = true;
  168. }
  169. else
  170. {
  171. ServiceStatus = "服务状态: 已停止";
  172. Servicebrushes = Brushes.Red;
  173. StartServiceEnable = true;
  174. StopServiceEnable = false;
  175. }
  176. }
  177. }
  178. }