diff --git a/DentistryHandpieces/HiddenSpeedSettingsViewModel.cs b/DentistryHandpieces/HiddenSpeedSettingsViewModel.cs index dfb0831..77faefb 100644 --- a/DentistryHandpieces/HiddenSpeedSettingsViewModel.cs +++ b/DentistryHandpieces/HiddenSpeedSettingsViewModel.cs @@ -87,7 +87,7 @@ public sealed class HiddenSpeedSettingsViewModel : ObservableObject } catch (Exception ex) { - StatusText = $"读取失败:{ex.Message}"; + StatusText = $"读取失败:{OperatorMessageFormatter.FromException(ex)}"; } finally { @@ -119,7 +119,7 @@ public sealed class HiddenSpeedSettingsViewModel : ObservableObject } catch (Exception ex) { - StatusText = $"负载转速设置保存失败:{ex.Message}"; + StatusText = $"负载转速设置保存失败:{OperatorMessageFormatter.FromException(ex)}"; } finally { @@ -156,7 +156,7 @@ public sealed class HiddenSpeedSettingsViewModel : ObservableObject } catch (Exception ex) { - StatusText = $"{row.RangeText}保存失败:{ex.Message}"; + StatusText = $"{row.RangeText}保存失败:{OperatorMessageFormatter.FromException(ex)}"; } finally { diff --git a/DentistryHandpieces/MainWindowViewModel.cs b/DentistryHandpieces/MainWindowViewModel.cs index 0b71078..b2ec3b9 100644 --- a/DentistryHandpieces/MainWindowViewModel.cs +++ b/DentistryHandpieces/MainWindowViewModel.cs @@ -728,7 +728,7 @@ public sealed class MainWindowViewModel : ObservableObject { if (!_lastRealtimeReadFailed) { - StatusText = $"实时数据读取失败:{ex.Message}"; + StatusText = $"实时数据读取失败:{OperatorMessageFormatter.FromException(ex)}"; _lastRealtimeReadFailed = true; Log.Warning(ex, "PLC实时数据读取失败,后续相同故障将等待恢复后再记录"); } @@ -833,7 +833,7 @@ public sealed class MainWindowViewModel : ObservableObject } catch (Exception ex) { - ParameterStatusText = $"通信读取失败,正在重试:{ex.Message}"; + ParameterStatusText = $"通信读取失败,正在重试:{OperatorMessageFormatter.FromException(ex)}"; if (DateTime.UtcNow - _lastParameterReadFailureLogAt >= TimeSpan.FromMinutes(1)) { _lastParameterReadFailureLogAt = DateTime.UtcNow; @@ -1130,7 +1130,7 @@ public sealed class MainWindowViewModel : ObservableObject } catch (Exception ex) { - StatusText = $"导出失败:{ex.Message}"; + StatusText = $"导出失败:{OperatorMessageFormatter.FromException(ex)}"; UpdateDataCaptureStatus("导出校验:失败"); Log.Error(ex, "报表导出失败,路径 {ExportPath}", exportPath); } @@ -1852,7 +1852,7 @@ public sealed class MainWindowViewModel : ObservableObject } catch (Exception ex) { - ParameterStatusText = $"PLC配置写入失败:{ex.Message}"; + ParameterStatusText = $"PLC配置写入失败:{OperatorMessageFormatter.FromException(ex)}"; Log.Error(ex, "PLC参数配置写入失败"); } finally @@ -1892,7 +1892,8 @@ public sealed class MainWindowViewModel : ObservableObject } await _plcRegisterService.WriteFloatAsync(plcConfig, registerAddress, (float)newValue); - changedItems.Add($"{fieldName}(D{registerAddress})"); + changedItems.Add(fieldName); + Log.Information("PLC参数写入成功,参数 {FieldName},D{RegisterAddress}={Value}", fieldName, registerAddress, newValue); } } @@ -2008,13 +2009,13 @@ public sealed class MainWindowViewModel : ObservableObject UpdateAxialForceSetpointModeText(); ApplyActiveAxialForceSetpointInput(); UpdateParameterSummaries(); - ParameterStatusText = $"已切换到{GetActiveAxialForceSetpointName()},M30={(usePullForceSetpoint ? 1 : 0)}"; + ParameterStatusText = $"已切换到{GetActiveAxialForceSetpointName()}。"; Log.Information("轴向力模式切换成功,M{CoilAddress}={Value},当前模式 {Mode}", AxialForceModeCoil, usePullForceSetpoint ? 1 : 0, GetActiveAxialForceSetpointName()); await AutoStopIfSetpointReachedAsync(); } catch (Exception ex) { - ParameterStatusText = $"轴向力切换失败:{ex.Message}"; + ParameterStatusText = $"轴向力切换失败:{OperatorMessageFormatter.FromException(ex)}"; Log.Error(ex, "轴向力模式切换失败,M{CoilAddress}={Value}", AxialForceModeCoil, usePullForceSetpoint ? 1 : 0); } } @@ -2043,7 +2044,7 @@ public sealed class MainWindowViewModel : ObservableObject { if (await PulsePlcAsync(VentValveCoil, "通气阀")) { - StatusText = $"通气阀已触发,M{VentValveCoil}脉冲完成。"; + StatusText = "通气阀已触发。"; } } @@ -2097,7 +2098,7 @@ public sealed class MainWindowViewModel : ObservableObject } catch (Exception ex) { - SetTestPageInputStatus($"{fieldName}自动写入 D{registerAddress} 失败:{ex.Message}"); + SetTestPageInputStatus($"{fieldName}自动写入失败:{OperatorMessageFormatter.FromException(ex)}"); Log.Error(ex, "{FieldName}自动写入失败,D{RegisterAddress}={Value}", fieldName, registerAddress, value); } finally @@ -2120,7 +2121,7 @@ public sealed class MainWindowViewModel : ObservableObject } catch (Exception ex) { - SetTestPageInputStatus($"{fieldName}自动写入 D{registerAddress} 失败:{ex.Message}"); + SetTestPageInputStatus($"{fieldName}自动写入失败:{OperatorMessageFormatter.FromException(ex)}"); Log.Error(ex, "{FieldName}自动写入失败,D{RegisterAddress}={Value}", fieldName, registerAddress, value); } finally @@ -2147,7 +2148,7 @@ public sealed class MainWindowViewModel : ObservableObject UpdateTorqueCurveStatus(); } - SetTestPageInputStatus($"{fieldName}已自动写入并回读确认 D{registerAddress}={FormatConfigNumber(confirmedValue)}"); + SetTestPageInputStatus($"{fieldName}已自动写入并回读确认:{FormatConfigNumber(confirmedValue)}"); Log.Information("{FieldName}自动写入并回读成功,D{RegisterAddress}={Value}", fieldName, registerAddress, confirmedValue); if (parameter is TestPageInputParameter.AxialPullForceSetpoint or TestPageInputParameter.AxialJumpForceSetpoint) @@ -2470,7 +2471,7 @@ public sealed class MainWindowViewModel : ObservableObject } catch (Exception ex) { - StatusText = $"PLC {actionName}状态读取失败:M{enabledCoil}/M{doneCoil},{ex.Message}"; + StatusText = $"PLC {actionName}状态读取失败:{OperatorMessageFormatter.FromException(ex)}"; if (!hasLoggedReadFailure) { hasLoggedReadFailure = true; @@ -2481,7 +2482,7 @@ public sealed class MainWindowViewModel : ObservableObject await Task.Delay(200); } - StatusText = $"PLC {actionName}完成状态未确认:M{doneCoil} 未置位。"; + StatusText = $"PLC {actionName}完成状态未确认。"; Log.Warning("PLC {ActionName}完成状态超时未确认,M{DoneCoil}未置位", actionName, doneCoil); return false; } @@ -2496,7 +2497,7 @@ public sealed class MainWindowViewModel : ObservableObject } catch (Exception ex) { - StatusText = $"PLC {actionName}失败:M{coilAddress},{ex.Message}"; + StatusText = $"PLC {actionName}失败:{OperatorMessageFormatter.FromException(ex)}"; Log.Error(ex, "PLC动作失败:{ActionName},M{CoilAddress}", actionName, coilAddress); return false; } @@ -2534,7 +2535,7 @@ public sealed class MainWindowViewModel : ObservableObject } catch (Exception ex) { - StatusText = $"PLC {actionName}失败:M{coilAddress}={(value ? 1 : 0)},{ex.Message}"; + StatusText = $"PLC {actionName}失败:{OperatorMessageFormatter.FromException(ex)}"; Log.Error(ex, "PLC手动动作失败:{ActionName},M{CoilAddress}={Value}", actionName, coilAddress, value ? 1 : 0); return false; } @@ -2794,7 +2795,7 @@ public sealed class MainWindowViewModel : ObservableObject } catch (Exception ex) { - StatusText = $"转速/扭矩结束提示读取失败:M{SpeedTorqueLimitWarningCoil}/M{SpeedTorqueWearPlateWarningCoil},{ex.Message}"; + StatusText = $"转速/扭矩结束提示读取失败:{OperatorMessageFormatter.FromException(ex)}"; Log.Error(ex, "转速/扭矩结束提示读取失败,M{LimitCoil}/M{WearCoil}", SpeedTorqueLimitWarningCoil, SpeedTorqueWearPlateWarningCoil); } } diff --git a/DentistryHandpieces/OperatorMessageFormatter.cs b/DentistryHandpieces/OperatorMessageFormatter.cs new file mode 100644 index 0000000..b9cfeb6 --- /dev/null +++ b/DentistryHandpieces/OperatorMessageFormatter.cs @@ -0,0 +1,19 @@ +using System.Text.RegularExpressions; + +namespace DentistryHandpieces; + +internal static partial class OperatorMessageFormatter +{ + [GeneratedRegex(@"\b[DM]\d+\b", RegexOptions.CultureInvariant)] + private static partial Regex PlcAddressRegex(); + + [GeneratedRegex(@"[ \t]{2,}", RegexOptions.CultureInvariant)] + private static partial Regex RepeatedWhitespaceRegex(); + + public static string FromException(Exception exception) + { + string message = PlcAddressRegex().Replace(exception.Message, string.Empty); + string cleanedMessage = RepeatedWhitespaceRegex().Replace(message, " ").Trim(); + return string.IsNullOrWhiteSpace(cleanedMessage) ? "请检查设备通信。" : cleanedMessage; + } +}