更新
This commit is contained in:
@@ -21,6 +21,7 @@ namespace COFTester.ViewModels
|
|||||||
private readonly IDataAcquisitionService _daqService;
|
private readonly IDataAcquisitionService _daqService;
|
||||||
private readonly DataProcessingService _processingService;
|
private readonly DataProcessingService _processingService;
|
||||||
private readonly DispatcherTimer _clockTimer;
|
private readonly DispatcherTimer _clockTimer;
|
||||||
|
private readonly DispatcherTimer _m31StatusTimer; // M31状态检查定时器
|
||||||
private WpfPlot? _wpfPlot;
|
private WpfPlot? _wpfPlot;
|
||||||
private readonly AppConfig _config;
|
private readonly AppConfig _config;
|
||||||
private ScottPlot.Plottables.Scatter? _scatterPlot;
|
private ScottPlot.Plottables.Scatter? _scatterPlot;
|
||||||
@@ -42,6 +43,8 @@ namespace COFTester.ViewModels
|
|||||||
private string _selectedDirection = ""; // 选中的方向:Up/Down/Right/Left
|
private string _selectedDirection = ""; // 选中的方向:Up/Down/Right/Left
|
||||||
private string _resetButtonText; // 复位按钮文本
|
private string _resetButtonText; // 复位按钮文本
|
||||||
private string _testButtonText; // 测试按钮文本
|
private string _testButtonText; // 测试按钮文本
|
||||||
|
private bool _m31Status = false; // M31状态:true=测试中,false=停止
|
||||||
|
private bool _canStartTest = true; // 是否可以开始测试
|
||||||
|
|
||||||
public MainViewModel(IDataAcquisitionService daqService, DataProcessingService processingService, AppConfig config)
|
public MainViewModel(IDataAcquisitionService daqService, DataProcessingService processingService, AppConfig config)
|
||||||
{
|
{
|
||||||
@@ -64,7 +67,7 @@ namespace COFTester.ViewModels
|
|||||||
ConnectCommand = new AsyncRelayCommand(ConnectAsync, () => !IsConnected && !_isConnecting);
|
ConnectCommand = new AsyncRelayCommand(ConnectAsync, () => !IsConnected && !_isConnecting);
|
||||||
DisconnectCommand = new RelayCommand(Disconnect, () => IsConnected && !_isTesting);
|
DisconnectCommand = new RelayCommand(Disconnect, () => IsConnected && !_isTesting);
|
||||||
ToggleConnectionCommand = new AsyncRelayCommand(ToggleConnectionAsync, () => !_isConnecting && !_isTesting);
|
ToggleConnectionCommand = new AsyncRelayCommand(ToggleConnectionAsync, () => !_isConnecting && !_isTesting);
|
||||||
StartCommand = new RelayCommand(StartTest, () => !_isTesting && IsConnected);
|
StartCommand = new RelayCommand(StartTest, CanStartTest);
|
||||||
StopCommand = new RelayCommand(StopTest, () => _isTesting);
|
StopCommand = new RelayCommand(StopTest, () => _isTesting);
|
||||||
ResetCommand = new RelayCommand(Reset, () => !_isTesting && IsConnected);
|
ResetCommand = new RelayCommand(Reset, () => !_isTesting && IsConnected);
|
||||||
SwitchLanguageCommand = new RelayCommand(SwitchLanguage);
|
SwitchLanguageCommand = new RelayCommand(SwitchLanguage);
|
||||||
@@ -93,12 +96,90 @@ namespace COFTester.ViewModels
|
|||||||
_clockTimer.Tick += (s, e) => CurrentDateTime = DateTime.Now;
|
_clockTimer.Tick += (s, e) => CurrentDateTime = DateTime.Now;
|
||||||
_clockTimer.Start();
|
_clockTimer.Start();
|
||||||
|
|
||||||
|
// 启动M31状态检查定时器(每500ms检查一次)
|
||||||
|
_m31StatusTimer = new DispatcherTimer
|
||||||
|
{
|
||||||
|
Interval = TimeSpan.FromMilliseconds(500)
|
||||||
|
};
|
||||||
|
_m31StatusTimer.Tick += async (s, e) => await CheckM31StatusAsync();
|
||||||
|
_m31StatusTimer.Start();
|
||||||
|
|
||||||
// 根據配置自動連接
|
// 根據配置自動連接
|
||||||
AutoConnectOnStartup();
|
AutoConnectOnStartup();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 设置图表控件(由TestPage调用)
|
/// 定期检查M31状态
|
||||||
|
/// </summary>
|
||||||
|
private async Task CheckM31StatusAsync()
|
||||||
|
{
|
||||||
|
// 只在连接状态下检查
|
||||||
|
if (!IsConnected || _daqService is not ModbusServiceBase modbusService)
|
||||||
|
{
|
||||||
|
_m31Status = false;
|
||||||
|
_canStartTest = IsConnected;
|
||||||
|
CommandManager.InvalidateRequerySuggested();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
// 读取M31状态
|
||||||
|
bool currentM31Status = await modbusService.ReadTestStatusAsync();
|
||||||
|
|
||||||
|
// 状态变化时更新UI
|
||||||
|
if (currentM31Status != _m31Status)
|
||||||
|
{
|
||||||
|
_m31Status = currentM31Status;
|
||||||
|
System.Diagnostics.Debug.WriteLine($"[ViewModel] M31状态变化: {(_m31Status ? "1 (测试中)" : "0 (停止)")}");
|
||||||
|
|
||||||
|
// 如果M31从1变为0,说明测试完成
|
||||||
|
if (!_m31Status && _isTesting)
|
||||||
|
{
|
||||||
|
System.Diagnostics.Debug.WriteLine("[ViewModel] M31变为0,测试自动完成");
|
||||||
|
// 测试完成会由OnTestFinished事件处理
|
||||||
|
}
|
||||||
|
|
||||||
|
// 更新按钮可用性
|
||||||
|
UpdateCanStartTest();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
// 读取失败不影响正常操作,只记录日志
|
||||||
|
System.Diagnostics.Debug.WriteLine($"[ViewModel] 检查M31状态失败: {ex.Message}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 更新是否可以开始测试的状态
|
||||||
|
/// </summary>
|
||||||
|
private void UpdateCanStartTest()
|
||||||
|
{
|
||||||
|
// 可以开始测试的条件:
|
||||||
|
// 1. 已连接设备
|
||||||
|
// 2. 当前没有正在测试
|
||||||
|
// 3. M31状态为0(没有测试在运行)
|
||||||
|
bool oldCanStartTest = _canStartTest;
|
||||||
|
_canStartTest = IsConnected && !_isTesting && !_m31Status;
|
||||||
|
|
||||||
|
if (oldCanStartTest != _canStartTest)
|
||||||
|
{
|
||||||
|
System.Diagnostics.Debug.WriteLine($"[ViewModel] 开始按钮可用性变化: {_canStartTest}");
|
||||||
|
CommandManager.InvalidateRequerySuggested();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 判断是否可以开始测试
|
||||||
|
/// </summary>
|
||||||
|
private bool CanStartTest()
|
||||||
|
{
|
||||||
|
return _canStartTest;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 設置圖表控件(由TestPage調用)
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public void SetPlot(WpfPlot wpfPlot)
|
public void SetPlot(WpfPlot wpfPlot)
|
||||||
{
|
{
|
||||||
@@ -474,6 +555,8 @@ namespace COFTester.ViewModels
|
|||||||
{
|
{
|
||||||
IsTesting = false;
|
IsTesting = false;
|
||||||
TestButtonText = Lang.StartTest; // 测试完成后恢复按钮文本
|
TestButtonText = Lang.StartTest; // 测试完成后恢复按钮文本
|
||||||
|
_m31Status = false; // 测试完成,M31应该为0
|
||||||
|
UpdateCanStartTest(); // 更新按钮可用性
|
||||||
StatusMessage = Lang.TestCompleted;
|
StatusMessage = Lang.TestCompleted;
|
||||||
|
|
||||||
// 数据处理与计算
|
// 数据处理与计算
|
||||||
@@ -551,6 +634,21 @@ namespace COFTester.ViewModels
|
|||||||
StatusMessage = $"已连接 - {ConnectionInfo}";
|
StatusMessage = $"已连接 - {ConnectionInfo}";
|
||||||
OnPropertyChanged(nameof(IsConnected));
|
OnPropertyChanged(nameof(IsConnected));
|
||||||
OnPropertyChanged(nameof(ConnectionButtonText));
|
OnPropertyChanged(nameof(ConnectionButtonText));
|
||||||
|
|
||||||
|
// 连接成功后立即检查M31状态
|
||||||
|
if (_daqService is ModbusServiceBase modbusService)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
_m31Status = await modbusService.ReadTestStatusAsync();
|
||||||
|
System.Diagnostics.Debug.WriteLine($"[ViewModel] 连接后M31状态: {(_m31Status ? "1 (测试中)" : "0 (停止)")}");
|
||||||
|
UpdateCanStartTest();
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
System.Diagnostics.Debug.WriteLine($"[ViewModel] 读取M31状态失败: {ex.Message}");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -589,6 +687,8 @@ namespace COFTester.ViewModels
|
|||||||
|
|
||||||
_daqService.Disconnect();
|
_daqService.Disconnect();
|
||||||
StatusMessage = "已断开连接";
|
StatusMessage = "已断开连接";
|
||||||
|
_m31Status = false;
|
||||||
|
UpdateCanStartTest(); // 更新按钮可用性
|
||||||
OnPropertyChanged(nameof(IsConnected));
|
OnPropertyChanged(nameof(IsConnected));
|
||||||
OnPropertyChanged(nameof(ConnectionButtonText));
|
OnPropertyChanged(nameof(ConnectionButtonText));
|
||||||
CommandManager.InvalidateRequerySuggested();
|
CommandManager.InvalidateRequerySuggested();
|
||||||
@@ -623,6 +723,14 @@ namespace COFTester.ViewModels
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 检查M31状态,如果M31=1说明有测试在运行
|
||||||
|
if (_m31Status)
|
||||||
|
{
|
||||||
|
StatusMessage = "测试正在运行中,请等待完成或先复位";
|
||||||
|
MessageBox.Show("检测到测试正在运行中,请等待测试完成或先执行复位操作", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
//if (string.IsNullOrEmpty(_selectedDirection))
|
//if (string.IsNullOrEmpty(_selectedDirection))
|
||||||
//{
|
//{
|
||||||
// StatusMessage = "请先选择测试方向";
|
// StatusMessage = "请先选择测试方向";
|
||||||
@@ -637,6 +745,7 @@ namespace COFTester.ViewModels
|
|||||||
// 更新测试状态和按钮文本
|
// 更新测试状态和按钮文本
|
||||||
IsTesting = true;
|
IsTesting = true;
|
||||||
TestButtonText = Lang.TestInProgress; // 按钮显示"测试中"
|
TestButtonText = Lang.TestInProgress; // 按钮显示"测试中"
|
||||||
|
UpdateCanStartTest(); // 更新按钮可用性
|
||||||
StatusMessage = $"开始测试 - {_selectedDirection}方向";
|
StatusMessage = $"开始测试 - {_selectedDirection}方向";
|
||||||
UpdateScottPlot();
|
UpdateScottPlot();
|
||||||
|
|
||||||
@@ -662,16 +771,19 @@ namespace COFTester.ViewModels
|
|||||||
{
|
{
|
||||||
if (isTestRunning)
|
if (isTestRunning)
|
||||||
{
|
{
|
||||||
|
_m31Status = true; // 更新M31状态
|
||||||
System.Diagnostics.Debug.WriteLine("[ViewModel] M31 = 1,测试已启动");
|
System.Diagnostics.Debug.WriteLine("[ViewModel] M31 = 1,测试已启动");
|
||||||
StatusMessage = "测试已启动";
|
StatusMessage = "测试已启动";
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
_m31Status = false; // 更新M31状态
|
||||||
System.Diagnostics.Debug.WriteLine("[ViewModel] M31 = 0,测试未启动");
|
System.Diagnostics.Debug.WriteLine("[ViewModel] M31 = 0,测试未启动");
|
||||||
StatusMessage = "测试启动失败,请检查设备";
|
StatusMessage = "测试启动失败,请检查设备";
|
||||||
// 启动失败时恢复按钮状态
|
// 启动失败时恢复按钮状态
|
||||||
IsTesting = false;
|
IsTesting = false;
|
||||||
TestButtonText = Lang.StartTest;
|
TestButtonText = Lang.StartTest;
|
||||||
|
UpdateCanStartTest(); // 更新按钮可用性
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -683,6 +795,8 @@ namespace COFTester.ViewModels
|
|||||||
// 启动失败时恢复按钮状态
|
// 启动失败时恢复按钮状态
|
||||||
IsTesting = false;
|
IsTesting = false;
|
||||||
TestButtonText = Lang.StartTest;
|
TestButtonText = Lang.StartTest;
|
||||||
|
_m31Status = false;
|
||||||
|
UpdateCanStartTest(); // 更新按钮可用性
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -702,6 +816,7 @@ namespace COFTester.ViewModels
|
|||||||
IsTesting = false;
|
IsTesting = false;
|
||||||
TestButtonText = Lang.StartTest; // 恢复按钮文本为"开始测试"
|
TestButtonText = Lang.StartTest; // 恢复按钮文本为"开始测试"
|
||||||
StatusMessage = Lang.TestStopped;
|
StatusMessage = Lang.TestStopped;
|
||||||
|
UpdateCanStartTest(); // 更新按钮可用性
|
||||||
|
|
||||||
System.Diagnostics.Debug.WriteLine("[ViewModel] 停止测试,等待 PLC 将 M31 设置为 0");
|
System.Diagnostics.Debug.WriteLine("[ViewModel] 停止测试,等待 PLC 将 M31 设置为 0");
|
||||||
}
|
}
|
||||||
@@ -765,12 +880,19 @@ namespace COFTester.ViewModels
|
|||||||
System.Diagnostics.Debug.WriteLine("[ViewModel] 复位超时");
|
System.Diagnostics.Debug.WriteLine("[ViewModel] 复位超时");
|
||||||
StatusMessage = "复位超时,请检查设备";
|
StatusMessage = "复位超时,请检查设备";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 复位完成后,重新检查M31状态
|
||||||
|
_m31Status = await modbusService.ReadTestStatusAsync();
|
||||||
|
System.Diagnostics.Debug.WriteLine($"[ViewModel] 复位后M31状态: {(_m31Status ? "1" : "0")}");
|
||||||
|
UpdateCanStartTest(); // 更新按钮可用性
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// 兼容旧的复位方式
|
// 兼容旧的复位方式
|
||||||
_daqService.ResetSensors();
|
_daqService.ResetSensors();
|
||||||
StatusMessage = Lang.SystemReset;
|
StatusMessage = Lang.SystemReset;
|
||||||
|
_m31Status = false;
|
||||||
|
UpdateCanStartTest(); // 更新按钮可用性
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
@@ -1297,6 +1419,9 @@ namespace COFTester.ViewModels
|
|||||||
// 停止時鐘
|
// 停止時鐘
|
||||||
_clockTimer?.Stop();
|
_clockTimer?.Stop();
|
||||||
|
|
||||||
|
// 停止M31状态检查定时器
|
||||||
|
_m31StatusTimer?.Stop();
|
||||||
|
|
||||||
// 取消訂閱事件
|
// 取消訂閱事件
|
||||||
if (_daqService != null)
|
if (_daqService != null)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user