| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257 |
- using LocalhostMES.Api.Hosting;
- using LocalhostMES.Api.Services;
- using LocalhostMES.Core;
- using LocalhostMES.ViewModels.Services;
- using Prism.Commands;
- using Prism.Ioc;
- using Prism.Mvvm;
- using System;
- using System.Configuration;
- using System.Windows.Media;
- namespace LocalhostMES.ViewModels.Tabs
- {
- public class SystemSettingsViewModel : BindableBase
- {
- private readonly IMesWorkspace _workspace;
- private readonly IContainerProvider _container;
- public SystemSettingsViewModel(IMesWorkspace workspace, IContainerProvider container)
- {
- _workspace = workspace;
- _container = container;
- ViewLoadedCommand = new DelegateCommand(UpdateServiceStatus);
- _isDarkTheme = ThemeManager.CurrentTheme == ThemeMode.Dark;
- ApiUrl= ConfigurationManager.AppSettings[ "ApiUrl" ];
- AppKey = ConfigurationManager.AppSettings[ "AppKey" ];
- Token = ConfigurationManager.AppSettings[ "Token" ];
- }
- public DelegateCommand ViewLoadedCommand { get; }
- private bool _isDarkTheme;
- public bool IsDarkTheme
- {
- get => _isDarkTheme;
- set
- {
- if (SetProperty(ref _isDarkTheme, value))
- {
- ThemeManager.ApplyTheme(value ? ThemeMode.Dark : ThemeMode.Light);
- _workspace.ShowStatus(value ? "已切换到深色主题" : "已切换到浅色主题", false);
- }
- }
- }
- public Management Management => _workspace.MesManagement;
- private int _serverPort = 8081;
- public int ServerPort
- {
- get => _serverPort;
- set => SetProperty(ref _serverPort, value);
- }
- private string _apiUrl = "http://192.168.1.26:5000";
- public string ApiUrl
- {
- get => _apiUrl;
- set => SetProperty(ref _apiUrl, value);
- }
- private string _appKey = "API_XXM";
- public string AppKey
- {
- get => _appKey;
- set => SetProperty(ref _appKey, value);
- }
- private string _token = "7c5766afa3ca82f8a21af120f9b1cca6d";
- public string Token
- {
- get => _token;
- set => SetProperty(ref _token, value);
- }
- private string _serviceStatus = "服务状态: 已停止";
- public string ServiceStatus
- {
- get => _serviceStatus;
- set => SetProperty(ref _serviceStatus, value);
- }
- private Brush _serviceBrushes = Brushes.Green;
- public Brush Servicebrushes
- {
- get => _serviceBrushes;
- set => SetProperty(ref _serviceBrushes, value);
- }
- private bool _startServiceEnable = true;
- public bool StartServiceEnable
- {
- get => _startServiceEnable;
- set => SetProperty(ref _startServiceEnable, value);
- }
- private bool _stopServiceEnable = true;
- public bool StopServiceEnable
- {
- get => _stopServiceEnable;
- set => SetProperty(ref _stopServiceEnable, value);
- }
- private DelegateCommand _startServiceCommand;
- public DelegateCommand StartServiceCommand =>
- _startServiceCommand ?? (_startServiceCommand = new DelegateCommand(StartService));
- private DelegateCommand _startLocalhostServiceCommand;
- public DelegateCommand StartLocalhostServiceCommand =>
- _startLocalhostServiceCommand ?? ( _startLocalhostServiceCommand = new DelegateCommand(StartLocalhostService) );
- private DelegateCommand _stopServiceCommand;
- public DelegateCommand StopServiceCommand =>
- _stopServiceCommand ?? (_stopServiceCommand = new DelegateCommand(StopService));
- private DelegateCommand _saveSettingsCommand;
- public DelegateCommand SaveSettingsCommand =>
- _saveSettingsCommand ?? (_saveSettingsCommand = new DelegateCommand(SaveSettings));
- public async void StartService()
- {
- try
- {
- _workspace.Initialize();
- if (ServerPort < 1 || ServerPort > 65535)
- {
- _workspace.ShowStatus("请输入有效的端口号(1-65535)", true);
- return;
- }
- if (Management.WebApiService == null)
- {
- Management.WebApiService = _container.Resolve<WebApiService>();
- }
- if (Management.WebApiService.IsRunning)
- {
- await Management.WebApiService.StopAsync();
- }
- Management.WebApiService.Init(ServerPort);
- await Management.WebApiService.StartAsync();
- UpdateServiceStatus();
- _workspace.ShowStatus($"Web API服务已启动,端口: {ServerPort}", false);
- }
- catch (Exception ex)
- {
- _workspace.ShowStatus($"服务启动失败: {ex.Message}", true);
- }
- }
- public async void StartLocalhostService()
- {
- try
- {
- _workspace.Initialize();
- if ( ServerPort < 1 || ServerPort > 65535 )
- {
- _workspace.ShowStatus("请输入有效的端口号(1-65535)", true);
- return;
- }
- if ( Management.WebApiService == null )
- {
- Management.WebApiService = _container.Resolve<WebApiService>();
- }
- if ( Management.WebApiService.IsRunning )
- {
- await Management.WebApiService.StopAsync();
- }
- Management.WebApiService.InitLocalhost(ServerPort);
- await Management.WebApiService.StartAsync();
- UpdateServiceStatus();
- _workspace.ShowStatus($"Web API服务已启动,端口: {ServerPort}", false);
- }
- catch ( Exception ex )
- {
- _workspace.ShowStatus($"服务启动失败: {ex.Message}", true);
- }
- }
- private async void StopService()
- {
- try
- {
- if (Management.WebApiService != null)
- {
- await Management.WebApiService.StopAsync();
- }
- UpdateServiceStatus();
- _workspace.ShowStatus("Web API服务已停止", false);
- }
- catch (Exception ex)
- {
- _workspace.ShowStatus($"服务停止失败: {ex.Message}", true);
- }
- }
- public void SaveSettings()
- {
- try
- {
- var apiUrl = ApiUrl.Trim();
- var appKey = AppKey.Trim();
- var token = Token.Trim();
- if (string.IsNullOrEmpty(apiUrl))
- {
- _workspace.ShowStatus("请输入API地址", true);
- return;
- }
- if(Management.ApiClient!= null) { Management.ApiClient.Dispose(); }
- Management.ApiClient = new MesApiClient(apiUrl, appKey, token);
- Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
- config.AppSettings.Settings[ "ApiUrl" ].Value = ApiUrl;
- config.AppSettings.Settings[ "AppKey" ].Value = AppKey;
- config.AppSettings.Settings[ "Token" ].Value = Token;
- config.Save(ConfigurationSaveMode.Modified);
- ConfigurationManager.RefreshSection("appSettings");
- _workspace.ShowStatus("API客户端配置已保存", false);
- }
- catch (Exception ex)
- {
- _workspace.ShowStatus($"保存失败: {ex.Message}", true);
- }
- }
- private void UpdateServiceStatus()
- {
- var running = Management.WebApiService?.IsRunning == true;
- if (running)
- {
- ServiceStatus = "服务状态: 运行中";
- Servicebrushes = Brushes.Green;
- StartServiceEnable = false;
- StopServiceEnable = true;
- }
- else
- {
- ServiceStatus = "服务状态: 已停止";
- Servicebrushes = Brushes.Red;
- StartServiceEnable = true;
- StopServiceEnable = false;
- }
- }
- }
- }
|