refactor(demos): retarget session-message samples off MixedProgress0 to the three partitioned sinks
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 <noreply@anthropic.com>
This commit is contained in:
parent
7a4d0d7999
commit
1ba32813bb
@ -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<IMessage> shellMessages = shellProgress == null
|
||||
? new List<IMessage>() : 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<PresentAttribute>();
|
||||
string mrrUnit = mrrPresent?.TailUnitString;
|
||||
string mrrFormat = mrrPresent?.DataFormatString;
|
||||
PresentAttribute torquePresent = typeof(MachiningStep).GetProperty(nameof(MachiningStep.AvgAbsTorque_Nm)).GetCustomAttribute<PresentAttribute>();
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
@ -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) =>
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user