diff --git a/README.md b/README.md index c2b99dc..9ee1c74 100644 --- a/README.md +++ b/README.md @@ -57,6 +57,8 @@ pwsh -File tools/Publish-Avalonia.ps1 `--import-recipes 原数据根目录` 可以导入已有配方,`--check-standalone` / `--check-runtime` 检查本地手部模型和使用模拟 AI 的步骤门控,`--check-gpu` 额外要求手部推理使用 GPU;这些本地诊断不代表真实 D3 接口联调成功。 +主界面步骤卡片以背景条显示已触发检查窗口的剩余时间,背景随剩余时间递减;确认完成后变绿,异常跳过后变红,不显示步骤名后的判定提示。 + 主界面每个步骤显示正常次数和异常次数:正常完成计入正常,异常跳过计入异常;按配方保存在 `workspace/step-counts.json`,复位、停止、重新应用配方或重启软件均保留;仅点击“重置计数”按钮,才会清零当前已应用配方的计数,不影响步骤进度及其他配方。 ## 验证 diff --git a/src/SopHelper.Avalonia/VVMs/SopPage/SopPage.axaml b/src/SopHelper.Avalonia/VVMs/SopPage/SopPage.axaml index f8f7e7d..916cfce 100644 --- a/src/SopHelper.Avalonia/VVMs/SopPage/SopPage.axaml +++ b/src/SopHelper.Avalonia/VVMs/SopPage/SopPage.axaml @@ -100,21 +100,28 @@ - - - - - - - - + + + + + + + + + + + + + + + + + + + + - - - - - - + diff --git a/src/SopHelper.Avalonia/VVMs/SopPage/SopPageViewModel.cs b/src/SopHelper.Avalonia/VVMs/SopPage/SopPageViewModel.cs index 5273714..ef21f6b 100644 --- a/src/SopHelper.Avalonia/VVMs/SopPage/SopPageViewModel.cs +++ b/src/SopHelper.Avalonia/VVMs/SopPage/SopPageViewModel.cs @@ -917,7 +917,11 @@ public class SopPageViewModel : ViewModelBase AverageDurationText = FormatDuration(statistics.AverageDuration); StandardDurationText = FormatDuration(!string.IsNullOrWhiteSpace(recipe) && Recipes.Contains(recipe) ? SopHelper.Engine.Training.SopStandardVideoDuration.Get(DesktopPlatform.Recipes.DirectoryFor(name)) : TimeSpan.Zero); - foreach (var stepCard in StepCards) UpdateStepCounts(stepCard, _engine.Statistics); + foreach (var stepCard in StepCards) + { + UpdateStepCounts(stepCard, _engine.Statistics); + UpdateCheckTime(stepCard); + } if (IsRunning && _engine.CurrentStep is { } step && StepCards.FirstOrDefault(c => c.StepId == step.Id) is { } card) card.ElapsedText = StepElapsedText; } @@ -1049,6 +1053,13 @@ public class SopPageViewModel : ViewModelBase card.AbnormalCount = counts.AbnormalCount; } + private void UpdateCheckTime(StepCard card) + { + var progress = _engine.GetCheckWindowProgress(card.StepId); + card.ShowRemainingCheckTime = IsRunning && card.IsCurrent && !card.IsMet && !card.IsSkipped && progress.HasValue; + card.RemainingCheckFraction = progress is { } value ? value.RemainingMs / value.TotalMs : 0; + } + private void UpdateCard(StepSnapshot snapshot) { var card = StepCards.FirstOrDefault(c => c.StepId == snapshot.StepId); @@ -1058,6 +1069,7 @@ public class SopPageViewModel : ViewModelBase card.IsCurrent = snapshot.IsCurrent; card.IsMet = snapshot.IsMet; card.IsSkipped = snapshot.IsSkipped; + UpdateCheckTime(card); card.Message = snapshot.Message; if (snapshot.Elapsed is { } elapsed) card.ElapsedText = FormatDuration(elapsed); using var aiEvidence = snapshot.IsMet && snapshot.EvidenceJpeg is { } jpeg @@ -1122,6 +1134,21 @@ public class SopPageViewModel : ViewModelBase /// public class StepCard : ViewModelBase { + private bool _showRemainingCheckTime; + private double _remainingCheckFraction; + public bool ShowRemainingCheckTime { get => _showRemainingCheckTime; set => SetProperty(ref _showRemainingCheckTime, value); } + public double RemainingCheckFraction + { + get => _remainingCheckFraction; + set + { + if (!SetProperty(ref _remainingCheckFraction, Math.Clamp(value, 0, 1))) return; + RaisePropertyChanged(nameof(RemainingCheckWidth)); + RaisePropertyChanged(nameof(ElapsedCheckWidth)); + } + } + public Avalonia.Controls.GridLength RemainingCheckWidth => new(RemainingCheckFraction, Avalonia.Controls.GridUnitType.Star); + public Avalonia.Controls.GridLength ElapsedCheckWidth => new(1 - RemainingCheckFraction, Avalonia.Controls.GridUnitType.Star); private long _normalCount, _abnormalCount; public long NormalCount { get => _normalCount; set => SetProperty(ref _normalCount, value); } public long AbnormalCount { get => _abnormalCount; set => SetProperty(ref _abnormalCount, value); } diff --git a/src/SopHelper.CrossPlatform.Engine/Sop/SopEngine.ContinuousChecks.cs b/src/SopHelper.CrossPlatform.Engine/Sop/SopEngine.ContinuousChecks.cs index 775c0f8..a3f8115 100644 --- a/src/SopHelper.CrossPlatform.Engine/Sop/SopEngine.ContinuousChecks.cs +++ b/src/SopHelper.CrossPlatform.Engine/Sop/SopEngine.ContinuousChecks.cs @@ -2,8 +2,20 @@ namespace SopHelper.Engine.Sop; +public readonly record struct StepCheckWindowProgress(double RemainingMs, double TotalMs); + public partial class SopEngine { + /// 读取当前步骤真实检查窗口的剩余时间,不触发检查或改变重试规则。 + public StepCheckWindowProgress? GetCheckWindowProgress(int stepId) + { + if (State != EngineState.Running || StepAt(_index)?.Id != stepId + || !_continuousChecks.TryGetValue(_index, out var state) || !state.BudgetStarted + || state.Step.AiConfirmation?.CheckWindowMs is not > 0) return null; + var total = state.Step.AiConfirmation.CheckWindowMs; + return new(Math.Clamp(total - state.ElapsedMs, 0, total), total); + } + private const int HandSettleMs = 100; private readonly Dictionary _continuousChecks = []; public event Action>? DecisionCandidatesObserved; diff --git a/tests/Avalonia.Smoke/Program.cs b/tests/Avalonia.Smoke/Program.cs index f6e80ae..3b5dc27 100644 --- a/tests/Avalonia.Smoke/Program.cs +++ b/tests/Avalonia.Smoke/Program.cs @@ -87,6 +87,8 @@ internal static class Smoke var userCameraBefore=File.Exists(userCameraPath)?File.ReadAllBytes(userCameraPath):null; await Task.Delay(150); var app=(App)Application.Current!;var window=lifetime.MainWindow!; + if(Environment.GetCommandLineArgs().Contains("--check-progress-only")) + { await CheckRemainingWindowProgress(app,window);_exit=0;return; } if(Environment.GetCommandLineArgs().Contains("--standard-sop-only")) { await CheckStandardSopPlayback(app,window,lifetime);_exit=0;return; } if(Environment.GetCommandLineArgs().Contains("--step-counts-only")) @@ -664,6 +666,66 @@ internal static class Smoke } finally{sop.IsRunning=false;sop.ApplyRecipeCommand.Execute();} } + private static async Task CheckRemainingWindowProgress(App app,Window window) + { + var model=app.Container.Resolve(); + var engine=app.Container.Resolve(); + var store=new RecipeWorkspace(Environment.GetEnvironmentVariable("SOPHELPER_WORKSPACE_ROOT")!); + const string name="剩余检查时间测试"; + var config=new SopHelper.Engine.Sop.SopConfig();config.Sop.Name=name; + for(var id=1;id<=3;id++)config.Sop.Steps.Add(new(){Id=id,Name="步骤"+id,Description="零件到位",SaveCompletionImage=false, + Detection=new(){Method="time_elapsed",DurationMs=1,StableFrames=1}, + AiConfirmation=new(){ImageMode="full_frame",CheckWindowMs=10000,RetryDelayMs=50}}); + Directory.CreateDirectory(store.DirectoryFor(name));SopHelper.Engine.Sop.SopConfigLoader.Save(config,store.ConfigPath(name)); + model.Recipes.Add(name+".yaml");model.SelectedRecipe=name+".yaml";model.ApplyRecipeCommand.Execute(); + engine.Config.Sop.Settings.AutoResetOnComplete=false; + var client=new UiVisionDecision();engine.VisionDecisionClient=client; + var receive=typeof(SopPageViewModel).GetMethod("OnResultFrame",System.Reflection.BindingFlags.NonPublic|System.Reflection.BindingFlags.Instance)!; + var frameId=0; + async Task Emit(double seconds) + { + using var frame=new Mat(48,64,MatType.CV_8UC3,new Scalar(30,60,90)); + receive.Invoke(model,[new SopHelper.Engine.Common.FrameResult{FrameId=frameId++,SourceSeconds=seconds, + Timestamp=DateTime.UnixEpoch.AddSeconds(seconds),Size=(64,48)},frame]); + await Task.Delay(20); + } + model.IsRunning=true;engine.Start(); + Check(engine.GetCheckWindowProgress(1) is null&&model.StepCards.All(c=>!c.ShowRemainingCheckTime),"No remaining-time background appears before the check window starts"); + for(var i=0;i<10&&!model.StepCards[0].ShowRemainingCheckTime;i++)await Emit(0); + Check(model.StepCards[0].ShowRemainingCheckTime&&model.StepCards[0].RemainingCheckFraction>.95,"Triggered window starts with a full remaining-time background"); + var before=model.StepCards[0].RemainingCheckFraction;await Task.Delay(150); + Check(model.StepCards[0].RemainingCheckFraction().Single();window.WindowState=WindowState.Normal;window.Width=1180;window.Height=820;window.UpdateLayout(); + var card=view.GetVisualDescendants().OfType().First(b=>b.Name=="card"); + var fill=card.GetVisualDescendants().OfType().Single(b=>b.Name=="RemainingCheckFill"); + Check(fill.IsEffectivelyVisible&&fill.Bounds.Width>card.Bounds.Width*.4&&fill.Bounds.Widthcard.Bounds.Height-6,"Remaining-time fill spans the card background with proportional width"); + var cardTexts=card.GetVisualDescendants().OfType().Select(t=>t.Text).ToArray(); + Check(!cardTexts.Contains(model.StepCards[0].Message)&&!cardTexts.Contains("已跳过/未通过")&&cardTexts.Contains("步骤1"),"Step names are displayed without decision hints or skip text"); + await Emit(12); + Check(model.StepCards[0].IsSkipped&&!model.StepCards[0].ShowRemainingCheckTime&&model.StepCards[0].AbnormalCount==1,"Window expiry still skips the step and replaces the background with its abnormal state"); + client=new UiVisionDecision();engine.VisionDecisionClient=client; + for(var i=0;i<10&&!client.Called.Task.IsCompleted;i++)await Emit(12.1); + Check(client.Called.Task.IsCompleted,"The following step keeps the normal retry-and-check workflow"); + client.Result.SetResult(new(true,.99,"确认完成")); + for(var i=0;i<10&&engine.CurrentStep?.Id==2;i++)await Emit(12.2); + Check(model.StepCards[1].IsMet&&!model.StepCards[1].ShowRemainingCheckTime&&model.StepCards[1].NormalCount==1,"Confirmed steps keep their completed background and normal count"); + engine.VisionDecisionClient=new UiVisionDecision(); + for(var i=0;i<10&&!model.StepCards[2].ShowRemainingCheckTime;i++)await Emit(12.3); + await Emit(17.3);await Task.Delay(40);window.UpdateLayout(); + var screenshots=Path.GetFullPath(Path.Combine("..","scratch","screenshots"));Directory.CreateDirectory(screenshots); + using(var rendered=new RenderTargetBitmap(new PixelSize((int)window.Bounds.Width,(int)window.Bounds.Height))) + {rendered.Render(window);rendered.Save(Path.Combine(screenshots,"remaining-check-background.png"));} + engine.Stop();model.IsRunning=false;await Task.Delay(120); + Check(model.StepCards.All(c=>!c.ShowRemainingCheckTime),"Stopping clears active progress without leaving a stale time background"); + engine.Reset();await Task.Delay(30); + Check(model.StepCards.All(c=>!c.ShowRemainingCheckTime)&&model.StepCards[0].AbnormalCount==1&&model.StepCards[1].NormalCount==1,"Reset clears progress while preserving accumulated results"); + engine.Stop(); + } + private static async Task CheckStandardSopPlayback(App app,Window window,IClassicDesktopStyleApplicationLifetime lifetime) { var model=app.Container.Resolve();