From 1ba32813bb587e53d6b0ab247a6bbc1167dfeadc Mon Sep 17 00:00:00 2001 From: iamboss Date: Thu, 2 Jul 2026 12:50:33 +0800 Subject: [PATCH] refactor(demos): retarget session-message samples off MixedProgress0 to the three partitioned sinks MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit DemoUseSessionMessageHost now reads ShellProgress / NcDiagnosticProgress / StepDiagnosticProgress (severity + id + notification, with the per-kind NC-line / step anchors). Drop DemoUseSessionMessageHost2 — its premise (reading MachiningStep objects out of the mixed message list) dissolves under the partition; step-data demos live in ShowStepPresent and SessionStepBuilt. DemoUseMachiningProject's abnormal-message log now subscribes the three sinks' MessageAdded events (shell via the OnShellMessageAdded app-lifetime bridge). Part of MixedProgress0 retirement (migration P6). Build x64 green. Co-Authored-By: Claude Fable 5 --- Common/DemoSessionMessage.cs | 133 ++++++--------------------- Machining/DemoUseMachiningProject.cs | 28 +++--- 2 files changed, 45 insertions(+), 116 deletions(-) diff --git a/Common/DemoSessionMessage.cs b/Common/DemoSessionMessage.cs index bd3a207..352e9ee 100644 --- a/Common/DemoSessionMessage.cs +++ b/Common/DemoSessionMessage.cs @@ -2,17 +2,12 @@ using System; using System.Collections.Generic; using System.IO; using System.Linq; -using System.Reflection; -using Hi.Common; using Hi.Common.FileLines; using Hi.Common.Messages; -using Hi.Geom; using Hi.HiNcKits; using Hi.MachiningProcs; using Hi.MachiningSteps; -using Hi.Mech; -using Hi.Mech.Topo; -using Hi.Numerical; +using Hi.NcParsers; namespace Sample.Common; @@ -25,112 +20,40 @@ public static class DemoSessionMessage #region Demo_UseSessionMessageHost internal static void DemoUseSessionMessageHost(LocalProjectService localProjectService) { - MixedProgress0 sessionMessageHost = localProjectService.MixedProgress; + // Session messages are partitioned by kind into three sinks on LocalProjectService: + // - ShellProgress: session-level routine / lifecycle messages + // (session-scoped: null outside BeginSession/EndSession). + // - NcDiagnosticProgress: NC-pipeline diagnostics, anchored to the NC source sentence. + // - StepDiagnosticProgress: diagnostics anchored to a motion step. - MixedProgress0.FilterFlag filterFlags = - MixedProgress0.FilterFlag.NC | - MixedProgress0.FilterFlag.Progress | - MixedProgress0.FilterFlag.Error; - string filterText = null; - var filteredSessionMessageList = sessionMessageHost - .GetFliteredList(filterFlags, filterText); + ShellProgress shellProgress = localProjectService.ShellProgress; + List shellMessages = shellProgress == null + ? new List() : shellProgress.Messages.ToList(); + foreach (IMessage message in shellMessages) + Console.WriteLine( + $"Shell [{message.GetSeverity()}] {message.GetId()}: {message.GetNotification()}"); - foreach (var sessionMessage in filteredSessionMessageList) + foreach (NcDiagnostic diagnostic in + localProjectService.NcDiagnosticProgress.Diagnostics.ToList()) { - //M.I.: Message Index. - Console.Write($"M.I.: {sessionMessage.Index}; Role: {sessionMessage.MessageRoleText}"); - - // For SessionMessageHost.FilterFlag.NC - var nc = sessionMessage.DirectInstantSourceCommand; - if (nc != null) - Console.Write($"Message/NC: {nc.Line}; File: {nc.FilePath}; LineNo: {nc.GetLineNo()}; "); - - // For SessionMessageHost.FilterFlag.Progress or Error. - if (sessionMessage.Data is IMessage message) - Console.WriteLine($"Message/NC: {message.GetNotification()}"); - var exception = sessionMessage.Exception; - if (exception != null) - Console.WriteLine($"Message/NC: {exception.Message}"); + var ncLine = diagnostic.SentenceCarrier?.GetSentence()?.FirstIndexedFileLine; + Console.WriteLine( + $"NC [{diagnostic.GetSeverity()}] {diagnostic.GetId()}: {diagnostic.GetNotification()}; " + + $"File: {ncLine?.FilePath}; LineNo: {ncLine?.GetLineNo()}; NC: {ncLine?.Line}"); } + + foreach (StepDiagnostic diagnostic in + localProjectService.StepDiagnosticProgress.Messages.ToList()) + Console.WriteLine( + $"Step {diagnostic.StepIndex} [{diagnostic.GetSeverity()}] " + + $"{diagnostic.GetId()}: {diagnostic.GetNotification()}"); + File.WriteAllLines("output-session-messages.txt", - filteredSessionMessageList.Select(m => - $"Msg[{m.Index}][{m.MessageRoleText}]: {m}")); + shellMessages.Select(m => + $"[{m.GetSeverity()}] {m.GetId()}: {m.GetNotification()}")); } #endregion - internal static void DemoUseSessionMessageHost2(LocalProjectService localProjectService) - { - MixedProgress0 sessionMessageHost = localProjectService.MixedProgress; - IMachiningChain machiningChain = localProjectService.MachiningChain; - - PresentAttribute mrrPresent = typeof(MachiningStep).GetProperty(nameof(MachiningStep.Mrr_mm3ds)).GetCustomAttribute(); - string mrrUnit = mrrPresent?.TailUnitString; - string mrrFormat = mrrPresent?.DataFormatString; - PresentAttribute torquePresent = typeof(MachiningStep).GetProperty(nameof(MachiningStep.AvgAbsTorque_Nm)).GetCustomAttribute(); - string torqueUnit = torquePresent?.TailUnitString; - string torqueFormat = torquePresent?.DataFormatString; - - MixedProgress0.FilterFlag filterFlags = - MixedProgress0.FilterFlag.Step | - MixedProgress0.FilterFlag.NC | - MixedProgress0.FilterFlag.Progress | - MixedProgress0.FilterFlag.Error; - string filterText = null; - var filteredSessionMessageList = sessionMessageHost - .GetFliteredList(filterFlags, filterText); - - foreach (var sessionMessage in filteredSessionMessageList) - { - //M.I.: Message Index. - Console.Write($"M.I.: {sessionMessage.Index}; Role: {sessionMessage.MessageRoleText}"); - - // For SessionMessageHost.FilterFlag.Step - var step = sessionMessage.MachiningStep; - if (step != null) - { - string[] machineCoordinateValueTexts = GetMachineCoordinateValueTexts(step, machiningChain); - var machineCoordinatesText = string.Join("; ", Enumerable.Range(0, machiningChain.McCodes.Length) - .Select(i => $"MC.{machiningChain.McCodes[i]}: {machineCoordinateValueTexts[i]}")); - Console.Write($"Time: {step.EndTimecode:G}; MRR = {step.Mrr_mm3ds.ToString(mrrFormat)} {mrrUnit}; Torque = {step.AvgAbsTorque_Nm?.ToString(torqueFormat)} {torqueUnit}; {machineCoordinatesText}; "); - var nc_ = sessionMessageHost.GetSourceCommand(sessionMessage); - Console.WriteLine($"Message/NC: {nc_.Line}; File: {nc_.FilePath}; LineNo: {nc_.GetLineNo()}"); - } - - // For SessionMessageHost.FilterFlag.NC - var nc = sessionMessage.DirectInstantSourceCommand; - if (nc != null) - { - Console.Write($"Message/NC: {nc.Line}; File: {nc.FilePath}; LineNo: {nc.GetLineNo()}; "); - if (nc is HardNcLine ncLine) - Console.WriteLine($"T: {ncLine.T}; S: {ncLine.S}; F: {ncLine.F}; NC-Flags: {ncLine.FlagsText}"); - } - - // For SessionMessageHost.FilterFlag.Progress or Error. - if (sessionMessage.Data is IMessage message) - Console.WriteLine($"Message/NC: {message.GetNotification()}"); - var exception = sessionMessage.Exception; - if (exception != null) - Console.WriteLine($"Message/NC: {exception.Message}"); - } - } - static string[] GetMachineCoordinateValueTexts(MachiningStep step, IMachiningChain machiningChain) - { - var mcTransformers = machiningChain.McTransformers; - string[] dst = new string[mcTransformers.Length]; - if (mcTransformers != null) - { - for (int i = 0; i < mcTransformers.Length; i++) - { - if (mcTransformers[i] == null) - continue; - if (mcTransformers[i] is DynamicRotation) - dst[i] = MathUtil.ToDeg(step.GetMcValue(i).Value).ToString("F4"); - else - dst[i] = step.GetMcValue(i)?.ToString("F5"); - } - } - return dst; - } #region ShowStepPresent internal static void ShowStepPresent( UserService userEnv, MachiningStep machiningStep) @@ -143,4 +66,4 @@ public static class DemoSessionMessage } } #endregion -} \ No newline at end of file +} diff --git a/Machining/DemoUseMachiningProject.cs b/Machining/DemoUseMachiningProject.cs index 6289040..a906fb0 100644 --- a/Machining/DemoUseMachiningProject.cs +++ b/Machining/DemoUseMachiningProject.cs @@ -2,6 +2,7 @@ using Hi.Common.Messages; using Hi.HiNcKits; using Hi.MachiningProcs; +using Hi.NcParsers; using Microsoft.Extensions.Logging; using System; using System.IO; @@ -37,17 +38,22 @@ public static class DemoUseMachiningProject Console.WriteLine($"Set message event."); using StreamWriter writer = new StreamWriter("msg.txt"); - //show message if something abnormal. - localProjectService.MixedProgress.CollectionItemAdded += pack => - { - if (pack.Tags.Contains(MessageFlag.Warning.ToString()) || - pack.Tags.Contains(MessageFlag.Error.ToString()) || - pack.Tags.Contains(MessageFlag.Exception.ToString())) - { - var sourceCommand = pack.SourceCommand; - writer.WriteLine($"{pack.Message} At \"{sourceCommand?.FilePath}\" (Line {sourceCommand?.GetLineNo()}) \"{sourceCommand?.Line}\""); - } - }; + //show message if something abnormal, from each partitioned message sink: + //shell (session lifecycle), NC diagnostics, and step-anchored diagnostics. + void LogIfAbnormal(IMessage message, ISentenceCarrier sentenceCarrier) + { + var severity = message.GetSeverity(); + if (severity != Severity.Warning && severity != Severity.Error) + return; + var ncLine = sentenceCarrier?.GetSentence()?.FirstIndexedFileLine; + writer.WriteLine($"{message.GetNotification()} At \"{ncLine?.FilePath}\" (Line {ncLine?.GetLineNo()}) \"{ncLine?.Line}\""); + } + localProjectService.OnShellMessageAdded += (index, message) + => LogIfAbnormal(message, null); + localProjectService.NcDiagnosticProgress.MessageAdded += (index, diagnostic) + => LogIfAbnormal(diagnostic, diagnostic.SentenceCarrier); + localProjectService.StepDiagnosticProgress.MessageAdded += (index, diagnostic) + => LogIfAbnormal(diagnostic, diagnostic.SentenceCarrier); Console.WriteLine($"Set machining step event."); //show MRR. localProjectService.SessionShell.SessionStepBuilt += (preStep, curStep) =>