using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Hi.Common.FileLines;
using Hi.Common.Messages;
using Hi.HiNcKits;
using Hi.MachiningProcs;
using Hi.MachiningSteps;
using Hi.NcParsers;
namespace Sample.Common;
///
/// ### Source Code
/// [!code-csharp[SampleCode](~/../Hi.Sample/Common/DemoSessionMessage.cs)]
///
public static class DemoSessionMessage
{
#region Demo_UseSessionMessageHost
internal static void DemoUseSessionMessageHost(LocalProjectService localProjectService)
{
// 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.
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 (NcDiagnostic diagnostic in
localProjectService.NcDiagnosticProgress.Diagnostics.ToList())
{
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",
shellMessages.Select(m =>
$"[{m.GetSeverity()}] {m.GetId()}: {m.GetNotification()}"));
}
#endregion
#region ShowStepPresent
internal static void ShowStepPresent(
UserService userEnv, MachiningStep machiningStep)
{
foreach (var entry in userEnv.DisplayedStepPresentAccessList)
{
var present = entry.Value.Present;
var valueText = string.Format("{0:" + present.DataFormatString + "}", entry.Value.GetValueFunc.Invoke(machiningStep));
Console.WriteLine($"{present.ShortName}: {valueText} {present.TailUnitString} ({present.Name} [{entry.Key}])");
}
}
#endregion
}