SystemSettingsViewModel.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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.Configuration;
  10. using System.Windows.Media;
  11. namespace LocalhostMES.ViewModels.Tabs
  12. {
  13. public class SystemSettingsViewModel : BindableBase
  14. {
  15. private readonly IMesWorkspace _workspace;
  16. private readonly IContainerProvider _container;
  17. public SystemSettingsViewModel(IMesWorkspace workspace, IContainerProvider container)
  18. {
  19. _workspace = workspace;
  20. _container = container;
  21. ViewLoadedCommand = new DelegateCommand(UpdateServiceStatus);
  22. _isDarkTheme = ThemeManager.CurrentTheme == ThemeMode.Dark;
  23. ApiUrl= ConfigurationManager.AppSettings[ "ApiUrl" ];
  24. AppKey = ConfigurationManager.AppSettings[ "AppKey" ];
  25. Token = ConfigurationManager.AppSettings[ "Token" ];
  26. }
  27. public DelegateCommand ViewLoadedCommand { get; }
  28. private bool _isDarkTheme;
  29. public bool IsDarkTheme
  30. {
  31. get => _isDarkTheme;
  32. set
  33. {
  34. if (SetProperty(ref _isDarkTheme, value))
  35. {
  36. ThemeManager.ApplyTheme(value ? ThemeMode.Dark : ThemeMode.Light);
  37. _workspace.ShowStatus(value ? "已切换到深色主题" : "已切换到浅色主题", false);
  38. }
  39. }
  40. }
  41. public Management Management => _workspace.MesManagement;
  42. private int _serverPort = 8081;
  43. public int ServerPort
  44. {
  45. get => _serverPort;
  46. set => SetProperty(ref _serverPort, value);
  47. }
  48. private string _apiUrl = "http://192.168.1.26:5000";
  49. public string ApiUrl
  50. {
  51. get => _apiUrl;
  52. set => SetProperty(ref _apiUrl, value);
  53. }
  54. private string _appKey = "API_XXM";
  55. public string AppKey
  56. {
  57. get => _appKey;
  58. set => SetProperty(ref _appKey, value);
  59. }
  60. private string _token = "7c5766afa3ca82f8a21af120f9b1cca6d";
  61. public string Token
  62. {
  63. get => _token;
  64. set => SetProperty(ref _token, value);
  65. }
  66. private string _serviceStatus = "服务状态: 已停止";
  67. public string ServiceStatus
  68. {
  69. get => _serviceStatus;
  70. set => SetProperty(ref _serviceStatus, value);
  71. }
  72. private Brush _serviceBrushes = Brushes.Green;
  73. public Brush Servicebrushes
  74. {
  75. get => _serviceBrushes;
  76. set => SetProperty(ref _serviceBrushes, value);
  77. }
  78. private bool _startServiceEnable = true;
  79. public bool StartServiceEnable
  80. {
  81. get => _startServiceEnable;
  82. set => SetProperty(ref _startServiceEnable, value);
  83. }
  84. private bool _stopServiceEnable = true;
  85. public bool StopServiceEnable
  86. {
  87. get => _stopServiceEnable;
  88. set => SetProperty(ref _stopServiceEnable, value);
  89. }
  90. private DelegateCommand _startServiceCommand;
  91. public DelegateCommand StartServiceCommand =>
  92. _startServiceCommand ?? (_startServiceCommand = new DelegateCommand(StartService));
  93. private DelegateCommand _startLocalhostServiceCommand;
  94. public DelegateCommand StartLocalhostServiceCommand =>
  95. _startLocalhostServiceCommand ?? ( _startLocalhostServiceCommand = new DelegateCommand(StartLocalhostService) );
  96. private DelegateCommand _stopServiceCommand;
  97. public DelegateCommand StopServiceCommand =>
  98. _stopServiceCommand ?? (_stopServiceCommand = new DelegateCommand(StopService));
  99. private DelegateCommand _saveSettingsCommand;
  100. public DelegateCommand SaveSettingsCommand =>
  101. _saveSettingsCommand ?? (_saveSettingsCommand = new DelegateCommand(SaveSettings));
  102. public async void StartService()
  103. {
  104. try
  105. {
  106. _workspace.Initialize();
  107. if (ServerPort < 1 || ServerPort > 65535)
  108. {
  109. _workspace.ShowStatus("请输入有效的端口号(1-65535)", true);
  110. return;
  111. }
  112. if (Management.WebApiService == null)
  113. {
  114. Management.WebApiService = _container.Resolve<WebApiService>();
  115. }
  116. if (Management.WebApiService.IsRunning)
  117. {
  118. await Management.WebApiService.StopAsync();
  119. }
  120. Management.WebApiService.Init(ServerPort);
  121. await Management.WebApiService.StartAsync();
  122. UpdateServiceStatus();
  123. _workspace.ShowStatus($"Web API服务已启动,端口: {ServerPort}", false);
  124. }
  125. catch (Exception ex)
  126. {
  127. _workspace.ShowStatus($"服务启动失败: {ex.Message}", true);
  128. }
  129. }
  130. public async void StartLocalhostService()
  131. {
  132. try
  133. {
  134. _workspace.Initialize();
  135. if ( ServerPort < 1 || ServerPort > 65535 )
  136. {
  137. _workspace.ShowStatus("请输入有效的端口号(1-65535)", true);
  138. return;
  139. }
  140. if ( Management.WebApiService == null )
  141. {
  142. Management.WebApiService = _container.Resolve<WebApiService>();
  143. }
  144. if ( Management.WebApiService.IsRunning )
  145. {
  146. await Management.WebApiService.StopAsync();
  147. }
  148. Management.WebApiService.InitLocalhost(ServerPort);
  149. await Management.WebApiService.StartAsync();
  150. UpdateServiceStatus();
  151. _workspace.ShowStatus($"Web API服务已启动,端口: {ServerPort}", false);
  152. }
  153. catch ( Exception ex )
  154. {
  155. _workspace.ShowStatus($"服务启动失败: {ex.Message}", true);
  156. }
  157. }
  158. private async void StopService()
  159. {
  160. try
  161. {
  162. if (Management.WebApiService != null)
  163. {
  164. await Management.WebApiService.StopAsync();
  165. }
  166. UpdateServiceStatus();
  167. _workspace.ShowStatus("Web API服务已停止", false);
  168. }
  169. catch (Exception ex)
  170. {
  171. _workspace.ShowStatus($"服务停止失败: {ex.Message}", true);
  172. }
  173. }
  174. public void SaveSettings()
  175. {
  176. try
  177. {
  178. var apiUrl = ApiUrl.Trim();
  179. var appKey = AppKey.Trim();
  180. var token = Token.Trim();
  181. if (string.IsNullOrEmpty(apiUrl))
  182. {
  183. _workspace.ShowStatus("请输入API地址", true);
  184. return;
  185. }
  186. if(Management.ApiClient!= null) { Management.ApiClient.Dispose(); }
  187. Management.ApiClient = new MesApiClient(apiUrl, appKey, token);
  188. Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
  189. config.AppSettings.Settings[ "ApiUrl" ].Value = ApiUrl;
  190. config.AppSettings.Settings[ "AppKey" ].Value = AppKey;
  191. config.AppSettings.Settings[ "Token" ].Value = Token;
  192. config.Save(ConfigurationSaveMode.Modified);
  193. ConfigurationManager.RefreshSection("appSettings");
  194. _workspace.ShowStatus("API客户端配置已保存", false);
  195. }
  196. catch (Exception ex)
  197. {
  198. _workspace.ShowStatus($"保存失败: {ex.Message}", true);
  199. }
  200. }
  201. private void UpdateServiceStatus()
  202. {
  203. var running = Management.WebApiService?.IsRunning == true;
  204. if (running)
  205. {
  206. ServiceStatus = "服务状态: 运行中";
  207. Servicebrushes = Brushes.Green;
  208. StartServiceEnable = false;
  209. StopServiceEnable = true;
  210. }
  211. else
  212. {
  213. ServiceStatus = "服务状态: 已停止";
  214. Servicebrushes = Brushes.Red;
  215. StartServiceEnable = true;
  216. StopServiceEnable = false;
  217. }
  218. }
  219. }
  220. }