using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; using Serilog; using YZWater.Core.Models; using YZWater.Core.Services; namespace YZWater.Core.ViewModels; /// /// 参数设置视图 ViewModel /// public partial class ViewBViewModel : ObservableObject { [ObservableProperty] private string _plcIp = "192.168.0.150"; [ObservableProperty] private int _plcPort = 5000; [ObservableProperty] private bool _autoConnect = true; [ObservableProperty] private float _levelHighAlarm = 80f; [ObservableProperty] private float _levelLowAlarm = 20f; [ObservableProperty] private float _flowHighAlarm = 100f; [ObservableProperty] private float _pumpFrequency = 50f; [ObservableProperty] private string _connectionStatus = "未连接"; [ObservableProperty] private bool _isConnecting; // ─── 翻译文字 ─── private readonly LanguageService _lang = LanguageService.Instance; [ObservableProperty] private string _titleText; [ObservableProperty] private string _subtitleText; [ObservableProperty] private string _plcConnectionText; [ObservableProperty] private string _ipAddressText; [ObservableProperty] private string _portText; [ObservableProperty] private string _autoConnectText; [ObservableProperty] private string _statusText; [ObservableProperty] private string _testText; [ObservableProperty] private string _alarmThresholdsText; [ObservableProperty] private string _levelHighText; [ObservableProperty] private string _levelLowText; [ObservableProperty] private string _flowHighText; [ObservableProperty] private string _pumpSettingsText; [ObservableProperty] private string _frequencyText; [ObservableProperty] private string _saveConfigText; [ObservableProperty] private string _resetDefaultText; [ObservableProperty] private string _connectedText; [ObservableProperty] private string _disconnectedText; [ObservableProperty] private string _connectingText; public ViewBViewModel() { UpdateTexts(); _lang.LanguageChanged += UpdateTexts; LoadConfig(); } private void UpdateTexts() { TitleText = _lang.Get("Parameters"); SubtitleText = _lang.Get("SysConfig"); PlcConnectionText = _lang.Get("PlcConnection"); IpAddressText = _lang.Get("IpAddress"); PortText = _lang.Get("Port"); AutoConnectText = _lang.Get("AutoConnect"); StatusText = _lang.Get("Status"); TestText = _lang.Get("Test"); AlarmThresholdsText = _lang.Get("AlarmThresholds"); LevelHighText = _lang.Get("LevelHigh"); LevelLowText = _lang.Get("LevelLow"); FlowHighText = _lang.Get("FlowHigh"); PumpSettingsText = _lang.Get("PumpSettings"); FrequencyText = _lang.Get("Frequency"); SaveConfigText = _lang.Get("SaveConfig"); ResetDefaultText = _lang.Get("ResetDefault"); ConnectedText = _lang.Get("Connected"); DisconnectedText = _lang.Get("Disconnected"); ConnectingText = _lang.Get("Connecting"); } /// /// 加载配置 /// [RelayCommand] private void LoadConfig() { var config = ConfigService.GetConfig(); if (config != null) { PlcIp = config.PlcIp; PlcPort = config.PlcPort; AutoConnect = config.AutoConnect; LevelHighAlarm = config.LevelHighAlarm; LevelLowAlarm = config.LevelLowAlarm; FlowHighAlarm = config.FlowHighAlarm; PumpFrequency = config.PumpFrequency; } Log.Debug("配置已加载"); } /// /// 保存配置 /// [RelayCommand] private void SaveConfig() { var config = new SystemConfig { PlcIp = PlcIp, PlcPort = PlcPort, AutoConnect = AutoConnect, LevelHighAlarm = LevelHighAlarm, LevelLowAlarm = LevelLowAlarm, FlowHighAlarm = FlowHighAlarm, PumpFrequency = PumpFrequency }; ConfigService.UpdateConfig(config); Log.Information("配置已保存"); } /// /// 测试连接 /// [RelayCommand] private async Task TestConnectionAsync() { IsConnecting = true; ConnectionStatus = "连接中..."; try { // 更新 PLC IP PlcService.Initialize(); var success = await PlcService.ConnectAsync(); ConnectionStatus = success ? "连接成功" : "连接失败"; if (success) { Log.Information("PLC 连接测试成功"); } else { Log.Warning("PLC 连接测试失败"); } } catch (Exception ex) { ConnectionStatus = "连接异常"; Log.Error(ex, "PLC 连接测试异常"); } finally { IsConnecting = false; } } /// /// 恢复默认设置 /// [RelayCommand] private void ResetToDefault() { PlcIp = "192.168.0.150"; PlcPort = 5000; AutoConnect = true; LevelHighAlarm = 80f; LevelLowAlarm = 20f; FlowHighAlarm = 100f; PumpFrequency = 50f; Log.Information("已恢复默认设置"); } }