添加项目文件。
This commit is contained in:
97
ViewModels/StationViewModel.cs
Normal file
97
ViewModels/StationViewModel.cs
Normal file
@@ -0,0 +1,97 @@
|
||||
using System.Windows.Input;
|
||||
using MembranePoreTester.Communication;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
|
||||
namespace MembranePoreTester.ViewModels
|
||||
{
|
||||
public class StationViewModel : ViewModelBase
|
||||
{
|
||||
private readonly IPlcService _plcService;
|
||||
private readonly PlcConfiguration _plcConfig;
|
||||
private bool _isPressing;
|
||||
private string _pressButtonText = "加压";
|
||||
private string _highLowPressure = "低压";
|
||||
private bool _enableChecked;
|
||||
|
||||
public int StationId { get; set; }
|
||||
|
||||
public BubblePointViewModel BubblePointVM { get; } = new();
|
||||
public PoreDistributionViewModel PoreDistributionVM { get; } = new();
|
||||
|
||||
public string HighLowPressure
|
||||
{
|
||||
get => _highLowPressure;
|
||||
set => SetProperty(ref _highLowPressure, value);
|
||||
}
|
||||
|
||||
public string PressButtonText
|
||||
{
|
||||
get => _pressButtonText;
|
||||
set => SetProperty(ref _pressButtonText, value);
|
||||
}
|
||||
|
||||
public bool EnableChecked
|
||||
{
|
||||
get => _enableChecked;
|
||||
set => SetProperty(ref _enableChecked, value);
|
||||
}
|
||||
|
||||
public ICommand PressCommand { get; }
|
||||
public ICommand BurstCommand { get; }
|
||||
public ICommand StartCommand { get; }
|
||||
public ICommand StopCommand { get; }
|
||||
public ICommand EnableCommand { get; } // 用于使能复选框
|
||||
|
||||
public StationViewModel()
|
||||
{
|
||||
_plcService = App.PlcService;
|
||||
_plcConfig = App.PlcConfig;
|
||||
BubblePointVM.StationId = StationId;
|
||||
PoreDistributionVM.StationId = StationId;
|
||||
// 初始化按钮文字
|
||||
_pressButtonText = "加压"; // 直接设置字段,避免触发属性通知循环
|
||||
PressButtonText = "加压"; // 或者通过属性设置
|
||||
System.Diagnostics.Debug.WriteLine($"工位{StationId} PressButtonText初始值: {PressButtonText}");
|
||||
PressCommand = new RelayCommand(async () => await TogglePressAsync());
|
||||
BurstCommand = new RelayCommand(async () => await ReadBurstPressureAsync());
|
||||
StartCommand = new RelayCommand(async () => await WriteCoilAsync(_plcConfig.StartCoil, true));
|
||||
StopCommand = new RelayCommand(async () => await WriteCoilAsync(_plcConfig.StopCoil, true));
|
||||
// 使能复选框点击时写入对应线圈
|
||||
EnableCommand = new RelayCommand(async () => await WriteCoilAsync(_plcConfig.EnableCoil, EnableChecked));
|
||||
}
|
||||
|
||||
private async Task TogglePressAsync()
|
||||
{
|
||||
_isPressing = !_isPressing;
|
||||
await WriteCoilAsync(_plcConfig.PressCoil, _isPressing);
|
||||
PressButtonText = _isPressing ? "停止加压" : "加压";
|
||||
}
|
||||
|
||||
private async Task ReadBurstPressureAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
float pressure = await ((ModbusTcpPlcService)_plcService).ReadPressureAsync(StationId);
|
||||
BubblePointVM.UpdateBubblePointPressure(pressure * _plcConfig.PressureFactor);
|
||||
MessageBox.Show($"涨破压力: {pressure} {BubblePointVM.Record.PressureUnit}", "提示");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
MessageBox.Show($"读取压力失败: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
private async Task WriteCoilAsync(ushort coil, bool value)
|
||||
{
|
||||
try
|
||||
{
|
||||
await ((ModbusTcpPlcService)_plcService).WriteCoilAsync(coil, value);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
MessageBox.Show($"写线圈失败: {ex.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user