异常处理
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Windows.Input;
|
||||
using DeviceCommand.Devices;
|
||||
using Logger;
|
||||
@@ -20,6 +22,8 @@ namespace MainModule.ViewModels
|
||||
private string _testStatus;
|
||||
private readonly IScopedProvider _scope;
|
||||
private bool IsInitialized =false;
|
||||
private bool _isRunningErrorSteps = false;
|
||||
private SubscriptionToken? _alarmToken;
|
||||
#endregion
|
||||
|
||||
#region 属性
|
||||
@@ -80,6 +84,7 @@ namespace MainModule.ViewModels
|
||||
BackToProtocolCommand = new DelegateCommand(OnBackToProtocol);
|
||||
LoadedCommand = new AsyncDelegateCommand(OnLoad);
|
||||
_eventAggregator.GetEvent<SilenceBuzzerEvent>().Subscribe(async (scope) => await SilenceBuzzer(scope));
|
||||
_alarmToken = _eventAggregator.GetEvent<AlarmEvent>().Subscribe(OnAlarmTriggered);
|
||||
}
|
||||
|
||||
private async Task SilenceBuzzer(string scope)
|
||||
@@ -104,6 +109,56 @@ namespace MainModule.ViewModels
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// AlarmEvent 回调:
|
||||
/// 上下限 → 蜂鸣器报警 true
|
||||
/// 上下极限 → 蜂鸣器报警 true + 执行对应 scope 的异常流程
|
||||
/// </summary>
|
||||
private async void OnAlarmTriggered((string Scope, string Fingerprint, string Status) args)
|
||||
{
|
||||
if (args.Scope != TestStatus) return;
|
||||
if (_deviceManager?.IOGroup == null) return;
|
||||
|
||||
int 台架号 = _systemConfig.SharedParameterList
|
||||
.FirstOrDefault(x => x.ParameterName == "台架序号")?.Value ?? 0;
|
||||
if (台架号 < 1 || 台架号 > 8) return;
|
||||
|
||||
// 上下限 / 上下极限 均触发蜂鸣器报警
|
||||
await _deviceManager.IOGroup.执行台架动作Async(台架号, 功能动作.蜂鸣器报警, true);
|
||||
LoggerHelper.WarnWithNotify(args.Scope, $"[{args.Scope}] 报警触发: {args.Status} ({args.Fingerprint}),蜂鸣器已开启。");
|
||||
|
||||
// 上下极限额外触发异常流程
|
||||
if (args.Status == "超上极限" || args.Status == "超下极限")
|
||||
{
|
||||
_ = TriggerErrorStepsAsync();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 执行异常流程(带并发保护,同一时间仅允许一次)
|
||||
/// </summary>
|
||||
private async Task TriggerErrorStepsAsync()
|
||||
{
|
||||
if (_isRunningErrorSteps) return;
|
||||
_isRunningErrorSteps = true;
|
||||
try
|
||||
{
|
||||
await _stepRunning.ExecuteErrorSteps(
|
||||
_scopedContext.Program,
|
||||
cancellationToken: _stepRunning.errorStepCTS.Token);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LoggerHelper.ErrorWithNotify(TestStatus, $"[{TestStatus}] 报警触发异常流程失败: {ex.Message}");
|
||||
}
|
||||
finally
|
||||
{
|
||||
_isRunningErrorSteps = false;
|
||||
if (_stepRunning.errorStepCTS.IsCancellationRequested)
|
||||
_stepRunning.errorStepCTS = new CancellationTokenSource();
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
// 1. 如果 TestStatus 为空,说明还没走到 OnNavigatedTo 赋值,直接释放 scope 即可
|
||||
@@ -115,6 +170,8 @@ namespace MainModule.ViewModels
|
||||
|
||||
try
|
||||
{
|
||||
if (_alarmToken != null)
|
||||
_eventAggregator.GetEvent<AlarmEvent>().Unsubscribe(_alarmToken);
|
||||
_eventAggregator.GetEvent<SilenceBuzzerEvent>().Unsubscribe(async (scope) => await SilenceBuzzer(scope));
|
||||
// 2. 显式释放硬件资源(防止端口占用/死锁)
|
||||
if (_deviceManager is IDisposable disposableDevice)
|
||||
|
||||
@@ -96,10 +96,10 @@ namespace UIShare.GlobalVariable
|
||||
Time = now
|
||||
});
|
||||
|
||||
string MonitorSatus = ValueLimitAlarmHelper.CheckAlarm(scope, signalFingerprint, methodName, physicalValue, _globalInfo);
|
||||
if(MonitorSatus!=""|| MonitorSatus != "未报警")
|
||||
string MonitorStatus = ValueLimitAlarmHelper.CheckAlarm(scope, signalFingerprint, methodName, physicalValue, _globalInfo);
|
||||
if (MonitorStatus != "" && MonitorStatus != "未报警")
|
||||
{
|
||||
//_eventAggregator.GetEvent<AlarmEvent>().Publish(canFingerprint, MonitorSatus);
|
||||
_eventAggregator.GetEvent<AlarmEvent>().Publish((scope,canFingerprint, MonitorStatus));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -181,10 +181,10 @@ namespace UIShare.GlobalVariable
|
||||
});
|
||||
|
||||
// 同步检查该作用域 ValueLimitList 是否超限
|
||||
string MonitorSatus= ValueLimitAlarmHelper.CheckAlarm(scope, entry.Fingerprint, entry.MethodName, value, _globalInfo);
|
||||
if (MonitorSatus != "" || MonitorSatus != "未报警")
|
||||
string MonitorStatus= ValueLimitAlarmHelper.CheckAlarm(scope, entry.Fingerprint, entry.MethodName, value, _globalInfo);
|
||||
if (MonitorStatus != "" && MonitorStatus != "未报警")
|
||||
{
|
||||
// _eventAggregator.GetEvent<AlarmEvent>().Publish(entry.Fingerprint, MonitorSatus);
|
||||
_eventAggregator.GetEvent<AlarmEvent>().Publish((scope,entry.Fingerprint, MonitorStatus));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace UIShare.PubEvent
|
||||
{
|
||||
public class AlarmEvent :PubSubEvent<Tuple<string,string>>
|
||||
public class AlarmEvent : PubSubEvent<(string Scope, string Fingerprint, string Status)>
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user