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.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; } 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://localhost: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 _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(); } 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); } } 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; } Management.ApiClient = new MesApiClient(apiUrl, appKey, token); _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; } } } }