| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- using CommunityToolkit.Mvvm.ComponentModel;
- using CommunityToolkit.Mvvm.Input;
- using Serilog;
- using YZWater.Core.Models;
- using YZWater.Core.Services;
- namespace YZWater.Core.ViewModels;
- /// <summary>
- /// 参数设置视图 ViewModel
- /// </summary>
- 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");
- }
- /// <summary>
- /// 加载配置
- /// </summary>
- [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("配置已加载");
- }
- /// <summary>
- /// 保存配置
- /// </summary>
- [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("配置已保存");
- }
- /// <summary>
- /// 测试连接
- /// </summary>
- [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;
- }
- }
- /// <summary>
- /// 恢复默认设置
- /// </summary>
- [RelayCommand]
- private void ResetToDefault()
- {
- PlcIp = "192.168.0.150";
- PlcPort = 5000;
- AutoConnect = true;
- LevelHighAlarm = 80f;
- LevelLowAlarm = 20f;
- FlowHighAlarm = 100f;
- PumpFrequency = 50f;
- Log.Information("已恢复默认设置");
- }
- }
|