using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; using AciTester.Models; using AciTester.Services; using System.Windows; namespace AciTester.ViewModels { public partial class ConfigViewModel : ObservableObject { private readonly IPlcService _plcService; private readonly PlcConfiguration _config; private readonly CalibrationConfig _calib; public ConfigViewModel(IPlcService plcService, PlcConfiguration config, CalibrationConfig calib) { _plcService = plcService; _config = config; _calib = calib; LoadConfigCommand = new AsyncRelayCommand(LoadConfigAsync); SaveConfigCommand = new AsyncRelayCommand(SaveConfigAsync); } public IAsyncRelayCommand LoadConfigCommand { get; } public IAsyncRelayCommand SaveConfigCommand { get; } public CalibrationConfig Calibration => _calib; private async Task LoadConfigAsync() { if (!_plcService.IsConnected) { MessageBox.Show("请先连接PLC", "提示", MessageBoxButton.OK, MessageBoxImage.Warning); return; } try { _calib.FlowCalibration = await _plcService.ReadFloatAsync(_config.FlowCalibrationReg); _calib.TemperatureCalibration = await _plcService.ReadFloatAsync(_config.TempCalibrationReg); _calib.PumpPressureCalibration = await _plcService.ReadFloatAsync(_config.PumpPressureCalibReg); _calib.ImpactorPressureCalibration = await _plcService.ReadFloatAsync(_config.ImpactorPressureCalibReg); // 读取流量保护值(假设高限低限分别存储,这里简化) var protect = await _plcService.ReadFloatAsync(_config.FlowProtectReg); _calib.FlowLowLimit = protect - 5; // 示例,实际根据协议解析 _calib.FlowHighLimit = protect + 5; // 读取线圈状态(新增) _calib.FlowCalibrationEnabled = await _plcService.ReadCoilAsync(_config.FlowCalibrationCoil); _calib.PumpPressureCalibrationEnabled = await _plcService.ReadCoilAsync(_config.PumpPressureCalibCoil); _calib.ImpactorPressureCalibrationEnabled = await _plcService.ReadCoilAsync(_config.ImpactorPressureCalibCoil); } catch (System.Exception ex) { MessageBox.Show($"读取配置失败: {ex.Message}", "错误"); } } private async Task SaveConfigAsync() { if (!_plcService.IsConnected) { MessageBox.Show("请先连接PLC", "提示"); return; } try { await _plcService.WriteMultipleRegistersAsync(_config.FlowCalibrationReg, _calib.FlowCalibration); await _plcService.WriteMultipleRegistersAsync(_config.TempCalibrationReg, _calib.TemperatureCalibration); await _plcService.WriteMultipleRegistersAsync(_config.PumpPressureCalibReg, _calib.PumpPressureCalibration); await _plcService.WriteMultipleRegistersAsync(_config.ImpactorPressureCalibReg, _calib.ImpactorPressureCalibration); // 写入流量保护值(需根据实际协议拆分高低限) await _plcService.WriteCoilAsync(_config.FlowCalibrationCoil, _calib.FlowCalibrationEnabled); await _plcService.WriteCoilAsync(_config.PumpPressureCalibCoil, _calib.PumpPressureCalibrationEnabled); await _plcService.WriteCoilAsync(_config.ImpactorPressureCalibCoil, _calib.ImpactorPressureCalibrationEnabled); MessageBox.Show("配置保存成功", "提示"); // 可选:触发重新校准 await _plcService.WriteCoilAsync(_config.ImpactorPressureCalibCoil, true); await Task.Delay(100); await _plcService.WriteCoilAsync(_config.ImpactorPressureCalibCoil, false); } catch (System.Exception ex) { MessageBox.Show($"保存失败: {ex.Message}", "错误"); } } } }